iamstrong commited on
Commit
c4ee7a9
·
verified ·
1 Parent(s): a4671fc

Upload 3 files

Browse files
Files changed (2) hide show
  1. app.py +123 -101
  2. requirements.txt +7 -2
app.py CHANGED
@@ -8,28 +8,31 @@ import datetime
8
  user_info = {"name": "", "age": "", "gender": "", "language": "english"}
9
  chat_history = []
10
  journal_entries = []
 
11
  calm_tips = [
12
  "Take 3 deep breaths.", "Listen to nature.", "Stretch your body.",
13
  "Drink water.", "Think of one good thing today.", "Close your eyes for 1 minute.",
14
  "Write your feelings.", "Smile at yourself.", "Imagine a peaceful place.",
15
  "Say a positive affirmation."
16
  ]
 
17
  study_tips = [
18
  "Use Pomodoro: 25min study, 5min break", "Make a daily to-do list",
19
  "Avoid multitasking", "Use color-coded notes", "Take 10-min exercise breaks",
20
  "Sleep 7–9 hrs daily", "Drink water during study", "Use active recall",
21
  "Study hardest topics first", "Test yourself often"
22
  ]
 
23
  tip_index = {"calm": 0, "study": 0}
 
24
  lang_codes = {
25
  "english": "en", "hindi": "hi", "marathi": "mr", "bengali": "bn",
26
  "tamil": "ta", "telugu": "te", "malayalam": "ml", "spanish": "es",
27
  "french": "fr", "german": "de"
28
  }
29
 
