Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,58 @@ from transformers import pipeline
|
|
| 4 |
# Load the emotion classification model
|
| 5 |
emotion_analyzer = pipeline("text-classification", model="cardiffnlp/twitter-roberta-base-sentiment")
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
# Streamlit app interface
|
| 8 |
st.title("Emotion Prediction and Well-Being Suggestions")
|
| 9 |
|
|
@@ -17,20 +69,20 @@ if st.button("Get Suggestions"):
|
|
| 17 |
user_input = f"{question1} {question2} {question3}"
|
| 18 |
|
| 19 |
# Perform emotion analysis
|
| 20 |
-
|
| 21 |
|
| 22 |
-
#
|
| 23 |
-
|
| 24 |
-
suggestion = "It's great to hear that you're feeling positive! Consider doing a relaxing activity like walking on the beach or practicing mindfulness."
|
| 25 |
-
video_url = "https://www.youtube.com/watch?v=1LkV2STw2so" # Example relaxation video
|
| 26 |
-
elif "negative" in emotion[0]['label'].lower():
|
| 27 |
-
suggestion = "It seems like you're experiencing some stress. Try deep breathing exercises or a walk in nature to relieve tension."
|
| 28 |
-
video_url = "https://www.youtube.com/watch?v=5J5nZ9Tr6Cw" # Example deep breathing exercise video
|
| 29 |
-
else:
|
| 30 |
-
suggestion = "It seems like you're having a mixed experience. Balance your day with some light exercise like hula dancing or yoga."
|
| 31 |
-
video_url = "https://www.youtube.com/watch?v=Hk4pJOPsFq4" # Example hula dancing video
|
| 32 |
|
| 33 |
-
#
|
| 34 |
-
st.write(
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
# Load the emotion classification model
|
| 5 |
emotion_analyzer = pipeline("text-classification", model="cardiffnlp/twitter-roberta-base-sentiment")
|
| 6 |
|
| 7 |
+
# Enhanced Suggestion Database (Now includes resources)
|
| 8 |
+
suggestion_database = {
|
| 9 |
+
"sadness": {
|
| 10 |
+
"suggestions": ["Try a guided meditation", "Take a walk in nature", "Connect with a friend"],
|
| 11 |
+
"articles": [
|
| 12 |
+
{"title": "Overcoming Sadness", "url": "https://example.com/sadness1"},
|
| 13 |
+
{"title": "Understanding Depression", "url": "https://example.com/sadness2"},
|
| 14 |
+
],
|
| 15 |
+
"videos": [
|
| 16 |
+
{"title": "Mindfulness for Sadness", "url": "https://www.youtube.com/watch?v=sadnessvideo1"},
|
| 17 |
+
{"title": "Coping with Grief", "url": "https://www.youtube.com/watch?v=sadnessvideo2"},
|
| 18 |
+
],
|
| 19 |
+
},
|
| 20 |
+
"joy": {
|
| 21 |
+
"suggestions": ["Practice gratitude", "Engage in a hobby", "Spend time with loved ones"],
|
| 22 |
+
"articles": [
|
| 23 |
+
{"title": "The Benefits of Joy", "url": "https://example.com/joy1"},
|
| 24 |
+
{"title": "Maintaining Positive Emotions", "url": "https://example.com/joy2"},
|
| 25 |
+
],
|
| 26 |
+
"videos": [
|
| 27 |
+
{"title": "Boosting Your Happiness", "url": "https://www.youtube.com/watch?v=joyvideo1"},
|
| 28 |
+
{"title": "Practicing Gratitude", "url": "https://www.youtube.com/watch?v=joyvideo2"},
|
| 29 |
+
],
|
| 30 |
+
},
|
| 31 |
+
"neutral": {
|
| 32 |
+
"suggestions": ["Take a break", "Engage in a relaxing activity", "Spend time in nature"],
|
| 33 |
+
"articles": [
|
| 34 |
+
{"title": "Importance of Self-Care", "url": "https://example.com/selfcare1"},
|
| 35 |
+
{"title": "Stress Management Techniques", "url": "https://example.com/stress1"},
|
| 36 |
+
],
|
| 37 |
+
"videos": [
|
| 38 |
+
{"title": "Relaxation Techniques", "url": "https://www.youtube.com/watch?v=relaxvideo1"},
|
| 39 |
+
{"title": "Mindfulness Exercises", "url": "https://www.youtube.com/watch?v=mindfulnessvideo1"},
|
| 40 |
+
],
|
| 41 |
+
}
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
# Function to fetch relevant resources based on emotion
|
| 45 |
+
def get_relevant_resources(emotion):
|
| 46 |
+
resources = suggestion_database.get(emotion, {})
|
| 47 |
+
return resources.get("suggestions", []), resources.get("articles", []), resources.get("videos", [])
|
| 48 |
+
|
| 49 |
+
# Enhanced Suggestion Function
|
| 50 |
+
def suggest_activity(emotion_analysis):
|
| 51 |
+
max_emotion = max(emotion_analysis, key=emotion_analysis.get) if emotion_analysis else "neutral"
|
| 52 |
+
suggestions, articles, videos = get_relevant_resources(max_emotion)
|
| 53 |
+
return {
|
| 54 |
+
"suggestions": suggestions,
|
| 55 |
+
"articles": articles,
|
| 56 |
+
"videos": videos,
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
# Streamlit app interface
|
| 60 |
st.title("Emotion Prediction and Well-Being Suggestions")
|
| 61 |
|
|
|
|
| 69 |
user_input = f"{question1} {question2} {question3}"
|
| 70 |
|
| 71 |
# Perform emotion analysis
|
| 72 |
+
emotion_analysis = emotion_analyzer(user_input)
|
| 73 |
|
| 74 |
+
# Get suggestions, articles, and videos based on the emotion
|
| 75 |
+
resources = suggest_activity(emotion_analysis)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
+
# Display suggestions, articles, and videos
|
| 78 |
+
st.write("Here are some suggestions to help you:")
|
| 79 |
+
for suggestion in resources["suggestions"]:
|
| 80 |
+
st.write(f"- {suggestion}")
|
| 81 |
+
|
| 82 |
+
st.write("Articles you may find helpful:")
|
| 83 |
+
for article in resources["articles"]:
|
| 84 |
+
st.markdown(f"[{article['title']}]({article['url']})")
|
| 85 |
+
|
| 86 |
+
st.write("Videos you may find helpful:")
|
| 87 |
+
for video in resources["videos"]:
|
| 88 |
+
st.markdown(f"[{video['title']}]({video['url']})")
|