Update app.py
Browse files
app.py
CHANGED
|
@@ -18,7 +18,6 @@ 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, initialize it
|
| 22 |
if history is None:
|
| 23 |
history = []
|
| 24 |
|
|
@@ -32,40 +31,25 @@ def main_orchestrator(audio_input, text_input, history):
|
|
| 32 |
except:
|
| 33 |
emotion = "Neutral"
|
| 34 |
|
| 35 |
-
# 2.
|
| 36 |
-
#
|
| 37 |
api_history = []
|
| 38 |
for u, b in history:
|
| 39 |
-
api_history.append({
|
| 40 |
-
|
| 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 |
-
|
| 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
|
|
@@ -75,12 +59,10 @@ def main_orchestrator(audio_input, text_input, history):
|
|
| 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 |
|
| 83 |
-
# --- UI ---
|
| 84 |
with gr.Blocks() as demo:
|
| 85 |
state = gr.State([])
|
| 86 |
with gr.Row():
|
|
@@ -89,11 +71,10 @@ with gr.Blocks() as demo:
|
|
| 89 |
text_in = gr.Textbox(label="Message")
|
| 90 |
btn = gr.Button("Send")
|
| 91 |
with gr.Column():
|
| 92 |
-
# type="messages"
|
| 93 |
chatbot_ui = gr.Chatbot(label="Therapy History")
|
| 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()
|
|
|
|
| 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 |
|
|
|
|
| 31 |
except:
|
| 32 |
emotion = "Neutral"
|
| 33 |
|
| 34 |
+
# 2. FORMAT FOR SUB-SPACE (STRICT DICTIONARY)
|
| 35 |
+
# Even if our UI is old, our sub-space (TherapyEnglish) needs the new format
|
| 36 |
api_history = []
|
| 37 |
for u, b in history:
|
| 38 |
+
api_history.append({"role": "user", "content": [{"type": "text", "text": str(u)}]})
|
| 39 |
+
api_history.append({"role": "assistant", "content": [{"type": "text", "text": str(b)}]})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
|
|
|
| 41 |
bundled_text = f"Context: User is {emotion}. Message: {text_input}"
|
| 42 |
+
current_message = {"role": "user", "content": [{"type": "text", "text": bundled_text}]}
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
# 3. CALL LLM
|
| 45 |
try:
|
|
|
|
| 46 |
response = llm_english.predict(
|
| 47 |
message=current_message,
|
| 48 |
history=api_history,
|
| 49 |
api_name="/chat"
|
| 50 |
)
|
| 51 |
except Exception as e:
|
| 52 |
+
response = f"LLM Error: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
# 4. TTS LOGIC
|
| 55 |
audio_res = None
|
|
|
|
| 59 |
except:
|
| 60 |
audio_res = None
|
| 61 |
|
| 62 |
+
# 5. UPDATE HISTORY (Simple list of lists for old Gradio UI)
|
|
|
|
| 63 |
history.append([text_input, response])
|
| 64 |
return history, audio_res
|
| 65 |
|
|
|
|
| 66 |
with gr.Blocks() as demo:
|
| 67 |
state = gr.State([])
|
| 68 |
with gr.Row():
|
|
|
|
| 71 |
text_in = gr.Textbox(label="Message")
|
| 72 |
btn = gr.Button("Send")
|
| 73 |
with gr.Column():
|
| 74 |
+
# REMOVED type="messages" to stop the crash
|
| 75 |
chatbot_ui = gr.Chatbot(label="Therapy History")
|
| 76 |
audio_out = gr.Audio(autoplay=True)
|
| 77 |
|
| 78 |
btn.click(main_orchestrator, [audio_in, text_in, state], [chatbot_ui, audio_out])
|
| 79 |
|
|
|
|
| 80 |
demo.launch()
|