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.")