Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,13 +7,13 @@ hf_token = os.environ.get("HF_TOKEN")
|
|
| 7 |
|
| 8 |
def safe_client(space_id):
|
| 9 |
try:
|
| 10 |
-
# Using 'token' 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}")
|
| 14 |
return None
|
| 15 |
|
| 16 |
-
# Initializing clients
|
| 17 |
speech_emo_client = safe_client("E-motionAssistant/ser-wav2vec")
|
| 18 |
text_emo_client = safe_client("E-motionAssistant/Space4")
|
| 19 |
llm_english = safe_client("E-motionAssistant/TherapyEnglish")
|
|
@@ -23,9 +23,8 @@ def main_orchestrator(audio_input, text_input, history):
|
|
| 23 |
if history is None:
|
| 24 |
history = []
|
| 25 |
|
| 26 |
-
# Check if the English space is online
|
| 27 |
if not llm_english:
|
| 28 |
-
return history + [[text_input, "⚠️
|
| 29 |
|
| 30 |
# --- 1. GET EMOTION ---
|
| 31 |
emotion = "Neutral"
|
|
@@ -37,20 +36,29 @@ def main_orchestrator(audio_input, text_input, history):
|
|
| 37 |
except:
|
| 38 |
emotion = "Neutral"
|
| 39 |
|
| 40 |
-
# --- 2.
|
| 41 |
-
#
|
| 42 |
api_history = []
|
| 43 |
for user_msg, bot_msg in history:
|
| 44 |
-
api_history.append({
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
# Prepare current message
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
| 51 |
|
| 52 |
# --- 3. CALL LLM ---
|
| 53 |
try:
|
|
|
|
| 54 |
response_text = llm_english.predict(
|
| 55 |
message=current_message,
|
| 56 |
history=api_history,
|
|
@@ -58,7 +66,7 @@ def main_orchestrator(audio_input, text_input, history):
|
|
| 58 |
)
|
| 59 |
except Exception as e:
|
| 60 |
print(f"LLM API Error: {e}")
|
| 61 |
-
response_text = "I'm having trouble connecting to
|
| 62 |
|
| 63 |
# --- 4. TEXT TO SPEECH ---
|
| 64 |
audio_res = None
|
|
@@ -69,7 +77,7 @@ def main_orchestrator(audio_input, text_input, history):
|
|
| 69 |
audio_res = None
|
| 70 |
|
| 71 |
# --- 5. UPDATE UI ---
|
| 72 |
-
#
|
| 73 |
history.append([text_input, response_text])
|
| 74 |
return history, audio_res
|
| 75 |
|
|
@@ -95,5 +103,5 @@ with gr.Blocks() as demo:
|
|
| 95 |
outputs=[chatbot_ui, audio_out]
|
| 96 |
)
|
| 97 |
|
| 98 |
-
#
|
| 99 |
demo.launch(theme=gr.themes.Soft())
|
|
|
|
| 7 |
|
| 8 |
def safe_client(space_id):
|
| 9 |
try:
|
| 10 |
+
# Using 'token' 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}")
|
| 14 |
return None
|
| 15 |
|
| 16 |
+
# Initializing clients
|
| 17 |
speech_emo_client = safe_client("E-motionAssistant/ser-wav2vec")
|
| 18 |
text_emo_client = safe_client("E-motionAssistant/Space4")
|
| 19 |
llm_english = safe_client("E-motionAssistant/TherapyEnglish")
|
|
|
|
| 23 |
if history is None:
|
| 24 |
history = []
|
| 25 |
|
|
|
|
| 26 |
if not llm_english:
|
| 27 |
+
return history + [[text_input, "⚠️ LLM Space Offline."]], None
|
| 28 |
|
| 29 |
# --- 1. GET EMOTION ---
|
| 30 |
emotion = "Neutral"
|
|
|
|
| 36 |
except:
|
| 37 |
emotion = "Neutral"
|
| 38 |
|
| 39 |
+
# --- 2. THE CONTENT BLOCK FIX (Gradio 5/6 Strict Schema) ---
|
| 40 |
+
# We transform history into a list of dictionaries with 'role' and 'content' as LISTS
|
| 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 same strict list-of-dicts format
|
| 53 |
+
bundled_text = f"Context: User is {emotion}. Message: {text_input}"
|
| 54 |
+
current_message = {
|
| 55 |
+
"role": "user",
|
| 56 |
+
"content": [{"type": "text", "text": bundled_text}]
|
| 57 |
+
}
|
| 58 |
|
| 59 |
# --- 3. CALL LLM ---
|
| 60 |
try:
|
| 61 |
+
# Passing current_message and api_history to satisfy the "messages format"
|
| 62 |
response_text = llm_english.predict(
|
| 63 |
message=current_message,
|
| 64 |
history=api_history,
|
|
|
|
| 66 |
)
|
| 67 |
except Exception as e:
|
| 68 |
print(f"LLM API Error: {e}")
|
| 69 |
+
response_text = "I'm having trouble connecting to the therapy space. Please ensure the sub-space is running."
|
| 70 |
|
| 71 |
# --- 4. TEXT TO SPEECH ---
|
| 72 |
audio_res = None
|
|
|
|
| 77 |
audio_res = None
|
| 78 |
|
| 79 |
# --- 5. UPDATE UI ---
|
| 80 |
+
# We return the simple list-of-lists format for the local UI chatbot
|
| 81 |
history.append([text_input, response_text])
|
| 82 |
return history, audio_res
|
| 83 |
|
|
|
|
| 103 |
outputs=[chatbot_ui, audio_out]
|
| 104 |
)
|
| 105 |
|
| 106 |
+
# Theme and launching
|
| 107 |
demo.launch(theme=gr.themes.Soft())
|