Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,51 +2,30 @@ 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 |
-
# On HF Spaces, 'token' or 'hf_token' usually both work,
|
| 11 |
-
# but 'token' is safer for general gradio_client compatibility.
|
| 12 |
return Client(space_id, token=hf_token)
|
| 13 |
except Exception as e:
|
| 14 |
print(f"⚠️ Connection failed for {space_id}: {e}")
|
| 15 |
return None
|
| 16 |
|
| 17 |
-
#
|
| 18 |
speech_emo_client = safe_client("E-motionAssistant/ser-wav2vec")
|
| 19 |
text_emo_client = safe_client("E-motionAssistant/Space4")
|
| 20 |
llm_english = safe_client("E-motionAssistant/TherapyEnglish")
|
| 21 |
tts_client = safe_client("E-motionAssistant/Space3")
|
| 22 |
|
| 23 |
def main_orchestrator(audio_input, text_input, history):
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
# The documentation explicitly shows only 2 parameters: audio_input and text_input
|
| 30 |
-
result = client.predict(
|
| 31 |
-
audio_input=handle_file('https://github.com/gradio-app/gradio/raw/main/test/test_files/audio_sample.wav'),
|
| 32 |
-
text_input="Hello!!",
|
| 33 |
-
api_name="/main_orchestrator"
|
| 34 |
-
)
|
| 35 |
-
|
| 36 |
-
# result is a tuple: [0] is the Chat history list, [1] is the AI Voice filepath
|
| 37 |
-
chat_history = result[0]
|
| 38 |
-
audio_path = result[1]
|
| 39 |
-
|
| 40 |
-
print(f"AI Response: {chat_history[-1]['content'][0]['text']}")
|
| 41 |
-
print(f"Audio File Saved At: {audio_path}")
|
| 42 |
if history is None:
|
| 43 |
history = []
|
| 44 |
|
| 45 |
-
# If the LLM space is down, don't crash
|
| 46 |
-
if not llm_english:
|
| 47 |
-
history.append([text_input, "⚠️ The Therapy Space is currently unavailable. Please try again later."])
|
| 48 |
-
return history, None
|
| 49 |
-
|
| 50 |
# --- 1. EMOTION DETECTION ---
|
| 51 |
emotion = "Neutral"
|
| 52 |
try:
|
|
@@ -54,42 +33,30 @@ print(f"Audio File Saved At: {audio_path}")
|
|
| 54 |
emotion = speech_emo_client.predict(handle_file(audio_input), api_name="/predict")
|
| 55 |
elif text_input:
|
| 56 |
emotion = text_emo_client.predict(text_input, api_name="/predict")
|
| 57 |
-
except
|
| 58 |
emotion = "Neutral"
|
| 59 |
|
| 60 |
-
# --- 2.
|
| 61 |
-
# We convert the UI's simple list [[u,b]] into the API's required Dictionary format
|
| 62 |
api_history = []
|
| 63 |
-
for
|
| 64 |
-
api_history.append({
|
| 65 |
-
|
| 66 |
-
"content": [{"type": "text", "text": str(user_msg)}]
|
| 67 |
-
})
|
| 68 |
-
api_history.append({
|
| 69 |
-
"role": "assistant",
|
| 70 |
-
"content": [{"type": "text", "text": str(bot_msg)}]
|
| 71 |
-
})
|
| 72 |
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
current_message = {
|
| 76 |
-
"role": "user",
|
| 77 |
-
"content": [{"type": "text", "text": bundled_text}]
|
| 78 |
-
}
|
| 79 |
|
| 80 |
-
# --- 3. LLM
|
| 81 |
try:
|
| 82 |
-
#
|
| 83 |
response_text = llm_english.predict(
|
| 84 |
message=current_message,
|
| 85 |
history=api_history,
|
| 86 |
api_name="/chat"
|
| 87 |
)
|
| 88 |
except Exception as e:
|
| 89 |
-
|
| 90 |
-
response_text = "I'm sorry, I'm having trouble connecting to my thoughts. Could you repeat that?"
|
| 91 |
|
| 92 |
-
# --- 4.
|
| 93 |
audio_res = None
|
| 94 |
if tts_client and response_text:
|
| 95 |
try:
|
|
@@ -97,41 +64,37 @@ print(f"Audio File Saved At: {audio_path}")
|
|
| 97 |
except:
|
| 98 |
audio_res = None
|
| 99 |
|
| 100 |
-
# --- 5. UPDATE
|
| 101 |
-
# We
|
|
|
|
| 102 |
history.append([text_input, response_text])
|
|
|
|
| 103 |
return history, audio_res
|
| 104 |
|
| 105 |
-
# --- THE
|
| 106 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 107 |
-
gr.Markdown("#
|
| 108 |
-
gr.Markdown("Memory is preserved during your session but clears if you refresh the page.")
|
| 109 |
|
| 110 |
-
# Session
|
| 111 |
chat_state = gr.State([])
|
| 112 |
|
| 113 |
with gr.Row():
|
| 114 |
-
with gr.Column(
|
|
|
|
| 115 |
audio_in = gr.Audio(label="Voice Input (Speech Emotion)", type="filepath")
|
| 116 |
-
text_in = gr.Textbox(label="Your Message"
|
|
|
|
| 117 |
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
with gr.Column(scale=2):
|
| 123 |
-
chatbot_ui = gr.Chatbot(label="Therapy Session History")
|
| 124 |
audio_out = gr.Audio(label="AI Therapist Voice", autoplay=True)
|
| 125 |
|
| 126 |
-
#
|
| 127 |
submit_btn.click(
|
| 128 |
main_orchestrator,
|
| 129 |
inputs=[audio_in, text_in, chat_state],
|
| 130 |
-
outputs=[chatbot_ui,
|
| 131 |
)
|
| 132 |
-
|
| 133 |
-
# 2. CLEAR ACTION (Wipes memory without refresh)
|
| 134 |
-
clear_btn.click(lambda: ([], [], None), None, [chatbot_ui, chat_state, audio_out])
|
| 135 |
|
| 136 |
-
# IMPORTANT: In your HF Space 'Settings', add a Secret called 'HF_TOKEN'
|
| 137 |
demo.launch()
|
|
|
|
| 2 |
from gradio_client import Client, handle_file
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
# 1. SETUP: API Connections to your sub-spaces
|
| 6 |
hf_token = os.environ.get("HF_TOKEN")
|
| 7 |
|
| 8 |
def safe_client(space_id):
|
| 9 |
try:
|
|
|
|
|
|
|
| 10 |
return Client(space_id, token=hf_token)
|
| 11 |
except Exception as e:
|
| 12 |
print(f"⚠️ Connection failed for {space_id}: {e}")
|
| 13 |
return None
|
| 14 |
|
| 15 |
+
# Sub-space clients
|
| 16 |
speech_emo_client = safe_client("E-motionAssistant/ser-wav2vec")
|
| 17 |
text_emo_client = safe_client("E-motionAssistant/Space4")
|
| 18 |
llm_english = safe_client("E-motionAssistant/TherapyEnglish")
|
| 19 |
tts_client = safe_client("E-motionAssistant/Space3")
|
| 20 |
|
| 21 |
def main_orchestrator(audio_input, text_input, history):
|
| 22 |
+
"""
|
| 23 |
+
This function matches the API docs:
|
| 24 |
+
Inputs: audio_input (filepath), text_input (str), history (gr.State)
|
| 25 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
if history is None:
|
| 27 |
history = []
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
# --- 1. EMOTION DETECTION ---
|
| 30 |
emotion = "Neutral"
|
| 31 |
try:
|
|
|
|
| 33 |
emotion = speech_emo_client.predict(handle_file(audio_input), api_name="/predict")
|
| 34 |
elif text_input:
|
| 35 |
emotion = text_emo_client.predict(text_input, api_name="/predict")
|
| 36 |
+
except:
|
| 37 |
emotion = "Neutral"
|
| 38 |
|
| 39 |
+
# --- 2. FORMAT FOR LLM (Strict Gradio 5/6 ChatMessage) ---
|
|
|
|
| 40 |
api_history = []
|
| 41 |
+
for u, b in history:
|
| 42 |
+
api_history.append({"role": "user", "content": [{"type": "text", "text": str(u)}]})
|
| 43 |
+
api_history.append({"role": "assistant", "content": [{"type": "text", "text": str(b)}]})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
+
bundled_text = f"Context: User is {emotion}. Message: {text_input}"
|
| 46 |
+
current_message = {"role": "user", "content": [{"type": "text", "text": bundled_text}]}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
+
# --- 3. CALL LLM ---
|
| 49 |
try:
|
| 50 |
+
# We use .predict() to call the TherapyEnglish space
|
| 51 |
response_text = llm_english.predict(
|
| 52 |
message=current_message,
|
| 53 |
history=api_history,
|
| 54 |
api_name="/chat"
|
| 55 |
)
|
| 56 |
except Exception as e:
|
| 57 |
+
response_text = "I'm having trouble connecting to my therapy tools. Please try again."
|
|
|
|
| 58 |
|
| 59 |
+
# --- 4. TTS ---
|
| 60 |
audio_res = None
|
| 61 |
if tts_client and response_text:
|
| 62 |
try:
|
|
|
|
| 64 |
except:
|
| 65 |
audio_res = None
|
| 66 |
|
| 67 |
+
# --- 5. UPDATE HISTORY ---
|
| 68 |
+
# We store as simple [user, bot] pairs; Gradio Chatbot (type='messages')
|
| 69 |
+
# handles the conversion to the complex dicts you saw in the docs.
|
| 70 |
history.append([text_input, response_text])
|
| 71 |
+
|
| 72 |
return history, audio_res
|
| 73 |
|
| 74 |
+
# --- THE UI (This generates the API docs you shared) ---
|
| 75 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 76 |
+
gr.Markdown("# 🧠 Session-Memory Orchestrator")
|
|
|
|
| 77 |
|
| 78 |
+
# Session memory state
|
| 79 |
chat_state = gr.State([])
|
| 80 |
|
| 81 |
with gr.Row():
|
| 82 |
+
with gr.Column():
|
| 83 |
+
# These two components map to 'audio_input' and 'text_input' in the API
|
| 84 |
audio_in = gr.Audio(label="Voice Input (Speech Emotion)", type="filepath")
|
| 85 |
+
text_in = gr.Textbox(label="Your Message")
|
| 86 |
+
submit_btn = gr.Button("Send", variant="primary")
|
| 87 |
|
| 88 |
+
with gr.Column():
|
| 89 |
+
# This 'type="messages"' is why your API docs show complex dicts!
|
| 90 |
+
chatbot_ui = gr.Chatbot(label="Therapy Session History", type="messages")
|
|
|
|
|
|
|
|
|
|
| 91 |
audio_out = gr.Audio(label="AI Therapist Voice", autoplay=True)
|
| 92 |
|
| 93 |
+
# Connecting the button
|
| 94 |
submit_btn.click(
|
| 95 |
main_orchestrator,
|
| 96 |
inputs=[audio_in, text_in, chat_state],
|
| 97 |
+
outputs=[chatbot_ui, audio_out]
|
| 98 |
)
|
|
|
|
|
|
|
|
|
|
| 99 |
|
|
|
|
| 100 |
demo.launch()
|