File size: 2,668 Bytes
a3ad4ce f59d609 a3ad4ce f59d609 a3ad4ce d4c95d0 a3ad4ce f6e2462 a3ad4ce f6e2462 a3ad4ce 330bbb1 f6e2462 330bbb1 d4c95d0 a3ad4ce 330bbb1 a3ad4ce f6e2462 a3ad4ce d4c95d0 a3ad4ce 330bbb1 a3ad4ce f6e2462 a3ad4ce f6e2462 8bc1789 a3ad4ce f59d609 a3ad4ce | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | import gradio as gr
from gradio_client import Client, handle_file
import os
hf_token = os.environ.get("HF_TOKEN")
def safe_client(space_id):
try:
return Client(space_id, token=hf_token)
except Exception as e:
print(f"⚠️ Connection failed for {space_id}: {e}")
return None
# Sub-space clients
speech_emo_client = safe_client("E-motionAssistant/ser-wav2vec")
text_emo_client = safe_client("E-motionAssistant/Space4")
llm_english = safe_client("E-motionAssistant/TherapyEnglish")
tts_client = safe_client("E-motionAssistant/Space3")
def main_orchestrator(audio_input, text_input, history):
if history is None:
history = []
# 1. EMOTION LOGIC
emotion = "Neutral"
try:
if audio_input:
emotion = speech_emo_client.predict(handle_file(audio_input), api_name="/predict")
elif text_input:
emotion = text_emo_client.predict(text_input, api_name="/predict")
except:
emotion = "Neutral"
# 2. FORMAT FOR SUB-SPACE (STRICT DICTIONARY)
# Even if our UI is old, our sub-space (TherapyEnglish) needs the new format
api_history = []
for u, b in history:
api_history.append({"role": "user", "content": [{"type": "text", "text": str(u)}]})
api_history.append({"role": "assistant", "content": [{"type": "text", "text": str(b)}]})
bundled_text = f"Context: User is {emotion}. Message: {text_input}"
current_message = {"role": "user", "content": [{"type": "text", "text": bundled_text}]}
# 3. CALL LLM
try:
response = llm_english.predict(
message=current_message,
history=api_history,
api_name="/chat"
)
except Exception as e:
response = f"LLM Error: {str(e)}"
# 4. TTS LOGIC
audio_res = None
try:
if tts_client and response:
audio_res = tts_client.predict(str(response), api_name="/predict")
except:
audio_res = None
# 5. UPDATE HISTORY (Simple list of lists for old Gradio UI)
history.append([text_input, response])
return history, audio_res
with gr.Blocks() as demo:
state = gr.State([])
with gr.Row():
with gr.Column():
audio_in = gr.Audio(label="Voice", type="filepath")
text_in = gr.Textbox(label="Message")
btn = gr.Button("Send")
with gr.Column():
# REMOVED type="messages" to stop the crash
chatbot_ui = gr.Chatbot(label="Therapy History")
audio_out = gr.Audio(autoplay=True)
btn.click(main_orchestrator, [audio_in, text_in, state], [chatbot_ui, audio_out])
demo.launch() |