unknownbb007 commited on
Commit
5d517fd
·
verified ·
1 Parent(s): adfdb4f

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +133 -0
  2. requirement.txt +5 -0
app.py ADDED
@@ -0,0 +1,133 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import pipeline
3
+ from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
4
+ import nltk
5
+ import re
6
+
7
+ # -------------------------
8
+ # Safety Disclaimer
9
+ # -------------------------
10
+ DISCLAIMER = """
11
+ ⚠️ I’m not a therapist. I provide emotional support, not medical advice.
12
+ If you're in crisis, please seek professional help immediately.
13
+ India Helpline: AASRA 9820466726
14
+ US: Call or text 988
15
+ """
16
+
17
+ # -------------------------
18
+ # Load NLP Models
19
+ # -------------------------
20
+ @st.cache_resource
21
+ def load_models():
22
+ sentiment_model = pipeline("sentiment-analysis")
23
+ vader = SentimentIntensityAnalyzer()
24
+ return sentiment_model, vader
25
+
26
+ sentiment_model, vader = load_models()
27
+
28
+ # -------------------------
29
+ # Crisis Detection
30
+ # -------------------------
31
+ CRISIS_KEYWORDS = [
32
+ "suicide", "kill myself", "end my life",
33
+ "self harm", "die", "hopeless", "no reason to live"
34
+ ]
35
+
36
+ def detect_crisis(text):
37
+ text_lower = text.lower()
38
+ return any(keyword in text_lower for keyword in CRISIS_KEYWORDS)
39
+
40
+ # -------------------------
41
+ # Mood Detection
42
+ # -------------------------
43
+ def detect_mood(text):
44
+ if detect_crisis(text):
45
+ return "critical"
46
+
47
+ vader_score = vader.polarity_scores(text)['compound']
48
+
49
+ if vader_score >= 0.5:
50
+ return "positive"
51
+ elif vader_score <= -0.5:
52
+ if "anxious" in text.lower() or "panic" in text.lower():
53
+ return "anxious"
54
+ return "sad"
55
+ elif "stress" in text.lower() or "exam" in text.lower():
56
+ return "stressed"
57
+ else:
58
+ return "neutral"
59
+
60
+ # -------------------------
61
+ # Empathetic Responses
62
+ # -------------------------
63
+ def generate_response(mood, user_text):
64
+
65
+ if mood == "critical":
66
+ return (
67
+ "I'm really sorry you're feeling this much pain. "
68
+ "You deserve support right now. Please consider reaching out to "
69
+ "AASRA (India: 9820466726) or 988 (US). "
70
+ "If you're in immediate danger, call emergency services. "
71
+ "You're not alone, even if it feels that way."
72
+ )
73
+
74
+ elif mood == "sad":
75
+ return (
76
+ "I hear that you're feeling low. It’s completely okay to feel this way. "
77
+ "Would you like to share what’s weighing on you? "
78
+ "Even small steps count. You’ve handled difficult days before."
79
+ )
80
+
81
+ elif mood == "anxious":
82
+ return (
83
+ "It sounds like you're feeling anxious. That can be really overwhelming. "
84
+ "Let’s try something simple: inhale for 4 seconds, hold for 7, "
85
+ "exhale for 8. Repeat 3 times. "
86
+ "Your mind deserves calm."
87
+ )
88
+
89
+ elif mood == "stressed":
90
+ return (
91
+ "I hear you're feeling stressed—maybe exams or placements? "
92
+ "Break it into small tasks. Study 25 minutes, rest 5. "
93
+ "You’ve tackled tough assignments before—you’ve got this!"
94
+ )
95
+
96
+ elif mood == "positive":
97
+ return (
98
+ "That’s great to hear! 😊 "
99
+ "Keep nurturing what’s working for you. "
100
+ "Would you like a quick productivity boost or gratitude exercise?"
101
+ )
102
+
103
+ else:
104
+ return (
105
+ "Thanks for sharing. How are you feeling today? "
106
+ "You can talk freely here—this space is for you."
107
+ )
108
+
109
+ # -------------------------
110
+ # Streamlit UI
111
+ # -------------------------
112
+ st.set_page_config(page_title="Mental Health Companion", page_icon="🧠")
113
+
114
+ st.title("🧠 Student Mental Health Companion")
115
+ st.write(DISCLAIMER)
116
+
117
+ if "chat_history" not in st.session_state:
118
+ st.session_state.chat_history = []
119
+
120
+ user_input = st.text_input("How are you feeling today?")
121
+
122
+ if user_input:
123
+ mood = detect_mood(user_input)
124
+ response = generate_response(mood, user_input)
125
+
126
+ st.session_state.chat_history.append(("You", user_input))
127
+ st.session_state.chat_history.append(("Companion", response))
128
+
129
+ for sender, message in st.session_state.chat_history:
130
+ if sender == "You":
131
+ st.markdown(f"**You:** {message}")
132
+ else:
133
+ st.markdown(f"**Companion:** {message}")
requirement.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ streamlit
2
+ transformers
3
+ torch
4
+ nltk
5
+ vaderSentiment