Spaces:
Sleeping
Sleeping
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.") | |