mental-health / app.py
itzbhav's picture
Update app.py
a56f6f0 verified
raw
history blame contribute delete
3.97 kB
import streamlit as st
from textblob import TextBlob
import nltk
from nltk.sentiment.vader import SentimentIntensityAnalyzer
from transformers import pipeline
# Ensure necessary NLTK data is downloaded
nltk.download('vader_lexicon')
# Sidebar Navigation
st.sidebar.title("🧠 Mental Health Analyzer")
page = st.sidebar.radio("🌟 Navigate", ["❀️ Importance of Mental Health", "🧩 ML-Based Analysis", "πŸ€– Transformer Tips"])
# Page 1: Importance of Mental Health
if page == "❀️ Importance of Mental Health":
st.title("🌼 Why Mental Health Matters")
st.write("""
Mental health is crucial for overall well-being. It influences how we think, feel, and act. πŸ§ πŸ’¬
Good mental health can:
- 🌟 Improve productivity and performance
- 🀝 Enhance relationships
- πŸ’ͺ Increase self-esteem and confidence
- 🧘 Help manage emotions and stress
Remember, it's okay to seek help and talk about your feelings. πŸ€—
""")
# Page 2: ML-Based Analysis
elif page == "🧩 ML-Based Analysis":
st.title("πŸ§ͺ Analyze Your Mental State")
user_input = st.text_area("✍️ Share your current thoughts or feelings:")
if st.button("πŸ” Analyze"):
if user_input:
st.subheader("πŸ“ˆ Analysis Results")
# TextBlob Sentiment
blob = TextBlob(user_input)
polarity = blob.sentiment.polarity
subjectivity = blob.sentiment.subjectivity
st.write(f"**πŸ§ͺ TextBlob Polarity:** {polarity:.2f}")
st.write(f"**πŸ”¬ TextBlob Subjectivity:** {subjectivity:.2f}")
# VADER Sentiment
sid = SentimentIntensityAnalyzer()
vader_scores = sid.polarity_scores(user_input)
st.write(f"**⚑ VADER Compound Score:** {vader_scores['compound']:.2f}")
# Emotion Detection with Transformers
emotion_pipe = pipeline("text-classification", model="bhadresh-savani/distilbert-base-uncased-emotion", top_k=2)
emotions = emotion_pipe(user_input)
st.write("**🎭 Emotion Predictions:**")
for inner_list in emotions:
for emo in inner_list:
st.write(f"- {emo['label']} (Score: {emo['score']:.2f})")
# Tips based on polarity
st.subheader("🌈 Tips to Improve Your Mood")
if polarity < -0.2 or vader_scores['compound'] < -0.2:
st.write("- πŸ§˜β€β™€οΈ Practice mindfulness and deep breathing exercises.")
st.write("- πŸ‘₯ Talk to a trusted friend or family member.")
st.write("- 🚢 Engage in light physical activity, like a short walk.")
elif polarity > 0.2 and vader_scores['compound'] > 0.2:
st.write("- πŸ“– Keep up with activities that make you happy.")
st.write("- ✍️ Share your positive feelings in a gratitude journal.")
st.write("- πŸ€— Continue connecting with supportive people.")
else:
st.write("- 🧩 Take breaks and reflect on your feelings.")
st.write("- πŸ“ Try journaling or meditation to gain clarity.")
st.write("- πŸ›Œ Maintain a balanced routine of work and rest.")
else:
st.warning("⚠️ Please enter some text to analyze.")
# Page 3: Transformer-Based Tips Generator
else:
st.title("πŸ€– Get Personalized Tips")
user_input = st.text_area("πŸ“ Describe how you're feeling or what you need tips on:")
if st.button("πŸš€ Generate Tips"):
if user_input:
gen = pipeline("text-generation", model="gpt2", max_length=150)
prompt = f"As a supportive mental health assistant, provide tips for someone feeling: {user_input}\nTips:"
result = gen(prompt, num_return_sequences=1)[0]['generated_text']
st.write(result.replace(prompt, "").strip())
else:
st.warning("⚠️ Please share your thoughts to get tips.")