Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,50 +1,98 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from gradio_client import Client
|
|
|
|
| 3 |
|
| 4 |
-
# 1.
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
def main_orchestrator(audio_input, text_input, history):
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
emotion = speech_emo_client.predict(audio_input, api_name="/predict")
|
| 17 |
-
else:
|
| 18 |
-
emotion = text_emo_client.predict(text_input, api_name="/predict")
|
| 19 |
-
|
| 20 |
-
# B. Select the right LLM based on language (Simple logic example)
|
| 21 |
-
# You could add a language detection step here too
|
| 22 |
-
selected_llm = llm_english
|
| 23 |
-
if "හෙලෝ" in text_input: selected_llm = llm_sinhala
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
with gr.Row():
|
| 40 |
with gr.Column():
|
| 41 |
-
audio_in = gr.Audio(
|
| 42 |
-
text_in = gr.Textbox()
|
| 43 |
-
submit_btn = gr.Button("Send")
|
|
|
|
| 44 |
with gr.Column():
|
| 45 |
-
chatbot = gr.Chatbot()
|
| 46 |
-
audio_out = gr.Audio()
|
| 47 |
|
| 48 |
-
submit_btn.click(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from gradio_client import Client, handle_file
|
| 3 |
+
import os
|
| 4 |
|
| 5 |
+
# 1. SETUP: Get your HF Token from Settings > Secrets for private access
|
| 6 |
+
hf_token = os.environ.get("HF_TOKEN")
|
| 7 |
+
|
| 8 |
+
def safe_client(space_id):
|
| 9 |
+
"""Attempt to connect to a space, return None if it fails."""
|
| 10 |
+
try:
|
| 11 |
+
# We use the direct hf.space URL for better stability with Docker/Custom spaces
|
| 12 |
+
space_url = f"https://{space_id.replace('/', '-').lower()}.hf.space"
|
| 13 |
+
return Client(space_url, hf_token=hf_token)
|
| 14 |
+
except Exception as e:
|
| 15 |
+
print(f"⚠️ Warning: Could not connect to {space_id}. Error: {e}")
|
| 16 |
+
return None
|
| 17 |
+
|
| 18 |
+
# 2. CONNECT TO YOUR 6 SPACES
|
| 19 |
+
speech_emo_client = safe_client("E-motionAssistant/ser-wav2vec")
|
| 20 |
+
text_emo_client = safe_client("E-motionAssistant/Space4")
|
| 21 |
+
llm_sinhala = safe_client("E-motionAssistant/Therapy-Sinhala")
|
| 22 |
+
llm_english = safe_client("E-motionAssistant/TherapyEnglish")
|
| 23 |
+
llm_tamil = safe_client("E-motionAssistant/TherapyTamil")
|
| 24 |
+
tts_client = safe_client("E-motionAssistant/Space3")
|
| 25 |
+
|
| 26 |
+
def detect_language(text):
|
| 27 |
+
"""Simple script-based language detection."""
|
| 28 |
+
# Check for Sinhala characters
|
| 29 |
+
if any("\u0D80" <= char <= "\u0DFF" for char in text):
|
| 30 |
+
return "sinhala"
|
| 31 |
+
# Check for Tamil characters
|
| 32 |
+
if any("\u0B80" <= char <= "\u0BFF" for char in text):
|
| 33 |
+
return "tamil"
|
| 34 |
+
return "english"
|
| 35 |
|
| 36 |
def main_orchestrator(audio_input, text_input, history):
|
| 37 |
+
if history is None:
|
| 38 |
+
history = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
+
try:
|
| 41 |
+
# A. Determine Emotion
|
| 42 |
+
emotion = "Neutral" # Default
|
| 43 |
+
if audio_input and speech_emo_client:
|
| 44 |
+
emotion = speech_emo_client.predict(handle_file(audio_input), api_name="/predict")
|
| 45 |
+
elif text_input and text_emo_client:
|
| 46 |
+
emotion = text_emo_client.predict(text_input, api_name="/predict")
|
| 47 |
+
|
| 48 |
+
# B. Select LLM based on Language
|
| 49 |
+
lang = detect_language(text_input)
|
| 50 |
+
if lang == "sinhala" and llm_sinhala:
|
| 51 |
+
selected_llm = llm_sinhala
|
| 52 |
+
elif lang == "tamil" and llm_tamil:
|
| 53 |
+
selected_llm = llm_tamil
|
| 54 |
+
else:
|
| 55 |
+
selected_llm = llm_english
|
| 56 |
|
| 57 |
+
if not selected_llm:
|
| 58 |
+
return history + [["Error", "Selected LLM Space is offline."]], None
|
| 59 |
|
| 60 |
+
# C. Call LLM (Passing history for memory)
|
| 61 |
+
# Note: Ensure your LLM spaces accept (text, emotion, history)
|
| 62 |
+
response_text = selected_llm.predict(text_input, emotion, history, api_name="/chat")
|
| 63 |
|
| 64 |
+
# D. Convert to Speech
|
| 65 |
+
audio_response = None
|
| 66 |
+
if tts_client:
|
| 67 |
+
audio_response = tts_client.predict(response_text, api_name="/predict")
|
| 68 |
+
|
| 69 |
+
# E. Update Memory (State clears on refresh)
|
| 70 |
+
history.append((text_input, response_text))
|
| 71 |
+
return history, audio_response
|
| 72 |
+
|
| 73 |
+
except Exception as e:
|
| 74 |
+
history.append((text_input, f"System Error: {str(e)}"))
|
| 75 |
+
return history, None
|
| 76 |
+
|
| 77 |
+
# 3. THE UI
|
| 78 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 79 |
+
gr.Markdown("# 🧠 Multi-Space Emotional Assistant")
|
| 80 |
+
chat_history = gr.State([])
|
| 81 |
+
|
| 82 |
with gr.Row():
|
| 83 |
with gr.Column():
|
| 84 |
+
audio_in = gr.Audio(label="Speak", type="filepath")
|
| 85 |
+
text_in = gr.Textbox(label="Type here", placeholder="Say something...")
|
| 86 |
+
submit_btn = gr.Button("Send", variant="primary")
|
| 87 |
+
|
| 88 |
with gr.Column():
|
| 89 |
+
chatbot = gr.Chatbot(label="Therapy Session")
|
| 90 |
+
audio_out = gr.Audio(label="Assistant Voice")
|
| 91 |
|
| 92 |
+
submit_btn.click(
|
| 93 |
+
main_orchestrator,
|
| 94 |
+
[audio_in, text_in, chat_history],
|
| 95 |
+
[chatbot, audio_out]
|
| 96 |
+
)
|
| 97 |
|
| 98 |
demo.launch()
|