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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -16
app.py CHANGED
@@ -1,44 +1,100 @@
 
1
  import streamlit as st
2
  from transformers import pipeline
3
 
4
  # Emotion classifier (use a pre-trained model from Hugging Face)
5
  emotion_analyzer = pipeline("text-classification", model="distilbert-base-uncased")
6
 
7
- # Enhanced Suggestion Database with resources
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  suggestion_database = {
9
  "NEGATIVE": {
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
  "POSITIVE": {
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
@@ -58,18 +114,22 @@ def suggest_activity(emotion_analysis):
58
 
59
  # Streamlit app
60
  def main():
61
- st.title("Emotion Detection and Suggestions")
62
 
 
 
 
 
 
63
  st.write("Please answer the following questions:")
 
 
 
 
64
 
65
- # Step 1: Collect answers to three questions
66
- question_1 = st.text_input("How are you feeling today? (e.g., happy, sad, stressed)")
67
- question_2 = st.text_input("What's something that is currently on your mind?")
68
- question_3 = st.text_input("Do you feel overwhelmed or calm right now?")
69
-
70
- # Step 2: Analyze sentiment of responses (only proceed if all questions are answered)
71
- if question_1 and question_2 and question_3:
72
- text_to_analyze = f"{question_1} {question_2} {question_3}"
73
  analysis_result = emotion_analyzer(text_to_analyze)
74
  emotion = analysis_result[0]['label'] # Get the emotion from the analysis result
75
 
@@ -83,7 +143,7 @@ def main():
83
 
84
  st.write(f"Emotion detected: {emotion}")
85
 
86
- # Step 3: Suggest activities, articles, and videos
87
  resources = suggest_activity({emotion: 1})
88
 
89
  # Display suggestions, articles, and videos
@@ -99,7 +159,7 @@ def main():
99
  for video in resources["videos"]:
100
  st.write(f"- [{video['title']}]({video['url']})")
101
  else:
102
- st.write("Please answer all three questions to receive suggestions.")
103
 
104
  if __name__ == "__main__":
105
  main()
 
1
+ import random
2
  import streamlit as st
3
  from transformers import pipeline
4
 
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
 
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
 
 
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
 
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()