Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 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 |
+
# 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 = [
|
| 47 |
+
"How would you describe your mood today? (e.g., happy, stressed, calm)",
|
| 48 |
+
"Are there any recent events that might be affecting your emotional state?",
|
| 49 |
+
"How do you generally cope with stress or emotional challenges?"
|
| 50 |
+
]
|
| 51 |
+
|
| 52 |
+
responses = []
|
| 53 |
+
for question in questions:
|
| 54 |
+
response = st.text_input(question)
|
| 55 |
+
responses.append(response)
|
| 56 |
+
|
| 57 |
+
# Process the responses
|
| 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)")
|
| 76 |
+
st.markdown("[Hula Dance for Well-being](https://hulahealsthesoul.com)")
|
| 77 |
+
|
| 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)
|