tarrasyed19472007's picture
Create app.py
a77dfbc verified
raw
history blame
3.8 kB
import streamlit as st
from transformers import DistilBertTokenizer, DistilBertForSequenceClassification, pipeline
import os
# Set page config
st.set_page_config(page_title="Emotion Prediction & Well-being Suggestions", page_icon="๐ŸŒบ", layout="centered")
# Title and Introduction
st.title("Emotion Prediction & Well-being Suggestions")
st.markdown("""
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.
""")
# Load the emotion classification model and tokenizer
def load_model():
model_name = "j-hartmann/emotion-english-distilroberta-base"
try:
# Attempt to load the model and tokenizer
tokenizer = DistilBertTokenizer.from_pretrained(model_name)
model = DistilBertForSequenceClassification.from_pretrained(model_name)
emotion_classifier = pipeline("text-classification", model=model, tokenizer=tokenizer)
return emotion_classifier
except Exception as e:
st.error(f"Error loading model: {e}")
return None
emotion_classifier = load_model()
# If the model didn't load, exit the app
if not emotion_classifier:
st.stop()
# Define well-being suggestions based on the emotion
def get_wellbeing_suggestions(emotion):
suggestions = {
"joy": ["Keep enjoying life! Consider a walk on the beach, or some hula dancing to feel the rhythm of the island."],
"anger": ["Practice deep breathing and mindfulness exercises. Perhaps try surfing or a peaceful walk in nature."],
"sadness": ["Try a short meditation session or call a friend to chat. Hawaii offers stunning sunsets perfect for reflection."],
"fear": ["Take a calming breath and practice grounding techniques. Explore Hawaiian mindfulness practices to feel centered."],
"surprise": ["Embrace the feeling and explore new activities like paddleboarding or hiking."],
"disgust": ["Do something relaxing, like yoga or watching the waves crash against the shore."]
}
return suggestions.get(emotion, ["Try some breathing exercises and give yourself time to relax."])
# Emotional Health Questions
questions = [
"How would you describe your mood today? (e.g., happy, stressed, calm)",
"Are there any recent events that might be affecting your emotional state?",
"How do you generally cope with stress or emotional challenges?"
]
responses = []
for question in questions:
response = st.text_input(question)
responses.append(response)
# Process the responses
if all(responses):
user_input = " ".join(responses)
# Predict emotion based on the input
emotion_results = emotion_classifier(user_input)
emotion = emotion_results[0]['label']
# Show emotion and well-being suggestions
st.subheader(f"Your Predicted Emotion: {emotion.capitalize()}")
st.write(f"Based on your responses, we suggest the following to improve your well-being:")
wellbeing_suggestions = get_wellbeing_suggestions(emotion.lower())
for suggestion in wellbeing_suggestions:
st.write(f"- {suggestion}")
st.markdown("---")
st.write("For more well-being tips and resources, explore the following links:")
st.markdown("[Hawaii Mindfulness Practice](https://www.hawaiimindfulness.org)")
st.markdown("[Hula Dance for Well-being](https://hulahealsthesoul.com)")
else:
st.warning("Please answer all the questions to receive emotional health suggestions.")
# Add custom background image
st.markdown("""
<style>
.stApp {
background-image: url('https://images.unsplash.com/photo-1602231353203-b1e5e2191b68');
background-size: cover;
}
</style>
""", unsafe_allow_html=True)