Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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)
|
| 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)
|
| 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 |
-
#
|
| 155 |
-
def
|
| 156 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 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
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 224 |
|
| 225 |
-
submit_chat.click(
|
| 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 |
-
#
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 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()
|