amasha03 commited on
Commit
60b6dfd
·
verified ·
1 Parent(s): faea470

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -42
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
- if history is None: history = []
30
-
31
- try:
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
- # --- B. LLM SELECTION ---
41
- lang = detect_language(text_input)
42
- selected_llm = llm_sinhala if lang == "sinhala" else llm_tamil if lang == "tamil" else llm_english
43
- if not selected_llm: return history + [[text_input, "LLM Offline"]], None
 
 
44
 
45
- # --- C. THE GRADIO 6 FIX (BUNDLING) ---
46
- # We build a 'Contextual Remainder' string
47
- context_text = f"[User Emotion: {emotion}]\n"
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
- # D. SENDING AS A GRADIO 6 CONTENT BLOCK
56
- # Most Gradio 6 ChatInterfaces expect this exact dictionary shape
57
- response_text = selected_llm.predict(
58
- message={"text": full_string, "files": []},
59
- api_name="/chat"
60
- )
 
61
 
62
- # --- E. TTS & UI ---
63
- audio_response = tts_client.predict(response_text, api_name="/predict") if tts_client else None
64
- history.append((text_input, response_text))
65
- return history, audio_response
66
 
67
- except Exception as e:
68
- # FINAL FALLBACK: If the dictionary failed, try the plain string one last time
69
- try:
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: