welcometoFightclub commited on
Commit
b13e366
·
verified ·
1 Parent(s): eea9139

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -63
app.py CHANGED
@@ -1,6 +1,5 @@
1
  import gradio as gr
2
  import torch
3
- import speech_recognition as sr
4
  from groq import Groq
5
  import os
6
  import tempfile
@@ -45,22 +44,6 @@ def predict_text_emotion(text):
45
  logger.error(f"Error with Groq API (text emotion): {str(e)}")
46
  return "neutral"
47
 
48
- def transcribe_audio(audio_path):
49
- r = sr.Recognizer()
50
- try:
51
- with sr.AudioFile(audio_path) as source:
52
- audio_text = r.listen(source)
53
- text = r.recognize_google(audio_text)
54
- return text
55
- except sr.UnknownValueError:
56
- return "I didn’t catch that—could you try again?"
57
- except sr.RequestError as e:
58
- logger.error(f"Speech recognition error: {str(e)}")
59
- return "Speech recognition unavailable—try typing instead."
60
- except Exception as e:
61
- logger.error(f"Unexpected error in audio transcription: {str(e)}")
62
- return "Error processing audio."
63
-
64
  def generate_response(user_input, emotion):
65
  prompt = f"The user is feeling {emotion}. They said: '{user_input}'. Respond in a friendly caring manner with the user so the user feels being loved."
66
  try:
@@ -87,14 +70,12 @@ def text_to_speech(text):
87
  logger.error(f"Error generating speech: {str(e)}")
88
  return None
89
 
90
- # Chat function for Gradio with voice output
91
  def chat_function(input_type, text_input, audio_input, chat_history):
92
  if input_type == "text" and text_input:
93
  user_input = text_input
94
- elif input_type == "voice" and audio_input:
95
- user_input = transcribe_audio(audio_input)
96
  else:
97
- return chat_history, "Please provide text or voice input.", gr.update(value=text_input), None
98
 
99
  emotion = predict_text_emotion(user_input)
100
  response = generate_response(user_input, emotion)
@@ -129,48 +110,53 @@ css = """
129
  """
130
 
131
  # Build the Gradio interface
132
- with gr.Blocks(theme=gr.themes.Soft(), css=css) as app:
133
- gr.Markdown(
134
- """
135
- # Multimodal Mental Health AI Agent
136
- Chat with our empathetic AI designed to support you by understanding your emotions through text and voice.
137
- """
138
- )
139
-
140
- with gr.Row():
141
- with gr.Column(scale=1):
142
- emotion_display = gr.Textbox(label="Emotion", interactive=False, placeholder="Detected emotion will appear here")
143
-
144
- with gr.Column(scale=3):
145
- chatbot = gr.Chatbot(label="Conversation History", height=500, type="messages", elem_classes="chatbot")
146
-
147
- with gr.Row(elem_classes="input-container"):
148
- input_type = gr.Radio(["text", "voice"], label="Input Method", value="text")
149
- text_input = gr.Textbox(label="Type Your Message", placeholder="How are you feeling today?", visible=True)
150
- audio_input = gr.Audio(sources=["microphone"], type="filepath", label="Record Your Message", visible=False)
151
- submit_btn = gr.Button("Send", variant="primary")
152
- clear_btn = gr.Button("Clear Chat", variant="secondary")
153
- audio_output = gr.Audio(label="Assistant Response", type="filepath", interactive=False, autoplay=True)
154
-
155
- # Dynamic visibility based on input type
156
- def update_visibility(input_type):
157
- return gr.update(visible=input_type == "text"), gr.update(visible=input_type == "voice")
158
-
159
- input_type.change(fn=update_visibility, inputs=input_type, outputs=[text_input, audio_input])
160
-
161
- # Submit action with voice output
162
- submit_btn.click(
163
- fn=chat_function,
164
- inputs=[input_type, text_input, audio_input, chatbot],
165
- outputs=[chatbot, emotion_display, text_input, audio_output]
166
- )
167
-
168
- # Clear chat and audio
169
- clear_btn.click(
170
- lambda: ([], "", "", None),
171
- inputs=None,
172
- outputs=[chatbot, emotion_display, text_input, audio_output]
173
- )
 
 
 
 
 
