DreamStream-1 commited on
Commit
6239dea
·
verified ·
1 Parent(s): 5e7f7ee

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -5
app.py CHANGED
@@ -169,6 +169,43 @@ def get_wellness_professionals(location):
169
  # Fallback to scraping if API is not available or fails
170
  return scrape_wellness_professionals(query, location)
171
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
172
  # Gradio interface setup for user interaction
173
  def user_interface(message, location, history):
174
  history, history = chat(message, history)
@@ -179,14 +216,15 @@ def user_interface(message, location, history):
179
  sentiment = ["Negative", "Neutral", "Positive"][torch.argmax(outputs.logits, dim=1).item()]
180
 
181
  # Emotion detection
182
- pipe = pipeline("text-classification", model=model_emotion, tokenizer=tokenizer_emotion)
183
- emotion_result = pipe(message)
184
- emotion = emotion_result[0]['label']
185
 
186
  # Get wellness professionals
187
  wellness_data = get_wellness_professionals(location)
188
 
189
- return history, history, sentiment, emotion, wellness_data
 
 
 
190
 
191
  # Gradio chatbot interface
192
  chatbot = gr.Chatbot(label="Mental Health Chatbot")
@@ -196,7 +234,7 @@ location_input = gr.Textbox(label="Enter your location (latitude,longitude)", pl
196
  demo = gr.Interface(
197
  user_interface,
198
  [gr.Textbox(label="Message"), location_input, "state"],
199
- [chatbot, "state", "text", "text", "json"],
200
  allow_flagging="never",
201
  title="Mental Health & Well-being Assistant"
202
  )
 
169
  # Fallback to scraping if API is not available or fails
170
  return scrape_wellness_professionals(query, location)
171
 
172
+ # Emotion detection function with suggestions
173
+ def detect_emotion(user_input):
174
+ pipe = pipeline("text-classification", model=model_emotion, tokenizer=tokenizer_emotion)
175
+ result = pipe(user_input)
176
+ emotion = result[0]['label']
177
+
178
+ # Provide suggestions based on the detected emotion
179
+ if emotion == 'joy':
180
+ return ("You're feeling happy! Keep up the great mood!",
181
+ [("Relaxation Techniques", "https://www.helpguide.org/mental-health/meditation/mindful-breathing-meditation"),
182
+ ("Dealing with Stress", "https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety"),
183
+ ("Emotional Wellness Toolkit", "https://www.nih.gov/health-information/emotional-wellness-toolkit")],
184
+ "Watch on YouTube: https://youtu.be/m1vaUGtyo-A")
185
+ elif emotion == 'anger':
186
+ return ("You're feeling angry. It's okay to feel this way. Let's try to calm down.",
187
+ [("Emotional Wellness Toolkit", "https://www.nih.gov/health-information/emotional-wellness-toolkit"),
188
+ ("Stress Management Tips", "https://www.health.harvard.edu/health-a-to-z"),
189
+ ("Dealing with Anger", "https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety")],
190
+ "Watch on YouTube: https://youtu.be/MIc299Flibs")
191
+ elif emotion == 'fear':
192
+ return ("You're feeling fearful. Take a moment to breathe and relax.",
193
+ [("Mindfulness Practices", "https://www.helpguide.org/mental-health/meditation/mindful-breathing-meditation"),
194
+ ("Coping with Anxiety", "https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety"),
195
+ ("Emotional Wellness Toolkit", "https://www.nih.gov/health-information/emotional-wellness-toolkit")],
196
+ "Watch on YouTube: https://youtu.be/yGKKz185M5o")
197
+ elif emotion == 'sadness':
198
+ return ("You're feeling sad. It's okay to take a break.",
199
+ [("Emotional Wellness Toolkit", "https://www.nih.gov/health-information/emotional-wellness-toolkit"),
200
+ ("Dealing with Anxiety", "https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety")],
201
+ "Watch on YouTube: https://youtu.be/-e-4Kx5px_I")
202
+ elif emotion == 'surprise':
203
+ return ("You're feeling surprised. It's okay to feel neutral!",
204
+ [("Managing Stress", "https://www.health.harvard.edu/health-a-to-z"),
205
+ ("Coping Strategies", "https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety")],
206
+ "Watch on YouTube: https://youtu.be/m1vaUGtyo-A")
207
+ return ("Could not detect emotion.", [], "")
208
+
209
  # Gradio interface setup for user interaction
210
  def user_interface(message, location, history):
211
  history, history = chat(message, history)
 
216
  sentiment = ["Negative", "Neutral", "Positive"][torch.argmax(outputs.logits, dim=1).item()]
217
 
218
  # Emotion detection
219
+ emotion_msg, resources, video_link = detect_emotion(message)
 
 
220
 
221
  # Get wellness professionals
222
  wellness_data = get_wellness_professionals(location)
223
 
224
+ # Display wellness professionals in a table format
225
+ wellness_df = pd.DataFrame(wellness_data, columns=["Name", "Address", "Latitude", "Longitude"])
226
+
227
+ return history, history, sentiment, emotion_msg, resources, video_link, wellness_df.to_html(escape=False)
228
 
229
  # Gradio chatbot interface
230
  chatbot = gr.Chatbot(label="Mental Health Chatbot")
 
234
  demo = gr.Interface(
235
  user_interface,
236
  [gr.Textbox(label="Message"), location_input, "state"],
237
+ [chatbot, "state", "text", "text", "json", "text", "html"],
238
  allow_flagging="never",
239
  title="Mental Health & Well-being Assistant"
240
  )