Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,86 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
# CRITICAL: This is the line the error is complaining about
|
| 3 |
+
from gradio_client import Client, handle_file
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# Get the token from Space Secrets
|
| 7 |
+
hf_token = os.environ.get("HF_TOKEN")
|
| 8 |
+
|
| 9 |
+
# 1. SETUP CLIENTS
|
| 10 |
+
# We wrap this in a function to handle errors gracefully
|
| 11 |
+
def safe_client(space_id):
|
| 12 |
+
try:
|
| 13 |
+
# If the import worked, this will now run
|
| 14 |
+
return Client(space_id, token=hf_token)
|
| 15 |
+
except Exception as e:
|
| 16 |
+
print(f"⚠️ Could not connect to {space_id}: {e}")
|
| 17 |
+
return None
|
| 18 |
+
|
| 19 |
+
# Initializing your sub-spaces
|
| 20 |
+
speech_emo_client = safe_client("E-motionAssistant/ser-wav2vec")
|
| 21 |
+
text_emo_client = safe_client("E-motionAssistant/Space4")
|
| 22 |
+
llm_english = safe_client("E-motionAssistant/TherapyEnglish")
|
| 23 |
+
tts_client = safe_client("E-motionAssistant/Space3")
|
| 24 |
+
|
| 25 |
+
def main_orchestrator(audio_input, text_input, history):
|
| 26 |
+
if history is None:
|
| 27 |
+
history = []
|
| 28 |
+
|
| 29 |
+
# Verify LLM is connected
|
| 30 |
+
if not llm_english:
|
| 31 |
+
return history + [["System", "Error: TherapyEnglish client not initialized."]], None
|
| 32 |
+
|
| 33 |
+
# --- EMOTION LOGIC ---
|
| 34 |
+
emotion = "Neutral"
|
| 35 |
+
try:
|
| 36 |
+
if audio_input:
|
| 37 |
+
emotion = speech_emo_client.predict(handle_file(audio_input), api_name="/predict")
|
| 38 |
+
elif text_input:
|
| 39 |
+
emotion = text_emo_client.predict(text_input, api_name="/predict")
|
| 40 |
+
except:
|
| 41 |
+
emotion = "Neutral"
|
| 42 |
+
|
| 43 |
+
# --- LLM LOGIC ---
|
| 44 |
+
# Convert history for Gradio 6
|
| 45 |
+
api_history = []
|
| 46 |
+
for u, b in history:
|
| 47 |
+
api_history.append({"role": "user", "content": [{"type": "text", "text": str(u)}]})
|
| 48 |
+
api_history.append({"role": "assistant", "content": [{"type": "text", "text": str(b)}]})
|
| 49 |
+
|
| 50 |
+
prompt = f"Context: User is {emotion}. Message: {text_input}"
|
| 51 |
+
|
| 52 |
+
try:
|
| 53 |
+
response = llm_english.predict(
|
| 54 |
+
message={"role": "user", "content": [{"type": "text", "text": prompt}]},
|
| 55 |
+
history=api_history,
|
| 56 |
+
api_name="/chat"
|
| 57 |
+
)
|
| 58 |
+
except Exception as e:
|
| 59 |
+
response = f"LLM Error: {str(e)}"
|
| 60 |
+
|
| 61 |
+
# --- TTS ---
|
| 62 |
+
audio_res = None
|
| 63 |
+
try:
|
| 64 |
+
if tts_client:
|
| 65 |
+
audio_res = tts_client.predict(str(response), api_name="/predict")
|
| 66 |
+
except:
|
| 67 |
+
audio_res = None
|
| 68 |
+
|
| 69 |
+
history.append([text_input, response])
|
| 70 |
+
return history, audio_res
|
| 71 |
+
|
| 72 |
+
# --- UI ---
|
| 73 |
+
with gr.Blocks() as demo:
|
| 74 |
+
state = gr.State([])
|
| 75 |
+
with gr.Row():
|
| 76 |
+
with gr.Column():
|
| 77 |
+
audio_in = gr.Audio(label="Voice", type="filepath")
|
| 78 |
+
text_in = gr.Textbox(label="Message")
|
| 79 |
+
btn = gr.Button("Send")
|
| 80 |
+
with gr.Column():
|
| 81 |
+
chat = gr.Chatbot(label="Therapy History", type="messages")
|
| 82 |
+
audio_out = gr.Audio(autoplay=True)
|
| 83 |
+
|
| 84 |
+
btn.click(main_orchestrator, [audio_in, text_in, state], [chat, audio_out])
|
| 85 |
+
|
| 86 |
+
demo.launch()
|