amasha03 commited on
Commit
b65026c
·
verified ·
1 Parent(s): 33d5184

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -54
app.py CHANGED
@@ -2,17 +2,16 @@ import gradio as gr
2
  from gradio_client import Client, handle_file
3
  import os
4
 
5
- # 1. SETUP: Get your HF Token from Settings > Secrets
6
  hf_token = os.environ.get("HF_TOKEN")
7
 
8
  def safe_client(space_id):
9
- """Connect to a space via its direct URL for better Docker/Gradio compatibility."""
10
  try:
11
- # Converts "User/Space-Name" to "https://user-space-name.hf.space"
12
  space_url = f"https://{space_id.replace('/', '-').lower()}.hf.space"
13
  return Client(space_url, hf_token=hf_token)
14
  except Exception as e:
15
- print(f"⚠️ Could not connect to {space_id}: {e}")
16
  return None
17
 
18
  # 2. CONNECT TO YOUR 6 SPACES
@@ -24,7 +23,7 @@ llm_tamil = safe_client("E-motionAssistant/TherapyTamil")
24
  tts_client = safe_client("E-motionAssistant/Space3")
25
 
26
  def detect_language(text):
27
- """Detects Sinhala, Tamil, or English based on character ranges."""
28
  if any("\u0D80" <= char <= "\u0DFF" for char in text):
29
  return "sinhala"
30
  if any("\u0B80" <= char <= "\u0BFF" for char in text):
@@ -35,27 +34,20 @@ def main_orchestrator(audio_input, text_input, history):
35
  if history is None:
36
  history = []
37
 
38
- # --- STEP A: Format History for the LLM API (List of Dicts) ---
39
- # This fixes the "Data incompatible" error for Gradio 5/6
40
- api_history = []
41
- for user_msg, bot_msg in history:
42
- api_history.append({"role": "user", "content": user_msg})
43
- api_history.append({"role": "assistant", "content": bot_msg})
44
-
45
  try:
46
- # --- STEP B: Determine Emotion ---
47
  emotion = "Neutral"
48
  if audio_input and speech_emo_client:
49
- # Use handle_file for local temporary audio files
50
  emotion = speech_emo_client.predict(handle_file(audio_input), api_name="/predict")
51
  elif text_input and text_emo_client:
52
- # If Space4 is Docker and fails, we wrap this in a try/except
53
  try:
54
  emotion = text_emo_client.predict(text_input, api_name="/predict")
55
  except:
56
- emotion = "Neutral (Space4 Connection Issues)"
57
 
58
- # --- STEP C: Select LLM & Language ---
59
  lang = detect_language(text_input)
60
  if lang == "sinhala":
61
  selected_llm = llm_sinhala
@@ -65,35 +57,27 @@ def main_orchestrator(audio_input, text_input, history):
65
  selected_llm = llm_english
66
 
67
  if not selected_llm:
68
- return history + [[text_input, "Target LLM Space is offline."]], None
69
 
70
- # --- STEP D: Call the LLM ---
71
- # Bundle Emotion + History + Current Message into ONE string
72
- # so the LLM has "Contextual Memory" in a single turn.
73
 
74
- # 1. Create a "Context Block" from previous messages
75
- context_memory = ""
76
  if len(history) > 0:
77
- context_memory = "Previous Conversation:\n"
78
- # Take last 3 turns to keep it concise
79
- for user_msg, bot_msg in history[-3:]:
80
- context_memory += f"User: {user_msg}\nAssistant: {bot_msg}\n"
81
 
82
- # 2. Combine everything into the single 'message' parameter
83
- # This tells the LLM the emotion and what was said before
84
- final_bundled_message = (
85
- f"Context:\nUser is feeling {emotion}.\n"
86
- f"{context_memory}\n"
87
- f"Current User Message: {text_input}"
88
- )
89
 
90
- # 3. Predict using ONLY the one required parameter
 
91
  response_text = selected_llm.predict(
92
- message=final_bundled_message,
93
  api_name="/chat"
94
  )
95
 
96
- # --- STEP E: Generate Speech (Optional) ---
97
  audio_response = None
98
  if tts_client and response_text:
99
  try:
@@ -101,37 +85,35 @@ def main_orchestrator(audio_input, text_input, history):
101
  except:
102
  audio_response = None
103
 
104
- # --- STEP F: Update Local UI Memory ---
105
- # Gradio's Chatbot UI still needs the List-of-Lists format
106
  history.append((text_input, response_text))
107
  return history, audio_response
108
 
109
  except Exception as e:
110
- history.append((text_input, f"System Error: {str(e)}"))
111
  return history, None
112
 
113
- # --- STEP 3: THE GRADIO INTERFACE ---
114
- with gr.Blocks(theme=gr.themes.Soft()) as demo:
115
- gr.Markdown("# 🌍 Multilingual Emotional Therapy Hub")
116
- gr.Markdown("Memory is active for this session. Refreshing the page will clear history.")
117
 
118
- session_history = gr.State([])
119
 
120
  with gr.Row():
121
  with gr.Column(scale=1):
