Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,17 +2,16 @@ 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 |
-
"""
|
| 10 |
try:
|
| 11 |
-
# Converts "User/Space-Name" to "https://user-space-name.hf.space"
|
| 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"⚠️
|
| 16 |
return None
|
| 17 |
|
| 18 |
# 2. CONNECT TO YOUR 6 SPACES
|
|
@@ -24,7 +23,7 @@ llm_tamil = safe_client("E-motionAssistant/TherapyTamil")
|
|
| 24 |
tts_client = safe_client("E-motionAssistant/Space3")
|
| 25 |
|
| 26 |
def detect_language(text):
|
| 27 |
-
"""Detects Sinhala, Tamil, or English
|
| 28 |
if any("\u0D80" <= char <= "\u0DFF" for char in text):
|
| 29 |
return "sinhala"
|
| 30 |
if any("\u0B80" <= char <= "\u0BFF" for char in text):
|
|
@@ -35,27 +34,20 @@ def main_orchestrator(audio_input, text_input, history):
|
|
| 35 |
if history is None:
|
| 36 |
history = []
|
| 37 |
|
| 38 |
-
# --- STEP A: Format History for the LLM API (List of Dicts) ---
|
| 39 |
-
# This fixes the "Data incompatible" error for Gradio 5/6
|
| 40 |
-
api_history = []
|
| 41 |
-
for user_msg, bot_msg in history:
|
| 42 |
-
api_history.append({"role": "user", "content": user_msg})
|
| 43 |
-
api_history.append({"role": "assistant", "content": bot_msg})
|
| 44 |
-
|
| 45 |
try:
|
| 46 |
-
# ---
|
| 47 |
emotion = "Neutral"
|
| 48 |
if audio_input and speech_emo_client:
|
| 49 |
-
#
|
| 50 |
emotion = speech_emo_client.predict(handle_file(audio_input), api_name="/predict")
|
| 51 |
elif text_input and text_emo_client:
|
| 52 |
-
#
|
| 53 |
try:
|
| 54 |
emotion = text_emo_client.predict(text_input, api_name="/predict")
|
| 55 |
except:
|
| 56 |
-
emotion = "Neutral
|
| 57 |
|
| 58 |
-
# ---
|
| 59 |
lang = detect_language(text_input)
|
| 60 |
if lang == "sinhala":
|
| 61 |
selected_llm = llm_sinhala
|
|
@@ -65,35 +57,27 @@ def main_orchestrator(audio_input, text_input, history):
|
|
| 65 |
selected_llm = llm_english
|
| 66 |
|
| 67 |
if not selected_llm:
|
| 68 |
-
return history + [[text_input, "Target LLM Space is
|
| 69 |
|
| 70 |
-
# ---
|
| 71 |
-
#
|
| 72 |
-
|
| 73 |
|
| 74 |
-
# 1. Create a "Context Block" from previous messages
|
| 75 |
-
context_memory = ""
|
| 76 |
if len(history) > 0:
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
context_memory += f"User: {user_msg}\nAssistant: {bot_msg}\n"
|
| 81 |
|
| 82 |
-
|
| 83 |
-
# This tells the LLM the emotion and what was said before
|
| 84 |
-
final_bundled_message = (
|
| 85 |
-
f"Context:\nUser is feeling {emotion}.\n"
|
| 86 |
-
f"{context_memory}\n"
|
| 87 |
-
f"Current User Message: {text_input}"
|
| 88 |
-
)
|
| 89 |
|
| 90 |
-
#
|
|
|
|
| 91 |
response_text = selected_llm.predict(
|
| 92 |
-
message=
|
| 93 |
api_name="/chat"
|
| 94 |
)
|
| 95 |
|
| 96 |
-
# ---
|
| 97 |
audio_response = None
|
| 98 |
if tts_client and response_text:
|
| 99 |
try:
|
|
@@ -101,37 +85,35 @@ def main_orchestrator(audio_input, text_input, history):
|
|
| 101 |
except:
|
| 102 |
audio_response = None
|
| 103 |
|
| 104 |
-
# ---
|
| 105 |
-
# Gradio's Chatbot UI still needs the List-of-Lists format
|
| 106 |
history.append((text_input, response_text))
|
| 107 |
return history, audio_response
|
| 108 |
|
| 109 |
except Exception as e:
|
| 110 |
-
history.append((text_input, f"
|
| 111 |
return history, None
|
| 112 |
|
| 113 |
-
# ---
|
| 114 |
-
with gr.Blocks(theme=gr.themes.
|
| 115 |
-
gr.Markdown("#
|
| 116 |
-
gr.Markdown("
|
| 117 |
|
| 118 |
-
|
| 119 |
|
| 120 |
with gr.Row():
|
| 121 |
with gr.Column(scale=1):
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
|
| 126 |
with gr.Column(scale=2):
|
| 127 |
-
|
| 128 |
-
|
| 129 |
|
| 130 |
-
|
| 131 |
-
submit_btn.click(
|
| 132 |
main_orchestrator,
|
| 133 |
-
inputs=[
|
| 134 |
-
outputs=[
|
| 135 |
)
|
| 136 |
|
| 137 |
demo.launch()
|
|
|
|
| 2 |
from gradio_client import Client, handle_file
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
# 1. SETUP: Access Token for Private Spaces
|
| 6 |
hf_token = os.environ.get("HF_TOKEN")
|
| 7 |
|
| 8 |
def safe_client(space_id):
|
| 9 |
+
"""Connects to a space using its direct URL to ensure compatibility."""
|
| 10 |
try:
|
|
|
|
| 11 |
space_url = f"https://{space_id.replace('/', '-').lower()}.hf.space"
|
| 12 |
return Client(space_url, hf_token=hf_token)
|
| 13 |
except Exception as e:
|
| 14 |
+
print(f"⚠️ Connection failed for {space_id}: {e}")
|
| 15 |
return None
|
| 16 |
|
| 17 |
# 2. CONNECT TO YOUR 6 SPACES
|
|
|
|
| 23 |
tts_client = safe_client("E-motionAssistant/Space3")
|
| 24 |
|
| 25 |
def detect_language(text):
|
| 26 |
+
"""Detects Sinhala, Tamil, or English script."""
|
| 27 |
if any("\u0D80" <= char <= "\u0DFF" for char in text):
|
| 28 |
return "sinhala"
|
| 29 |
if any("\u0B80" <= char <= "\u0BFF" for char in text):
|
|
|
|
| 34 |
if history is None:
|
| 35 |
history = []
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
try:
|
| 38 |
+
# --- A. EMOTION DETECTION ---
|
| 39 |
emotion = "Neutral"
|
| 40 |
if audio_input and speech_emo_client:
|
| 41 |
+
# Get emotion from Speech (SER)
|
| 42 |
emotion = speech_emo_client.predict(handle_file(audio_input), api_name="/predict")
|
| 43 |
elif text_input and text_emo_client:
|
| 44 |
+
# Get emotion from Text (TER)
|
| 45 |
try:
|
| 46 |
emotion = text_emo_client.predict(text_input, api_name="/predict")
|
| 47 |
except:
|
| 48 |
+
emotion = "Neutral"
|
| 49 |
|
| 50 |
+
# --- B. LANGUAGE ROUTING ---
|
| 51 |
lang = detect_language(text_input)
|
| 52 |
if lang == "sinhala":
|
| 53 |
selected_llm = llm_sinhala
|
|
|
|
| 57 |
selected_llm = llm_english
|
| 58 |
|
| 59 |
if not selected_llm:
|
| 60 |
+
return history + [[text_input, "Target LLM Space is Offline."]], None
|
| 61 |
|
| 62 |
+
# --- C. THE BUNDLE (The 'One Parameter' Fix) ---
|
| 63 |
+
# We package History + Emotion + Message into one string
|
| 64 |
+
context_block = f"[Instruction: User is feeling {emotion}]\n"
|
| 65 |
|
|
|
|
|
|
|
| 66 |
if len(history) > 0:
|
| 67 |
+
context_block += "Previous turns:\n"
|
| 68 |
+
for user_msg, bot_msg in history[-2:]: # Last 2 turns for context
|
| 69 |
+
context_block += f"User: {user_msg}\nAssistant: {bot_msg}\n"
|
|
|
|
| 70 |
|
| 71 |
+
bundled_message = f"{context_block}\nCurrent Message: {text_input}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
+
# --- D. GET LLM RESPONSE ---
|
| 74 |
+
# Sending only the 'message' parameter as required by your LLM spaces
|
| 75 |
response_text = selected_llm.predict(
|
| 76 |
+
message=bundled_message,
|
| 77 |
api_name="/chat"
|
| 78 |
)
|
| 79 |
|
| 80 |
+
# --- E. TEXT TO SPEECH ---
|
| 81 |
audio_response = None
|
| 82 |
if tts_client and response_text:
|
| 83 |
try:
|
|
|
|
| 85 |
except:
|
| 86 |
audio_response = None
|
| 87 |
|
| 88 |
+
# --- F. UPDATE UI ---
|
|
|
|
| 89 |
history.append((text_input, response_text))
|
| 90 |
return history, audio_response
|
| 91 |
|
| 92 |
except Exception as e:
|
| 93 |
+
history.append((text_input, f"Connection Error: {str(e)}"))
|
| 94 |
return history, None
|
| 95 |
|
| 96 |
+
# --- 3. THE USER INTERFACE ---
|
| 97 |
+
with gr.Blocks(theme=gr.themes.Default()) as demo:
|
| 98 |
+
gr.Markdown("# 🧠 Emotional Therapy AI Hub")
|
| 99 |
+
gr.Markdown("English | සිංහල | தமிழ்")
|
| 100 |
|
| 101 |
+
chat_state = gr.State([]) # Local Session Memory
|
| 102 |
|
| 103 |
with gr.Row():
|
| 104 |
with gr.Column(scale=1):
|
| 105 |
+
audio_input = gr.Audio(label="Voice (for Emotion)", type="filepath")
|
| 106 |
+
text_input = gr.Textbox(label="Message", placeholder="Tell me how you feel...")
|
| 107 |
+
send_btn = gr.Button("Send", variant="primary")
|
| 108 |
|
| 109 |
with gr.Column(scale=2):
|
| 110 |
+
chatbot_ui = gr.Chatbot(label="Therapy Session")
|
| 111 |
+
voice_output = gr.Audio(label="AI Voice Response", autoplay=True)
|
| 112 |
|
| 113 |
+
send_btn.click(
|
|
|
|
| 114 |
main_orchestrator,
|
| 115 |
+
inputs=[audio_input, text_input, chat_state],
|
| 116 |
+
outputs=[chatbot_ui, voice_output]
|
| 117 |
)
|
| 118 |
|
| 119 |
demo.launch()
|