DreamStream-1 commited on
Commit
3218176
·
verified ·
1 Parent(s): e5f8869

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -33
app.py CHANGED
@@ -100,8 +100,8 @@ model_sentiment = AutoModelForSequenceClassification.from_pretrained("cardiffnlp
100
  sentiment_pipeline = pipeline("sentiment-analysis")
101
 
102
  def predict_sentiment(text):
103
- result = sentiment_pipeline(text)[0]
104
- return result['label']
105
 
106
  # Emotion detection
107
  tokenizer_emotion = AutoTokenizer.from_pretrained("j-hartmann/emotion-english-distilroberta-base")
@@ -109,8 +109,8 @@ model_emotion = AutoModelForSequenceClassification.from_pretrained("j-hartmann/e
109
  emotion_pipeline = pipeline("text-classification", model=model_emotion, tokenizer=tokenizer_emotion)
110
 
111
  def predict_emotion(text):
112
- result = emotion_pipeline(text)[0]
113
- return result['label']
114
 
115
  # Fetching nearby health professionals
116
  google_places_url = "https://maps.googleapis.com/maps/api/place/textsearch/json"
@@ -151,9 +151,46 @@ def fetch_nearby_health_professionals(location):
151
 
152
  return pd.DataFrame(data, columns=['Name', 'Rating', 'Phone Number'])
153
 
154
- # Save results to CSV
155
- def save_to_csv(data, filename):
156
- data.to_csv(filename, index=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
157
 
158
  # Gradio interface
159
  with gr.Blocks() as demo:
@@ -165,8 +202,7 @@ with gr.Blocks() as demo:
165
 
166
  # Model prediction for emotion detection
167
  def predict_emotion(text):
168
- result = emotion_pipeline(text)
169
- return result[0]['label']
170
 
171
  # Show suggestions based on the detected emotion
172
  def show_suggestions(emotion):
@@ -197,7 +233,7 @@ with gr.Blocks() as demo:
197
  message_input = gr.Textbox(lines=1, label="Message")
198
  submit_chat = gr.Button("Send")
199
 
200
- def chat(message, history):
201
  history = history or []
202
  message = message.lower()
203
 
@@ -220,9 +256,14 @@ with gr.Blocks() as demo:
220
  response = f"An error occurred: {str(e)}"
221
 
222
  history.append((message, response))
223
- return history, history
 
 
 
 
 
224
 
225
- submit_chat.click(chat, inputs=[message_input, gr.State()], outputs=[chatbot, gr.State()])
226
 
227
  # Location input for fetching nearby health professionals
228
  location_input = gr.Textbox(lines=1, label="Enter your location (plain English):")
@@ -236,27 +277,21 @@ with gr.Blocks() as demo:
236
  nearby_health_professionals_table = gr.Dataframe(headers=["Name", "Rating", "Phone Number"])
237
  submit_location.click(fetch_nearby_health_professionals, inputs=location_input, outputs=nearby_health_professionals_table)
238
 
239
- # User input for text (sentiment analysis)
240
- user_input_sentiment = gr.Textbox(lines=1, label="Enter text to analyze sentiment:")
241
- submit_sentiment = gr.Button("Submit")
242
-
243
- # Prediction button for sentiment analysis
244
- def predict_sentiment(text):
245
- result = sentiment_pipeline(text)
246
- return result[0]['label']
247
-
248
- sentiment_output = gr.Textbox(label="Predicted Sentiment")
249
- submit_sentiment.click(predict_sentiment, inputs=user_input_sentiment, outputs=sentiment_output)
250
-
251
- # Button to fetch wellness professionals data
252
- fetch_button = gr.Button("Fetch Wellness Professionals Data")
253
- data_output = gr.Dataframe(headers=["Name", "Rating", "Phone Number"])
254
-
255
- def fetch_data():
256
- df = fetch_nearby_health_professionals("Hawaii")
257
- return df
258
-
259
- fetch_button.click(fetch_data, inputs=None, outputs=data_output)
260
 
261
  # Launch Gradio interface
262
  demo.launch()
 
100
  sentiment_pipeline = pipeline("sentiment-analysis")
101
 
102
  def predict_sentiment(text):
103
+ result = sentiment_pipeline(text)
104
+ return result[0]['label']
105
 
106
  # Emotion detection
107
  tokenizer_emotion = AutoTokenizer.from_pretrained("j-hartmann/emotion-english-distilroberta-base")
 
109
  emotion_pipeline = pipeline("text-classification", model=model_emotion, tokenizer=tokenizer_emotion)
110
 
111
  def predict_emotion(text):
112
+ result = emotion_pipeline(text)
113
+ return result[0]['label']
114
 
115
  # Fetching nearby health professionals
116
  google_places_url = "https://maps.googleapis.com/maps/api/place/textsearch/json"
 
151
 
152
  return pd.DataFrame(data, columns=['Name', 'Rating', 'Phone Number'])
153
 
154
+ # Gradio interface for recommendations
155
+ def generate_recommendations(emotion):
156
+ if emotion == 'joy':
157
+ return [
158
+ {"title": "Mindful Breathing Meditation", "link": "https://www.helpguide.org/mental-health/meditation/mindful-breathing-meditation"},
159
+ {"title": "Dealing with Stress", "link": "https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety"},
160
+ {"title": "Emotional Wellness Toolkit", "link": "https://www.nih.gov/health-information/emotional-wellness-toolkit"}
161
+ ]
162
+ elif emotion == 'anger':
163
+ return [
164
+ {"title": "Emotional Wellness Toolkit", "link": "https://www.nih.gov/health-information/emotional-wellness-toolkit"},
165
+ {"title": "Stress Management Tips", "link": "https://www.health.harvard.edu/health-a-to-z"},
166
+ {"title": "Dealing with Anger", "link": "https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety"}
167
+ ]
168
+ elif emotion == 'fear':
169
+ return [
170
+ {"title": "Mindfulness Practices", "link": "https://www.helpguide.org/mental-health/meditation/mindful-breathing-meditation"},
171
+ {"title": "Coping with Anxiety", "link": "https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety"},
172
+ {"title": "Emotional Wellness Toolkit", "link": "https://www.nih.gov/health-information/emotional-wellness-toolkit"}
173
+ ]
174
+ elif emotion == 'sadness':
175
+ return [
176
+ {"title": "Emotional Wellness Toolkit", "link": "https://www.nih.gov/health-information/emotional-wellness-toolkit"},
177
+ {"title": "Dealing with Anxiety", "link": "https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety"}
178
+ ]
179
+ elif emotion == 'surprise':
180
+ return [
181
+ {"title": "Managing Stress", "link": "https://www.health.harvard.edu/health-a-to-z"},
182
+ {"title": "Coping Strategies", "link": "https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety"}
183
+ ]
184
+ else:
185
+ return []
186
+
187
+ def recommendations_interface(emotion):
188
+ recs = generate_recommendations(emotion)
189
+ html = "<ul>"
190
+ for rec in recs:
191
+ html += f"<li><a href='{rec['link']}'>{rec['title']}</a></li>"
192
+ html += "</ul>"
193
+ return html
194
 
195
  # Gradio interface
196
  with gr.Blocks() as demo:
 
202
 
203
  # Model prediction for emotion detection
204
  def predict_emotion(text):
205
+ return predict_emotion(text)
 
206
 
207
  # Show suggestions based on the detected emotion
208
  def show_suggestions(emotion):
 
233
  message_input = gr.Textbox(lines=1, label="Message")
234
  submit_chat = gr.Button("Send")
235
 
236
+ def chat_with_sentiment_and_emotion(message, history):
237
  history = history or []
238
  message = message.lower()
239
 
 
256
  response = f"An error occurred: {str(e)}"
257
 
258
  history.append((message, response))
259
+
260
+ # Sentiment analysis
261
+ sentiment_result = predict_sentiment(message)
262
+ emotion_result = predict_emotion(message)
263
+
264
+ return history, history, sentiment_result, emotion_result
265
 
266
+ submit_chat.click(chat_with_sentiment_and_emotion, inputs=[message_input, gr.State()], outputs=[chatbot, gr.State(), gr.Textbox(), gr.Textbox()])
267
 
268
  # Location input for fetching nearby health professionals
269
  location_input = gr.Textbox(lines=1, label="Enter your location (plain English):")
 
277
  nearby_health_professionals_table = gr.Dataframe(headers=["Name", "Rating", "Phone Number"])
278
  submit_location.click(fetch_nearby_health_professionals, inputs=location_input, outputs=nearby_health_professionals_table)
279
 
280
+ # Article and video recommendations
281
+ article_video_recs = gr.HTML("<h2>Article and Video Recommendations</h2><p>No recommendations yet.</p>")
282
+ recommendations_ui = gr.Interface(
283
+ fn=recommendations_interface,
284
+ inputs="text",
285
+ outputs="html",
286
+ title="Article and Video Recommendations",
287
+ description="Get personalized recommendations based on your mood."
288
+ )
289
+
290
+ def update_recommendations(emotion):
291
+ recs_html = recommendations_interface(emotion)
292
+ article_video_recs.update(recs_html)
293
+
294
+ emotion_output.change(update_recommendations, inputs=emotion_output, outputs=article_video_recs)
 
 
 
 
 
 
295
 
296
  # Launch Gradio interface
297
  demo.launch()