tarrasyed19472007 commited on
Commit
8b475fa
·
verified ·
1 Parent(s): 0edf523

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +125 -102
app.py CHANGED
@@ -1,117 +1,140 @@
1
  import streamlit as st
2
- from transformers import AutoTokenizer, AutoModelForSequenceClassification
3
- from transformers import pipeline
4
 
5
- # Set page configuration - MUST be the first 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)")
 
1
  import streamlit as st
2
+ from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
 
3
 
4
+ # Load the emotion prediction model
 
 
 
5
  @st.cache_resource
6
  def load_model():
7
+ try:
8
+ # Use Hugging Face's pipeline for text classification
9
+ emotion_classifier = pipeline("text-classification", model="j-hartmann/emotion-english-distilroberta-base")
10
+ return emotion_classifier
11
+ except Exception as e:
12
+ st.error(f"Error loading model: {str(e)}")
13
+ return None
14
+
15
+ emotion_classifier = load_model()
16
 
17
+ # Well-being suggestions based on emotions
18
+ def get_well_being_suggestions(emotion):
19
+ suggestions = {
20
+ "joy": {
21
+ "text": "You're feeling joyful! Keep the positivity going.",
22
+ "links": [
23
+ "https://www.nih.gov/health-information/emotional-wellness-toolkit",
24
+ "https://www.health.harvard.edu/health-a-to-z",
25
+ "https://www.helpguide.org/mental-health/meditation/mindful-breathing-meditation"
26
+ ],
27
+ "videos": [
28
+ "https://youtu.be/m1vaUGtyo-A",
29
+ "https://youtu.be/MIc299Flibs"
30
+ ]
31
+ },
32
+ "anger": {
33
+ "text": "You're feeling angry. Take a moment to calm down.",
34
+ "links": [
35
+ "https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety",
36
+ "https://www.helpguide.org/mental-health/meditation/mindful-breathing-meditation"
37
+ ],
38
+ "videos": [
39
+ "https://youtu.be/m1vaUGtyo-A",
40
+ "https://www.youtube.com/shorts/fwH8Ygb0K60?feature=share"
41
+ ]
42
+ },
43
+ "sadness": {
44
+ "text": "You're feeling sad. It's okay to take a break.",
45
+ "links": [
46
+ "https://www.nih.gov/health-information/emotional-wellness-toolkit",
47
+ "https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety"
48
+ ],
49
+ "videos": [
50
+ "https://youtu.be/-e-4Kx5px_I",
51
+ "https://youtu.be/Y8HIFRPU6pM"
52
+ ]
53
+ },
54
+ "fear": {
55
+ "text": "You're feeling fearful. Try some relaxation techniques.",
56
+ "links": [
57
+ "https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety",
58
+ "https://www.health.harvard.edu/health-a-to-z"
59
+ ],
60
+ "videos": [
61
+ "https://www.youtube.com/shorts/Tq49ajl7c8Q?feature=share",
62
+ "https://youtu.be/yGKKz185M5o"
63
+ ]
64
+ },
65
+ "disgust": {
66
+ "text": "You're feeling disgusted. Take a deep breath and refocus.",
67
+ "links": [
68
+ "https://www.health.harvard.edu/health-a-to-z",
69
+ "https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety"
70
+ ],
71
+ "videos": [
72
+ "https://youtu.be/MIc299Flibs",
73
+ "https://youtu.be/-e-4Kx5px_I"
74
+ ]
75
+ },
76
+ }
77
+ return suggestions.get(emotion, {
78
+ "text": "Feeling neutral? That's okay! Take care of your mental health.",
79
+ "links": [],
80
+ "videos": []
81
+ })
82
 
83
+ # Streamlit UI
84
+ def main():
85
+ # Set the background image
86
+ st.markdown("""
87
  <style>
88
+ .stApp {
89
+ background-image: url('https://www.example.com/your-image.jpg');
90
  background-size: cover;
91
+ background-position: center;
 
 
 
 
 
 
 
92
  }
93
  </style>
94
+ """, unsafe_allow_html=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
 
96
+ # Title of the app
97
+ st.title("Emotion Prediction and Well-being Suggestions")
98
+
99
+ # User input for emotional state
100
+ st.header("Tell us how you're feeling today!")
101
+
102
+ user_input = st.text_area("Enter a short sentence about your current mood:", "")
103
+
104
+ if user_input:
105
+ # Use the model to predict emotion
106
+ try:
107
+ result = emotion_classifier(user_input)
108
+ emotion = result[0]['label'].lower()
 
 
 
 
 
 
 
109
 
110
+ st.subheader(f"Emotion Detected: {emotion.capitalize()}")
111
+
112
+ # Get well-being suggestions based on emotion
113
+ suggestions = get_well_being_suggestions(emotion)
114
+
115
+ # Display text suggestions
116
+ st.write(suggestions["text"])
 
 
117
 
118
+ # Display links
119
+ if suggestions["links"]:
120
+ st.write("Useful Resources:")
121
+ for link in suggestions["links"]:
122
+ st.markdown(f"[{link}]({link})")
 
 
 
123
 
124
+ # Display video links
125
+ if suggestions["videos"]:
126
+ st.write("Relaxation Videos:")
127
+ for video in suggestions["videos"]:
128
+ st.markdown(f"[Watch here]({video})")
129
+
130
+ # Add a button for a summary
131
+ if st.button('Summary'):
132
+ st.write(f"Emotion detected: {emotion.capitalize()}. Here are your well-being suggestions to enhance your mood.")
133
+ st.write("Explore the links and videos to improve your emotional health!")
134
+
135
+ except Exception as e:
136
+ st.error(f"Error predicting emotion: {str(e)}")
137
 
138
+ # Run the Streamlit app
139
+ if __name__ == "__main__":
140
+ main()