tarrasyed19472007 commited on
Commit
5e733eb
·
verified ·
1 Parent(s): 5023ab3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +101 -128
app.py CHANGED
@@ -1,144 +1,117 @@
1
  import streamlit as st
 
2
  from transformers import pipeline
3
 
4
- # Load emotion classification model
 
 
 
5
  @st.cache_resource
6
  def load_model():
7
- try:
8
- emotion_classifier = pipeline("text-classification", model="j-hartmann/emotion-english-distilroberta-base")
9
- return emotion_classifier
10
- except Exception as e:
11
- st.error(f"Error loading model: {str(e)}")
12
- return None
13
-
14
- emotion_classifier = load_model()
15
 
16
- # Well-being suggestions based on emotions
17
- def get_well_being_suggestions(emotion):
18
- suggestions = {
19
- "joy": {
20
- "text": "You're feeling joyful! Keep the positivity going.",
21
- "links": [
22
- "https://www.nih.gov/health-information/emotional-wellness-toolkit",
23
- "https://www.health.harvard.edu/health-a-to-z",
24
- "https://www.helpguide.org/mental-health/meditation/mindful-breathing-meditation"
25
- ],
26
- "videos": [
27
- "https://youtu.be/m1vaUGtyo-A",
28
- "https://youtu.be/MIc299Flibs"
29
- ]
30
- },
31
- "anger": {
32
- "text": "You're feeling angry. Take a moment to calm down.",
33
- "links": [
34
- "https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety",
35
- "https://www.helpguide.org/mental-health/meditation/mindful-breathing-meditation"
36
- ],
37
- "videos": [
38
- "https://youtu.be/m1vaUGtyo-A",
39
- "https://www.youtube.com/shorts/fwH8Ygb0K60?feature=share"
40
- ]
41
- },
42
- "sadness": {
43
- "text": "You're feeling sad. It's okay to take a break.",
44
- "links": [
45
- "https://www.nih.gov/health-information/emotional-wellness-toolkit",
46
- "https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety"
47
- ],
48
- "videos": [
49
- "https://youtu.be/-e-4Kx5px_I",
50
- "https://youtu.be/Y8HIFRPU6pM"
51
- ]
52
- },
53
- "fear": {
54
- "text": "You're feeling fearful. Try some relaxation techniques.",
55
- "links": [
56
- "https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety",
57
- "https://www.health.harvard.edu/health-a-to-z"
58
- ],
59
- "videos": [
60
- "https://www.youtube.com/shorts/Tq49ajl7c8Q?feature=share",
61
- "https://youtu.be/yGKKz185M5o"
62
- ]
63
- },
64
- "disgust": {
65
- "text": "You're feeling disgusted. Take a deep breath and refocus.",
66
- "links": [
67
- "https://www.health.harvard.edu/health-a-to-z",
68
- "https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety"
69
- ],
70
- "videos": [
71
- "https://youtu.be/MIc299Flibs",
72
- "https://youtu.be/-e-4Kx5px_I"
73
- ]
74
- },
75
- }
76
- return suggestions.get(emotion, {
77
- "text": "Feeling neutral? That's okay! Take care of your mental health.",
78
- "links": [],
79
- "videos": []
80
- })
81
 
82
- # Streamlit UI
83
- def main():
84
- # Set the background image
85
- st.markdown("""
86
  <style>
87
- .stApp {
88
- background-image: url('https://www.example.com/your-image.jpg');
89
  background-size: cover;
90
- background-position: center;
 
 
 
 
 
 
 
91
  }
92
  </style>
93
- """, unsafe_allow_html=True)
94
-
95
- # Title of the app
96
- st.title("Emotion Prediction and Well-being Suggestions")
97
-
98
- # User input for emotional state
99
- st.header("Tell us how you're feeling today!")
100
-
101
- user_input = st.text_area("Enter a short sentence about your current mood:", "")
 
 
 
 
 
 
 
 
 
 
 
102
 
103
- if user_input:
104
- # Clean the input text (stripping unnecessary spaces, lowercasing)
105
- clean_input = user_input.strip().lower()
 
 
 
 
106
 
107
- # Use the model to predict emotion
108
- try:
109
- result = emotion_classifier(clean_input)
110
- st.write(f"Raw Model Result: {result}") # Debug output to see raw result
111
-
112
- emotion = result[0]['label'].lower()
113
-
114
- st.subheader(f"Emotion Detected: {emotion.capitalize()}")
115
-
116
- # Get well-being suggestions based on emotion
117
- suggestions = get_well_being_suggestions(emotion)
118
-
119
- # Display text suggestions
120
- st.write(suggestions["text"])
121
 
122
- # Display links
123
- if suggestions["links"]:
124
- st.write("Useful Resources:")
125
- for link in suggestions["links"]:
126
- st.markdown(f"[{link}]({link})")
 
 
 
 
127
 
