DreamStream-1 commited on
Commit
864d91e
·
verified ·
1 Parent(s): 5255f91

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -14
app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import nltk
2
  import numpy as np
3
  import tflearn
@@ -5,14 +6,14 @@ import tensorflow
5
  import random
6
  import json
7
  import pickle
8
- import gradio as gr
9
  from nltk.tokenize import word_tokenize
10
  from nltk.stem.lancaster import LancasterStemmer
11
  from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
12
  import googlemaps
13
  import folium
14
  import os
15
- import torch # Add this for sentiment analysis
 
16
 
17
  # Ensure necessary NLTK resources are downloaded
18
  nltk.download('punkt')
@@ -20,15 +21,15 @@ nltk.download('punkt')
20
  # Initialize the stemmer
21
  stemmer = LancasterStemmer()
22
 
23
- # Load intents.json for Mental Health Chatbot
24
  with open("intents.json") as file:
25
  data = json.load(file)
26
 
27
- # Load preprocessed data for Mental Health Chatbot
28
  with open("data.pickle", "rb") as f:
29
  words, labels, training, output = pickle.load(f)
30
 
31
- # Build the model structure for Mental Health Chatbot
32
  net = tflearn.input_data(shape=[None, len(training[0])])
33
  net = tflearn.fully_connected(net, 8)
34
  net = tflearn.fully_connected(net, 8)
@@ -50,7 +51,7 @@ def bag_of_words(s, words):
50
  bag[i] = 1
51
  return np.array(bag)
52
 
53
- # Chat function for Mental Health Chatbot
54
  def chatbot(message, history):
55
  history = history or []
56
  message = message.lower()
@@ -74,7 +75,6 @@ def chatbot(message, history):
74
  history.append((message, response))
75
  return history, history
76
 
77
-
78
  # Sentiment Analysis using Hugging Face model
79
  tokenizer = AutoTokenizer.from_pretrained("cardiffnlp/twitter-roberta-base-sentiment")
80
  model_sentiment = AutoModelForSequenceClassification.from_pretrained("cardiffnlp/twitter-roberta-base-sentiment")
