tarrasyed19472007 commited on
Commit
92e98fb
·
verified ·
1 Parent(s): 4c0c8ad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -123
app.py CHANGED
@@ -5,161 +5,98 @@ from transformers import pipeline
5
  # Emotion classifier (use a pre-trained model from Hugging Face)
6
  emotion_analyzer = pipeline("text-classification", model="distilbert-base-uncased")
7
 
8
- # Expanded Question Database
9
  questions = [
10
- "How are you feeling today?",
11
- "What's something that recently brought you joy?",
12
- "Are you experiencing any stress or pressure currently?",
13
- "What’s one thing you’re looking forward to?",
14
- "Do you feel motivated or tired today?",
15
- "Have you spent quality time with someone recently?",
16
- "What was your most challenging moment today?",
17
- "How would you describe your mood right now?",
18
- "Are you excited about any upcoming events?",
19
- "Do you feel calm or restless at the moment?",
20
- "What is currently taking up most of your mental space?",
21
- "How do you feel about your current workload?",
22
- "Have you smiled or laughed today? If yes, why?",
23
- "Do you feel supported by others around you?",
24
- "Are there any worries that keep recurring for you?",
25
- "What’s the best thing that happened to you recently?",
26
- "Do you feel emotionally drained or refreshed?",
27
- "What thoughts are you waking up with lately?",
28
- "Do you feel connected with nature or your surroundings?",
29
- "Have you had a chance to relax today?",
30
- "What’s your level of excitement about your current projects?",
31
- "Do you feel appreciated by people around you?",
32
- "What’s the last memory that made you feel deeply at peace?",
33
- "Are there any emotions you’re struggling to understand?",
34
- "What is something that makes you proud about yourself?",
35
- "Do you feel overwhelmed by responsibilities today?",
36
- "What’s one thing you wish you could change right now?",
37
- "Are you spending time on activities that energize you?",
38
- "Do you feel hopeful about your personal goals?",
39
- "What’s something that inspires you daily?",
40
  ]
41
 
42
- # Expanded Suggestion Database
 
 
 
 
 
 
 
 
43
  suggestion_database = {
44
- "NEGATIVE": {
45
- "suggestions": [
46
- "Try a guided meditation", "Take a walk in nature", "Connect with a trusted friend",
47
- "Write down your feelings in a journal", "Do some light yoga", "Listen to soothing music",
48
- "Practice deep breathing exercises", "Declutter a small space", "Watch an uplifting movie",
49
- "Try a creative activity like drawing or writing"
50
- ],
51
- "articles": [
52
- {"title": "Overcoming Sadness", "url": "https://example.com/sadness1"},
53
- {"title": "Understanding Depression", "url": "https://example.com/sadness2"},
54
- {"title": "Ways to Break Negative Thinking", "url": "https://example.com/negativity"},
55
- ],
56
- "videos": [
57
- {"title": "Mindfulness for Sadness", "url": "https://www.youtube.com/watch?v=sadnessvideo1"},
58
- {"title": "Coping with Grief", "url": "https://www.youtube.com/watch?v=sadnessvideo2"},
59
- {"title": "Stress Relief Techniques", "url": "https://www.youtube.com/watch?v=stressrelief"},
60
- ],
61
- },
62
  "POSITIVE": {
63
- "suggestions": [
64
- "Celebrate your achievements", "Practice gratitude journaling", "Spend time with loved ones",
65
- "Plan an activity that excites you", "Try a new hobby", "Volunteer for a cause you care about",
66
- "Reflect on positive memories", "Cook your favorite meal", "Dance to your favorite songs",
67
- "Go outside and enjoy the weather"
68
- ],
69
- "articles": [
70
- {"title": "The Benefits of Joy", "url": "https://example.com/joy1"},
71
- {"title": "Maintaining Positive Emotions", "url": "https://example.com/joy2"},
72
- {"title": "Creating a Happiness Routine", "url": "https://example.com/happiness"},
73
- ],
74
- "videos": [
75
- {"title": "Boosting Your Happiness", "url": "https://www.youtube.com/watch?v=joyvideo1"},
76
- {"title": "Practicing Gratitude", "url": "https://www.youtube.com/watch?v=joyvideo2"},
77
- {"title": "Inspiring Talks on Positivity", "url": "https://www.youtube.com/watch?v=positivitytalk"},
78
- ],
79
  },
80
  "NEUTRAL": {
81
- "suggestions": [
82
- "Take a short break", "Go for a quiet walk", "Read a book you enjoy",
83
- "Practice mindfulness exercises", "Write down your thoughts", "Do light stretching",
84
- "Disconnect from screens for a while", "Spend time in nature", "Organize your workspace",
85
- "Plan your week to reduce uncertainty"
86
- ],
87
- "articles": [
88
- {"title": "Importance of Self-Care", "url": "https://example.com/selfcare1"},
89
- {"title": "Stress Management Techniques", "url": "https://example.com/stress1"},
90
- {"title": "Finding Balance in Daily Life", "url": "https://example.com/balance"},
91
- ],
92
- "videos": [
93
- {"title": "Relaxation Techniques", "url": "https://www.youtube.com/watch?v=relaxvideo1"},
94
- {"title": "Mindfulness Exercises", "url": "https://www.youtube.com/watch?v=mindfulnessvideo1"},
95
- {"title": "Creating a Peaceful Routine", "url": "https://www.youtube.com/watch?v=peacefulroutine"},
96
- ],
97
  },
98
  }
