tarrasyed19472007's picture
Update app.py
b525e52 verified
raw
history blame
5.78 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")
# Updated Question Database (Doctor-prescribed questions)
questions = [
"How are you feeling emotionally right now?",
"What physical sensations or discomfort are you experiencing?",
"Is there something specific that's been troubling your thoughts?"
]
# 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": "Emotional Wellness Toolkit", "url": "https://www.nih.gov/health-information/emotional-wellness-toolkit"},
{"title": "Health A to Z", "url": "https://www.health.harvard.edu/health-a-to-z"},
],
"videos": [
{"title": "Boosting Happiness", "url": "https://youtu.be/m1vaUGtyo-A"},
{"title": "Motivational Short Video", "url": "https://www.youtube.com/shorts/Tq49ajl7c8Q?feature=share"},
],
},
"NEGATIVE": {
"suggestions": ["Take a break to relax.", "Talk to someone you trust.", "Try mindfulness exercises."],
"articles": [
{"title": "Tips for Dealing with Anxiety", "url": "https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety"},
{"title": "Mindful Breathing Meditation", "url": "https://www.helpguide.org/mental-health/meditation/mindful-breathing-meditation"},
],
"videos": [
{"title": "Coping with Anxiety", "url": "https://youtu.be/MIc299Flibs"},
{"title": "Relaxation Techniques", "url": "https://www.youtube.com/shorts/fwH8Ygb0K60?feature=share"},
],
},
"NEUTRAL": {
"suggestions": ["Take a short walk.", "Plan your next task mindfully.", "Enjoy a calming activity like reading."],
"articles": [
{"title": "Finding Balance", "url": "https://www.health.harvard.edu/health-a-to-z"},
{"title": "Emotional Wellness Toolkit", "url": "https://www.nih.gov/health-information/emotional-wellness-toolkit"},
],
"videos": [
{"title": "Mindfulness for Beginners", "url": "https://youtu.be/Y8HIFRPU6pM"},
{"title": "Peaceful Mind Short", "url": "https://www.youtube.com/shorts/hTXMi7ZBKdM?feature=share"},
],
},
"STRESSED": {
"suggestions": ["Practice deep breathing exercises.", "Write down your thoughts to release stress.", "Spend time in a quiet environment."],
"articles": [
{"title": "Mindful Breathing Meditation", "url": "https://www.helpguide.org/mental-health/meditation/mindful-breathing-meditation"},
{"title": "Tips for Dealing with Anxiety", "url": "https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety"},
],
"videos": [
{"title": "Managing Stress", "url": "https://youtu.be/-e-4Kx5px_I"},
{"title": "Overcoming Overwhelm", "url": "https://www.youtube.com/shorts/fwH8Ygb0K60?feature=share"},
],
},
}
# 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"
elif mood in ["Angry", "Frustrated", "Anxious", "Stressed", "Overwhelmed"]:
return "NEGATIVE"
else:
return "STRESSED"
# 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 3 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()