Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,76 +2,110 @@ import random
|
|
2 |
import streamlit as st
|
3 |
from transformers import pipeline
|
4 |
|
5 |
-
#
|
6 |
emotion_analyzer = pipeline("text-classification", model="distilbert-base-uncased")
|
7 |
|
8 |
-
#
|
9 |
questions = [
|
10 |
-
"How are you feeling
|
11 |
-
"What
|
12 |
-
"
|
13 |
]
|
14 |
|
15 |
-
#
|
16 |
-
moods = [
|
17 |
-
"Happy", "Excited", "Relaxed", "Grateful", "Calm",
|
18 |
-
"Dull", "Neutral", "Tired", "Bored", "Lonely",
|
19 |
-
"Angry", "Frustrated", "Anxious", "Stressed", "Overwhelmed",
|
20 |
-
"Hopeful", "Confused", "Motivated", "Curious", "Peaceful"
|
21 |
-
]
|
22 |
-
|
23 |
-
# Suggestion Database
|
24 |
suggestion_database = {
|
25 |
-
"
|
26 |
-
"suggestions": ["
|
27 |
"articles": [
|
28 |
{"title": "Emotional Wellness Toolkit", "url": "https://www.nih.gov/health-information/emotional-wellness-toolkit"},
|
29 |
-
{"title": "
|
30 |
],
|
31 |
"videos": [
|
32 |
-
{"title": "
|
33 |
-
{"title": "
|
34 |
],
|
35 |
},
|
36 |
-
"
|
37 |
-
"suggestions": ["
|
38 |
"articles": [
|
39 |
-
{"title": "
|
40 |
-
{"title": "
|
41 |
],
|
42 |
"videos": [
|
43 |
-
{"title": "
|
44 |
-
{"title": "
|
45 |
],
|
46 |
},
|
47 |
"NEUTRAL": {
|
48 |
-
"suggestions": ["Take a short
|
49 |
-
"articles": [
|
50 |
-
{"title": "Finding Balance", "url": "https://www.health.harvard.edu/health-a-to-z"},
|
51 |
-
{"title": "Emotional Wellness Toolkit", "url": "https://www.nih.gov/health-information/emotional-wellness-toolkit"},
|
52 |
-
],
|
53 |
-
"videos": [
|
54 |
-
{"title": "Mindfulness for Beginners", "url": "https://youtu.be/Y8HIFRPU6pM"},
|
55 |
-
{"title": "Peaceful Mind Short", "url": "https://www.youtube.com/shorts/hTXMi7ZBKdM?feature=share"},
|
56 |
-
],
|
57 |
-
},
|
58 |
-
"STRESSED": {
|
59 |
-
"suggestions": ["Practice deep breathing exercises.", "Write down your thoughts to release stress.", "Spend time in a quiet environment."],
|
60 |
"articles": [
|
61 |
-
{"title": "
|
62 |
-
{"title": "
|
63 |
],
|
64 |
"videos": [
|
65 |
-
{"title": "
|
66 |
-
{"title": "
|
67 |
],
|
68 |
},
|
69 |
}
|
70 |
|
71 |
-
# Function to
|
72 |
-
def
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import streamlit as st
|
3 |
from transformers import pipeline
|
4 |
|
5 |
+
# Load the emotion analyzer model
|
6 |
emotion_analyzer = pipeline("text-classification", model="distilbert-base-uncased")
|
7 |
|
8 |
+
# Define the questions for mood analysis
|
9 |
questions = [
|
10 |
+
"How are you feeling today in one word?",
|
11 |
+
"What's currently on your mind?",
|
12 |
+
"Do you feel calm or overwhelmed right now?",
|
13 |
]
|
14 |
|
15 |
+
# Define a suggestion database for different moods
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
suggestion_database = {
|
17 |
+
"NEGATIVE": {
|
18 |
+
"suggestions": ["Try guided meditation", "Take a walk in nature", "Connect with a loved one"],
|
19 |
"articles": [
|
20 |
{"title": "Emotional Wellness Toolkit", "url": "https://www.nih.gov/health-information/emotional-wellness-toolkit"},
|
21 |
+
{"title": "Understanding Anxiety", "url": "https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety"},
|
22 |
],
|
23 |
"videos": [
|
24 |
+
{"title": "Mindfulness for Calm", "url": "https://youtu.be/m1vaUGtyo-A"},
|
25 |
+
{"title": "Relaxation Techniques", "url": "https://www.youtube.com/shorts/Tq49ajl7c8Q?feature=share"},
|
26 |
],
|
27 |
},
|
28 |
+
"POSITIVE": {
|
29 |
+
"suggestions": ["Practice gratitude", "Engage in a hobby", "Celebrate your wins"],
|
30 |
"articles": [
|
31 |
+
{"title": "Benefits of Joy", "url": "https://www.health.harvard.edu/health-a-to-z"},
|
32 |
+
{"title": "Gratitude Practices", "url": "https://www.helpguide.org/mental-health/meditation/mindful-breathing-meditation"},
|
33 |
],
|
34 |
"videos": [
|
35 |
+
{"title": "Boosting Happiness", "url": "https://youtu.be/MIc299Flibs"},
|
36 |
+
{"title": "Celebrating Wins", "url": "https://www.youtube.com/shorts/fwH8Ygb0K60?feature=share"},
|
37 |
],
|
38 |
},
|
39 |
"NEUTRAL": {
|
40 |
+
"suggestions": ["Take a short break", "Engage in a relaxing activity", "Spend time outdoors"],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
"articles": [
|
42 |
+
{"title": "Self-Care Practices", "url": "https://www.nih.gov/health-information/emotional-wellness-toolkit"},
|
43 |
+
{"title": "Stress Management", "url": "https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety"},
|
44 |
],
|
45 |
"videos": [
|
46 |
+
{"title": "Relaxation Exercises", "url": "https://youtu.be/Y8HIFRPU6pM"},
|
47 |
+
{"title": "Mindfulness Tips", "url": "https://youtu.be/-e-4Kx5px_I"},
|
48 |
],
|
49 |
},
|
50 |
}
|
51 |
|
52 |
+
# Function to fetch relevant resources based on detected mood
|
53 |
+
def get_relevant_resources(mood):
|
54 |
+
resources = suggestion_database.get(mood, {})
|
55 |
+
return resources.get("suggestions", []), resources.get("articles", []), resources.get("videos", [])
|
56 |
+
|
57 |
+
# Function to suggest activities based on the mood
|
58 |
+
def suggest_activity(mood):
|
59 |
+
suggestions, articles, videos = get_relevant_resources(mood)
|
60 |
+
return {
|
61 |
+
"suggestions": suggestions,
|
62 |
+
"articles": articles,
|
63 |
+
"videos": videos,
|
64 |
+
}
|
65 |
+
|
66 |
+
# Streamlit app
|
67 |
+
def main():
|
68 |
+
st.title("Mood Analysis and Suggestions")
|
69 |
+
st.write("Answer the following 3 questions to help us understand your mood:")
|
70 |
+
|
71 |
+
# Collect responses
|
72 |
+
responses = []
|
73 |
+
for i, question in enumerate(questions):
|
74 |
+
response = st.text_input(f"{i+1}. {question}")
|
75 |
+
if response:
|
76 |
+
responses.append(response)
|
77 |
+
|
78 |
+
# Analyze responses if all questions are answered
|
79 |
+
if len(responses) == len(questions):
|
80 |
+
combined_text = " ".join(responses)
|
81 |
+
analysis_result = emotion_analyzer(combined_text)
|
82 |
+
detected_emotion = analysis_result[0]['label']
|
83 |
+
|
84 |
+
# Map detected emotion to a mood state
|
85 |
+
mood_mapping = {"LABEL_0": "NEGATIVE", "LABEL_1": "POSITIVE", "LABEL_2": "NEUTRAL"}
|
86 |
+
mood = mood_mapping.get(detected_emotion, "NEUTRAL")
|
87 |
+
|
88 |
+
st.write(f"Detected Mood: {mood}")
|
89 |
+
|
90 |
+
# Fetch suggestions based on mood
|
91 |
+
resources = suggest_activity(mood)
|
92 |
+
|
93 |
+
# Display suggestions
|
94 |
+
st.write("### Suggestions")
|
95 |
+
for suggestion in resources["suggestions"]:
|
96 |
+
st.write(f"- {suggestion}")
|
97 |
+
|
98 |
+
# Display articles
|
99 |
+
st.write("### Articles")
|
100 |
+
for article in resources["articles"]:
|
101 |
+
st.write(f"- [{article['title']}]({article['url']})")
|
102 |
+
|
103 |
+
# Display videos
|
104 |
+
st.write("### Videos")
|
105 |
+
for video in resources["videos"]:
|
106 |
+
st.write(f"- [{video['title']}]({video['url']})")
|
107 |
+
else:
|
108 |
+
st.write("Please answer all 3 questions to receive suggestions.")
|
109 |
+
|
110 |
+
if __name__ == "__main__":
|
111 |
+
main()
|