99
 
100
- # Function to fetch relevant resources based on emotion
101
- def get_relevant_resources(emotion):
102
- resources = suggestion_database.get(emotion, {})
103
- return resources.get("suggestions", []), resources.get("articles", []), resources.get("videos", [])
 
 
 
 
104
 
105
- # Function to suggest activities based on the emotion analysis result
106
- def suggest_activity(emotion_analysis):
107
- max_emotion = max(emotion_analysis, key=emotion_analysis.get) if emotion_analysis else "NEUTRAL"
108
- suggestions, articles, videos = get_relevant_resources(max_emotion)
109
- return {
110
- "suggestions": suggestions,
111
- "articles": articles,
112
- "videos": videos,
113
- }
114
 
115
  # Streamlit app
116
  def main():
117
- st.title("Enhanced Emotion Detection and Suggestions")
118
-
119
- # Step 1: Randomly pick 20 questions from the database
120
- selected_questions = random.sample(questions, 20)
121
 
122
- # Step 2: Collect answers from the user
123
- user_responses = []
124
- st.write("Please answer the following questions:")
125
- for i, question in enumerate(selected_questions, start=1):
126
  response = st.text_input(f"{i}. {question}")
127
  if response:
128
- user_responses.append(response)
129
 
130
- # Step 3: Analyze sentiment of the responses if all questions are answered
131
- if len(user_responses) == len(selected_questions):
132
- text_to_analyze = " ".join(user_responses)
133
- analysis_result = emotion_analyzer(text_to_analyze)
134
- emotion = analysis_result[0]['label'] # Get the emotion from the analysis result
135
 
136
- # Map emotion label from the model to our suggestion database
137
- if emotion == "LABEL_0":
138
- emotion = "NEGATIVE"
139
- elif emotion == "LABEL_1":
140
- emotion = "POSITIVE"
141
- else:
142
- emotion = "NEUTRAL"
143
 
144
- st.write(f"Emotion detected: {emotion}")
 
 
145
 
146
- # Step 4: Suggest activities, articles, and videos
147
- resources = suggest_activity({emotion: 1})
148
 
149
  # Display suggestions, articles, and videos
150
  st.write("Suggestions:")
151
- for suggestion in resources["suggestions"]:
152
  st.write(f"- {suggestion}")
153
 
154
  st.write("Articles:")
155
- for article in resources["articles"]:
156
  st.write(f"- [{article['title']}]({article['url']})")
157
 
158
  st.write("Videos:")
159
- for video in resources["videos"]:
160
  st.write(f"- [{video['title']}]({video['url']})")
161
  else:
162
- st.write("Please answer all the questions to receive suggestions.")
163
 
164
  if __name__ == "__main__":
165
  main()
 
5
  # Emotion classifier (use a pre-trained model from Hugging Face)
6
  emotion_analyzer = pipeline("text-classification", model="distilbert-base-uncased")
7
 
8
+ # Question Database
9
  questions = [
10
+ "How are you feeling right now?",
11
+ "Whats something that’s been on your mind lately?",
12
+ "Do you feel energized or tired at this moment?",
13
+ "What’s the most significant event in your day so far?",
14
+ "If you had to describe your mood in one word, what would it be?",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  ]
16
 
