amasha03 commited on
Commit
e828692
·
verified ·
1 Parent(s): 534e530

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -36
app.py CHANGED
@@ -1,50 +1,98 @@
1
  import gradio as gr
2
- from gradio_client import Client
 
3
 
4
- # 1. Connect to all your specialized Spaces
5
- # Replace 'username/space-name' with your actual Space paths
6
- speech_emo_client = Client("E-motionAssistant/ser-wav2vec")
7
- text_emo_client = Client("E-motionAssistant/Space4")
8
- llm_sinhala = Client("E-motionAssistant/Therapy-Sinhala")
9
- llm_english = Client("E-motionAssistant/TherapyEnglish")
10
- llm_tamil = Client("E-motionAssistant/TherapyTamil")
11
- tts_client = Client("E-motionAssistant/Space3")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
  def main_orchestrator(audio_input, text_input, history):
14
- # A. Determine Emotion (from Speech or Text)
15
- if audio_input:
16
- emotion = speech_emo_client.predict(audio_input, api_name="/predict")
17
- else:
18
- emotion = text_emo_client.predict(text_input, api_name="/predict")
19
-
20
- # B. Select the right LLM based on language (Simple logic example)
21
- # You could add a language detection step here too
22
- selected_llm = llm_english
23
- if "හෙලෝ" in text_input: selected_llm = llm_sinhala
24
 
25
- # C. Call the LLM with the Emotion Context (The Memory Layer)
26
- # We pass the history here so the specific LLM Space can stay 'stateless'
27
- response_text = selected_llm.predict(text_input, emotion, history, api_name="/chat")
 
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29
- # D. Convert Response to Speech
30
- audio_response = tts_client.predict(response_text, api_name="/predict")
31
 
32
- # E. Update History (Memory clears on refresh because it's in gr.State)
33
- history.append((text_input, response_text))
34
- return history, audio_response
35
 
36
- # 3. The Interface
37
- with gr.Blocks() as demo:
38
- chat_history = gr.State([]) # YOUR SESSION MEMORY
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  with gr.Row():
40
  with gr.Column():
41
- audio_in = gr.Audio(source="microphone", type="filepath")
42
- text_in = gr.Textbox()
43
- submit_btn = gr.Button("Send")
 
44
  with gr.Column():
45
- chatbot = gr.Chatbot()
46
- audio_out = gr.Audio()
47
 
48
- submit_btn.click(main_orchestrator, [audio_in, text_in, chat_history], [chatbot, audio_out])
 
 
 
 
49
 
50
  demo.launch()
 
1
  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 for private access
6
+ hf_token = os.environ.get("HF_TOKEN")
7
+
8
+ def safe_client(space_id):
9
+ """Attempt to connect to a space, return None if it fails."""
10
+ try:
11
+ # We use the direct hf.space URL for better stability with Docker/Custom spaces
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"⚠️ Warning: Could not connect to {space_id}. Error: {e}")
16
+ return None
17
+
18
+ # 2. CONNECT TO YOUR 6 SPACES
19
+ speech_emo_client = safe_client("E-motionAssistant/ser-wav2vec")
20
+ text_emo_client = safe_client("E-motionAssistant/Space4")
21
+ llm_sinhala = safe_client("E-motionAssistant/Therapy-Sinhala")
22
+ llm_english = safe_client("E-motionAssistant/TherapyEnglish")
23
+ llm_tamil = safe_client("E-motionAssistant/TherapyTamil")
24
+ tts_client = safe_client("E-motionAssistant/Space3")
25
+
26
+ def detect_language(text):
27
+ """Simple script-based language detection."""
28
+ # Check for Sinhala characters
29
+ if any("\u0D80" <= char <= "\u0DFF" for char in text):
30
+ return "sinhala"
31
+ # Check for Tamil characters
32
+ if any("\u0B80" <= char <= "\u0BFF" for char in text):
33
+ return "tamil"
34
+ return "english"
35
 
36
  def main_orchestrator(audio_input, text_input, history):
37
+ if history is None:
38
+ history = []
 
 
 
 
 
 
 
 
39
 
40
+ try:
41
+ # A. Determine Emotion
42
+ emotion = "Neutral" # Default
43
+ if audio_input and speech_emo_client:
44
+ emotion = speech_emo_client.predict(handle_file(audio_input), api_name="/predict")
45
+ elif text_input and text_emo_client:
46
+ emotion = text_emo_client.predict(text_input, api_name="/predict")
47
+
48
+ # B. Select LLM based on Language
49
+ lang = detect_language(text_input)
50
+ if lang == "sinhala" and llm_sinhala:
51
+ selected_llm = llm_sinhala
52
+ elif lang == "tamil" and llm_tamil:
53
+ selected_llm = llm_tamil
54
+ else:
55
+ selected_llm = llm_english
56
 
57
+ if not selected_llm:
58
+ return history + [["Error", "Selected LLM Space is offline."]], None
59
 
60
+ # C. Call LLM (Passing history for memory)
61
+ # Note: Ensure your LLM spaces accept (text, emotion, history)
62
+ response_text = selected_llm.predict(text_input, emotion, history, api_name="/chat")
63
 
64
+ # D. Convert to Speech
65
+ audio_response = None
66
+ if tts_client:
67
+ audio_response = tts_client.predict(response_text, api_name="/predict")
68
+
69
+ # E. Update Memory (State clears on refresh)
70
+ history.append((text_input, response_text))
71
+ return history, audio_response
72
+
73
+ except Exception as e:
74
+ history.append((text_input, f"System Error: {str(e)}"))
75
+ return history, None
76
+
77
+ # 3. THE UI
78
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
79
+ gr.Markdown("# 🧠 Multi-Space Emotional Assistant")
80
+ chat_history = gr.State([])
81
+
82
  with gr.Row():
83
  with gr.Column():
84
+ audio_in = gr.Audio(label="Speak", type="filepath")
85
+ text_in = gr.Textbox(label="Type here", placeholder="Say something...")
86
+ submit_btn = gr.Button("Send", variant="primary")
87
+
88
  with gr.Column():
89
+ chatbot = gr.Chatbot(label="Therapy Session")
90
+ audio_out = gr.Audio(label="Assistant Voice")
91
 
92
+ submit_btn.click(
93
+ main_orchestrator,
94
+ [audio_in, text_in, chat_history],
95
+ [chatbot, audio_out]
96
+ )
97
 
98
  demo.launch()