Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,12 +2,13 @@ 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 |
-
#
|
|
|
|
| 11 |
return Client(space_id, token=hf_token)
|
| 12 |
except Exception as e:
|
| 13 |
print(f"⚠️ Connection failed for {space_id}: {e}")
|
|
@@ -37,19 +38,19 @@ def main_orchestrator(audio_input, text_input, history):
|
|
| 37 |
emotion = "Neutral"
|
| 38 |
|
| 39 |
# --- 2. THE CONTENT BLOCK FIX (Gradio 5/6 Strict Schema) ---
|
| 40 |
-
#
|
| 41 |
api_history = []
|
| 42 |
for user_msg, bot_msg in history:
|
| 43 |
api_history.append({
|
| 44 |
"role": "user",
|
| 45 |
-
"content": [{"type": "text", "text": user_msg}]
|
| 46 |
})
|
| 47 |
api_history.append({
|
| 48 |
"role": "assistant",
|
| 49 |
-
"content": [{"type": "text", "text": bot_msg}]
|
| 50 |
})
|
| 51 |
|
| 52 |
-
# Prepare current message using the
|
| 53 |
bundled_text = f"Context: User is {emotion}. Message: {text_input}"
|
| 54 |
current_message = {
|
| 55 |
"role": "user",
|
|
@@ -58,7 +59,7 @@ def main_orchestrator(audio_input, text_input, history):
|
|
| 58 |
|
| 59 |
# --- 3. CALL LLM ---
|
| 60 |
try:
|
| 61 |
-
#
|
| 62 |
response_text = llm_english.predict(
|
| 63 |
message=current_message,
|
| 64 |
history=api_history,
|
|
@@ -66,18 +67,22 @@ def main_orchestrator(audio_input, text_input, history):
|
|
| 66 |
)
|
| 67 |
except Exception as e:
|
| 68 |
print(f"LLM API Error: {e}")
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
# --- 4. TEXT TO SPEECH ---
|
| 72 |
audio_res = None
|
| 73 |
if tts_client and response_text:
|
| 74 |
try:
|
| 75 |
-
audio_res = tts_client.predict(response_text, api_name="/predict")
|
| 76 |
except:
|
| 77 |
audio_res = None
|
| 78 |
|
| 79 |
# --- 5. UPDATE UI ---
|
| 80 |
-
#
|
| 81 |
history.append([text_input, response_text])
|
| 82 |
return history, audio_res
|
| 83 |
|
|
@@ -89,8 +94,8 @@ with gr.Blocks() as demo:
|
|
| 89 |
|
| 90 |
with gr.Row():
|
| 91 |
with gr.Column():
|
| 92 |
-
audio_in = gr.Audio(label="Voice Input
|
| 93 |
-
text_in = gr.Textbox(label="Message"
|
| 94 |
submit_btn = gr.Button("Send", variant="primary")
|
| 95 |
|
| 96 |
with gr.Column():
|
|
@@ -103,5 +108,4 @@ with gr.Blocks() as demo:
|
|
| 103 |
outputs=[chatbot_ui, audio_out]
|
| 104 |
)
|
| 105 |
|
| 106 |
-
# Theme and launching
|
| 107 |
demo.launch(theme=gr.themes.Soft())
|
|
|
|
| 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 |
+
# 'token' is used for older gradio_client; 'hf_token' for newer.
|
| 11 |
+
# We use token=hf_token to be safe.
|
| 12 |
return Client(space_id, token=hf_token)
|
| 13 |
except Exception as e:
|
| 14 |
print(f"⚠️ Connection failed for {space_id}: {e}")
|
|
|
|
| 38 |
emotion = "Neutral"
|
| 39 |
|
| 40 |
# --- 2. THE CONTENT BLOCK FIX (Gradio 5/6 Strict Schema) ---
|
| 41 |
+
# Convert history into a list of dictionaries where 'content' is a LIST of DICTS
|
| 42 |
api_history = []
|
| 43 |
for user_msg, bot_msg in history:
|
| 44 |
api_history.append({
|
| 45 |
"role": "user",
|
| 46 |
+
"content": [{"type": "text", "text": str(user_msg)}]
|
| 47 |
})
|
| 48 |
api_history.append({
|
| 49 |
"role": "assistant",
|
| 50 |
+
"content": [{"type": "text", "text": str(bot_msg)}]
|
| 51 |
})
|
| 52 |
|
| 53 |
+
# Prepare current message using the SAME strict list-of-dicts format
|
| 54 |
bundled_text = f"Context: User is {emotion}. Message: {text_input}"
|
| 55 |
current_message = {
|
| 56 |
"role": "user",
|
|
|
|
| 59 |
|
| 60 |
# --- 3. CALL LLM ---
|
| 61 |
try:
|
| 62 |
+
# This structure matches the "ChatMessage" object exactly
|
| 63 |
response_text = llm_english.predict(
|
| 64 |
message=current_message,
|
| 65 |
history=api_history,
|
|
|
|
| 67 |
)
|
| 68 |
except Exception as e:
|
| 69 |
print(f"LLM API Error: {e}")
|
| 70 |
+
# Final Fallback: If the strict dict fails, try sending just the string
|
| 71 |
+
try:
|
| 72 |
+
response_text = llm_english.predict(message=bundled_text, api_name="/chat")
|
| 73 |
+
except:
|
| 74 |
+
response_text = "Connection Error. Please ensure the TherapyEnglish space is 'Running'."
|
| 75 |
|
| 76 |
# --- 4. TEXT TO SPEECH ---
|
| 77 |
audio_res = None
|
| 78 |
if tts_client and response_text:
|
| 79 |
try:
|
| 80 |
+
audio_res = tts_client.predict(str(response_text), api_name="/predict")
|
| 81 |
except:
|
| 82 |
audio_res = None
|
| 83 |
|
| 84 |
# --- 5. UPDATE UI ---
|
| 85 |
+
# Return simple list-of-lists for the local Gradio Chatbot
|
| 86 |
history.append([text_input, response_text])
|
| 87 |
return history, audio_res
|
| 88 |
|
|
|
|
| 94 |
|
| 95 |
with gr.Row():
|
| 96 |
with gr.Column():
|
| 97 |
+
audio_in = gr.Audio(label="Voice Input", type="filepath")
|
| 98 |
+
text_in = gr.Textbox(label="Message")
|
| 99 |
submit_btn = gr.Button("Send", variant="primary")
|
| 100 |
|
| 101 |
with gr.Column():
|
|
|
|
| 108 |
outputs=[chatbot_ui, audio_out]
|
| 109 |
)
|
| 110 |
|
|
|
|
| 111 |
demo.launch(theme=gr.themes.Soft())
|