LiKenun commited on
Commit
0f3cd78
·
1 Parent(s): 55d79e2

Updated code to address “UserWarning: You have not specified a value for the `type` parameter”

Browse files
Files changed (1) hide show
  1. 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
- and manages the conversion between Gradio's chat format and the internal
190
- conversation history format.
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
- chatbot_history = gr.State(value=None) # Store the conversation history.
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 | None, conversation_state: list[dict] | None):
202
- """Handle chatbot interaction with Gradio chat format.
203
 
204
- This function serves as the bridge between Gradio's chat interface format
205
- and the internal chatbot API. It converts formats, handles empty messages,
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 [user_msg, bot_msg] pairs).
211
- conversation_state: Internal conversation history format (list of dicts).
212
 
213
  Returns:
214
  Tuple containing:
215
- - Updated Gradio chat history
216
- - Updated internal conversation state
217
  - Empty string (to clear the input field)
218
  """
219
  if not message.strip():
220
- return history, conversation_state, ""
221
- response, updated_conversation = chat(model, message, conversation_state) # Get response from chatbot.
222
- if history is None: # Update Gradio chat history format: list of [user_message, bot_message] pairs.
223
- history = []
224
- history.append([message, response])
225
- return history, updated_conversation, "" # Clear input field for the next message from the user.
226
 
227
  chatbot_send_button.click(
228
  fn=chat_interface,
229
- inputs=[chatbot_input, chatbot_output, chatbot_history],
230
- outputs=[chatbot_output, chatbot_history, chatbot_input]
231
  )
232
  chatbot_input.submit(
233
  fn=chat_interface,
234
- inputs=[chatbot_input, chatbot_output, chatbot_history],
235
- outputs=[chatbot_output, chatbot_history, chatbot_input]
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
  )