Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,18 +2,18 @@ import gradio as gr
|
|
| 2 |
from gradio_client import Client, handle_file
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
# 1. SETUP:
|
| 6 |
hf_token = os.environ.get("HF_TOKEN")
|
| 7 |
|
| 8 |
def safe_client(space_id):
|
| 9 |
try:
|
| 10 |
-
|
| 11 |
-
return Client(
|
| 12 |
except Exception as e:
|
| 13 |
print(f"⚠️ Connection failed for {space_id}: {e}")
|
| 14 |
return None
|
| 15 |
|
| 16 |
-
# Initializing
|
| 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")
|
|
@@ -30,20 +30,14 @@ def main_orchestrator(audio_input, text_input, history):
|
|
| 30 |
if history is None:
|
| 31 |
history = []
|
| 32 |
|
| 33 |
-
#
|
| 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 + [
|
| 45 |
|
| 46 |
-
#
|
| 47 |
emotion = "Neutral"
|
| 48 |
try:
|
| 49 |
if audio_input:
|
|
@@ -53,47 +47,37 @@ def main_orchestrator(audio_input, text_input, history):
|
|
| 53 |
except:
|
| 54 |
emotion = "Neutral"
|
| 55 |
|
| 56 |
-
#
|
| 57 |
-
|
| 58 |
-
current_payload = f"CONTEXT: User is feeling {emotion}. MESSAGE: {text_input}"
|
| 59 |
|
| 60 |
-
# --- 4. CALL LLM (Gradio 6 Dictionary Format) ---
|
| 61 |
try:
|
| 62 |
-
#
|
| 63 |
response_text = selected_llm.predict(
|
| 64 |
-
message={"text":
|
| 65 |
-
history=history,
|
| 66 |
api_name="/chat"
|
| 67 |
)
|
| 68 |
-
except
|
| 69 |
-
|
| 70 |
-
|
| 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 |
-
#
|
| 82 |
-
|
| 83 |
if tts_client:
|
| 84 |
try:
|
| 85 |
-
|
| 86 |
except:
|
| 87 |
-
|
| 88 |
|
| 89 |
-
|
|
|
|
|
|
|
| 90 |
|
| 91 |
# --- UI ---
|
| 92 |
-
|
| 93 |
-
|
|
|
|
| 94 |
|
| 95 |
-
|
| 96 |
-
state = gr.State([])
|
| 97 |
|
| 98 |
with gr.Row():
|
| 99 |
with gr.Column():
|
|
@@ -101,10 +85,11 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 101 |
text_in = gr.Textbox(label="Message")
|
| 102 |
btn = gr.Button("Send", variant="primary")
|
| 103 |
with gr.Column():
|
| 104 |
-
# type="messages"
|
| 105 |
-
chat = gr.Chatbot(
|
| 106 |
audio_out = gr.Audio(autoplay=True)
|
| 107 |
|
| 108 |
btn.click(main_orchestrator, [audio_in, text_in, state], [chat, audio_out])
|
| 109 |
|
| 110 |
-
|
|
|
|
|
|
| 2 |
from gradio_client import Client, handle_file
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
# 1. SETUP: Use 'token' instead of 'hf_token' for compatibility
|
| 6 |
hf_token = os.environ.get("HF_TOKEN")
|
| 7 |
|
| 8 |
def safe_client(space_id):
|
| 9 |
try:
|
| 10 |
+
# Changed 'hf_token' to 'token' for older client versions
|
| 11 |
+
return Client(space_id, token=hf_token)
|
| 12 |
except Exception as e:
|
| 13 |
print(f"⚠️ Connection failed for {space_id}: {e}")
|
| 14 |
return None
|
| 15 |
|
| 16 |
+
# Initializing 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")
|
|
|
|
| 30 |
if history is None:
|
| 31 |
history = []
|
| 32 |
|
| 33 |
+
# Select LLM
|
|
|
|
| 34 |
lang = detect_language(text_input)
|
| 35 |
+
selected_llm = llm_sinhala if lang == "sinhala" else llm_tamil if lang == "tamil" else llm_english
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
if not selected_llm:
|
| 38 |
+
return history + [["System", "LLM Space Offline"]], None
|
| 39 |
|
| 40 |
+
# Get Emotion
|
| 41 |
emotion = "Neutral"
|
| 42 |
try:
|
| 43 |
if audio_input:
|
|
|
|
| 47 |
except:
|
| 48 |
emotion = "Neutral"
|
| 49 |
|
| 50 |
+
# Bundle message for LLM
|
| 51 |
+
bundled_payload = f"Emotion: {emotion}\nMessage: {text_input}"
|
|
|
|
| 52 |
|
|
|
|
| 53 |
try:
|
| 54 |
+
# Calling LLM with the 'message' dictionary
|
| 55 |
response_text = selected_llm.predict(
|
| 56 |
+
message={"text": bundled_payload, "files": []},
|
|
|
|
| 57 |
api_name="/chat"
|
| 58 |
)
|
| 59 |
+
except:
|
| 60 |
+
# Fallback for older API versions
|
| 61 |
+
response_text = selected_llm.predict(bundled_payload, api_name="/predict")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
+
# TTS
|
| 64 |
+
audio_res = None
|
| 65 |
if tts_client:
|
| 66 |
try:
|
| 67 |
+
audio_res = tts_client.predict(response_text, api_name="/predict")
|
| 68 |
except:
|
| 69 |
+
audio_res = None
|
| 70 |
|
| 71 |
+
# Update history in OLD format [[user, bot]]
|
| 72 |
+
history.append([text_input, response_text])
|
| 73 |
+
return history, audio_res
|
| 74 |
|
| 75 |
# --- UI ---
|
| 76 |
+
# Removed 'theme' from here to fix the warning
|
| 77 |
+
with gr.Blocks() as demo:
|
| 78 |
+
gr.Markdown("# 🧠 E-motion Therapy Orchestrator (Legacy Fix)")
|
| 79 |
|
| 80 |
+
state = gr.State([]) # List of lists
|
|
|
|
| 81 |
|
| 82 |
with gr.Row():
|
| 83 |
with gr.Column():
|
|
|
|
| 85 |
text_in = gr.Textbox(label="Message")
|
| 86 |
btn = gr.Button("Send", variant="primary")
|
| 87 |
with gr.Column():
|
| 88 |
+
# Removed 'type="messages"' to fix the TypeError
|
| 89 |
+
chat = gr.Chatbot()
|
| 90 |
audio_out = gr.Audio(autoplay=True)
|
| 91 |
|
| 92 |
btn.click(main_orchestrator, [audio_in, text_in, state], [chat, audio_out])
|
| 93 |
|
| 94 |
+
# Theme passed to launch() as requested by Gradio 6 warning
|
| 95 |
+
demo.launch(theme=gr.themes.Soft())
|