zsolnai commited on
Commit
002e245
Β·
1 Parent(s): 24befd4

Fix claude mistake v7

Browse files
Files changed (1) hide show
  1. app.py +32 -26
app.py CHANGED
@@ -33,10 +33,9 @@ tts_model = TTS(model_name=TTS_MODEL_NAME, progress_bar=False)
33
  def chat_with_bot(message, history, chat_history_ids=None):
34
  """
35
  Chat with the conversational AI model using DialoGPT.
36
- CRITICAL FIX: Ensures history is a list of tuples [("user", "bot"), ...]
37
- Returns: (updated_history, updated_chat_ids, response_text)
38
  """
39
- # Ensure history is correctly initialized as a list of tuples
40
  if history is None:
41
  history = []
42
 
@@ -73,14 +72,15 @@ def chat_with_bot(message, history, chat_history_ids=None):
73
  chat_history_ids[:, bot_input_ids.shape[-1] :][0], skip_special_tokens=True
74
  )
75
 
76
- # FIX: Append to history in the required Gradio Chatbot (list of tuples) format
77
- history.append((message, response))
 
78
 
79
  return history, chat_history_ids, response
80
 
81
  except Exception as e:
82
- # FIX: Append error to history in the required Gradio Chatbot (list of tuples) format
83
- history.append((message, f"Error: {e}"))
84
  return history, chat_history_ids, f"Error: {e}"
85
 
86
 
@@ -176,12 +176,16 @@ with gr.Blocks() as demo:
176
 
177
  with gr.Tabs():
178
 
179
- # --- FULL VOICE CHAT TAB (STT -> CHAT -> TTS) ---
180
  with gr.TabItem("πŸ—£οΈ Voice Assistant"):
181
  gr.Markdown("## Talk to the AI Assistant")
182
 
 
183
  voice_chat_history = gr.Chatbot(
184
- label="Conversation Log", elem_classes=["chatbot"], value=[]
 
 
 
185
  )
186
  voice_chat_state = gr.State(value=None)
187
 
@@ -202,7 +206,6 @@ with gr.Blocks() as demo:
202
 
203
  voice_status = gr.Textbox(elem_id="status", label="Status")
204
 
