Spaces:
Sleeping
Sleeping
import streamlit as st | |
from transformers import pipeline | |
# Load the emotion classification model | |
emotion_analyzer = pipeline("text-classification", model="cardiffnlp/twitter-roberta-base-sentiment") | |
# Enhanced Suggestion Database (Now includes resources) | |
suggestion_database = { | |
"sadness": { | |
"suggestions": ["Try a guided meditation", "Take a walk in nature", "Connect with a friend"], | |
"articles": [ | |
{"title": "Overcoming Sadness", "url": "https://example.com/sadness1"}, | |
{"title": "Understanding Depression", "url": "https://example.com/sadness2"}, | |
], | |
"videos": [ | |
{"title": "Mindfulness for Sadness", "url": "https://www.youtube.com/watch?v=sadnessvideo1"}, | |
{"title": "Coping with Grief", "url": "https://www.youtube.com/watch?v=sadnessvideo2"}, | |
], | |
}, | |
"joy": { | |
"suggestions": ["Practice gratitude", "Engage in a hobby", "Spend time with loved ones"], | |
"articles": [ | |
{"title": "The Benefits of Joy", "url": "https://example.com/joy1"}, | |
{"title": "Maintaining Positive Emotions", "url": "https://example.com/joy2"}, | |
], | |
"videos": [ | |
{"title": "Boosting Your Happiness", "url": "https://www.youtube.com/watch?v=joyvideo1"}, | |
{"title": "Practicing Gratitude", "url": "https://www.youtube.com/watch?v=joyvideo2"}, | |
], | |
}, | |
"neutral": { | |
"suggestions": ["Take a break", "Engage in a relaxing activity", "Spend time in nature"], | |
"articles": [ | |
{"title": "Importance of Self-Care", "url": "https://example.com/selfcare1"}, | |
{"title": "Stress Management Techniques", "url": "https://example.com/stress1"}, | |
], | |
"videos": [ | |
{"title": "Relaxation Techniques", "url": "https://www.youtube.com/watch?v=relaxvideo1"}, | |
{"title": "Mindfulness Exercises", "url": "https://www.youtube.com/watch?v=mindfulnessvideo1"}, | |
], | |
} | |
} | |
# Function to fetch relevant resources based on emotion | |
def get_relevant_resources(emotion): | |
resources = suggestion_database.get(emotion, {}) | |
return resources.get("suggestions", []), resources.get("articles", []), resources.get("videos", []) | |
# Enhanced Suggestion Function | |
def suggest_activity(emotion_analysis): | |
max_emotion = max(emotion_analysis, key=emotion_analysis.get) if emotion_analysis else "neutral" | |
suggestions, articles, videos = get_relevant_resources(max_emotion) | |
return { | |
"suggestions": suggestions, | |
"articles": articles, | |
"videos": videos, | |
} | |
# Streamlit app interface | |
st.title("Emotion Prediction and Well-Being Suggestions") | |
# Ask 3 questions to the user | |
question1 = st.text_input("How are you feeling today?") | |
question2 = st.text_input("Have you felt stressed recently?") | |
question3 = st.text_input("Do you have enough time for relaxation?") | |
if st.button("Get Suggestions"): | |
# Combine responses to analyze sentiment | |
user_input = f"{question1} {question2} {question3}" | |
# Perform emotion analysis | |
emotion_analysis = emotion_analyzer(user_input) | |
# Get suggestions, articles, and videos based on the emotion | |
resources = suggest_activity(emotion_analysis) | |
# Display suggestions, articles, and videos | |
st.write("Here are some suggestions to help you:") | |
for suggestion in resources["suggestions"]: | |
st.write(f"- {suggestion}") | |
st.write("Articles you may find helpful:") | |
for article in resources["articles"]: | |
st.markdown(f"[{article['title']}]({article['url']})") | |
st.write("Videos you may find helpful:") | |
for video in resources["videos"]: | |
st.markdown(f"[{video['title']}]({video['url']})") | |