tarrasyed19472007's picture
Update app.py
0900b9b verified
raw
history blame
4.11 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")
# Question Database
questions = [
"How are you feeling right now?",
"What’s something that’s been on your mind lately?",
"Do you feel energized or tired at this moment?",
"What’s the most significant event in your day so far?",
"If you had to describe your mood in one word, what would it be?",
]
# Expanded Mood States
moods = [
"Happy", "Excited", "Relaxed", "Grateful", "Calm",
"Dull", "Neutral", "Tired", "Bored", "Lonely",
"Angry", "Frustrated", "Anxious", "Stressed", "Overwhelmed",
"Hopeful", "Confused", "Motivated", "Curious", "Peaceful"
]
# Suggestion Database
suggestion_database = {
"POSITIVE": {
"suggestions": ["Celebrate your success!", "Share your happiness with someone.", "Reflect on what makes you feel this way."],
"articles": [{"title": "Staying Positive", "url": "https://example.com/positivity"}],
"videos": [{"title": "Boosting Happiness", "url": "https://www.youtube.com/watch?v=happinessboost"}],
},
"NEGATIVE": {
"suggestions": ["Take a break to relax.", "Talk to someone you trust.", "Try mindfulness exercises."],
"articles": [{"title": "Managing Stress", "url": "https://example.com/stressmanagement"}],
"videos": [{"title": "Dealing with Stress", "url": "https://www.youtube.com/watch?v=stressrelief"}],
},
"NEUTRAL": {
"suggestions": ["Take a short walk.", "Plan your next task mindfully.", "Enjoy a calming activity like reading."],
"articles": [{"title": "Finding Balance", "url": "https://example.com/balance"}],
"videos": [{"title": "Relaxation Techniques", "url": "https://www.youtube.com/watch?v=relaxvideo"}],
},
}
# Function to map moods to emotion categories
def map_mood_to_category(mood):
if mood in ["Happy", "Excited", "Relaxed", "Grateful", "Calm", "Hopeful", "Motivated", "Curious", "Peaceful"]:
return "POSITIVE"
elif mood in ["Dull", "Neutral", "Tired", "Bored", "Lonely"]:
return "NEUTRAL"
else: # Negative emotions
return "NEGATIVE"
# Function to suggest activities based on the mood
def suggest_activity(mood):
category = map_mood_to_category(mood)
resources = suggestion_database.get(category, {})
return resources
# Streamlit app
def main():
st.title("Mood Analysis and Suggestions")
# Step 1: Display the questions
st.write("Answer the following 5 questions to help us understand your mood:")
responses = []
for i, question in enumerate(questions, start=1):
response = st.text_input(f"{i}. {question}")
if response:
responses.append(response)
# Step 2: Analyze responses if all questions are answered
if len(responses) == len(questions):
combined_text = " ".join(responses)
# Analyze responses to determine mood
analysis_result = emotion_analyzer(combined_text)
detected_emotion = analysis_result[0]['label']
# Map detected emotion to a mood state
detected_mood = random.choice(moods) # Mock mapping for demonstration
st.write(f"Detected Mood: {detected_mood}")
# Step 3: Fetch suggestions based on mood
resources = suggest_activity(detected_mood)
# Display suggestions, articles, and videos
st.write("Suggestions:")
for suggestion in resources.get("suggestions", []):
st.write(f"- {suggestion}")
st.write("Articles:")
for article in resources.get("articles", []):
st.write(f"- [{article['title']}]({article['url']})")
st.write("Videos:")
for video in resources.get("videos", []):
st.write(f"- [{video['title']}]({video['url']})")
else:
st.write("Please answer all questions to receive suggestions.")
if __name__ == "__main__":
main()