tarrasyed19472007 commited on
Commit
11d653c
·
verified ·
1 Parent(s): 021a5a2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -35
app.py CHANGED
@@ -1,8 +1,8 @@
1
  import streamlit as st
2
- from transformers import DistilBertTokenizer, DistilBertForSequenceClassification, pipeline
3
- import os
4
 
5
- # Set page config
6
  st.set_page_config(page_title="Emotion Prediction & Well-being Suggestions", page_icon="🌺", layout="centered")
7
 
8
  # Title and Introduction
@@ -11,36 +11,49 @@ st.markdown("""
11
  This app uses AI to understand your emotions based on your responses. Afterward, you'll receive well-being suggestions to improve your mood, tailored specifically for you and your cultural context in Hawaii.
12
  """)
13
 
14
- # Load the emotion classification model and tokenizer
15
- def load_model():
16
- model_name = "j-hartmann/emotion-english-distilroberta-base"
17
- try:
18
- # Attempt to load the model and tokenizer
19
- tokenizer = DistilBertTokenizer.from_pretrained(model_name)
20
- model = DistilBertForSequenceClassification.from_pretrained(model_name)
21
- emotion_classifier = pipeline("text-classification", model=model, tokenizer=tokenizer)
22
- return emotion_classifier
23
- except Exception as e:
24
- st.error(f"Error loading model: {e}")
25
- return None
26
 
27
- emotion_classifier = load_model()
28
-
29
- # If the model didn't load, exit the app
30
- if not emotion_classifier:
31
- st.stop()
32
-
33
- # Define well-being suggestions based on the emotion
34
  def get_wellbeing_suggestions(emotion):
35
  suggestions = {
36
- "joy": ["Keep enjoying life! Consider a walk on the beach, or some hula dancing to feel the rhythm of the island."],
37
- "anger": ["Practice deep breathing and mindfulness exercises. Perhaps try surfing or a peaceful walk in nature."],
38
- "sadness": ["Try a short meditation session or call a friend to chat. Hawaii offers stunning sunsets perfect for reflection."],
39
- "fear": ["Take a calming breath and practice grounding techniques. Explore Hawaiian mindfulness practices to feel centered."],
40
- "surprise": ["Embrace the feeling and explore new activities like paddleboarding or hiking."],
41
- "disgust": ["Do something relaxing, like yoga or watching the waves crash against the shore."]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  }
43
- return suggestions.get(emotion, ["Try some breathing exercises and give yourself time to relax."])
 
 
 
 
44
 
45
  # Emotional Health Questions
