DreamStream-1 commited on
Commit
d30f6a2
Β·
verified Β·
1 Parent(s): 658d2e0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -44
app.py CHANGED
@@ -13,17 +13,17 @@ import googlemaps
13
  import folium
14
  import torch
15
 
16
- # Disable GPU usage for TensorFlow
17
  os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
18
- os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' # Suppress TensorFlow warnings
19
 
20
- # Ensure necessary NLTK resources are downloaded
21
  nltk.download("punkt")
22
 
23
- # Initialize stemmer
24
  stemmer = LancasterStemmer()
25
 
26
- # Load chatbot intents and training data
27
  with open("intents.json") as file:
28
  intents_data = json.load(file)
29
 
@@ -95,37 +95,21 @@ def detect_emotion(user_input):
95
  def generate_suggestions(emotion):
96
  suggestions = {
97
  "joy": [
98
- ["Relaxation Techniques", '<a href="https://www.helpguide.org/mental-health/meditation/mindful-breathing-meditation" target="_blank">Read</a>'],
99
- ["Dealing with Stress", '<a href="https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety" target="_blank">Read</a>'],
100
- ["Emotional Wellness Toolkit", '<a href="https://www.nih.gov/health-information/emotional-wellness-toolkit" target="_blank">Read</a>'],
101
  ["Relaxation Video", '<a href="https://youtu.be/m1vaUGtyo-A" target="_blank">Watch</a>'],
102
  ],
103
  "anger": [
104
- ["Emotional Wellness Toolkit", '<a href="https://www.nih.gov/health-information/emotional-wellness-toolkit" target="_blank">Read</a>'],
105
- ["Stress Management Tips", '<a href="https://www.health.harvard.edu/health-a-to-z" target="_blank">Read</a>'],
106
- ["Dealing with Anger", '<a href="https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety" target="_blank">Read</a>'],
107
  ["Relaxation Video", '<a href="https://youtu.be/MIc299Flibs" target="_blank">Watch</a>'],
108
  ],
109
- "fear": [
110
- ["Mindfulness Practices", '<a href="https://www.helpguide.org/mental-health/meditation/mindful-breathing-meditation" target="_blank">Read</a>'],
111
- ["Coping with Anxiety", '<a href="https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety" target="_blank">Read</a>'],
112
- ["Emotional Wellness Toolkit", '<a href="https://www.nih.gov/health-information/emotional-wellness-toolkit" target="_blank">Read</a>'],
113
- ["Relaxation Video", '<a href="https://youtu.be/yGKKz185M5o" target="_blank">Watch</a>'],
114
- ],
115
- "sadness": [
116
- ["Emotional Wellness Toolkit", '<a href="https://www.nih.gov/health-information/emotional-wellness-toolkit" target="_blank">Read</a>'],
117
- ["Dealing with Anxiety", '<a href="https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety" target="_blank">Read</a>'],
118
- ["Relaxation Video", '<a href="https://youtu.be/-e-4Kx5px_I" target="_blank">Watch</a>'],
119
- ],
120
- "surprise": [
121
- ["Managing Stress", '<a href="https://www.health.harvard.edu/health-a-to-z" target="_blank">Read</a>'],
122
- ["Coping Strategies", '<a href="https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety" target="_blank">Read</a>'],
123
- ["Relaxation Video", '<a href="https://youtu.be/m1vaUGtyo-A" target="_blank">Watch</a>'],
124
- ],
125
  }
126
- return suggestions.get(emotion)
127
 
128
- # Search nearby professionals and generate map
129
  def get_health_professionals_and_map(location, query):
130
  try:
131
  geo_location = gmaps.geocode(location)
@@ -140,42 +124,43 @@ def get_health_professionals_and_map(location, query):
140
  folium.Marker([place["geometry"]["location"]["lat"], place["geometry"]["location"]["lng"]],
141
  popup=place["name"]).add_to(map_)
142
  return professionals, map_._repr_html_()
143
- return [], ""
144
  except Exception as e:
145
  return [f"Error: {e}"], ""
146
 
147
- # Application logic integrated in one function
148
  def app_function(message, location, query, history):
149
  chatbot_history, _ = chatbot(message, history)
150
  sentiment = analyze_sentiment(message)
151
  emotion = detect_emotion(message.lower())
152
  suggestions = generate_suggestions(emotion)
153
- professionals_info, map_html = get_health_professionals_and_map(location, query)
154
- return chatbot_history, sentiment, emotion, suggestions, professionals_info, map_html
155
 
156
  # Gradio app interface
157
  with gr.Blocks() as app:
158
  gr.Markdown("# 🌟 Well-Being Companion")
159
- gr.Markdown("Empowering Your Mental Health Journey πŸ’š")
160
 
161
  with gr.Row():
162
  user_message = gr.Textbox(label="Your Message", placeholder="Enter your message...")