@@ -142,8 +142,46 @@ def get_health_professionals_and_map(current_location, health_professional_query
142
 
143
  return route_info, m._repr_html_()
144
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
145
  # Gradio interface
146
- def gradio_app(message, location, health_query, history, state):
147
  # Chatbot interaction
148
  history, _ = chatbot(message, history)
149
 
@@ -156,29 +194,38 @@ def gradio_app(message, location, health_query, history, state):
156
  # Health professional search and map display
157
  route_info, map_html = get_health_professionals_and_map(location, health_query)
158
 
159
- return history, sentiment_response, emotion_response, route_info, map_html, state
 
 
 
 
 
 
160
 
161
  # Gradio UI components
162
  message_input = gr.Textbox(lines=1, label="Message")
163
  location_input = gr.Textbox(value="Honolulu, HI", label="Current Location")
164
  health_query_input = gr.Textbox(value="doctor", label="Health Professional Query (e.g., doctor, psychiatrist, psychologist)")
 
165
 
166
- chat_history = gr.Chatbot(label="Chat History")
167
 
168
  # Outputs
169
  sentiment_output = gr.Textbox(label="Sentiment Analysis Result")
170
  emotion_output = gr.Textbox(label="Emotion Detection Result")
171
  route_info_output = gr.Textbox(label="Health Professionals Information")
172
  map_output = gr.HTML(label="Map with Health Professionals")
 
173
 
174
  # Create Gradio interface
175
  iface = gr.Interface(
176
  fn=gradio_app,
177
- inputs=[message_input, location_input, health_query_input, gr.State()], # Updated state input
178
- outputs=[chat_history, sentiment_output, emotion_output, route_info_output, map_output, gr.State()], # Updated state output
179
  allow_flagging="never",
180
  live=True,
181
- title="Wellbeing App: Mental Health, Sentiment, Emotion Detection & Health Professional Search"
182
  )
183
 
184
- iface.launch()
 
 
1
+ import gradio as gr
2
  import nltk
3
  import numpy as np
4
  import tflearn
 
6
  import random
7
  import json
8
  import pickle
 
9
  from nltk.tokenize import word_tokenize
10
  from nltk.stem.lancaster import LancasterStemmer
11
  from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
12
  import googlemaps
13
  import folium
14
  import os
15
+ import pandas as pd
16
+ import torch
17
 
18
  # Ensure necessary NLTK resources are downloaded
19
  nltk.download('punkt')
 
21
  # Initialize the stemmer
22
  stemmer = LancasterStemmer()
23
 
24
+ # Load intents.json for Well-Being Chatbot
25
  with open("intents.json") as file:
26
  data = json.load(file)
27
 
28
+ # Load preprocessed data for Well-Being Chatbot
29
  with open("data.pickle", "rb") as f:
30
  words, labels, training, output = pickle.load(f)
31
 
32
+ # Build the model structure for Well-Being Chatbot
33
  net = tflearn.input_data(shape=[None, len(training[0])])
34
  net = tflearn.fully_connected(net, 8)
35
  net = tflearn.fully_connected(net, 8)
 
51
  bag[i] = 1
52
  return np.array(bag)
53
 
54
+ # Chat function for Well-Being Chatbot
55
  def chatbot(message, history):
56
  history = history or []
57
  message = message.lower()
 
75
  history.append((message, response))
76
  return history, history
77
 
 
78
  # Sentiment Analysis using Hugging Face model
79
  tokenizer = AutoTokenizer.from_pretrained("cardiffnlp/twitter-roberta-base-sentiment")
80
  model_sentiment = AutoModelForSequenceClassification.from_pretrained("cardiffnlp/twitter-roberta-base-sentiment")
 
142
 
143
  return route_info, m._repr_html_()
144
 
145
+ # Function to generate suggestions based on the detected emotion
146
+ def generate_suggestions(emotion):
147
+ if emotion == 'joy':
148
+ return [
149
+ {"Title": "Relaxation Techniques", "Subject": "Relaxation", "Link": "https://www.helpguide.org/mental-health/meditation/mindful-breathing-meditation"},
150
+ {"Title": "Dealing with Stress", "Subject": "Stress Management", "Link": "https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety"},
151
+ {"Title": "Emotional Wellness Toolkit", "Subject": "Wellness", "Link": "https://www.nih.gov/health-information/emotional-wellness-toolkit"},
152
+ {"Title": "Relaxation Video", "Subject": "Video", "Link": "https://youtu.be/m1vaUGtyo-A"}
153
+ ]
154
+ elif emotion == 'anger':
155
+ return [
156
+ {"Title": "Emotional Wellness Toolkit", "Subject": "Wellness", "Link": "https://www.nih.gov/health-information/emotional-wellness-toolkit"},
157
+ {"Title": "Stress Management Tips", "Subject": "Stress Management", "Link": "https://www.health.harvard.edu/health-a-to-z"},
158
+ {"Title": "Dealing with Anger", "Subject": "Anger Management", "Link": "https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety"},
159
+ {"Title": "Relaxation Video", "Subject": "Video", "Link": "https://youtu.be/MIc299Flibs"}
160
+ ]
161
+ elif emotion == 'fear':
162
+ return [
163
+ {"Title": "Mindfulness Practices", "Subject": "Mindfulness", "Link": "https://www.helpguide.org/mental-health/meditation/mindful-breathing-meditation"},
164
+ {"Title": "Coping with Anxiety", "Subject": "Anxiety Management", "Link": "https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety"},
165
+ {"Title": "Emotional Wellness Toolkit", "Subject": "Wellness", "Link": "https://www.nih.gov/health-information/emotional-wellness-toolkit"},
166
+ {"Title": "Relaxation Video", "Subject": "Video", "Link": "https://youtu.be/yGKKz185M5o"}
167
+ ]
168
+ elif emotion == 'sadness':
169
+ return [
170
+ {"Title": "Emotional Wellness Toolkit", "Subject": "Wellness", "Link": "https://www.nih.gov/health-information/emotional-wellness-toolkit"},
171
+ {"Title": "Dealing with Anxiety", "Subject": "Anxiety Management", "Link": "https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety"},
172
+ {"Title": "Relaxation Video", "Subject": "Video", "Link": "https://youtu.be/-e-4Kx5px_I"}
173
+ ]
174
+ elif emotion == 'surprise':
175
+ return [
176
+ {"Title": "Managing Stress", "Subject": "Stress Management", "Link": "https://www.health.harvard.edu/health-a-to-z"},
177
+ {"Title": "Coping Strategies", "Subject": "Coping", "Link": "https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety"},
178
+ {"Title": "Relaxation Video", "Subject": "Video", "Link": "https://youtu.be/m1vaUGtyo-A"}
179
+ ]
180
+ else:
181
+ return []
182
+
183
  # Gradio interface
184
+ def gradio_app(message, location, health_query, submit_button, history, state):
185
  # Chatbot interaction
186
  history, _ = chatbot(message, history)
187
 
 
194
  # Health professional search and map display
195
  route_info, map_html = get_health_professionals_and_map(location, health_query)
196
 
197
+ # Generate suggestions based on the detected emotion
198
+ suggestions = generate_suggestions(emotion_response.split(': ')[1])
199
+
200
+ # Create a DataFrame for displaying suggestions
201
+ suggestions_df = pd.DataFrame(suggestions)
202
+
203
+ return history, sentiment_response, emotion_response, route_info, map_html, gr.DataFrame(suggestions_df, headers=["Title", "Subject", "Link"]), state
204
 
205
  # Gradio UI components
206
  message_input = gr.Textbox(lines=1, label="Message")
207
  location_input = gr.Textbox(value="Honolulu, HI", label="Current Location")
208
  health_query_input = gr.Textbox(value="doctor", label="Health Professional Query (e.g., doctor, psychiatrist, psychologist)")
209
+ submit_button = gr.Button("Submit") # Submit button for triggering the app function
210
 
211
+ chat_history = gr.Chatbot(label="Well-Being Chat History") # Renamed chatbot label
212
 
213
  # Outputs
214
  sentiment_output = gr.Textbox(label="Sentiment Analysis Result")
215
  emotion_output = gr.Textbox(label="Emotion Detection Result")
216
  route_info_output = gr.Textbox(label="Health Professionals Information")
217
  map_output = gr.HTML(label="Map with Health Professionals")
218
+ suggestions_output = gr.DataFrame(label="Well-Being Suggestions", headers=["Title", "Subject", "Link"]) # Renamed suggestions output
219
 
220
  # Create Gradio interface
221
  iface = gr.Interface(
222
  fn=gradio_app,
223
+ inputs=[message_input, location_input, health_query_input, submit_button, gr.State(), gr.State()], # Updated inputs to include submit button
224
+ outputs=[chat_history, sentiment_output, emotion_output, route_info_output, map_output, suggestions_output, gr.State()], # Outputs remain unchanged
225
  allow_flagging="never",
226
  live=True,
227
+ title="Well-Being App: Support, Sentiment, Emotion Detection & Health Professional Search"
228
  )
229
 
230
+ # Launch the Gradio interface
231
+ iface.launch()