128
- # Display video links
129
- if suggestions["videos"]:
130
- st.write("Relaxation Videos:")
131
- for video in suggestions["videos"]:
132
- st.markdown(f"[Watch here]({video})")
133
-
134
- # Add a button for a summary
135
- if st.button('Summary'):
136
- st.write(f"Emotion detected: {emotion.capitalize()}. Here are your well-being suggestions to enhance your mood.")
137
- st.write("Explore the links and videos to improve your emotional health!")
138
-
139
- except Exception as e:
140
- st.error(f"Error predicting emotion: {str(e)}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
141
 
142
- # Run the Streamlit app
143
- if __name__ == "__main__":
144
- main()
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
3
  from transformers import pipeline
4
 
5
+ # Set page config as the very first Streamlit command
6
+ st.set_page_config(page_title="Emotion Detection and Well-Being Suggestions", layout="wide")
7
+
8
+ # Load pre-trained model and tokenizer
9
  @st.cache_resource
10
  def load_model():
11
+ tokenizer = AutoTokenizer.from_pretrained("j-hartmann/emotion-english-distilroberta-base")
12
+ model = AutoModelForSequenceClassification.from_pretrained("j-hartmann/emotion-english-distilroberta-base")
13
+ return tokenizer, model
 
 
 
 
 
14
 
15
+ tokenizer, model = load_model()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
+ # Add a background image (use your own URL or file path here)
18
+ st.markdown(
19
+ """
 
20
  <style>
21
+ body {
22
+ background-image: url('https://drive.google.com/uc?export=view&id=1TydbjDRL5fXLMxQgaPkIg9oGOJlU2IWt');
23
  background-size: cover;
24
+ background-repeat: no-repeat;
25
+ background-attachment: fixed;
26
+ color: white;
27
+ }
28
+ .stButton button {
29
+ background-color: #6c63ff;
30
+ color: white;
31
+ font-size: 20px;
32
  }
33
  </style>
34
+ """,
35
+ unsafe_allow_html=True,
36
+ )
37
+
38
+ # Display header
39
+ st.title("Emotion Detection and Well-Being Suggestions")
40
+
41
+ # User input for text (emotion detection)
42
+ user_input = st.text_area("How are you feeling today?", "Enter your thoughts here...")
43
+
44
+ # Model prediction
45
+ if user_input:
46
+ pipe = pipeline("text-classification", model=model, tokenizer=tokenizer)
47
+ result = pipe(user_input)
48
+
49
+ # Extracting the emotion from the model's result
50
+ emotion = result[0]['label']
51
+
52
+ # Display emotion
53
+ st.write(f"**Emotion Detected:** {emotion}")
54
 
55
+ # Provide suggestions based on the detected emotion
56
+ if emotion == 'joy':
57
+ st.write("You're feeling happy! Keep up the great mood!")
58
+ st.write("Useful Resources:")
59
+ st.markdown("[Relaxation Techniques](https://www.helpguide.org/mental-health/meditation/mindful-breathing-meditation)")
60
+ st.write("[Dealing with Stress](https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety)")
61
+ st.write("[Emotional Wellness Toolkit](https://www.nih.gov/health-information/emotional-wellness-toolkit)")
62
 
63
+ st.write("Relaxation Videos:")
64
+ st.markdown("[Watch on YouTube](https://youtu.be/m1vaUGtyo-A)")
 
 
 
 
 
 
 
 
 
 
 
 
65
 
66
+ elif emotion == 'anger':
67
+ st.write("You're feeling angry. It's okay to feel this way. Let's try to calm down.")
68
+ st.write("Useful Resources:")
69
+ st.markdown("[Emotional Wellness Toolkit](https://www.nih.gov/health-information/emotional-wellness-toolkit)")
70
+ st.write("[Stress Management Tips](https://www.health.harvard.edu/health-a-to-z)")
71
+ st.write("[Dealing with Anger](https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety)")
72
+
73
+ st.write("Relaxation Videos:")
74
+ st.markdown("[Watch on YouTube](https://youtu.be/MIc299Flibs)")
75
 
76
+ elif emotion == 'fear':
77
+ st.write("You're feeling fearful. Take a moment to breathe and relax.")
78
+ st.write("Useful Resources:")
79
+ st.markdown("[Mindfulness Practices](https://www.helpguide.org/mental-health/meditation/mindful-breathing-meditation)")
80
+ st.write("[Coping with Anxiety](https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety)")
81
+ st.write("[Emotional Wellness Toolkit](https://www.nih.gov/health-information/emotional-wellness-toolkit)")
82
+
83
+ st.write("Relaxation Videos:")
84
+ st.markdown("[Watch on YouTube](https://youtu.be/yGKKz185M5o)")
85
+
86
+ elif emotion == 'sadness':
87
+ st.write("You're feeling sad. It's okay to take a break.")
88
+ st.write("Useful Resources:")
89
+ st.markdown("[Emotional Wellness Toolkit](https://www.nih.gov/health-information/emotional-wellness-toolkit)")
90
+ st.write("[Dealing with Anxiety](https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety)")
91
+
92
+ st.write("Relaxation Videos:")
93
+ st.markdown("[Watch on YouTube](https://youtu.be/-e-4Kx5px_I)")
94
+
95
+ elif emotion == 'surprise':
96
+ st.write("You're feeling surprised. It's okay to feel neutral!")
97
+ st.write("Useful Resources:")
98
+ st.markdown("[Managing Stress](https://www.health.harvard.edu/health-a-to-z)")
99
+ st.write("[Coping Strategies](https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety)")
100
+
101
+ st.write("Relaxation Videos:")
102
+ st.markdown("[Watch on YouTube](https://youtu.be/m1vaUGtyo-A)")
103
 
104
+ # Add a button for summary
105
+ if st.button("Show Summary"):
106
+ st.write(f"Emotion Detected: {emotion}")
107
+ st.write("Useful Resources based on your mood:")
108
+ if emotion == 'joy':
109
+ st.write("[Relaxation Techniques](https://www.helpguide.org/mental-health/meditation/mindful-breathing-meditation)")
110
+ elif emotion == 'anger':
111
+ st.write("[Stress Management Tips](https://www.health.harvard.edu/health-a-to-z)")
112
+ elif emotion == 'fear':
113
+ st.write("[Mindfulness Practices](https://www.helpguide.org/mental-health/meditation/mindful-breathing-meditation)")
114
+ elif emotion == 'sadness':
115
+ st.write("[Dealing with Anxiety](https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety)")
116
+ elif emotion == 'surprise':
117
+ st.write("[Managing Stress](https://www.health.harvard.edu/health-a-to-z)")