tarrasyed19472007's picture
Update app.py
25f2b4c verified
import streamlit as st
from transformers import AutoTokenizer, AutoModelForSequenceClassification
from transformers import pipeline
# Set page config as the very first Streamlit command
st.set_page_config(page_title="Emotion Detection and Well-Being Suggestions", layout="wide")
# Load pre-trained model and tokenizer
@st.cache_resource
def load_model():
tokenizer = AutoTokenizer.from_pretrained("j-hartmann/emotion-english-distilroberta-base")
model = AutoModelForSequenceClassification.from_pretrained("j-hartmann/emotion-english-distilroberta-base")
return tokenizer, model
tokenizer, model = load_model()
# Add an aesthetic background with a soothing gradient for a calming effect and black text
st.markdown(
"""
<style>
body {
background: linear-gradient(135deg, #8e2de2, #4a00e0); /* Purple gradient for a calm vibe */
background-size: cover;
background-attachment: fixed;
color: black; /* Set text color to black */
}
.stButton button {
background-color: #6c63ff; /* Blue button color to match the calming purple gradient */
color: white;
font-size: 20px;
}
.stTextInput input {
background-color: rgba(255, 255, 255, 0.2); /* Semi-transparent background for text input */
color: black; /* Black text for input fields */
font-size: 16px;
}
.stMarkdown {
color: black; /* Ensure markdown text is black for better readability */
}
</style>
""",
unsafe_allow_html=True,
)
# Display header
st.title("Emotion Detection and Well-Being Suggestions")
# User input for text (emotion detection)
user_input = st.text_area("How are you feeling today?", "Enter your thoughts here...")
# Model prediction
if user_input:
pipe = pipeline("text-classification", model=model, tokenizer=tokenizer)
result = pipe(user_input)
# Extracting the emotion from the model's result
emotion = result[0]['label']
# Display emotion
st.write(f"**Emotion Detected:** {emotion}")
# Provide suggestions based on the detected emotion
if emotion == 'joy':
st.write("You're feeling happy! Keep up the great mood!")
st.write("Useful Resources:")
st.markdown("[Relaxation Techniques](https://www.helpguide.org/mental-health/meditation/mindful-breathing-meditation)")
st.write("[Dealing with Stress](https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety)")
st.write("[Emotional Wellness Toolkit](https://www.nih.gov/health-information/emotional-wellness-toolkit)")
st.write("Relaxation Videos:")
st.markdown("[Watch on YouTube](https://youtu.be/m1vaUGtyo-A)")
elif emotion == 'anger':
st.write("You're feeling angry. It's okay to feel this way. Let's try to calm down.")
st.write("Useful Resources:")
st.markdown("[Emotional Wellness Toolkit](https://www.nih.gov/health-information/emotional-wellness-toolkit)")
st.write("[Stress Management Tips](https://www.health.harvard.edu/health-a-to-z)")
st.write("[Dealing with Anger](https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety)")
st.write("Relaxation Videos:")
st.markdown("[Watch on YouTube](https://youtu.be/MIc299Flibs)")
elif emotion == 'fear':
st.write("You're feeling fearful. Take a moment to breathe and relax.")
st.write("Useful Resources:")
st.markdown("[Mindfulness Practices](https://www.helpguide.org/mental-health/meditation/mindful-breathing-meditation)")
st.write("[Coping with Anxiety](https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety)")
st.write("[Emotional Wellness Toolkit](https://www.nih.gov/health-information/emotional-wellness-toolkit)")
st.write("Relaxation Videos:")
st.markdown("[Watch on YouTube](https://youtu.be/yGKKz185M5o)")
elif emotion == 'sadness':
st.write("You're feeling sad. It's okay to take a break.")
st.write("Useful Resources:")
st.markdown("[Emotional Wellness Toolkit](https://www.nih.gov/health-information/emotional-wellness-toolkit)")
st.write("[Dealing with Anxiety](https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety)")
st.write("Relaxation Videos:")
st.markdown("[Watch on YouTube](https://youtu.be/-e-4Kx5px_I)")
elif emotion == 'surprise':
st.write("You're feeling surprised. It's okay to feel neutral!")
st.write("Useful Resources:")
st.markdown("[Managing Stress](https://www.health.harvard.edu/health-a-to-z)")
st.write("[Coping Strategies](https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety)")
st.write("Relaxation Videos:")
st.markdown("[Watch on YouTube](https://youtu.be/m1vaUGtyo-A)")
# Add a button for summary
if st.button("Show Summary"):
st.write(f"Emotion Detected: {emotion}")
st.write("Useful Resources based on your mood:")
if emotion == 'joy':
st.write("[Relaxation Techniques](https://www.helpguide.org/mental-health/meditation/mindful-breathing-meditation)")
elif emotion == 'anger':
st.write("[Stress Management Tips](https://www.health.harvard.edu/health-a-to-z)")
elif emotion == 'fear':
st.write("[Mindfulness Practices](https://www.helpguide.org/mental-health/meditation/mindful-breathing-meditation)")
elif emotion == 'sadness':
st.write("[Dealing with Anxiety](https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety)")
elif emotion == 'surprise':
st.write("[Managing Stress](https://www.health.harvard.edu/health-a-to-z)")