17
+ # Expanded Mood States
18
+ moods = [
19
+ "Happy", "Excited", "Relaxed", "Grateful", "Calm",
20
+ "Dull", "Neutral", "Tired", "Bored", "Lonely",
21
+ "Angry", "Frustrated", "Anxious", "Stressed", "Overwhelmed",
22
+ "Hopeful", "Confused", "Motivated", "Curious", "Peaceful"
23
+ ]
24
+
25
+ # Suggestion Database
26
  suggestion_database = {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  "POSITIVE": {
28
+ "suggestions": ["Celebrate your success!", "Share your happiness with someone.", "Reflect on what makes you feel this way."],
29
+ "articles": [{"title": "Staying Positive", "url": "https://example.com/positivity"}],
30
+ "videos": [{"title": "Boosting Happiness", "url": "https://www.youtube.com/watch?v=happinessboost"}],
31
+ },
32
+ "NEGATIVE": {
33
+ "suggestions": ["Take a break to relax.", "Talk to someone you trust.", "Try mindfulness exercises."],
34
+ "articles": [{"title": "Managing Stress", "url": "https://example.com/stressmanagement"}],
35
+ "videos": [{"title": "Dealing with Stress", "url": "https://www.youtube.com/watch?v=stressrelief"}],
 
 
 
 
 
 
 
 
36
  },
37
  "NEUTRAL": {
38
+ "suggestions": ["Take a short walk.", "Plan your next task mindfully.", "Enjoy a calming activity like reading."],
39
+ "articles": [{"title": "Finding Balance", "url": "https://example.com/balance"}],
40
+ "videos": [{"title": "Relaxation Techniques", "url": "https://www.youtube.com/watch?v=relaxvideo"}],
 
 
 
 
 
 
 
 
 
 
 
 
 
41
  },
42
  }
43
 
44
+ # Function to map moods to emotion categories
45
+ def map_mood_to_category(mood):
46
+ if mood in ["Happy", "Excited", "Relaxed", "Grateful", "Calm", "Hopeful", "Motivated", "Curious", "Peaceful"]:
47
+ return "POSITIVE"
48
+ elif mood in ["Dull", "Neutral", "Tired", "Bored", "Lonely"]:
49
+ return "NEUTRAL"
50
+ else: # Negative emotions
51
+ return "NEGATIVE"
52
 
53
+ # Function to suggest activities based on the mood
54
+ def suggest_activity(mood):
55
+ category = map_mood_to_category(mood)
56
+ resources = suggestion_database.get(category, {})
57
+ return resources
 
 
 
 
58
 
59
  # Streamlit app
60
  def main():
61
+ st.title("Mood Analysis and Suggestions")
 
 
 
62
 
63
+ # Step 1: Display the questions
64
+ st.write("Answer the following 5 questions to help us understand your mood:")
65
+ responses = []
66
+ for i, question in enumerate(questions, start=1):
67
  response = st.text_input(f"{i}. {question}")
68
  if response:
69
+ responses.append(response)
70
 
71
+ # Step 2: Analyze responses if all questions are answered
72
+ if len(responses) == len(questions):
73
+ combined_text = " ".join(responses)
 
 
74
 
75
+ # Analyze responses to determine mood
76
+ analysis_result = emotion_analyzer(combined_text)
77
+ detected_emotion = analysis_result[0]['label']
 
 
 
 
78
 
79
+ # Map detected emotion to a mood state
80
+ detected_mood = random.choice(moods) # Mock mapping for demonstration
81
+ st.write(f"Detected Mood: {detected_mood}")
82
 
83
+ # Step 3: Fetch suggestions based on mood
84
+ resources = suggest_activity(detected_mood)
85
 
86
  # Display suggestions, articles, and videos
87
  st.write("Suggestions:")
88
+ for suggestion in resources.get("suggestions", []):
89
  st.write(f"- {suggestion}")
90
 
91
  st.write("Articles:")
92
+ for article in resources.get("articles", []):
93
  st.write(f"- [{article['title']}]({article['url']})")
94
 
95
  st.write("Videos:")
96
+ for video in resources.get("videos", []):
97
  st.write(f"- [{video['title']}]({video['url']})")
98
  else:
99
+ st.write("Please answer all questions to receive suggestions.")
100
 
101
  if __name__ == "__main__":
102
  main()