Update app.py
Browse files
app.py
CHANGED
|
@@ -1,22 +1,17 @@
|
|
| 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"⚠️
|
| 17 |
return None
|
| 18 |
|
| 19 |
-
#
|
| 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")
|
|
@@ -25,12 +20,8 @@ tts_client = safe_client("E-motionAssistant/Space3")
|
|
| 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 |
-
#
|
| 34 |
emotion = "Neutral"
|
| 35 |
try:
|
| 36 |
if audio_input:
|
|
@@ -40,8 +31,7 @@ def main_orchestrator(audio_input, text_input, history):
|
|
| 40 |
except:
|
| 41 |
emotion = "Neutral"
|
| 42 |
|
| 43 |
-
#
|
| 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)}]})
|
|
@@ -50,6 +40,7 @@ def main_orchestrator(audio_input, text_input, history):
|
|
| 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,
|
|
@@ -58,7 +49,7 @@ def main_orchestrator(audio_input, text_input, history):
|
|
| 58 |
except Exception as e:
|
| 59 |
response = f"LLM Error: {str(e)}"
|
| 60 |
|
| 61 |
-
#
|
| 62 |
audio_res = None
|
| 63 |
try:
|
| 64 |
if tts_client:
|
|
@@ -66,6 +57,8 @@ def main_orchestrator(audio_input, text_input, history):
|
|
| 66 |
except:
|
| 67 |
audio_res = None
|
| 68 |
|
|
|
|
|
|
|
| 69 |
history.append([text_input, response])
|
| 70 |
return history, audio_res
|
| 71 |
|
|
@@ -78,9 +71,10 @@ with gr.Blocks() as demo:
|
|
| 78 |
text_in = gr.Textbox(label="Message")
|
| 79 |
btn = gr.Button("Send")
|
| 80 |
with gr.Column():
|
| 81 |
-
|
|
|
|
| 82 |
audio_out = gr.Audio(autoplay=True)
|
| 83 |
|
| 84 |
-
btn.click(main_orchestrator, [audio_in, text_in, state], [
|
| 85 |
|
| 86 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
from gradio_client import Client, handle_file
|
| 3 |
import os
|
| 4 |
|
|
|
|
| 5 |
hf_token = os.environ.get("HF_TOKEN")
|
| 6 |
|
|
|
|
|
|
|
| 7 |
def safe_client(space_id):
|
| 8 |
try:
|
|
|
|
| 9 |
return Client(space_id, token=hf_token)
|
| 10 |
except Exception as e:
|
| 11 |
+
print(f"⚠️ Connection failed for {space_id}: {e}")
|
| 12 |
return None
|
| 13 |
|
| 14 |
+
# Sub-space clients
|
| 15 |
speech_emo_client = safe_client("E-motionAssistant/ser-wav2vec")
|
| 16 |
text_emo_client = safe_client("E-motionAssistant/Space4")
|
| 17 |
llm_english = safe_client("E-motionAssistant/TherapyEnglish")
|
|
|
|
| 20 |
def main_orchestrator(audio_input, text_input, history):
|
| 21 |
if history is None:
|
| 22 |
history = []
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
# 1. EMOTION LOGIC
|
| 25 |
emotion = "Neutral"
|
| 26 |
try:
|
| 27 |
if audio_input:
|
|
|
|
| 31 |
except:
|
| 32 |
emotion = "Neutral"
|
| 33 |
|
| 34 |
+
# 2. LLM LOGIC (Strict format for the sub-space API)
|
|
|
|
| 35 |
api_history = []
|
| 36 |
for u, b in history:
|
| 37 |
api_history.append({"role": "user", "content": [{"type": "text", "text": str(u)}]})
|
|
|
|
| 40 |
prompt = f"Context: User is {emotion}. Message: {text_input}"
|
| 41 |
|
| 42 |
try:
|
| 43 |
+
# Calling your sub-space
|
| 44 |
response = llm_english.predict(
|
| 45 |
message={"role": "user", "content": [{"type": "text", "text": prompt}]},
|
| 46 |
history=api_history,
|
|
|
|
| 49 |
except Exception as e:
|
| 50 |
response = f"LLM Error: {str(e)}"
|
| 51 |
|
| 52 |
+
# 3. TTS LOGIC
|
| 53 |
audio_res = None
|
| 54 |
try:
|
| 55 |
if tts_client:
|
|
|
|
| 57 |
except:
|
| 58 |
audio_res = None
|
| 59 |
|
| 60 |
+
# 4. UPDATE HISTORY
|
| 61 |
+
# Simple list format [[user, bot]] is compatible with ALL Gradio versions
|
| 62 |
history.append([text_input, response])
|
| 63 |
return history, audio_res
|
| 64 |
|
|
|
|
| 71 |
text_in = gr.Textbox(label="Message")
|
| 72 |
btn = gr.Button("Send")
|
| 73 |
with gr.Column():
|
| 74 |
+
# REMOVED type="messages" to fix the TypeError
|
| 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()
|