46
  questions = [
@@ -58,18 +71,26 @@ for question in questions:
58
  if all(responses):
59
  user_input = " ".join(responses)
60
 
61
- # Predict emotion based on the input
62
- emotion_results = emotion_classifier(user_input)
63
- emotion = emotion_results[0]['label']
 
 
64
 
65
  # Show emotion and well-being suggestions
66
- st.subheader(f"Your Predicted Emotion: {emotion.capitalize()}")
67
  st.write(f"Based on your responses, we suggest the following to improve your well-being:")
68
 
69
- wellbeing_suggestions = get_wellbeing_suggestions(emotion.lower())
70
  for suggestion in wellbeing_suggestions:
71
  st.write(f"- {suggestion}")
72
 
 
 
 
 
 
 
73
  st.markdown("---")
74
  st.write("For more well-being tips and resources, explore the following links:")
75
  st.markdown("[Hawaii Mindfulness Practice](https://www.hawaiimindfulness.org)")
@@ -78,12 +99,31 @@ if all(responses):
78
  else:
79
  st.warning("Please answer all the questions to receive emotional health suggestions.")
80
 
81
- # Add custom background image
82
  st.markdown("""
83
  <style>
84
  .stApp {
85
  background-image: url('https://images.unsplash.com/photo-1602231353203-b1e5e2191b68');
86
  background-size: cover;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
  }
88
  </style>
89
  """, unsafe_allow_html=True)
 
1
  import streamlit as st
2
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
3
+ import torch
4
 
5
+ # Set page config for a clean interface
6
  st.set_page_config(page_title="Emotion Prediction & Well-being Suggestions", page_icon="🌺", layout="centered")
7
 
8
  # Title and Introduction
 
11
  This app uses AI to understand your emotions based on your responses. Afterward, you'll receive well-being suggestions to improve your mood, tailored specifically for you and your cultural context in Hawaii.
12
  """)
13
 
14
+ # Load the tokenizer and model
15
+ tokenizer = AutoTokenizer.from_pretrained("j-hartmann/emotion-english-distilroberta-base")
16
+ model = AutoModelForSequenceClassification.from_pretrained("j-hartmann/emotion-english-distilroberta-base")
 
 
 
 
 
 
 
 
 
17
 
18
+ # Define well-being suggestions with URLs for articles and videos based on the emotion
 
 
 
 
 
 
19
  def get_wellbeing_suggestions(emotion):
20
  suggestions = {
21
+ "joy": [
22
+ "Keep enjoying life! Consider a walk on the beach, or some hula dancing to feel the rhythm of the island.",
23
+ "Articles and Resources: [Emotional Wellness Toolkit - NIH](https://www.nih.gov/health-information/emotional-wellness-toolkit)",
24
+ "Videos: [Hula Dance for Well-being](https://youtu.be/m1vaUGtyo-A)"
25
+ ],
26
+ "anger": [
27
+ "Practice deep breathing and mindfulness exercises. Perhaps try surfing or a peaceful walk in nature.",
28
+ "Articles and Resources: [Tips for Dealing with Anxiety - HelpGuide](https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety)",
29
+ "Videos: [Mindful Breathing Meditation](https://www.youtube.com/shorts/Tq49ajl7c8Q?feature=share)"
30
+ ],
31
+ "sadness": [
32
+ "Try a short meditation session or call a friend to chat. Hawaii offers stunning sunsets perfect for reflection.",
33
+ "Articles and Resources: [Health A-Z - Harvard Health](https://www.health.harvard.edu/health-a-to-z)",
34
+ "Videos: [Mindful Breathing Meditation](https://youtu.be/MIc299Flibs)"
35
+ ],
36
+ "fear": [
37
+ "Take a calming breath and practice grounding techniques. Explore Hawaiian mindfulness practices to feel centered.",
38
+ "Articles and Resources: [Mindful Breathing Meditation - HelpGuide](https://www.helpguide.org/mental-health/meditation/mindful-breathing-meditation)",
39
+ "Videos: [Relaxing Mindfulness Exercise](https://www.youtube.com/shorts/fwH8Ygb0K60?feature=share)"
40
+ ],
41
+ "surprise": [
42
+ "Embrace the feeling and explore new activities like paddleboarding or hiking.",
43
+ "Articles and Resources: [Emotional Wellness Toolkit - NIH](https://www.nih.gov/health-information/emotional-wellness-toolkit)",
44
+ "Videos: [Surprising Relaxation Tips](https://youtu.be/yGKKz185M5o)"
45
+ ],
46
+ "disgust": [
47
+ "Do something relaxing, like yoga or watching the waves crash against the shore.",
48
+ "Articles and Resources: [Tips for Dealing with Anxiety - HelpGuide](https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety)",
49
+ "Videos: [Hawaiian Mindfulness Practice](https://www.youtube.com/shorts/hTXMi7ZBKdM?feature=share)"
50
+ ]
51
  }
52
+ return suggestions.get(emotion, [
53
+ "Try some breathing exercises and give yourself time to relax.",
54
+ "Articles and Resources: [Health A-Z - Harvard Health](https://www.health.harvard.edu/health-a-to-z)",
55
+ "Videos: [Calming Breath Exercises](https://youtu.be/Y8HIFRPU6pM)"
56
+ ])
57
 
58
  # Emotional Health Questions
59
  questions = [
 
71
  if all(responses):
72
  user_input = " ".join(responses)
73
 
74
+ # Tokenize the user input and make predictions
75
+ inputs = tokenizer(user_input, return_tensors="pt", truncation=True, padding=True)
76
+ outputs = model(**inputs)
77
+ predictions = torch.argmax(outputs.logits, dim=-1) # Get the index of the predicted class
78
+ emotion_label = model.config.id2label[predictions.item()] # Get the label of the predicted class
79
 
80
  # Show emotion and well-being suggestions
81
+ st.subheader(f"Your Predicted Emotion: {emotion_label.capitalize()}")
82
  st.write(f"Based on your responses, we suggest the following to improve your well-being:")
83
 
84
+ wellbeing_suggestions = get_wellbeing_suggestions(emotion_label.lower())
85
  for suggestion in wellbeing_suggestions:
86
  st.write(f"- {suggestion}")
87
 
88
+ # Show Summary Button
89
+ if st.button("Show Summary"):
90
+ st.subheader("Summary of Well-being Suggestions")
91
+ for suggestion in wellbeing_suggestions:
92
+ st.write(f"- {suggestion}")
93
+
94
  st.markdown("---")
95
  st.write("For more well-being tips and resources, explore the following links:")
96
  st.markdown("[Hawaii Mindfulness Practice](https://www.hawaiimindfulness.org)")
 
99
  else:
100
  st.warning("Please answer all the questions to receive emotional health suggestions.")
101
 
102
+ # Add custom background image to make it visually appealing
103
  st.markdown("""
104
  <style>
105
  .stApp {
106
  background-image: url('https://images.unsplash.com/photo-1602231353203-b1e5e2191b68');
107
  background-size: cover;
108
+ background-position: center;
109
+ }
110
+ .stButton > button {
111
+ background-color: #F57F17;
112
+ color: white;
113
+ font-size: 18px;
114
+ padding: 10px;
115
+ }
116
+ .stButton > button:hover {
117
+ background-color: #FF6F00;
118
+ }
119
+ .stTextInput > div > input {
120
+ background-color: rgba(255, 255, 255, 0.7);
121
+ font-size: 16px;
122
+ color: #333;
123
+ }
124
+ .stMarkdown p {
125
+ font-size: 16px;
126
+ color: white;
127
  }
128
  </style>
129
  """, unsafe_allow_html=True)