205
- # Chain the functions together
206
  run_btn.click(
207
  fn=speech_to_text_and_chat,
208
  inputs=[audio_in, voice_chat_history, voice_chat_state],
@@ -233,8 +236,12 @@ with gr.Blocks() as demo:
233
  with gr.TabItem("πŸ’¬ Chat β†’ Voice Output"):
234
  gr.Markdown("## πŸ’¬ Chat with Voice Output")
235
 
 
236
  tts_chatbot = gr.Chatbot(
237
- label="Conversation", elem_classes=["chatbot"], value=[]
 
 
 
238
  )
239
  tts_msg = gr.Textbox(
240
  label="Your Message",
@@ -255,9 +262,7 @@ with gr.Blocks() as demo:
255
  tts_status = gr.Textbox(elem_id="status", label="Status")
256
 
257
  def chat_and_speak(message, history, chat_ids):
258
- """Send message to chat and convert response to speech."""
259
  # 1. Chatbot
260
- # The chat_with_bot returns history in the correct tuple format
261
  updated_history, updated_ids, last_response = chat_with_bot(
262
  message, history, chat_ids
263
  )
@@ -267,8 +272,7 @@ with gr.Blocks() as demo:
267
 
268
  return updated_history, updated_ids, last_response, audio_path, status
269
 
270
- # --- Submit listener for the Enter key ---
271
- fn_call = tts_msg.submit(
272
  fn=chat_and_speak,
273
  inputs=[tts_msg, tts_chatbot, tts_chat_state],
274
  outputs=[
@@ -308,8 +312,12 @@ with gr.Blocks() as demo:
308
  with gr.TabItem("πŸ’¬ Text Chat Only"):
309
  gr.Markdown("## Chat with AI Assistant")
310
 
 
311
  chatbot = gr.Chatbot(
312
- label="Conversation", elem_classes=["chatbot"], value=[]
 
 
 
313
  )
314
  msg = gr.Textbox(
315
  label="Your Message",
@@ -321,21 +329,19 @@ with gr.Blocks() as demo:
321
  submit_btn = gr.Button("Send", variant="primary")
322
  clear_btn = gr.Button("Clear Chat")
323
 
324
- # Chat functionality (uses global state, already wired for Enter key)
325
- fn_call = msg.submit(
326
- # Use slice [:2] to discard the third output (response text)
327
- lambda message, history, chat_state: chat_with_bot(
328
- message, history, chat_state
329
- )[:2],
 
330
  inputs=[msg, chatbot, global_chat_state],
331
  outputs=[chatbot, global_chat_state],
332
  ).then(lambda: "", None, msg)
333
 
334
  submit_btn.click(
335
- # Use slice [:2] to discard the third output (response text)
336
- lambda message, history, chat_state: chat_with_bot(
337
- message, history, chat_state
338
- )[:2],
339
  inputs=[msg, chatbot, global_chat_state],
340
  outputs=[chatbot, global_chat_state],
341
  ).then(lambda: "", None, msg)
 
33
  def chat_with_bot(message, history, chat_history_ids=None):
34
  """
35
  Chat with the conversational AI model using DialoGPT.
36
+ CRITICAL FIX: Uses Dictionary format {"role": "...", "content": "..."}
 
37
  """
38
+ # Ensure history is a list
39
  if history is None:
40
  history = []
41
 
 
72
  chat_history_ids[:, bot_input_ids.shape[-1] :][0], skip_special_tokens=True
73
  )
74
 
75
+ # CRITICAL FIX: Append dictionaries, not tuples
76
+ history.append({"role": "user", "content": message})
77
+ history.append({"role": "assistant", "content": response})
78
 
79
  return history, chat_history_ids, response
80
 
81
  except Exception as e:
82
+ history.append({"role": "user", "content": message})
83
+ history.append({"role": "assistant", "content": f"Error: {e}"})
84
  return history, chat_history_ids, f"Error: {e}"
85
 
86
 
 
176
 
177
  with gr.Tabs():
178
 
179
+ # --- FULL VOICE CHAT TAB ---
180
  with gr.TabItem("πŸ—£οΈ Voice Assistant"):
181
  gr.Markdown("## Talk to the AI Assistant")
182
 
183
+ # CRITICAL FIX: type="messages"
184
  voice_chat_history = gr.Chatbot(
185
+ label="Conversation Log",
186
+ elem_classes=["chatbot"],
187
+ value=[],
188
+ type="messages",
189
  )
190
  voice_chat_state = gr.State(value=None)
191
 
 
206
 
207
  voice_status = gr.Textbox(elem_id="status", label="Status")
208
 
 
209
  run_btn.click(
210
  fn=speech_to_text_and_chat,
211
  inputs=[audio_in, voice_chat_history, voice_chat_state],
 
236
  with gr.TabItem("πŸ’¬ Chat β†’ Voice Output"):
237
  gr.Markdown("## πŸ’¬ Chat with Voice Output")
238
 
239
+ # CRITICAL FIX: type="messages"
240
  tts_chatbot = gr.Chatbot(
241
+ label="Conversation",
242
+ elem_classes=["chatbot"],
243
+ value=[],
244
+ type="messages",
245
  )
246
  tts_msg = gr.Textbox(
247
  label="Your Message",
 
262
  tts_status = gr.Textbox(elem_id="status", label="Status")
263
 
264
  def chat_and_speak(message, history, chat_ids):
 
265
  # 1. Chatbot
 
266
  updated_history, updated_ids, last_response = chat_with_bot(
267
  message, history, chat_ids
268
  )
 
272
 
273
  return updated_history, updated_ids, last_response, audio_path, status
274
 
275
+ tts_msg.submit(
 
276
  fn=chat_and_speak,
277
  inputs=[tts_msg, tts_chatbot, tts_chat_state],
278
  outputs=[
 
312
  with gr.TabItem("πŸ’¬ Text Chat Only"):
313
  gr.Markdown("## Chat with AI Assistant")
314
 
315
+ # CRITICAL FIX: type="messages"
316
  chatbot = gr.Chatbot(
317
+ label="Conversation",
318
+ elem_classes=["chatbot"],
319
+ value=[],
320
+ type="messages",
321
  )
322
  msg = gr.Textbox(
323
  label="Your Message",
 
329
  submit_btn = gr.Button("Send", variant="primary")
330
  clear_btn = gr.Button("Clear Chat")
331
 
332
+ # Helper to just return history/state
333
+ def chat_text_only(message, history, chat_state):
334
+ h, s, _ = chat_with_bot(message, history, chat_state)
335
+ return h, s
336
+
337
+ msg.submit(
338
+ fn=chat_text_only,
339
  inputs=[msg, chatbot, global_chat_state],
340
  outputs=[chatbot, global_chat_state],
341
  ).then(lambda: "", None, msg)
342
 
343
  submit_btn.click(
344
+ fn=chat_text_only,
 
 
 
345
  inputs=[msg, chatbot, global_chat_state],
346
  outputs=[chatbot, global_chat_state],
347
  ).then(lambda: "", None, msg)