122
- audio_in = gr.Audio(label="Voice Input (Speech Emotion)", type="filepath")
123
- text_in = gr.Textbox(label="Text Input", placeholder="Type in English, සිංහල, or தமிழ்...")
124
- submit_btn = gr.Button("Send Message", variant="primary")
125
 
126
  with gr.Column(scale=2):
127
- chatbot = gr.Chatbot(label="Conversation History")
128
- audio_out = gr.Audio(label="Assistant Response (Voice)", autoplay=True)
129
 
130
- # Connecting the logic
131
- submit_btn.click(
132
  main_orchestrator,
133
- inputs=[audio_in, text_in, session_history],
134
- outputs=[chatbot, audio_out]
135
  )
136
 
137
  demo.launch()
 
2
  from gradio_client import Client, handle_file
3
  import os
4
 
5
+ # 1. SETUP: Access Token for Private Spaces
6
  hf_token = os.environ.get("HF_TOKEN")
7
 
8
  def safe_client(space_id):
9
+ """Connects to a space using its direct URL to ensure compatibility."""
10
  try:
 
11
  space_url = f"https://{space_id.replace('/', '-').lower()}.hf.space"
12
  return Client(space_url, hf_token=hf_token)
13
  except Exception as e:
14
+ print(f"⚠️ Connection failed for {space_id}: {e}")
15
  return None
16
 
17
  # 2. CONNECT TO YOUR 6 SPACES
 
23
  tts_client = safe_client("E-motionAssistant/Space3")
24
 
25
  def detect_language(text):
26
+ """Detects Sinhala, Tamil, or English script."""
27
  if any("\u0D80" <= char <= "\u0DFF" for char in text):
28
  return "sinhala"
29
  if any("\u0B80" <= char <= "\u0BFF" for char in text):
 
34
  if history is None:
35
  history = []
36
 
 
 
 
 
 
 
 
37
  try:
38
+ # --- A. EMOTION DETECTION ---
39
  emotion = "Neutral"
40
  if audio_input and speech_emo_client:
41
+ # Get emotion from Speech (SER)
42
  emotion = speech_emo_client.predict(handle_file(audio_input), api_name="/predict")
43
  elif text_input and text_emo_client:
44
+ # Get emotion from Text (TER)
45
  try:
46
  emotion = text_emo_client.predict(text_input, api_name="/predict")
47
  except:
48
+ emotion = "Neutral"
49
 
50
+ # --- B. LANGUAGE ROUTING ---
51
  lang = detect_language(text_input)
52
  if lang == "sinhala":
53
  selected_llm = llm_sinhala
 
57
  selected_llm = llm_english
58
 
59
  if not selected_llm:
60
+ return history + [[text_input, "Target LLM Space is Offline."]], None
61
 
62
+ # --- C. THE BUNDLE (The 'One Parameter' Fix) ---
63
+ # We package History + Emotion + Message into one string
64
+ context_block = f"[Instruction: User is feeling {emotion}]\n"
65
 
 
 
66
  if len(history) > 0:
67
+ context_block += "Previous turns:\n"
68
+ for user_msg, bot_msg in history[-2:]: # Last 2 turns for context
69
+ context_block += f"User: {user_msg}\nAssistant: {bot_msg}\n"
 
70
 
71
+ bundled_message = f"{context_block}\nCurrent Message: {text_input}"
 
 
 
 
 
 
72
 
73
+ # --- D. GET LLM RESPONSE ---
74
+ # Sending only the 'message' parameter as required by your LLM spaces
75
  response_text = selected_llm.predict(
76
+ message=bundled_message,
77
  api_name="/chat"
78
  )
79
 
80
+ # --- E. TEXT TO SPEECH ---
81
  audio_response = None
82
  if tts_client and response_text:
83
  try:
 
85
  except:
86
  audio_response = None
87
 
88
+ # --- F. UPDATE UI ---
 
89
  history.append((text_input, response_text))
90
  return history, audio_response
91
 
92
  except Exception as e:
93
+ history.append((text_input, f"Connection Error: {str(e)}"))
94
  return history, None
95
 
96
+ # --- 3. THE USER INTERFACE ---
97
+ with gr.Blocks(theme=gr.themes.Default()) as demo:
98
+ gr.Markdown("# 🧠 Emotional Therapy AI Hub")
99
+ gr.Markdown("English | සිංහල | தமிழ்")
100
 
101
+ chat_state = gr.State([]) # Local Session Memory
102
 
103
  with gr.Row():
104
  with gr.Column(scale=1):
105
+ audio_input = gr.Audio(label="Voice (for Emotion)", type="filepath")
106
+ text_input = gr.Textbox(label="Message", placeholder="Tell me how you feel...")
107
+ send_btn = gr.Button("Send", variant="primary")
108
 
109
  with gr.Column(scale=2):
110
+ chatbot_ui = gr.Chatbot(label="Therapy Session")
111
+ voice_output = gr.Audio(label="AI Voice Response", autoplay=True)
112
 
113
+ send_btn.click(
 
114
  main_orchestrator,
115
+ inputs=[audio_input, text_input, chat_state],
116
+ outputs=[chatbot_ui, voice_output]
117
  )
118
 
119
  demo.launch()