Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -26,53 +26,37 @@ def detect_language(text):
|
|
| 26 |
return "english"
|
| 27 |
|
| 28 |
def main_orchestrator(audio_input, text_input, history):
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
# --- A. EMOTION ---
|
| 33 |
-
emotion = "Neutral"
|
| 34 |
-
if audio_input and speech_emo_client:
|
| 35 |
-
emotion = speech_emo_client.predict(handle_file(audio_input), api_name="/predict")
|
| 36 |
-
elif text_input and text_emo_client:
|
| 37 |
-
try: emotion = text_emo_client.predict(text_input, api_name="/predict")
|
| 38 |
-
except: emotion = "Neutral"
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
if len(history) > 0:
|
| 49 |
-
context_text += "History:\n"
|
| 50 |
-
for u, b in history[-2:]:
|
| 51 |
-
context_text += f"User: {u}\nAI: {b}\n"
|
| 52 |
-
|
| 53 |
-
full_string = f"{context_text}\nCurrent Message: {text_input}"
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
response_text = selected_llm.predict(message=full_string, api_name="/chat")
|
| 71 |
-
history.append((text_input, response_text))
|
| 72 |
-
return history, None
|
| 73 |
-
except:
|
| 74 |
-
history.append((text_input, f"API Error: {str(e)}"))
|
| 75 |
-
return history, None
|
| 76 |
|
| 77 |
# --- UI ---
|
| 78 |
with gr.Blocks() as demo:
|
|
|
|
| 26 |
return "english"
|
| 27 |
|
| 28 |
def main_orchestrator(audio_input, text_input, history):
|
| 29 |
+
# 1. INITIALIZE: If it's the first message, history is an empty list []
|
| 30 |
+
if history is None:
|
| 31 |
+
history = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
+
# 2. GET EMOTION (The "Mood Note")
|
| 34 |
+
emotion = "Neutral"
|
| 35 |
+
if audio_input:
|
| 36 |
+
emotion = speech_emo_client.predict(handle_file(audio_input), api_name="/predict")
|
| 37 |
+
else:
|
| 38 |
+
emotion = text_emo_client.predict(text_input, api_name="/predict")
|
| 39 |
|
| 40 |
+
# 3. CONSTRUCT THE "BRIEF" (Bundling everything for the LLM)
|
| 41 |
+
# We include the current emotion and the current text
|
| 42 |
+
current_payload = f"USER EMOTION: {emotion}\nUSER MESSAGE: {text_input}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
+
# 4. SEND TO LLM (The "One Parameter" Dictionary Fix)
|
| 45 |
+
# Note: We send 'history' as a list of dicts because Gradio 6 is strict!
|
| 46 |
+
response_text = selected_llm.predict(
|
| 47 |
+
message={"text": current_payload, "files": []},
|
| 48 |
+
history=history, # This 'history' is the saved list from the previous turns
|
| 49 |
+
api_name="/chat"
|
| 50 |
+
)
|
| 51 |
|
| 52 |
+
# 5. SAVE EVERYTHING (The Memory Step)
|
| 53 |
+
# We convert the current turn into the Gradio 6 format and add it to our 'Journal'
|
| 54 |
+
history.append({"role": "user", "content": [{"type": "text", "text": text_input}]})
|
| 55 |
+
history.append({"role": "assistant", "content": [{"type": "text", "text": response_text}]})
|
| 56 |
|
| 57 |
+
# 6. RETURN TO UI
|
| 58 |
+
# The UI Chatbot will automatically render this list of dictionaries
|
| 59 |
+
return history, None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
# --- UI ---
|
| 62 |
with gr.Blocks() as demo:
|