174
 
175
  # Launch the app (commented out for Hugging Face Spaces)
176
  # if __name__ == "__main__":
 
1
  import gradio as gr
2
  import torch
 
3
  from groq import Groq
4
  import os
5
  import tempfile
 
44
  logger.error(f"Error with Groq API (text emotion): {str(e)}")
45
  return "neutral"
46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
  def generate_response(user_input, emotion):
48
  prompt = f"The user is feeling {emotion}. They said: '{user_input}'. Respond in a friendly caring manner with the user so the user feels being loved."
49
  try:
 
70
  logger.error(f"Error generating speech: {str(e)}")
71
  return None
72
 
73
+ # Chat function for Gradio with voice output (text input only)
74
  def chat_function(input_type, text_input, audio_input, chat_history):
75
  if input_type == "text" and text_input:
76
  user_input = text_input
 
 
77
  else:
78
+ return chat_history, "Please provide text input. Voice input is not supported.", gr.update(value=text_input), None
79
 
80
  emotion = predict_text_emotion(user_input)
81
  response = generate_response(user_input, emotion)
 
110
  """
111
 
112
  # Build the Gradio interface
113
+ try:
114
+ with gr.Blocks(theme=gr.themes.Soft(), css=css) as app:
115
+ gr.Markdown(
116
+ """
117
+ # Multimodal Mental Health AI Agent
118
+ Chat with our empathetic AI designed to support you by understanding your emotions through text.
119
+ """
120
+ )
121
+
122
+ with gr.Row():
123
+ with gr.Column(scale=1):
124
+ emotion_display = gr.Textbox(label="Emotion", interactive=False, placeholder="Detected emotion will appear here")
125
+
126
+ with gr.Column(scale=3):
127
+ chatbot = gr.Chatbot(label="Conversation History", height=500, type="messages", elem_classes="chatbot")
128
+
129
+ with gr.Row(elem_classes="input-container"):
130
+ input_type = gr.Radio(["text", "voice"], label="Input Method", value="text")
131
+ text_input = gr.Textbox(label="Type Your Message", placeholder="How are you feeling today?", visible=True)
132
+ audio_input = gr.Audio(sources=["microphone"], type="filepath", label="Record Your Message", visible=False)
133
+ submit_btn = gr.Button("Send", variant="primary")
134
+ clear_btn = gr.Button("Clear Chat", variant="secondary")
135
+ audio_output = gr.Audio(label="Assistant Response", type="filepath", interactive=False, autoplay=True)
136
+
137
+ # Dynamic visibility based on input type
138
+ def update_visibility(input_type):
139
+ return gr.update(visible=input_type == "text"), gr.update(visible=input_type == "voice")
140
+
141
+ input_type.change(fn=update_visibility, inputs=input_type, outputs=[text_input, audio_input])
142
+
143
+ # Submit action with voice output
144
+ submit_btn.click(
145
+ fn=chat_function,
146
+ inputs=[input_type, text_input, audio_input, chatbot],
147
+ outputs=[chatbot, emotion_display, text_input, audio_output]
148
+ )
149
+
150
+ # Clear chat and audio
151
+ clear_btn.click(
152
+ lambda: ([], "", "", None),
153
+ inputs=None,
154
+ outputs=[chatbot, emotion_display, text_input, audio_output]
155
+ )
156
+
157
+ except Exception as e:
158
+ logger.error(f"Error initializing Gradio interface: {str(e)}")
159
+ raise
160
 
161
  # Launch the app (commented out for Hugging Face Spaces)
162
  # if __name__ == "__main__":