amasha03 commited on
Commit
2d5c602
·
verified ·
1 Parent(s): 5160463

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -0
app.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()