Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -117,11 +117,24 @@ def update_chat_history(user_message: str, assistant_response: str, history: lis
|
|
117 |
save_chat_history(history)
|
118 |
return history
|
119 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
120 |
# Define the UI layout with additional features
|
121 |
with gr.Blocks() as demo:
|
122 |
gr.Markdown("# 🧠 AI Chatbot Interface")
|
123 |
gr.Markdown("### Customize your AI Chatbot's behavior and responses.")
|
124 |
-
|
125 |
with gr.Row():
|
126 |
with gr.Column():
|
127 |
system_message = gr.Textbox(
|
@@ -132,11 +145,18 @@ with gr.Blocks() as demo:
|
|
132 |
max_tokens = gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens")
|
133 |
temperature = gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature")
|
134 |
top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)")
|
135 |
-
|
136 |
-
|
|
|
|
|
|
|
|
|
|
|
137 |
with gr.Row():
|
138 |
chatbot = gr.Chatbot()
|
139 |
-
|
|
|
|
|
140 |
with gr.Row():
|
141 |
with gr.Column():
|
142 |
sample_prompt = gr.Dropdown(
|
@@ -168,20 +188,22 @@ with gr.Blocks() as demo:
|
|
168 |
history = load_chat_history()
|
169 |
response = list(respond(message, history, system_message, max_tokens, temperature, top_p))[0]
|
170 |
history = update_chat_history(message, response, history)
|
171 |
-
|
|
|
172 |
|
173 |
submit_btn.click(
|
174 |
fn=handle_send,
|
175 |
inputs=[message, system_message, max_tokens, temperature, top_p],
|
176 |
-
outputs=[chatbot, gr.State()],
|
177 |
show_progress=True
|
178 |
)
|
179 |
|
180 |
# Clear the chat history
|
181 |
def clear_chat() -> list:
|
182 |
-
|
|
|
183 |
|
184 |
-
clear_btn.click(fn=clear_chat, inputs=None, outputs=chatbot)
|
185 |
|
186 |
# Handle feedback submission
|
187 |
def submit_user_feedback(feedback: str):
|
@@ -193,4 +215,4 @@ with gr.Blocks() as demo:
|
|
193 |
|
194 |
# Launch the Gradio interface
|
195 |
if __name__ == "__main__":
|
196 |
-
demo.launch()
|
|
|
117 |
save_chat_history(history)
|
118 |
return history
|
119 |
|
120 |
+
# --- Enhanced UI Features ---
|
121 |
+
def update_settings(max_tokens, temperature, top_p):
|
122 |
+
"""Updates the settings based on user input."""
|
123 |
+
return gr.Markdown(f"**Current Settings:**\n* Max Tokens: {max_tokens}\n* Temperature: {temperature}\n* Top-p: {top_p}")
|
124 |
+
|
125 |
+
def display_history(history):
|
126 |
+
"""Displays the chat history in a more readable format."""
|
127 |
+
formatted_history = ""
|
128 |
+
for user_msg, assistant_msg in history:
|
129 |
+
formatted_history += f"**User:** {user_msg}\n**Assistant:** {assistant_msg}\n\n"
|
130 |
+
return formatted_history
|
131 |
+
|
132 |
+
|
133 |
# Define the UI layout with additional features
|
134 |
with gr.Blocks() as demo:
|
135 |
gr.Markdown("# 🧠 AI Chatbot Interface")
|
136 |
gr.Markdown("### Customize your AI Chatbot's behavior and responses.")
|
137 |
+
|
138 |
with gr.Row():
|
139 |
with gr.Column():
|
140 |
system_message = gr.Textbox(
|
|
|
145 |
max_tokens = gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens")
|
146 |
temperature = gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature")
|
147 |
top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)")
|
148 |
+
|
149 |
+
# Display current settings
|
150 |
+
settings_output = gr.Markdown(f"**Current Settings:**\n* Max Tokens: {max_tokens.value}\n* Temperature: {temperature.value}\n* Top-p: {top_p.value}")
|
151 |
+
max_tokens.change(fn=update_settings, inputs=[max_tokens, temperature, top_p], outputs=settings_output)
|
152 |
+
temperature.change(fn=update_settings, inputs=[max_tokens, temperature, top_p], outputs=settings_output)
|
153 |
+
top_p.change(fn=update_settings, inputs=[max_tokens, temperature, top_p], outputs=settings_output)
|
154 |
+
|
155 |
with gr.Row():
|
156 |
chatbot = gr.Chatbot()
|
157 |
+
# Display chat history in a separate area
|
158 |
+
history_output = gr.Textbox(label="Chat History", lines=10, interactive=False)
|
159 |
+
|
160 |
with gr.Row():
|
161 |
with gr.Column():
|
162 |
sample_prompt = gr.Dropdown(
|
|
|
188 |
history = load_chat_history()
|
189 |
response = list(respond(message, history, system_message, max_tokens, temperature, top_p))[0]
|
190 |
history = update_chat_history(message, response, history)
|
191 |
+
formatted_history = display_history(history)
|
192 |
+
return response, history, formatted_history
|
193 |
|
194 |
submit_btn.click(
|
195 |
fn=handle_send,
|
196 |
inputs=[message, system_message, max_tokens, temperature, top_p],
|
197 |
+
outputs=[chatbot, gr.State(), history_output],
|
198 |
show_progress=True
|
199 |
)
|
200 |
|
201 |
# Clear the chat history
|
202 |
def clear_chat() -> list:
|
203 |
+
save_chat_history([]) # Clear the saved history as well
|
204 |
+
return [], "" # Return empty list for chatbot and empty string for history output
|
205 |
|
206 |
+
clear_btn.click(fn=clear_chat, inputs=None, outputs=[chatbot, history_output])
|
207 |
|
208 |
# Handle feedback submission
|
209 |
def submit_user_feedback(feedback: str):
|
|
|
215 |
|
216 |
# Launch the Gradio interface
|
217 |
if __name__ == "__main__":
|
218 |
+
demo.launch()
|