Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,6 +13,7 @@ def safe_client(space_id):
|
|
| 13 |
print(f"⚠️ Connection failed for {space_id}: {e}")
|
| 14 |
return None
|
| 15 |
|
|
|
|
| 16 |
speech_emo_client = safe_client("E-motionAssistant/ser-wav2vec")
|
| 17 |
text_emo_client = safe_client("E-motionAssistant/Space4")
|
| 18 |
llm_sinhala = safe_client("E-motionAssistant/Therapy-Sinhala")
|
|
@@ -26,49 +27,82 @@ def detect_language(text):
|
|
| 26 |
return "english"
|
| 27 |
|
| 28 |
def main_orchestrator(audio_input, text_input, history):
|
| 29 |
-
# 1. INITIALIZE: If it's the first message, history is an empty list []
|
| 30 |
if history is None:
|
| 31 |
history = []
|
| 32 |
|
| 33 |
-
#
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
| 37 |
else:
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
#
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
#
|
| 45 |
-
#
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
history=history, # This 'history' is the saved list from the previous turns
|
| 49 |
-
api_name="/chat"
|
| 50 |
-
)
|
| 51 |
|
| 52 |
-
#
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
-
|
| 58 |
-
# The UI Chatbot will automatically render this list of dictionaries
|
| 59 |
-
return history, None
|
| 60 |
|
| 61 |
# --- UI ---
|
| 62 |
-
with gr.Blocks() as demo:
|
| 63 |
-
gr.Markdown("# 🧠
|
|
|
|
|
|
|
| 64 |
state = gr.State([])
|
|
|
|
| 65 |
with gr.Row():
|
| 66 |
with gr.Column():
|
| 67 |
-
audio_in = gr.Audio(label="Voice", type="filepath")
|
| 68 |
-
text_in = gr.Textbox(label="
|
| 69 |
-
btn = gr.Button("Send")
|
| 70 |
with gr.Column():
|
| 71 |
-
|
|
|
|
| 72 |
audio_out = gr.Audio(autoplay=True)
|
| 73 |
|
| 74 |
btn.click(main_orchestrator, [audio_in, text_in, state], [chat, audio_out])
|
|
|
|
| 13 |
print(f"⚠️ Connection failed for {space_id}: {e}")
|
| 14 |
return None
|
| 15 |
|
| 16 |
+
# Initializing all clients
|
| 17 |
speech_emo_client = safe_client("E-motionAssistant/ser-wav2vec")
|
| 18 |
text_emo_client = safe_client("E-motionAssistant/Space4")
|
| 19 |
llm_sinhala = safe_client("E-motionAssistant/Therapy-Sinhala")
|
|
|
|
| 27 |
return "english"
|
| 28 |
|
| 29 |
def main_orchestrator(audio_input, text_input, history):
|
|
|
|
| 30 |
if history is None:
|
| 31 |
history = []
|
| 32 |
|
| 33 |
+
# --- 1. LANGUAGE DETECTION & LLM SELECTION ---
|
| 34 |
+
# We must define 'selected_llm' inside the function!
|
| 35 |
+
lang = detect_language(text_input)
|
| 36 |
+
if lang == "sinhala":
|
| 37 |
+
selected_llm = llm_sinhala
|
| 38 |
+
elif lang == "tamil":
|
| 39 |
+
selected_llm = llm_tamil
|
| 40 |
else:
|
| 41 |
+
selected_llm = llm_english
|
| 42 |
+
|
| 43 |
+
if not selected_llm:
|
| 44 |
+
return history + [{"role": "assistant", "content": "LLM Space is offline."}], None
|
| 45 |
+
|
| 46 |
+
# --- 2. GET EMOTION ---
|
| 47 |
+
emotion = "Neutral"
|
| 48 |
+
try:
|
| 49 |
+
if audio_input:
|
| 50 |
+
emotion = speech_emo_client.predict(handle_file(audio_input), api_name="/predict")
|
| 51 |
+
elif text_input:
|
| 52 |
+
emotion = text_emo_client.predict(text_input, api_name="/predict")
|
| 53 |
+
except:
|
| 54 |
+
emotion = "Neutral"
|
| 55 |
+
|
| 56 |
+
# --- 3. CONSTRUCT THE PAYLOAD ---
|
| 57 |
+
# We bundle the emotion into the text since the LLM takes one parameter
|
| 58 |
+
current_payload = f"CONTEXT: User is feeling {emotion}. MESSAGE: {text_input}"
|
| 59 |
|
| 60 |
+
# --- 4. CALL LLM (Gradio 6 Dictionary Format) ---
|
| 61 |
+
try:
|
| 62 |
+
# We send the dictionary 'message' and the 'history'
|
| 63 |
+
response_text = selected_llm.predict(
|
| 64 |
+
message={"text": current_payload, "files": []},
|
| 65 |
+
history=history,
|
| 66 |
+
api_name="/chat"
|
| 67 |
+
)
|
| 68 |
+
except Exception as e:
|
| 69 |
+
print(f"Prediction Error: {e}")
|
| 70 |
+
# Fallback if history causes issues: try message only
|
| 71 |
+
response_text = selected_llm.predict(
|
| 72 |
+
message={"text": current_payload, "files": []},
|
| 73 |
+
api_name="/chat"
|
| 74 |
+
)
|
| 75 |
|
| 76 |
+
# --- 5. SAVE TO HISTORY (Gradio 6 format) ---
|
| 77 |
+
# Simplified format often works best for cross-space history
|
| 78 |
+
history.append({"role": "user", "content": text_input})
|
| 79 |
+
history.append({"role": "assistant", "content": response_text})
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
+
# --- 6. TTS ---
|
| 82 |
+
audio_response = None
|
| 83 |
+
if tts_client:
|
| 84 |
+
try:
|
| 85 |
+
audio_response = tts_client.predict(response_text, api_name="/predict")
|
| 86 |
+
except:
|
| 87 |
+
audio_response = None
|
| 88 |
|
| 89 |
+
return history, audio_response
|
|
|
|
|
|
|
| 90 |
|
| 91 |
# --- UI ---
|
| 92 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 93 |
+
gr.Markdown("# 🧠 E-motion Therapy Orchestrator")
|
| 94 |
+
|
| 95 |
+
# In Gradio 6, history state should be an empty list []
|
| 96 |
state = gr.State([])
|
| 97 |
+
|
| 98 |
with gr.Row():
|
| 99 |
with gr.Column():
|
| 100 |
+
audio_in = gr.Audio(label="Voice Emotion", type="filepath")
|
| 101 |
+
text_in = gr.Textbox(label="Message")
|
| 102 |
+
btn = gr.Button("Send", variant="primary")
|
| 103 |
with gr.Column():
|
| 104 |
+
# type="messages" is key for Gradio 6
|
| 105 |
+
chat = gr.Chatbot(type="messages")
|
| 106 |
audio_out = gr.Audio(autoplay=True)
|
| 107 |
|
| 108 |
btn.click(main_orchestrator, [audio_in, text_in, state], [chat, audio_out])
|