163
- user_location = gr.Textbox(label="Your Location", placeholder="Enter location...")
164
- search_query = gr.Textbox(label="Query (e.g., therapist)", placeholder="Search for professionals...")
165
- submit_btn = gr.Button(value="Submit")
166
-
167
- chatbot_box = gr.Chatbot(label="Chat History", type="messages")
168
- emotion_display = gr.Textbox(label="Detected Emotion")
169
- sentiment_display = gr.Textbox(label="Detected Sentiment")
170
- suggestions_table = gr.DataFrame(headers=["Title", "Links"], label="Suggestions", height=250)
171
  map_output = gr.HTML(label="Nearby Professionals Map")
172
- professional_display = gr.Textbox(label="Nearby Professionals", lines=5)
173
 
174
  submit_btn.click(
175
  app_function,
176
  inputs=[user_message, user_location, search_query, chatbot_box],
177
  outputs=[
178
- chatbot_box, sentiment_display, emotion_display, suggestions_table, professional_display, map_output,
 
179
  ],
180
  )
181
 
 
13
  import folium
14
  import torch
15
 
16
+ # Disable GPU usage for TensorFlow and suppress TensorFlow warnings
17
  os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
18
+ os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
19
 
20
+ # Download NLTK resources
21
  nltk.download("punkt")
22
 
23
+ # Initialize Lancaster Stemmer
24
  stemmer = LancasterStemmer()
25
 
26
+ # Load intents.json and training data for the chatbot
27
  with open("intents.json") as file:
28
  intents_data = json.load(file)
29
 
 
95
  def generate_suggestions(emotion):
96
  suggestions = {
97
  "joy": [
98
+ ["Relaxation Techniques", '<a href="https://www.helpguide.org/mental-health/meditation/mindful-breathing-meditation" target="_blank">Visit</a>'],
99
+ ["Dealing with Stress", '<a href="https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety" target="_blank">Visit</a>'],
100
+ ["Emotional Wellness Toolkit", '<a href="https://www.nih.gov/health-information/emotional-wellness-toolkit" target="_blank">Visit</a>'],
101
  ["Relaxation Video", '<a href="https://youtu.be/m1vaUGtyo-A" target="_blank">Watch</a>'],
102
  ],
103
  "anger": [
104
+ ["Emotional Wellness Toolkit", '<a href="https://www.nih.gov/health-information/emotional-wellness-toolkit" target="_blank">Visit</a>'],
105
+ ["Stress Management Tips", '<a href="https://www.health.harvard.edu/health-a-to-z" target="_blank">Visit</a>'],
106
+ ["Dealing with Anger", '<a href="https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety" target="_blank">Visit</a>'],
107
  ["Relaxation Video", '<a href="https://youtu.be/MIc299Flibs" target="_blank">Watch</a>'],
108
  ],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
109
  }
110
+ return suggestions.get(emotion, [["No suggestions available", "", ""]])
111
 
112
+ # Search professionals and generate map
113
  def get_health_professionals_and_map(location, query):
114
  try:
115
  geo_location = gmaps.geocode(location)
 
124
  folium.Marker([place["geometry"]["location"]["lat"], place["geometry"]["location"]["lng"]],
125
  popup=place["name"]).add_to(map_)
126
  return professionals, map_._repr_html_()
127
+ return ["No professionals found"], ""
128
  except Exception as e:
129
  return [f"Error: {e}"], ""
130
 
131
+ # Main app function
132
  def app_function(message, location, query, history):
133
  chatbot_history, _ = chatbot(message, history)
134
  sentiment = analyze_sentiment(message)
135
  emotion = detect_emotion(message.lower())
136
  suggestions = generate_suggestions(emotion)
137
+ professionals, map_html = get_health_professionals_and_map(location, query)
138
+ return chatbot_history, sentiment, emotion, suggestions, professionals, map_html
139
 
140
  # Gradio app interface
141
  with gr.Blocks() as app:
142
  gr.Markdown("# 🌟 Well-Being Companion")
143
+ gr.Markdown("Empowering your Well-Being journey πŸ’š")
144
 
145
  with gr.Row():
146
  user_message = gr.Textbox(label="Your Message", placeholder="Enter your message...")
147
+ user_location = gr.Textbox(label="Your Location", placeholder="Enter your location...")
148
+ search_query = gr.Textbox(label="Query", placeholder="Search for professionals...")
149
+ submit_btn = gr.Button("Submit")
150
+
151
+ chatbot_box = gr.Chatbot(label="Chat History")
152
+ emotion_output = gr.Textbox(label="Detected Emotion")
153
+ sentiment_output = gr.Textbox(label="Detected Sentiment")
154
+ suggestions_output = gr.DataFrame(headers=["Title", "Links"], label="Suggestions")
155
  map_output = gr.HTML(label="Nearby Professionals Map")
156
+ professional_list = gr.Textbox(label="Nearby Professionals", lines=5)
157
 
158
  submit_btn.click(
159
  app_function,
160
  inputs=[user_message, user_location, search_query, chatbot_box],
161
  outputs=[
162
+ chatbot_box, sentiment_output, emotion_output,
163
+ suggestions_output, professional_list, map_output,
164
  ],
165
  )
166