Spaces:
Sleeping
Sleeping
import streamlit as st | |
from transformers import pipeline | |
# Emotion classifier (use a pre-trained model from Hugging Face) | |
emotion_analyzer = pipeline("text-classification", model="distilbert-base-uncased") | |
# Enhanced Suggestion Database with resources | |
suggestion_database = { | |
"NEGATIVE": { | |
"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"}, | |
], | |
}, | |
"POSITIVE": { | |
"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", []) | |
# Function to suggest activities based on the emotion analysis result | |
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 | |
def main(): | |
st.title("Emotion Detection and Suggestions") | |
st.write("Please answer the following questions:") | |
# Step 1: Collect answers to three questions | |
question_1 = st.text_input("How are you feeling today? (e.g., happy, sad, stressed)") | |
question_2 = st.text_input("What's something that is currently on your mind?") | |
question_3 = st.text_input("Do you feel overwhelmed or calm right now?") | |
# Step 2: Analyze sentiment of responses (only proceed if all questions are answered) | |
if question_1 and question_2 and question_3: | |
text_to_analyze = f"{question_1} {question_2} {question_3}" | |
analysis_result = emotion_analyzer(text_to_analyze) | |
emotion = analysis_result[0]['label'] # Get the emotion from the analysis result | |
# Map emotion label from the model to our suggestion database | |
if emotion == "LABEL_0": | |
emotion = "NEGATIVE" | |
elif emotion == "LABEL_1": | |
emotion = "POSITIVE" | |
else: | |
emotion = "NEUTRAL" | |
st.write(f"Emotion detected: {emotion}") | |
# Step 3: Suggest activities, articles, and videos | |
resources = suggest_activity({emotion: 1}) | |
# Display suggestions, articles, and videos | |
st.write("Suggestions:") | |
for suggestion in resources["suggestions"]: | |
st.write(f"- {suggestion}") | |
st.write("Articles:") | |
for article in resources["articles"]: | |
st.write(f"- [{article['title']}]({article['url']})") | |
st.write("Videos:") | |
for video in resources["videos"]: | |
st.write(f"- [{video['title']}]({video['url']})") | |
else: | |
st.write("Please answer all three questions to receive suggestions.") | |
if __name__ == "__main__": | |
main() | |