30
- # Emotions dictionary (shortened for display here; your original full list still applies)
31
  emotions = {
32
- "sad": "It's okay to feel sad sometimes 🌧️", "anxiety": "Try breathing slowly 🧘",
33
  "angry": "Take deep breaths. Anger is natural.", "lonely": "I'm here for you 💚",
34
  "exams": "Let’s create a quick plan!", "friend": "Want to talk about them?",
35
  "tired": "You’ve been carrying a lot. Rest isn’t lazy — it’s necessary 😴",
@@ -130,63 +133,41 @@ emotions = {
130
  "crushed": "That sounds painful. Let’s unpack it together.",
131
  "troubled": "Name the trouble, then tame it. I’m here.",
132
  "underestimated": "You are more than they think. Show them. Gently.",
133
- "burned out": "Burnout needs healing. Step back to move forward."
134
-
135
-
136
  }
137
 
 
138
  def set_personal_info(name, age, gender, language):
139
  user_info.update({"name": name, "age": age, "gender": gender, "language": language})
140
  return f"Welcome {name}! Preferences saved."
141
 
 
142
  def show_personal_data():
143
  today = datetime.date.today().strftime("%Y-%m-%d (%A)")
144
- return f"📅 {today}\n👤 Name: {user_info['name']}\n🎂 Age: {user_info['age']}\n Gender: {user_info['gender']}\n🌐 Language: {user_info['language']}"
145
 
 
146
  def generate_reply(input_text):
147
  text = input_text.lower()
148
-
149
- crisis_words = {
150
- "harm", "cut", "die", "kill", "suicide", "end it", "hurt myself",
151
- "self-harm", "jump", "knife", "bleed", "worth ending", "give up",
152
- "no reason", "nothing matters", "nerves", "overdose"
153
- }
154
-
155
- if any(word in text for word in crisis_words):
156
- return (
157
- "⚠️ It sounds like you're going through a really tough time.\n\n"
158
- "Please reach out to a professional:\n\n"
159
- "🇮🇳 **India Helplines**\n"
160
- "🧠 *Dr. Rachna Khanna Singh* – +91 99103 90559\n"
161
- "📞 *iCall Helpline* – +91 9152987821\n"
162
- "📞 *Vandrevala Foundation* – 1860 266 2345 or 1800 233 3330\n\n"
163
- "🌍 **International Helplines**\n"
164
- "📞 *Lifeline (USA)* – 988\n"
165
- "📞 *Samaritans (UK)* – 116 123\n"
166
- "📞 *Lifeline (Australia)* – 13 11 14\n\n"
167
- "You are not alone. There are people who care and want to help 💚"
168
- )
169
-
170
  for word, reply in emotions.items():
171
  if word in text:
172
  return reply
173
  return "Tell me more about your day."
174
 
 
175
  def chat_function(audio_input, text_input):
176
- if audio_input:
177
- import whisper
178
- model = whisper.load_model("base")
179
- result = model.transcribe(audio_input)
180
- user_text = result["text"]
181
- else:
182
- user_text = text_input
183
 
184
  reply = generate_reply(user_text)
185
  chat_history.append({"user": user_text, "bot": reply})
 
186
  lang_code = lang_codes.get(user_info["language"], "en")
187
  tts = gTTS(reply, lang=lang_code)
188
  audio_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
189
  tts.save(audio_file.name)
 
190
  return reply, audio_file.name
191
 
192
  def get_chat_history():
@@ -213,71 +194,112 @@ def next_study_tip():
213
  tip_index["study"] += 1
214
  return tip
215
 
216
- with gr.Blocks(title="StrongMind Therapist 2.0") as app:
217
- with gr.Tab("1️⃣ Personal Info"):
218
- name = gr.Textbox(label="Name")
219
- age = gr.Textbox(label="Age")
220
- gender = gr.Dropdown(["Male", "Female", "Other"], label="Gender")
221
- language = gr.Dropdown(list(lang_codes.keys()), label="Preferred Language")
222
- btn = gr.Button("Save Info")
223
- output = gr.Textbox()
224
- btn.click(set_personal_info, [name, age, gender, language], output)
225
-
226
- with gr.Tab("2️⃣ Personal Info Data"):
227
- show = gr.Button("Show My Info")
228
- info_display = gr.Textbox(lines=6)
229
- show.click(show_personal_data, outputs=info_display)
230
-
231
- with gr.Tab("3️⃣ Chat"):
232
- gr.Markdown("🗣️ Describe your day in one word.")
233
- audio_input = gr.Audio(type="filepath", label="🎙️ Say something")
234
- text_input = gr.Textbox(label="⌨️ Or type here")
235
- send = gr.Button("Send")
236
- bot_reply = gr.Textbox(label="🧠 Therapist")
237
- voice = gr.Audio(label="🔊 Voice Reply")
238
- send.click(chat_function, [audio_input, text_input], [bot_reply, voice])
239
-
240
- with gr.Tab("4️⃣ Chat History"):
241
- show_history = gr.Button("📜 Show Chats")
242
- chat_out = gr.Textbox(lines=20, label="History")
243
- show_history.click(get_chat_history, outputs=chat_out)
244
-
245
- with gr.Tab("5️⃣ Journal"):
246
- journal_input = gr.Textbox(lines=6, label="Write your thoughts")
247
- save = gr.Button("Save")
248
- journal_status = gr.Textbox()
249
- save.click(save_journal, journal_input, journal_status)
250
-
251
- with gr.Tab("6️⃣ Journal History"):
252
- view = gr.Button("View Past Entries")
253
- past = gr.Textbox(lines=15, label="Previous Journals")
254
- view.click(show_journal_history, outputs=past)
255
-
256
- with gr.Tab("7️⃣ Calm Space"):
257
- tip_btn = gr.Button("🌿 Give Me a Calm Tip")
258
- calm_text = gr.Textbox()
259
- tip_btn.click(next_calm_tip, outputs=calm_text)
260
-
261
- with gr.Tab("8️⃣ Study Tips"):
262
- tip_btn2 = gr.Button("📚 Study Tip")
263
- study_text = gr.Textbox()
264
- tip_btn2.click(next_study_tip, outputs=study_text)
265
-
266
- with gr.Tab("9️⃣ Pomodoro"):
267
- gr.Markdown("Use 25 min study + 5 min break cycles.\n(For real timer integration, use front-end JS)")
268
-
269
- with gr.Tab("🔟 Games"):
270
- gr.Markdown("🎮 Play from the game portal below:")
271
- gr.HTML(
272
- '''
273
- <iframe
274
- src="https://www.onlinegames.io/embed/portal/"
275
- width="100%"
276
- height="1000"
277
- frameborder="0"
278
- allowfullscreen>
279
- </iframe>
280
- '''
281
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
282
 
283
  app.launch()
 
8
  user_info = {"name": "", "age": "", "gender": "", "language": "english"}
9
  chat_history = []
10
  journal_entries = []
11
+
12
  calm_tips = [
13
  "Take 3 deep breaths.", "Listen to nature.", "Stretch your body.",
14
  "Drink water.", "Think of one good thing today.", "Close your eyes for 1 minute.",
15
  "Write your feelings.", "Smile at yourself.", "Imagine a peaceful place.",
16
  "Say a positive affirmation."
17
  ]
18
+
19
  study_tips = [
20
  "Use Pomodoro: 25min study, 5min break", "Make a daily to-do list",
21
  "Avoid multitasking", "Use color-coded notes", "Take 10-min exercise breaks",
22
  "Sleep 7–9 hrs daily", "Drink water during study", "Use active recall",
23
  "Study hardest topics first", "Test yourself often"
24
  ]
25
+
26
  tip_index = {"calm": 0, "study": 0}
27
+
28
  lang_codes = {
29
  "english": "en", "hindi": "hi", "marathi": "mr", "bengali": "bn",
30
  "tamil": "ta", "telugu": "te", "malayalam": "ml", "spanish": "es",
31
  "french": "fr", "german": "de"
32
  }
33
 
 
34
  emotions = {
35
+ "sad": "It's okay to feel sad sometimes 🌧️", "anxiety": "Try breathing slowly 🧘",
36
  "angry": "Take deep breaths. Anger is natural.", "lonely": "I'm here for you 💚",
37
  "exams": "Let’s create a quick plan!", "friend": "Want to talk about them?",
38
  "tired": "You’ve been carrying a lot. Rest isn’t lazy — it’s necessary 😴",
 
133
  "crushed": "That sounds painful. Let’s unpack it together.",
134
  "troubled": "Name the trouble, then tame it. I’m here.",
135
  "underestimated": "You are more than they think. Show them. Gently.",
136
+ "Burned out": "Burnout needs healing. Step back to move forward."
 
 
137
  }
138
 
139
+ # Save personal information
140
  def set_personal_info(name, age, gender, language):
141
  user_info.update({"name": name, "age": age, "gender": gender, "language": language})
142
  return f"Welcome {name}! Preferences saved."
143
 
144
+ # Display user info
145
  def show_personal_data():
146
  today = datetime.date.today().strftime("%Y-%m-%d (%A)")
147
+ return f"📅 {today}\n👤 Name: {user_info['name']}\n🎂 Age: {user_info['age']}\n Gender: {user_info['gender']}\n🌐 Language: {user_info['language']}"
148
 
149
+ # Detect emotion from text
150
  def generate_reply(input_text):
151
  text = input_text.lower()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
152
  for word, reply in emotions.items():
153
  if word in text:
154
  return reply
155
  return "Tell me more about your day."
156
 
157
+ # Chat function
158
  def chat_function(audio_input, text_input):
159
+ user_text = text_input.strip()
160
+ if not user_text:
161
+ return "Please type something.", None
 
 
 
 
162
 
163
  reply = generate_reply(user_text)
164
  chat_history.append({"user": user_text, "bot": reply})
165
+
166
  lang_code = lang_codes.get(user_info["language"], "en")
167
  tts = gTTS(reply, lang=lang_code)
168
  audio_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
169
  tts.save(audio_file.name)
170
+
171
  return reply, audio_file.name
172
 
173
  def get_chat_history():
 
194
  tip_index["study"] += 1
195
  return tip
196
 
197
+ # 🧠 Main App
198
+ with gr.Blocks(css="""
199
+ #main-title {
200
+ font-size: 36px;
201
+ font-weight: bold;
202
+ margin-top: 10px;
203
+ color: white;
204
+ text-align: center;
205
+ }
206
+ #subtitle {
207
+ font-size: 20px;
208
+ font-weight: normal;
209
+ margin-bottom: 20px;
210
+ color: white;
211
+ text-align: center;
212
+ }
213
+ #start-btn {
214
+ display: block;
215
+ margin: 0 auto;
216
+ background-color: pink;
217
+ color: black;
218
+ font-size: 18px;
219
+ padding: 12px 24px;
220
+ border-radius: 12px;
221
+ border: none;
222
+ }
223
+ """) as app:
224
+
225
+ welcome_screen = gr.Column(visible=True)
226
+ full_app = gr.Tabs(visible=False)
227
+
228
+ with welcome_screen:
229
+ gr.Markdown("<h1 id='main-title'>StrongMind Therapist 2.0</h1>")
230
+ gr.Markdown("<h3 id='subtitle'>Your peaceful space to talk, journal, and focus.</h3>")
231
+ start_button = gr.Button("🌸 Get Started", elem_id="start-btn")
232
+
233
+ def start_app():
234
+ return gr.update(visible=False), gr.update(visible=True)
235
+
236
+ start_button.click(start_app, outputs=[welcome_screen, full_app])
237
+
238
+ with full_app:
239
+ with gr.Tab("1️⃣ Personal Info"):
240
+ name = gr.Textbox(label="Name")
241
+ age = gr.Textbox(label="Age")
242
+ gender = gr.Dropdown(["Male", "Female", "Other"], label="Gender")
243
+ language = gr.Dropdown(list(lang_codes.keys()), label="Preferred Language")
244
+ btn = gr.Button("Save Info")
245
+ output = gr.Textbox()
246
+ btn.click(set_personal_info, [name, age, gender, language], output)
247
+
248
+ with gr.Tab("2 Personal Info Data"):
249
+ show = gr.Button("Show My Info")
250
+ info_display = gr.Textbox(lines=6)
251
+ show.click(show_personal_data, outputs=info_display)
252
+
253
+ with gr.Tab("3️⃣ Chat"):
254
+ gr.Markdown("🗣️ Describe your day in one word.")
255
+ audio_input = gr.Audio(type="filepath", label="🎧 Say something")
256
+ text_input = gr.Textbox(label="⌨️ Or type here")
257
+ send = gr.Button("Send")
258
+ bot_reply = gr.Textbox(label="🧠 Therapist")
259
+ voice = gr.Audio(label="🔊 Voice Reply")
260
+ send.click(chat_function, [audio_input, text_input], [bot_reply, voice])
261
+
262
+ with gr.Tab("4️⃣ Chat History"):
263
+ show_history = gr.Button("📜 Show Chats")
264
+ chat_out = gr.Textbox(lines=20, label="History")
265
+ show_history.click(get_chat_history, outputs=chat_out)
266
+
267
+ with gr.Tab("5️⃣ Journal"):
268
+ journal_input = gr.Textbox(lines=6, label="Write your thoughts")
269
+ save = gr.Button("Save")
270
+ journal_status = gr.Textbox()
271
+ save.click(save_journal, journal_input, journal_status)
272
+
273
+ with gr.Tab("6️⃣ Journal History"):
274
+ view = gr.Button("View Past Entries")
275
+ past = gr.Textbox(lines=15, label="Previous Journals")
276
+ view.click(show_journal_history, outputs=past)
277
+
278
+ with gr.Tab("7️⃣ Calm Space"):
279
+ tip_btn = gr.Button("🌿 Give Me a Calm Tip")
280
+ calm_text = gr.Textbox()
281
+ tip_btn.click(next_calm_tip, outputs=calm_text)
282
+
283
+ with gr.Tab("8️⃣ Study Tips"):
284
+ tip_btn2 = gr.Button("📚 Study Tip")
285
+ study_text = gr.Textbox()
286
+ tip_btn2.click(next_study_tip, outputs=study_text)
287
+
288
+ with gr.Tab("9️⃣ Pomodoro"):
289
+ gr.Markdown("⏱️ Use 25 min study + 5 min break cycles.\n(For real timer, use front-end JavaScript or Android timers)")
290
+
291
+ with gr.Tab("💠 Games"):
292
+ gr.Markdown("🎮 Play from the game portal below:")
293
+ gr.HTML(
294
+ '''
295
+ <iframe
296
+ src="https://www.onlinegames.io/embed/portal/"
297
+ width="100%"
298
+ height="600"
299
+ frameborder="0"
300
+ allowfullscreen>
301
+ </iframe>
302
+ '''
303
+ )
304
 
305
  app.launch()
requirements.txt CHANGED
@@ -1,2 +1,7 @@
1
- gradio
2
- gtts
 
 
 
 
 
 
1
+ gradio==4.26.0
2
+ gtts==2.5.1
3
+ openai==1.30.1
4
+ whisper
5
+ torch
6
+ ffmpeg-python
7
+ SpeechRecognition==3.10.0