amasha03 commited on
Commit
d4c95d0
·
verified ·
1 Parent(s): 330bbb1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -13
app.py CHANGED
@@ -18,10 +18,11 @@ llm_english = safe_client("E-motionAssistant/TherapyEnglish")
18
  tts_client = safe_client("E-motionAssistant/Space3")
19
 
20
  def main_orchestrator(audio_input, text_input, history):
 
21
  if history is None:
22
  history = []
23
 
24
- # 1. EMOTION LOGIC (Keeping your current logic)
25
  emotion = "Neutral"
26
  try:
27
  if audio_input:
@@ -31,38 +32,42 @@ def main_orchestrator(audio_input, text_input, history):
31
  except:
32
  emotion = "Neutral"
33
 
34
- # 2. THE "STRICT" MESSAGE FIX
35
- # We must turn history into a LIST of DICTIONARIES where 'content' is ALSO A LIST
36
  api_history = []
37
  for u, b in history:
38
  api_history.append({
39
  "role": "user",
40
- "content": [{"type": "text", "text": str(u)}] # <--- This list is the fix!
41
  })
42
  api_history.append({
43
  "role": "assistant",
44
  "content": [{"type": "text", "text": str(b)}]
45
  })
46
 
47
- # 3. THE "STRICT" CURRENT MESSAGE FIX
48
  bundled_text = f"Context: User is {emotion}. Message: {text_input}"
49
  current_message = {
50
  "role": "user",
51
- "content": [{"type": "text", "text": bundled_text}] # <--- Nested list here too
52
  }
53
 
54
- # 4. CALL LLM
55
  try:
56
- # Now the data matches the 'ChatMessage' object exactly
57
  response = llm_english.predict(
58
  message=current_message,
59
  history=api_history,
60
  api_name="/chat"
61
  )
62
  except Exception as e:
63
- response = f"LLM Error: {str(e)}"
 
 
 
 
64
 
65
- # 5. TTS & RETURN
66
  audio_res = None
67
  try:
68
  if tts_client and response:
@@ -70,7 +75,8 @@ def main_orchestrator(audio_input, text_input, history):
70
  except:
71
  audio_res = None
72
 
73
- # For the UI Chatbot, we keep it simple so it doesn't crash older versions
 
74
  history.append([text_input, response])
75
  return history, audio_res
76
 
@@ -83,10 +89,11 @@ with gr.Blocks() as demo:
83
  text_in = gr.Textbox(label="Message")
84
  btn = gr.Button("Send")
85
  with gr.Column():
86
- # REMOVED type="messages" to fix the TypeError
87
- chatbot_ui = gr.Chatbot(label="Therapy History")
88
  audio_out = gr.Audio(autoplay=True)
89
 
90
  btn.click(main_orchestrator, [audio_in, text_in, state], [chatbot_ui, audio_out])
91
 
 
92
  demo.launch()
 
18
  tts_client = safe_client("E-motionAssistant/Space3")
19
 
20
  def main_orchestrator(audio_input, text_input, history):
21
+ # If history is None, initialize it
22
  if history is None:
23
  history = []
24
 
25
+ # 1. EMOTION LOGIC
26
  emotion = "Neutral"
27
  try:
28
  if audio_input:
 
32
  except:
33
  emotion = "Neutral"
34
 
35
+ # 2. THE STRICT MESSAGE FIX (This stops the "Data Incompatible" error)
36
+ # Convert simple history [[u,b]] to strict Gradio 5/6 api_history
37
  api_history = []
38
  for u, b in history:
39
  api_history.append({
40
  "role": "user",
41
+ "content": [{"type": "text", "text": str(u)}]
42
  })
43
  api_history.append({
44
  "role": "assistant",
45
  "content": [{"type": "text", "text": str(b)}]
46
  })
47
 
48
+ # Prepare the current message as a content block list
49
  bundled_text = f"Context: User is {emotion}. Message: {text_input}"
50
  current_message = {
51
  "role": "user",
52
+ "content": [{"type": "text", "text": bundled_text}]
53
  }
54
 
55
+ # 3. CALL LLM
56
  try:
57
+ # We send the strictly formatted current_message and api_history
58
  response = llm_english.predict(
59
  message=current_message,
60
  history=api_history,
61
  api_name="/chat"
62
  )
63
  except Exception as e:
64
+ # Fallback for LLMs that might still want simple strings
65
+ try:
66
+ response = llm_english.predict(message=bundled_text, history=history, api_name="/chat")
67
+ except:
68
+ response = f"LLM Error: {str(e)}"
69
 
70
+ # 4. TTS LOGIC
71
  audio_res = None
72
  try:
73
  if tts_client and response:
 
75
  except:
76
  audio_res = None
77
 
78
+ # 5. UPDATE HISTORY
79
+ # Return as list of lists for the UI Chatbot
80
  history.append([text_input, response])
81
  return history, audio_res
82
 
 
89
  text_in = gr.Textbox(label="Message")
90
  btn = gr.Button("Send")
91
  with gr.Column():
92
+ # type="messages" creates the dictionary format seen in your docs
93
+ chatbot_ui = gr.Chatbot(label="Therapy History", type="messages")
94
  audio_out = gr.Audio(autoplay=True)
95
 
96
  btn.click(main_orchestrator, [audio_in, text_in, state], [chatbot_ui, audio_out])
97
 
98
+ # Launch with ssr_mode=False if you see flickering
99
  demo.launch()