Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,12 +2,11 @@ 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 |
-
# Note: 'token' is the correct argument for older client versions
|
| 11 |
return Client(space_id, token=hf_token)
|
| 12 |
except Exception as e:
|
| 13 |
print(f"⚠️ Connection failed for {space_id}: {e}")
|
|
@@ -30,54 +29,45 @@ def main_orchestrator(audio_input, text_input, history):
|
|
| 30 |
if history is None:
|
| 31 |
history = []
|
| 32 |
|
| 33 |
-
#
|
| 34 |
lang = detect_language(text_input)
|
| 35 |
-
if lang == "sinhala"
|
| 36 |
-
selected_llm = llm_sinhala
|
| 37 |
-
elif lang == "tamil":
|
| 38 |
-
selected_llm = llm_tamil
|
| 39 |
-
else:
|
| 40 |
-
selected_llm = llm_english
|
| 41 |
|
| 42 |
if not selected_llm:
|
| 43 |
-
return history
|
| 44 |
|
| 45 |
-
#
|
| 46 |
emotion = "Neutral"
|
| 47 |
try:
|
| 48 |
if audio_input:
|
| 49 |
-
# Use handle_file for the audio path
|
| 50 |
emotion = speech_emo_client.predict(handle_file(audio_input), api_name="/predict")
|
| 51 |
elif text_input:
|
| 52 |
emotion = text_emo_client.predict(text_input, api_name="/predict")
|
| 53 |
except:
|
| 54 |
emotion = "Neutral"
|
| 55 |
|
| 56 |
-
# ---
|
| 57 |
-
#
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
context_message += "Past Context:\n"
|
| 61 |
-
for user_msg, bot_msg in history[-2:]: # Last 2 exchanges
|
| 62 |
-
context_message += f"User: {user_msg}\nAI: {bot_msg}\n"
|
| 63 |
-
|
| 64 |
-
final_input = f"{context_message}\nCurrent Message: {text_input}"
|
| 65 |
|
| 66 |
-
# --- 4. CALL LLM (The Dictionary Wrap Fix) ---
|
| 67 |
try:
|
| 68 |
-
#
|
| 69 |
response_text = selected_llm.predict(
|
| 70 |
-
message=
|
|
|
|
| 71 |
api_name="/chat"
|
| 72 |
)
|
| 73 |
-
except:
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
| 79 |
|
| 80 |
-
#
|
| 81 |
audio_res = None
|
| 82 |
if tts_client and response_text:
|
| 83 |
try:
|
|
@@ -85,33 +75,25 @@ def main_orchestrator(audio_input, text_input, history):
|
|
| 85 |
except:
|
| 86 |
audio_res = None
|
| 87 |
|
| 88 |
-
# --- 6. UPDATE HISTORY (List of Lists Format) ---
|
| 89 |
-
history.append([text_input, response_text])
|
| 90 |
return history, audio_res
|
| 91 |
|
| 92 |
-
# ---
|
| 93 |
with gr.Blocks() as demo:
|
| 94 |
gr.Markdown("# 🧠 E-motion Therapy Orchestrator")
|
| 95 |
|
| 96 |
-
#
|
| 97 |
-
|
| 98 |
|
| 99 |
with gr.Row():
|
| 100 |
with gr.Column():
|
| 101 |
-
audio_in = gr.Audio(label="Voice
|
| 102 |
-
text_in = gr.Textbox(label="
|
| 103 |
-
|
| 104 |
-
|
| 105 |
with gr.Column():
|
| 106 |
-
#
|
| 107 |
-
|
| 108 |
-
audio_out = gr.Audio(
|
| 109 |
|
| 110 |
-
|
| 111 |
-
main_orchestrator,
|
| 112 |
-
inputs=[audio_in, text_in, chat_state],
|
| 113 |
-
outputs=[chatbot_ui, audio_out]
|
| 114 |
-
)
|
| 115 |
|
| 116 |
-
# Theme is passed to launch() to resolve the UserWarning
|
| 117 |
demo.launch(theme=gr.themes.Soft())
|
|
|
|
| 2 |
from gradio_client import Client, handle_file
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
# 1. SETUP: Use 'token' for older client versions
|
| 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}")
|
|
|
|
| 29 |
if history is None:
|
| 30 |
history = []
|
| 31 |
|
| 32 |
+
# Select LLM
|
| 33 |
lang = detect_language(text_input)
|
| 34 |
+
selected_llm = llm_sinhala if lang == "sinhala" else llm_tamil if lang == "tamil" else llm_english
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
if not selected_llm:
|
| 37 |
+
return history, None
|
| 38 |
|
| 39 |
+
# Get Emotion
|
| 40 |
emotion = "Neutral"
|
| 41 |
try:
|
| 42 |
if audio_input:
|
|
|
|
| 43 |
emotion = speech_emo_client.predict(handle_file(audio_input), api_name="/predict")
|
| 44 |
elif text_input:
|
| 45 |
emotion = text_emo_client.predict(text_input, api_name="/predict")
|
| 46 |
except:
|
| 47 |
emotion = "Neutral"
|
| 48 |
|
| 49 |
+
# --- THE CRITICAL FIX: DICTIONARY FORMAT ---
|
| 50 |
+
# 1. Create the current message in the 'role'/'content' format
|
| 51 |
+
bundled_text = f"Context: User is {emotion}. Message: {text_input}"
|
| 52 |
+
current_message = {"role": "user", "content": bundled_text}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
|
|
|
| 54 |
try:
|
| 55 |
+
# 2. Call LLM with the message dictionary and history list
|
| 56 |
response_text = selected_llm.predict(
|
| 57 |
+
message=current_message,
|
| 58 |
+
history=history, # history must also be a list of dicts
|
| 59 |
api_name="/chat"
|
| 60 |
)
|
| 61 |
+
except Exception as e:
|
| 62 |
+
print(f"API Error: {e}")
|
| 63 |
+
response_text = "I'm sorry, I'm having trouble connecting to my brain right now."
|
| 64 |
+
|
| 65 |
+
# --- SAVE TO HISTORY AS DICTIONARIES ---
|
| 66 |
+
# We save in the format the LLM asked for: {'role': '...', 'content': '...'}
|
| 67 |
+
history.append({"role": "user", "content": text_input})
|
| 68 |
+
history.append({"role": "assistant", "content": response_text})
|
| 69 |
|
| 70 |
+
# TTS
|
| 71 |
audio_res = None
|
| 72 |
if tts_client and response_text:
|
| 73 |
try:
|
|
|
|
| 75 |
except:
|
| 76 |
audio_res = None
|
| 77 |
|
|
|
|
|
|
|
| 78 |
return history, audio_res
|
| 79 |
|
| 80 |
+
# --- UI ---
|
| 81 |
with gr.Blocks() as demo:
|
| 82 |
gr.Markdown("# 🧠 E-motion Therapy Orchestrator")
|
| 83 |
|
| 84 |
+
# In Gradio 6, state starts as an empty list []
|
| 85 |
+
state = gr.State([])
|
| 86 |
|
| 87 |
with gr.Row():
|
| 88 |
with gr.Column():
|
| 89 |
+
audio_in = gr.Audio(label="Voice Emotion", type="filepath")
|
| 90 |
+
text_in = gr.Textbox(label="Message")
|
| 91 |
+
btn = gr.Button("Send", variant="primary")
|
|
|
|
| 92 |
with gr.Column():
|
| 93 |
+
# For Gradio 6, use type="messages" to match the dictionaries
|
| 94 |
+
chat = gr.Chatbot(type="messages")
|
| 95 |
+
audio_out = gr.Audio(autoplay=True)
|
| 96 |
|
| 97 |
+
btn.click(main_orchestrator, [audio_in, text_in, state], [chat, audio_out])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
|
|
|
| 99 |
demo.launch(theme=gr.themes.Soft())
|