Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -13,7 +13,7 @@ st.set_page_config(
|
|
13 |
|
14 |
# ---- App Title ----
|
15 |
st.title("π Emotion Prediction App π")
|
16 |
-
st.subheader("
|
17 |
|
18 |
# ---- Function to Load Emotion Analysis Model ----
|
19 |
@st.cache_resource
|
@@ -49,35 +49,64 @@ def predict_emotion(text):
|
|
49 |
st.error(f"β οΈ Prediction failed: {e}")
|
50 |
return {"Error": f"Prediction failed: {e}"}
|
51 |
|
52 |
-
# ----
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
def suggest_activity(emotion_analysis):
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
return "Feeling anxious? It might help to do a quick breathing exercise or take a walk to ease your mind."
|
62 |
-
elif max_emotion == 'anger':
|
63 |
-
return "It seems like you're angry. Try taking a few deep breaths, or engage in a relaxing mindfulness exercise to calm your nerves."
|
64 |
-
elif max_emotion == 'surprise':
|
65 |
-
return "Youβre surprised! Take a moment to breathe deeply and reflect. A walk might help clear your thoughts."
|
66 |
-
elif max_emotion == 'disgust':
|
67 |
-
return "If youβre feeling disgusted, a change of environment might help. Go for a walk or try a mindfulness technique to relax."
|
68 |
-
elif max_emotion == 'sadness':
|
69 |
-
return "Itβs okay to feel sad. Try grounding yourself with some mindfulness or a breathing exercise."
|
70 |
-
else:
|
71 |
-
return "Keep doing great! If you feel overwhelmed, consider taking a deep breath or going for a short walk."
|
72 |
|
73 |
# ---- User Input Section ----
|
74 |
-
st.write("### πΊ
|
75 |
questions = [
|
76 |
-
"How are you feeling
|
77 |
-
"
|
78 |
-
"What
|
79 |
-
"How do you handle stress or challenges?",
|
80 |
-
"What motivates you the most right now?",
|
81 |
]
|
82 |
|
83 |
responses = {}
|
@@ -99,24 +128,31 @@ for i, question in enumerate(questions, start=1):
|
|
99 |
if analysis:
|
100 |
max_emotion = max(analysis, key=analysis.get)
|
101 |
activity_suggestion = suggest_activity(analysis)
|
102 |
-
st.write(f"### π§ Suggested Activity: {activity_suggestion}")
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
109 |
summary_data = []
|
110 |
for i, (question, details) in enumerate(responses.items(), start=1):
|
111 |
-
|
112 |
activity_suggestion = suggest_activity(details["Analysis"])
|
113 |
|
114 |
-
# Add data to the summary list
|
115 |
summary_data.append({
|
116 |
"Question": question,
|
117 |
"Your Response": details["Response"],
|
118 |
-
"Emotion Analysis":
|
119 |
-
"Suggested Activity": activity_suggestion
|
|
|
|
|
120 |
})
|
121 |
|
122 |
# Create a DataFrame from the summary data
|
@@ -127,7 +163,7 @@ if st.button("Submit Responses"):
|
|
127 |
|
128 |
# Provide additional suggestions at the end of all questions
|
129 |
st.write("### π Final Suggestions")
|
130 |
-
st.write("
|
131 |
|
132 |
# ---- Footer ----
|
133 |
st.markdown(
|
@@ -137,11 +173,3 @@ st.markdown(
|
|
137 |
Designed for a fun and intuitive experience! π
|
138 |
"""
|
139 |
)
|
140 |
-
|
141 |
-
# ---- Error Handling and User Suggestions ----
|
142 |
-
if emotion_analyzer is None:
|
143 |
-
st.error("β οΈ We couldn't load the emotion analysis model. Please check your internet connection or try again later.")
|
144 |
-
st.markdown("π§ **Troubleshooting Steps:**")
|
145 |
-
st.markdown("1. Ensure you have a stable internet connection.")
|
146 |
-
st.markdown("2. If the issue persists, please refresh the page and try again.")
|
147 |
-
st.markdown("3. Check if the model has been updated or is temporarily unavailable.")
|
|
|
13 |
|
14 |
# ---- App Title ----
|
15 |
st.title("π Emotion Prediction App π")
|
16 |
+
st.subheader("Get emotional insights with tailored resources!")
|
17 |
|
18 |
# ---- Function to Load Emotion Analysis Model ----
|
19 |
@st.cache_resource
|
|
|
49 |
st.error(f"β οΈ Prediction failed: {e}")
|
50 |
return {"Error": f"Prediction failed: {e}"}
|
51 |
|
52 |
+
# ---- Enhanced Suggestion Database ----
|
53 |
+
suggestion_database = {
|
54 |
+
"sadness": {
|
55 |
+
"suggestions": ["Try a guided meditation", "Take a walk in nature", "Connect with a friend"],
|
56 |
+
"articles": [
|
57 |
+
{"title": "Overcoming Sadness", "url": "https://example.com/sadness1"},
|
58 |
+
{"title": "Understanding Depression", "url": "https://example.com/sadness2"},
|
59 |
+
],
|
60 |
+
"videos": [
|
61 |
+
{"title": "Mindfulness for Sadness", "url": "https://www.youtube.com/watch?v=sadnessvideo1"},
|
62 |
+
{"title": "Coping with Grief", "url": "https://www.youtube.com/watch?v=sadnessvideo2"},
|
63 |
+
],
|
64 |
+
},
|
65 |
+
"joy": {
|
66 |
+
"suggestions": ["Practice gratitude", "Engage in a hobby", "Spend time with loved ones"],
|
67 |
+
"articles": [
|
68 |
+
{"title": "The Benefits of Joy", "url": "https://example.com/joy1"},
|
69 |
+
{"title": "Maintaining Positive Emotions", "url": "https://example.com/joy2"},
|
70 |
+
],
|
71 |
+
"videos": [
|
72 |
+
{"title": "Boosting Your Happiness", "url": "https://www.youtube.com/watch?v=joyvideo1"},
|
73 |
+
{"title": "Practicing Gratitude", "url": "https://www.youtube.com/watch?v=joyvideo2"},
|
74 |
+
],
|
75 |
+
},
|
76 |
+
"neutral": {
|
77 |
+
"suggestions": ["Take a break", "Engage in a relaxing activity", "Spend time in nature"],
|
78 |
+
"articles": [
|
79 |
+
{"title": "Importance of Self-Care", "url": "https://example.com/selfcare1"},
|
80 |
+
{"title": "Stress Management Techniques", "url": "https://example.com/stress1"}
|
81 |
+
],
|
82 |
+
"videos": [
|
83 |
+
{"title": "Relaxation Techniques", "url": "https://www.youtube.com/watch?v=relaxvideo1"},
|
84 |
+
{"title": "Mindfulness Exercises", "url": "https://www.youtube.com/watch?v=mindfulnessvideo1"}
|
85 |
+
]
|
86 |
+
}
|
87 |
+
}
|
88 |
+
|
89 |
+
# Function to fetch relevant resources
|
90 |
+
def get_relevant_resources(emotion):
|
91 |
+
resources = suggestion_database.get(emotion, {})
|
92 |
+
return resources.get("suggestions", []), resources.get("articles", []), resources.get("videos", [])
|
93 |
+
|
94 |
+
# Enhanced Suggestion Function
|
95 |
def suggest_activity(emotion_analysis):
|
96 |
+
max_emotion = max(emotion_analysis, key=emotion_analysis.get) if emotion_analysis else "neutral"
|
97 |
+
suggestions, articles, videos = get_relevant_resources(max_emotion)
|
98 |
+
return {
|
99 |
+
"suggestions": suggestions,
|
100 |
+
"articles": articles,
|
101 |
+
"videos": videos,
|
102 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
103 |
|
104 |
# ---- User Input Section ----
|
105 |
+
st.write("### πΊ Answer 3 Simple Questions!")
|
106 |
questions = [
|
107 |
+
"How are you feeling right now?",
|
108 |
+
"What was the most significant emotion you experienced today?",
|
109 |
+
"What helps you feel better when youβre stressed?"
|
|
|
|
|
110 |
]
|
111 |
|
112 |
responses = {}
|
|
|
128 |
if analysis:
|
129 |
max_emotion = max(analysis, key=analysis.get)
|
130 |
activity_suggestion = suggest_activity(analysis)
|
131 |
+
st.write(f"### π§ Suggested Activity: {activity_suggestion['suggestions'][0]}")
|
132 |
+
st.write(f"π Suggested Articles:")
|
133 |
+
for article in activity_suggestion['articles']:
|
134 |
+
st.markdown(f"- [{article['title']}]({article['url']})")
|
135 |
+
st.write(f"π₯ Suggested Videos:")
|
136 |
+
for video in activity_suggestion['videos']:
|
137 |
+
st.markdown(f"- [{video['title']}]({video['url']})")
|
138 |
+
|
139 |
+
# ---- Display Summary Button ----
|
140 |
+
if st.button("Get Summary"):
|
141 |
+
st.write("### π Your Emotional Insights Summary")
|
142 |
+
|
143 |
+
# Prepare the summary data
|
144 |
summary_data = []
|
145 |
for i, (question, details) in enumerate(responses.items(), start=1):
|
146 |
+
max_emotion = max(details["Analysis"], key=details["Analysis"].get)
|
147 |
activity_suggestion = suggest_activity(details["Analysis"])
|
148 |
|
|
|
149 |
summary_data.append({
|
150 |
"Question": question,
|
151 |
"Your Response": details["Response"],
|
152 |
+
"Emotion Analysis": max_emotion,
|
153 |
+
"Suggested Activity": activity_suggestion["suggestions"][0],
|
154 |
+
"Suggested Articles": ", ".join([article['title'] for article in activity_suggestion['articles']]),
|
155 |
+
"Suggested Videos": ", ".join([video['title'] for video in activity_suggestion['videos']]),
|
156 |
})
|
157 |
|
158 |
# Create a DataFrame from the summary data
|
|
|
163 |
|
164 |
# Provide additional suggestions at the end of all questions
|
165 |
st.write("### π Final Suggestions")
|
166 |
+
st.write("Incorporating mindfulness, deep breathing, and spending time outdoors can help with emotional balance. Keep exploring activities that promote well-being! π±")
|
167 |
|
168 |
# ---- Footer ----
|
169 |
st.markdown(
|
|
|
173 |
Designed for a fun and intuitive experience! π
|
174 |
"""
|
175 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|