Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,129 +1,140 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 3 |
-
import torch
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
st.title("Emotion Prediction & Well-being Suggestions")
|
| 10 |
-
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 |
-
#
|
| 15 |
-
|
| 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 |
-
"
|
| 23 |
-
"
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
"
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
"
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
"
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
}
|
| 52 |
-
return suggestions.get(emotion,
|
| 53 |
-
"
|
| 54 |
-
"
|
| 55 |
-
"
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
# Emotional Health Questions
|
| 59 |
-
questions = [
|
| 60 |
-
"How would you describe your mood today? (e.g., happy, stressed, calm)",
|
| 61 |
-
"Are there any recent events that might be affecting your emotional state?",
|
| 62 |
-
"How do you generally cope with stress or emotional challenges?"
|
| 63 |
-
]
|
| 64 |
-
|
| 65 |
-
responses = []
|
| 66 |
-
for question in questions:
|
| 67 |
-
response = st.text_input(question)
|
| 68 |
-
responses.append(response)
|
| 69 |
|
| 70 |
-
#
|
| 71 |
-
|
| 72 |
-
|
| 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)")
|
| 97 |
-
st.markdown("[Hula Dance for Well-being](https://hulahealsthesoul.com)")
|
| 98 |
-
|
| 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://
|
| 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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|