amasha03 commited on
Commit
8f109ed
·
verified ·
1 Parent(s): 7fea5d6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -45
app.py CHANGED
@@ -2,18 +2,18 @@ import gradio as gr
2
  from gradio_client import Client, handle_file
3
  import os
4
 
5
- # 1. SETUP: Connect to your Spaces
6
  hf_token = os.environ.get("HF_TOKEN")
7
 
8
  def safe_client(space_id):
9
  try:
10
- space_url = f"https://{space_id.replace('/', '-').lower()}.hf.space"
11
- return Client(space_url, hf_token=hf_token)
12
  except Exception as e:
13
  print(f"⚠️ Connection failed for {space_id}: {e}")
14
  return None
15
 
16
- # Initializing all clients
17
  speech_emo_client = safe_client("E-motionAssistant/ser-wav2vec")
18
  text_emo_client = safe_client("E-motionAssistant/Space4")
19
  llm_sinhala = safe_client("E-motionAssistant/Therapy-Sinhala")
@@ -30,20 +30,14 @@ def main_orchestrator(audio_input, text_input, history):
30
  if history is None:
31
  history = []
32
 
33
- # --- 1. LANGUAGE DETECTION & LLM SELECTION ---
34
- # We must define 'selected_llm' inside the function!
35
  lang = detect_language(text_input)
36
- if lang == "sinhala":
37
- selected_llm = llm_sinhala
38
- elif lang == "tamil":
39
- selected_llm = llm_tamil
40
- else:
41
- selected_llm = llm_english
42
 
43
  if not selected_llm:
44
- return history + [{"role": "assistant", "content": "LLM Space is offline."}], None
45
 
46
- # --- 2. GET EMOTION ---
47
  emotion = "Neutral"
48
  try:
49
  if audio_input:
@@ -53,47 +47,37 @@ def main_orchestrator(audio_input, text_input, history):
53
  except:
54
  emotion = "Neutral"
55
 
56
- # --- 3. CONSTRUCT THE PAYLOAD ---
57
- # We bundle the emotion into the text since the LLM takes one parameter
58
- current_payload = f"CONTEXT: User is feeling {emotion}. MESSAGE: {text_input}"
59
 
60
- # --- 4. CALL LLM (Gradio 6 Dictionary Format) ---
61
  try:
62
- # We send the dictionary 'message' and the 'history'
63
  response_text = selected_llm.predict(
64
- message={"text": current_payload, "files": []},
65
- history=history,
66
  api_name="/chat"
67
  )
68
- except Exception as e:
69
- print(f"Prediction Error: {e}")
70
- # Fallback if history causes issues: try message only
71
- response_text = selected_llm.predict(
72
- message={"text": current_payload, "files": []},
73
- api_name="/chat"
74
- )
75
-
76
- # --- 5. SAVE TO HISTORY (Gradio 6 format) ---
77
- # Simplified format often works best for cross-space history
78
- history.append({"role": "user", "content": text_input})
79
- history.append({"role": "assistant", "content": response_text})
80
 
81
- # --- 6. TTS ---
82
- audio_response = None
83
  if tts_client:
84
  try:
85
- audio_response = tts_client.predict(response_text, api_name="/predict")
86
  except:
87
- audio_response = None
88
 
89
- return history, audio_response
 
 
90
 
91
  # --- UI ---
92
- with gr.Blocks(theme=gr.themes.Soft()) as demo:
93
- gr.Markdown("# 🧠 E-motion Therapy Orchestrator")
 
94
 
95
- # In Gradio 6, history state should be an empty list []
96
- state = gr.State([])
97
 
98
  with gr.Row():
99
  with gr.Column():
@@ -101,10 +85,11 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
101
  text_in = gr.Textbox(label="Message")
102
  btn = gr.Button("Send", variant="primary")
103
  with gr.Column():
104
- # type="messages" is key for Gradio 6
105
- chat = gr.Chatbot(type="messages")
106
  audio_out = gr.Audio(autoplay=True)
107
 
108
  btn.click(main_orchestrator, [audio_in, text_in, state], [chat, audio_out])
109
 
110
- demo.launch()
 
 
2
  from gradio_client import Client, handle_file
3
  import os
4
 
5
+ # 1. SETUP: Use 'token' instead of 'hf_token' for compatibility
6
  hf_token = os.environ.get("HF_TOKEN")
7
 
8
  def safe_client(space_id):
9
  try:
10
+ # Changed 'hf_token' to '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_sinhala = safe_client("E-motionAssistant/Therapy-Sinhala")
 
30
  if history is None:
31
  history = []
32
 
33
+ # Select LLM
 
34
  lang = detect_language(text_input)
35
+ selected_llm = llm_sinhala if lang == "sinhala" else llm_tamil if lang == "tamil" else llm_english
 
 
 
 
 
36
 
37
  if not selected_llm:
38
+ return history + [["System", "LLM Space Offline"]], None
39
 
40
+ # Get Emotion
41
  emotion = "Neutral"
42
  try:
43
  if audio_input:
 
47
  except:
48
  emotion = "Neutral"
49
 
50
+ # Bundle message for LLM
51
+ bundled_payload = f"Emotion: {emotion}\nMessage: {text_input}"
 
52
 
 
53
  try:
54
+ # Calling LLM with the 'message' dictionary
55
  response_text = selected_llm.predict(
56
+ message={"text": bundled_payload, "files": []},
 
57
  api_name="/chat"
58
  )
59
+ except:
60
+ # Fallback for older API versions
61
+ response_text = selected_llm.predict(bundled_payload, api_name="/predict")
 
 
 
 
 
 
 
 
 
62
 
63
+ # TTS
64
+ audio_res = None
65
  if tts_client:
66
  try:
67
+ audio_res = tts_client.predict(response_text, api_name="/predict")
68
  except:
69
+ audio_res = None
70
 
71
+ # Update history in OLD format [[user, bot]]
72
+ history.append([text_input, response_text])
73
+ return history, audio_res
74
 
75
  # --- UI ---
76
+ # Removed 'theme' from here to fix the warning
77
+ with gr.Blocks() as demo:
78
+ gr.Markdown("# 🧠 E-motion Therapy Orchestrator (Legacy Fix)")
79
 
80
+ state = gr.State([]) # List of lists
 
81
 
82
  with gr.Row():
83
  with gr.Column():
 
85
  text_in = gr.Textbox(label="Message")
86
  btn = gr.Button("Send", variant="primary")
87
  with gr.Column():
88
+ # Removed 'type="messages"' to fix the TypeError
89
+ chat = gr.Chatbot()
90
  audio_out = gr.Audio(autoplay=True)
91
 
92
  btn.click(main_orchestrator, [audio_in, text_in, state], [chat, audio_out])
93
 
94
+ # Theme passed to launch() as requested by Gradio 6 warning
95
+ demo.launch(theme=gr.themes.Soft())