tarrasyed19472007's picture
Update app.py
4c0c8ad verified
raw
history blame
7.74 kB
import random
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")
# Expanded Question Database
questions = [
"How are you feeling today?",
"What's something that recently brought you joy?",
"Are you experiencing any stress or pressure currently?",
"What’s one thing you’re looking forward to?",
"Do you feel motivated or tired today?",
"Have you spent quality time with someone recently?",
"What was your most challenging moment today?",
"How would you describe your mood right now?",
"Are you excited about any upcoming events?",
"Do you feel calm or restless at the moment?",
"What is currently taking up most of your mental space?",
"How do you feel about your current workload?",
"Have you smiled or laughed today? If yes, why?",
"Do you feel supported by others around you?",
"Are there any worries that keep recurring for you?",
"What’s the best thing that happened to you recently?",
"Do you feel emotionally drained or refreshed?",
"What thoughts are you waking up with lately?",
"Do you feel connected with nature or your surroundings?",
"Have you had a chance to relax today?",
"What’s your level of excitement about your current projects?",
"Do you feel appreciated by people around you?",
"What’s the last memory that made you feel deeply at peace?",
"Are there any emotions you’re struggling to understand?",
"What is something that makes you proud about yourself?",
"Do you feel overwhelmed by responsibilities today?",
"What’s one thing you wish you could change right now?",
"Are you spending time on activities that energize you?",
"Do you feel hopeful about your personal goals?",
"What’s something that inspires you daily?",
]
# Expanded Suggestion Database
suggestion_database = {
"NEGATIVE": {
"suggestions": [
"Try a guided meditation", "Take a walk in nature", "Connect with a trusted friend",
"Write down your feelings in a journal", "Do some light yoga", "Listen to soothing music",
"Practice deep breathing exercises", "Declutter a small space", "Watch an uplifting movie",
"Try a creative activity like drawing or writing"
],
"articles": [
{"title": "Overcoming Sadness", "url": "https://example.com/sadness1"},
{"title": "Understanding Depression", "url": "https://example.com/sadness2"},
{"title": "Ways to Break Negative Thinking", "url": "https://example.com/negativity"},
],
"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"},
{"title": "Stress Relief Techniques", "url": "https://www.youtube.com/watch?v=stressrelief"},
],
},
"POSITIVE": {
"suggestions": [
"Celebrate your achievements", "Practice gratitude journaling", "Spend time with loved ones",
"Plan an activity that excites you", "Try a new hobby", "Volunteer for a cause you care about",
"Reflect on positive memories", "Cook your favorite meal", "Dance to your favorite songs",
"Go outside and enjoy the weather"
],
"articles": [
{"title": "The Benefits of Joy", "url": "https://example.com/joy1"},
{"title": "Maintaining Positive Emotions", "url": "https://example.com/joy2"},
{"title": "Creating a Happiness Routine", "url": "https://example.com/happiness"},
],
"videos": [
{"title": "Boosting Your Happiness", "url": "https://www.youtube.com/watch?v=joyvideo1"},
{"title": "Practicing Gratitude", "url": "https://www.youtube.com/watch?v=joyvideo2"},
{"title": "Inspiring Talks on Positivity", "url": "https://www.youtube.com/watch?v=positivitytalk"},
],
},
"NEUTRAL": {
"suggestions": [
"Take a short break", "Go for a quiet walk", "Read a book you enjoy",
"Practice mindfulness exercises", "Write down your thoughts", "Do light stretching",
"Disconnect from screens for a while", "Spend time in nature", "Organize your workspace",
"Plan your week to reduce uncertainty"
],
"articles": [
{"title": "Importance of Self-Care", "url": "https://example.com/selfcare1"},
{"title": "Stress Management Techniques", "url": "https://example.com/stress1"},
{"title": "Finding Balance in Daily Life", "url": "https://example.com/balance"},
],
"videos": [
{"title": "Relaxation Techniques", "url": "https://www.youtube.com/watch?v=relaxvideo1"},
{"title": "Mindfulness Exercises", "url": "https://www.youtube.com/watch?v=mindfulnessvideo1"},
{"title": "Creating a Peaceful Routine", "url": "https://www.youtube.com/watch?v=peacefulroutine"},
],
},
}
# 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("Enhanced Emotion Detection and Suggestions")
# Step 1: Randomly pick 20 questions from the database
selected_questions = random.sample(questions, 20)
# Step 2: Collect answers from the user
user_responses = []
st.write("Please answer the following questions:")
for i, question in enumerate(selected_questions, start=1):
response = st.text_input(f"{i}. {question}")
if response:
user_responses.append(response)
# Step 3: Analyze sentiment of the responses if all questions are answered
if len(user_responses) == len(selected_questions):
text_to_analyze = " ".join(user_responses)
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 4: 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 the questions to receive suggestions.")
if __name__ == "__main__":
main()