Spaces:
Sleeping
Sleeping
Upload 5 files
Browse files
app.py
CHANGED
|
@@ -1,13 +1,12 @@
|
|
| 1 |
-
|
| 2 |
import gradio as gr
|
| 3 |
from gtts import gTTS
|
| 4 |
import tempfile
|
| 5 |
import json
|
| 6 |
import datetime
|
| 7 |
-
import random
|
| 8 |
import os
|
|
|
|
| 9 |
|
| 10 |
-
#
|
| 11 |
lang_codes = {
|
| 12 |
"english": "en", "hindi": "hi", "marathi": "mr", "bengali": "bn",
|
| 13 |
"tamil": "ta", "telugu": "te", "malayalam": "ml", "spanish": "es",
|
|
@@ -16,48 +15,35 @@ lang_codes = {
|
|
| 16 |
|
| 17 |
user_info = {"name": "", "age": "", "language": "english", "mood": ""}
|
| 18 |
chat_history = []
|
| 19 |
-
journal_entries = []
|
| 20 |
|
| 21 |
def welcome_screen():
|
| 22 |
-
return "π§ Welcome to StrongMind Therapist!
|
| 23 |
-
Your safe space to talk, heal, and grow."
|
| 24 |
|
| 25 |
def set_preferences(name, age, language, mood):
|
| 26 |
user_info["name"] = name
|
| 27 |
user_info["age"] = age
|
| 28 |
user_info["language"] = language.lower()
|
| 29 |
user_info["mood"] = mood
|
| 30 |
-
|
| 31 |
-
return greeting
|
| 32 |
|
| 33 |
def generate_reply(user_input):
|
| 34 |
user_input = user_input.lower()
|
|
|
|
| 35 |
if any(word in user_input for word in ["suicide", "kill", "die", "harm", "cut"]):
|
| 36 |
-
return (
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
return
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
return ("π§ When you're confused, take 3 deep breaths and write down small steps you can take. "
|
| 51 |
-
"Would you like help listing some ideas?")
|
| 52 |
-
|
| 53 |
-
if "joke" in user_input or "make me happy" in user_input:
|
| 54 |
-
return random.choice([
|
| 55 |
-
"π Why donβt scientists trust atoms? Because they make up everything!",
|
| 56 |
-
"π I told my computer I needed a break, and it said: βWhy? Iβm not the one overloaded!β",
|
| 57 |
-
"πΉ Why did the student eat his homework? Because the teacher said it was a piece of cake!"
|
| 58 |
-
])
|
| 59 |
-
|
| 60 |
-
return "Thanks for sharing. I'm listening, tell me more. π"
|
| 61 |
|
| 62 |
def get_therapist_reply(audio, text_input):
|
| 63 |
try:
|
|
@@ -93,40 +79,34 @@ def get_therapist_reply(audio, text_input):
|
|
| 93 |
except Exception as e:
|
| 94 |
return f"β οΈ Error: {str(e)}", None
|
| 95 |
|
| 96 |
-
def
|
| 97 |
-
|
| 98 |
-
"
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
"
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
])
|
| 113 |
-
|
| 114 |
-
# Gradio UI
|
| 115 |
-
with gr.Blocks(theme=gr.themes.Soft(), css="body { background-color: #ffe6f0 }") as app:
|
| 116 |
-
gr.Markdown("## πΈ StrongMind Therapist 2.0 Pro")
|
| 117 |
-
gr.Markdown(welcome_screen())
|
| 118 |
-
gr.Audio(value="calm.mp3", autoplay=True, visible=False)
|
| 119 |
|
| 120 |
with gr.Tab("π οΈ Start"):
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
|
|
|
| 125 |
start = gr.Button("β
Start Session")
|
| 126 |
welcome_msg = gr.Textbox(label="System Message")
|
| 127 |
start.click(set_preferences, inputs=[name, age, lang, mood], outputs=welcome_msg)
|
| 128 |
|
| 129 |
-
with gr.Tab("π¬
|
| 130 |
audio = gr.Audio(type="filepath", label="ποΈ Speak")
|
| 131 |
text_input = gr.Textbox(label="β¨οΈ Or type here")
|
| 132 |
btn = gr.Button("π§ Get Response")
|
|
@@ -134,22 +114,29 @@ with gr.Blocks(theme=gr.themes.Soft(), css="body { background-color: #ffe6f0 }")
|
|
| 134 |
voice_out = gr.Audio(label="π Voice Reply")
|
| 135 |
btn.click(get_therapist_reply, inputs=[audio, text_input], outputs=[response, voice_out])
|
| 136 |
|
| 137 |
-
with gr.Tab("
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
|
| 148 |
with gr.Tab("π Chat History"):
|
| 149 |
-
def show_history():
|
| 150 |
-
if not chat_history:
|
| 151 |
-
return "No chats yet."
|
| 152 |
-
return "\n\n".join([f"π {c['time']}\nYou: {c['user']}\nBot: {c['reply']}" for c in chat_history])
|
| 153 |
history_btn = gr.Button("π Load Past Chats")
|
| 154 |
history_output = gr.Textbox(label="ποΈ Your Past Conversations", lines=20)
|
| 155 |
history_btn.click(show_history, outputs=history_output)
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from gtts import gTTS
|
| 3 |
import tempfile
|
| 4 |
import json
|
| 5 |
import datetime
|
|
|
|
| 6 |
import os
|
| 7 |
+
import base64
|
| 8 |
|
| 9 |
+
# Language codes for voice support
|
| 10 |
lang_codes = {
|
| 11 |
"english": "en", "hindi": "hi", "marathi": "mr", "bengali": "bn",
|
| 12 |
"tamil": "ta", "telugu": "te", "malayalam": "ml", "spanish": "es",
|
|
|
|
| 15 |
|
| 16 |
user_info = {"name": "", "age": "", "language": "english", "mood": ""}
|
| 17 |
chat_history = []
|
|
|
|
| 18 |
|
| 19 |
def welcome_screen():
|
| 20 |
+
return "π§ Welcome to StrongMind Therapist!\nYour safe space to talk, heal, and grow."
|
|
|
|
| 21 |
|
| 22 |
def set_preferences(name, age, language, mood):
|
| 23 |
user_info["name"] = name
|
| 24 |
user_info["age"] = age
|
| 25 |
user_info["language"] = language.lower()
|
| 26 |
user_info["mood"] = mood
|
| 27 |
+
return f"Welcome {name}! Let's begin your therapy session in {language}. You said you're feeling {mood}. I'm here to listen."
|
|
|
|
| 28 |
|
| 29 |
def generate_reply(user_input):
|
| 30 |
user_input = user_input.lower()
|
| 31 |
+
|
| 32 |
if any(word in user_input for word in ["suicide", "kill", "die", "harm", "cut"]):
|
| 33 |
+
return (
|
| 34 |
+
"π I believe you're very hurt, but I can't provide help in this situation.\n"
|
| 35 |
+
"Please contact a professional therapist:\n"
|
| 36 |
+
"π Dr. Meera Kapoor β +91 9876543210\n"
|
| 37 |
+
"π Dr. Arjun Das β +91 9123456780\nYou are not alone π"
|
| 38 |
+
)
|
| 39 |
+
elif any(word in user_input for word in ["sad", "anxiety", "angry", "unwell", "hate", "stress"]):
|
| 40 |
+
return "Hey, no need to worry. I'm here for you. Just tell me more β weβll figure it out together. π§‘"
|
| 41 |
+
elif any(word in user_input for word in ["what should i do", "advice", "guide", "suggest"]):
|
| 42 |
+
return "π§ When you're confused, take 3 deep breaths and write down small steps you can take. Would you like help listing ideas?"
|
| 43 |
+
elif "joke" in user_input or "make me happy" in user_input:
|
| 44 |
+
return "π Why don't scientists trust atoms? Because they make up everything!"
|
| 45 |
+
else:
|
| 46 |
+
return "Thanks for sharing. I'm here with you. You can keep talking β Iβm listening."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
def get_therapist_reply(audio, text_input):
|
| 49 |
try:
|
|
|
|
| 79 |
except Exception as e:
|
| 80 |
return f"β οΈ Error: {str(e)}", None
|
| 81 |
|
| 82 |
+
def show_history():
|
| 83 |
+
if not chat_history:
|
| 84 |
+
return "No chats yet."
|
| 85 |
+
return "\n".join([f"π {c['time']}\nYou: {c['user']}\nBot: {c['reply']}\n" for c in chat_history])
|
| 86 |
+
|
| 87 |
+
with gr.Blocks(css="body {background-color: #ffe6f0;}") as app:
|
| 88 |
+
with open("calm.mp3", "rb") as f:
|
| 89 |
+
audio_bytes = f.read()
|
| 90 |
+
encoded = base64.b64encode(audio_bytes).decode()
|
| 91 |
+
gr.HTML(f"""
|
| 92 |
+
<audio autoplay loop hidden>
|
| 93 |
+
<source src="data:audio/mp3;base64,{encoded}" type="audio/mp3">
|
| 94 |
+
</audio>
|
| 95 |
+
""")
|
| 96 |
+
|
| 97 |
+
gr.Markdown("## πΏ StrongMind Therapist 2.0")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
with gr.Tab("π οΈ Start"):
|
| 100 |
+
gr.Markdown(welcome_screen())
|
| 101 |
+
name = gr.Textbox(label="π€ Your Name")
|
| 102 |
+
age = gr.Textbox(label="π Your Age")
|
| 103 |
+
lang = gr.Dropdown(choices=list(lang_codes.keys()), label="π Preferred Language")
|
| 104 |
+
mood = gr.Textbox(label="π§ How are you feeling today?")
|
| 105 |
start = gr.Button("β
Start Session")
|
| 106 |
welcome_msg = gr.Textbox(label="System Message")
|
| 107 |
start.click(set_preferences, inputs=[name, age, lang, mood], outputs=welcome_msg)
|
| 108 |
|
| 109 |
+
with gr.Tab("π¬ Chat"):
|
| 110 |
audio = gr.Audio(type="filepath", label="ποΈ Speak")
|
| 111 |
text_input = gr.Textbox(label="β¨οΈ Or type here")
|
| 112 |
btn = gr.Button("π§ Get Response")
|
|
|
|
| 114 |
voice_out = gr.Audio(label="π Voice Reply")
|
| 115 |
btn.click(get_therapist_reply, inputs=[audio, text_input], outputs=[response, voice_out])
|
| 116 |
|
| 117 |
+
with gr.Tab("π Journal"):
|
| 118 |
+
journal = gr.Textbox(label="βοΈ Write your thoughts", lines=8)
|
| 119 |
+
save_journal = gr.Button("πΎ Save Entry")
|
| 120 |
+
status = gr.Textbox(label="ποΈ Status")
|
| 121 |
+
def save_entry(text):
|
| 122 |
+
with open("journal.txt", "a", encoding="utf-8") as f:
|
| 123 |
+
f.write(f"{datetime.datetime.now()}\n{text}\n\n")
|
| 124 |
+
return "Saved to journal.txt β
"
|
| 125 |
+
save_journal.click(save_entry, inputs=journal, outputs=status)
|
| 126 |
+
|
| 127 |
+
with gr.Tab("π Study Tips"):
|
| 128 |
+
def tips():
|
| 129 |
+
return (
|
| 130 |
+
"π§ Study Tips:\n"
|
| 131 |
+
"1. Pomodoro method: 25 min study, 5 min break\n"
|
| 132 |
+
"2. Eliminate distractions (put phone away)\n"
|
| 133 |
+
"3. Summarize what you learned out loud\n"
|
| 134 |
+
"4. Stay hydrated and eat light snacks\n"
|
| 135 |
+
"5. Sleep well!"
|
| 136 |
+
)
|
| 137 |
+
gr.Button("π Show Tips").click(tips, outputs=gr.Textbox(lines=8, label="Tips"))
|
| 138 |
|
| 139 |
with gr.Tab("π Chat History"):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
history_btn = gr.Button("π Load Past Chats")
|
| 141 |
history_output = gr.Textbox(label="ποΈ Your Past Conversations", lines=20)
|
| 142 |
history_btn.click(show_history, outputs=history_output)
|
calm.mp3
ADDED
|
File without changes
|