Spaces:
Running
on
Zero
Running
on
Zero
Updated code to address “UserWarning: You have not specified a value for the `type` parameter”
Browse files- chatbot.py +22 -26
chatbot.py
CHANGED
|
@@ -180,57 +180,53 @@ def create_chatbot_tab(model: str):
|
|
| 180 |
|
| 181 |
This function sets up all UI components for the conversational chatbot,
|
| 182 |
including:
|
| 183 |
-
- Chatbot component for displaying conversation history
|
| 184 |
- Text input box for user messages
|
| 185 |
- Send button and Enter key submission support
|
| 186 |
-
- Internal state management for conversation history
|
| 187 |
|
| 188 |
-
It also wires up event handlers for both button clicks and Enter key presses
|
| 189 |
-
|
| 190 |
-
|
| 191 |
|
| 192 |
Args:
|
| 193 |
model: Hugging Face model ID to use for the chatbot.
|
| 194 |
"""
|
| 195 |
gr.Markdown("Have a conversation with an AI chatbot.")
|
| 196 |
-
|
| 197 |
-
chatbot_output = gr.Chatbot(label="Conversation")
|
| 198 |
chatbot_input = gr.Textbox(label="Your message")
|
| 199 |
chatbot_send_button = gr.Button("Send")
|
| 200 |
|
| 201 |
-
def chat_interface(message: str, history: list
|
| 202 |
-
"""Handle chatbot interaction with Gradio
|
| 203 |
|
| 204 |
-
This function
|
| 205 |
-
|
| 206 |
-
and manages state updates.
|
| 207 |
|
| 208 |
Args:
|
| 209 |
message: The user's message string from the input box.
|
| 210 |
-
history: Gradio's chat history format (list of
|
| 211 |
-
|
| 212 |
|
| 213 |
Returns:
|
| 214 |
Tuple containing:
|
| 215 |
-
- Updated
|
| 216 |
-
- Updated internal conversation state
|
| 217 |
- Empty string (to clear the input field)
|
| 218 |
"""
|
| 219 |
if not message.strip():
|
| 220 |
-
return history,
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
return
|
| 226 |
|
| 227 |
chatbot_send_button.click(
|
| 228 |
fn=chat_interface,
|
| 229 |
-
inputs=[chatbot_input, chatbot_output
|
| 230 |
-
outputs=[chatbot_output,
|
| 231 |
)
|
| 232 |
chatbot_input.submit(
|
| 233 |
fn=chat_interface,
|
| 234 |
-
inputs=[chatbot_input, chatbot_output
|
| 235 |
-
outputs=[chatbot_output,
|
| 236 |
)
|
|
|
|
| 180 |
|
| 181 |
This function sets up all UI components for the conversational chatbot,
|
| 182 |
including:
|
| 183 |
+
- Chatbot component for displaying conversation history (using messages format)
|
| 184 |
- Text input box for user messages
|
| 185 |
- Send button and Enter key submission support
|
|
|
|
| 186 |
|
| 187 |
+
It also wires up event handlers for both button clicks and Enter key presses.
|
| 188 |
+
The conversation history uses Gradio's messages format (list of dicts with
|
| 189 |
+
"role" and "content" keys), which matches the internal chatbot API format.
|
| 190 |
|
| 191 |
Args:
|
| 192 |
model: Hugging Face model ID to use for the chatbot.
|
| 193 |
"""
|
| 194 |
gr.Markdown("Have a conversation with an AI chatbot.")
|
| 195 |
+
chatbot_output = gr.Chatbot(label="Conversation", type="messages")
|
|
|
|
| 196 |
chatbot_input = gr.Textbox(label="Your message")
|
| 197 |
chatbot_send_button = gr.Button("Send")
|
| 198 |
|
| 199 |
+
def chat_interface(message: str, history: list[dict] | None):
|
| 200 |
+
"""Handle chatbot interaction with Gradio messages format.
|
| 201 |
|
| 202 |
+
This function handles chatbot interactions using Gradio's messages format,
|
| 203 |
+
where each message is a dictionary with "role" and "content" keys.
|
|
|
|
| 204 |
|
| 205 |
Args:
|
| 206 |
message: The user's message string from the input box.
|
| 207 |
+
history: Gradio's chat history in messages format (list of dicts with
|
| 208 |
+
"role" and "content" keys). If None, starts a new conversation.
|
| 209 |
|
| 210 |
Returns:
|
| 211 |
Tuple containing:
|
| 212 |
+
- Updated chat history in messages format
|
|
|
|
| 213 |
- Empty string (to clear the input field)
|
| 214 |
"""
|
| 215 |
if not message.strip():
|
| 216 |
+
return history, ""
|
| 217 |
+
print(history)
|
| 218 |
+
|
| 219 |
+
# Use history directly as conversation_state since they're the same format
|
| 220 |
+
response, updated_conversation = chat(model, message, history)
|
| 221 |
+
return updated_conversation, "" # Return updated conversation history and clear input field
|
| 222 |
|
| 223 |
chatbot_send_button.click(
|
| 224 |
fn=chat_interface,
|
| 225 |
+
inputs=[chatbot_input, chatbot_output],
|
| 226 |
+
outputs=[chatbot_output, chatbot_input]
|
| 227 |
)
|
| 228 |
chatbot_input.submit(
|
| 229 |
fn=chat_interface,
|
| 230 |
+
inputs=[chatbot_input, chatbot_output],
|
| 231 |
+
outputs=[chatbot_output, chatbot_input]
|
| 232 |
)
|