|
import streamlit as st |
|
from transformers import pipeline |
|
|
|
|
|
@st.cache_resource |
|
def load_model(): |
|
try: |
|
emotion_classifier = pipeline("text-classification", model="j-hartmann/emotion-english-distilroberta-base") |
|
return emotion_classifier |
|
except Exception as e: |
|
st.error(f"Error loading model: {str(e)}") |
|
return None |
|
|
|
emotion_classifier = load_model() |
|
|
|
|
|
def get_well_being_suggestions(emotion): |
|
suggestions = { |
|
"joy": { |
|
"text": "You're feeling joyful! Keep the positivity going.", |
|
"links": [ |
|
"https://www.nih.gov/health-information/emotional-wellness-toolkit", |
|
"https://www.health.harvard.edu/health-a-to-z", |
|
"https://www.helpguide.org/mental-health/meditation/mindful-breathing-meditation" |
|
], |
|
"videos": [ |
|
"https://youtu.be/m1vaUGtyo-A", |
|
"https://youtu.be/MIc299Flibs" |
|
] |
|
}, |
|
"anger": { |
|
"text": "You're feeling angry. Take a moment to calm down.", |
|
"links": [ |
|
"https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety", |
|
"https://www.helpguide.org/mental-health/meditation/mindful-breathing-meditation" |
|
], |
|
"videos": [ |
|
"https://youtu.be/m1vaUGtyo-A", |
|
"https://www.youtube.com/shorts/fwH8Ygb0K60?feature=share" |
|
] |
|
}, |
|
"sadness": { |
|
"text": "You're feeling sad. It's okay to take a break.", |
|
"links": [ |
|
"https://www.nih.gov/health-information/emotional-wellness-toolkit", |
|
"https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety" |
|
], |
|
"videos": [ |
|
"https://youtu.be/-e-4Kx5px_I", |
|
"https://youtu.be/Y8HIFRPU6pM" |
|
] |
|
}, |
|
"fear": { |
|
"text": "You're feeling fearful. Try some relaxation techniques.", |
|
"links": [ |
|
"https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety", |
|
"https://www.health.harvard.edu/health-a-to-z" |
|
], |
|
"videos": [ |
|
"https://www.youtube.com/shorts/Tq49ajl7c8Q?feature=share", |
|
"https://youtu.be/yGKKz185M5o" |
|
] |
|
}, |
|
"disgust": { |
|
"text": "You're feeling disgusted. Take a deep breath and refocus.", |
|
"links": [ |
|
"https://www.health.harvard.edu/health-a-to-z", |
|
"https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety" |
|
], |
|
"videos": [ |
|
"https://youtu.be/MIc299Flibs", |
|
"https://youtu.be/-e-4Kx5px_I" |
|
] |
|
}, |
|
} |
|
return suggestions.get(emotion, { |
|
"text": "Feeling neutral? That's okay! Take care of your mental health.", |
|
"links": [], |
|
"videos": [] |
|
}) |
|
|
|
|
|
def main(): |
|
|
|
st.markdown(""" |
|
<style> |
|
.stApp { |
|
background-image: url('https://www.example.com/your-image.jpg'); |
|
background-size: cover; |
|
background-position: center; |
|
} |
|
</style> |
|
""", unsafe_allow_html=True) |
|
|
|
|
|
st.title("Emotion Prediction and Well-being Suggestions") |
|
|
|
|
|
st.header("Tell us how you're feeling today!") |
|
|
|
user_input = st.text_area("Enter a short sentence about your current mood:", "") |
|
|
|
if user_input: |
|
|
|
try: |
|
result = emotion_classifier(user_input) |
|
emotion = result[0]['label'].lower() |
|
|
|
st.subheader(f"Emotion Detected: {emotion.capitalize()}") |
|
|
|
|
|
suggestions = get_well_being_suggestions(emotion) |
|
|
|
|
|
st.write(suggestions["text"]) |
|
|
|
|
|
if suggestions["links"]: |
|
st.write("Useful Resources:") |
|
for link in suggestions["links"]: |
|
st.markdown(f"[{link}]({link})") |
|
|
|
|
|
if suggestions["videos"]: |
|
st.write("Relaxation Videos:") |
|
for video in suggestions["videos"]: |
|
st.markdown(f"[Watch here]({video})") |
|
|
|
|
|
if st.button('Summary'): |
|
st.write(f"Emotion detected: {emotion.capitalize()}. Here are your well-being suggestions to enhance your mood.") |
|
st.write("Explore the links and videos to improve your emotional health!") |
|
|
|
except Exception as e: |
|
st.error(f"Error predicting emotion: {str(e)}") |
|
|
|
|
|
if __name__ == "__main__": |
|
main() |
|
|