Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files
app.py
CHANGED
|
@@ -1,37 +1,64 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from gtts import gTTS
|
| 3 |
import tempfile
|
| 4 |
import json
|
| 5 |
import datetime
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
#
|
| 8 |
lang_codes = {
|
| 9 |
"english": "en", "hindi": "hi", "marathi": "mr", "bengali": "bn",
|
| 10 |
"tamil": "ta", "telugu": "te", "malayalam": "ml", "spanish": "es",
|
| 11 |
"french": "fr", "german": "de"
|
| 12 |
}
|
| 13 |
|
| 14 |
-
# π§ Store user info and chat history
|
| 15 |
user_info = {"name": "", "age": "", "language": "english", "mood": ""}
|
| 16 |
chat_history = []
|
|
|
|
| 17 |
|
| 18 |
-
# πΈ Welcome screen message
|
| 19 |
def welcome_screen():
|
| 20 |
-
return "π§ Welcome to StrongMind Therapist!
|
|
|
|
| 21 |
|
| 22 |
-
# β
Set user preferences
|
| 23 |
def set_preferences(name, age, language, mood):
|
| 24 |
user_info["name"] = name
|
| 25 |
user_info["age"] = age
|
| 26 |
user_info["language"] = language.lower()
|
| 27 |
user_info["mood"] = mood
|
| 28 |
-
greeting =
|
| 29 |
-
f"Welcome {name}! Let's begin your therapy session in {language.title()}. "
|
| 30 |
-
f"You said you're feeling {mood}. Iβm here to support you π±"
|
| 31 |
-
)
|
| 32 |
return greeting
|
| 33 |
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
def get_therapist_reply(audio, text_input):
|
| 36 |
try:
|
| 37 |
if audio:
|
|
@@ -42,11 +69,10 @@ def get_therapist_reply(audio, text_input):
|
|
| 42 |
elif text_input:
|
| 43 |
user_text = text_input
|
| 44 |
else:
|
| 45 |
-
return "Please
|
| 46 |
|
| 47 |
reply = generate_reply(user_text)
|
| 48 |
|
| 49 |
-
# πΎ Save chat
|
| 50 |
chat_entry = {
|
| 51 |
"user": user_text,
|
| 52 |
"reply": reply,
|
|
@@ -58,7 +84,6 @@ def get_therapist_reply(audio, text_input):
|
|
| 58 |
with open("chat_history.json", "w") as f:
|
| 59 |
json.dump(chat_history, f, indent=4)
|
| 60 |
|
| 61 |
-
# π§ Convert reply to voice
|
| 62 |
lang_code = lang_codes.get(user_info["language"], "en")
|
| 63 |
tts = gTTS(text=reply, lang=lang_code)
|
| 64 |
audio_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
|
|
@@ -68,69 +93,65 @@ def get_therapist_reply(audio, text_input):
|
|
| 68 |
except Exception as e:
|
| 69 |
return f"β οΈ Error: {str(e)}", None
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
if any(word in user_input for word in ["joke", "make me happy", "funny"]):
|
| 93 |
-
return "π Why donβt eggs tell each other secrets? Because they might crack up!"
|
| 94 |
-
|
| 95 |
-
return "πͺ· Thank you for sharing. You are brave. Keep going, I'm right here listening."
|
| 96 |
-
|
| 97 |
-
# π¨ Build the Gradio interface
|
| 98 |
-
with gr.Blocks(theme=gr.themes.Base(primary_hue="pink", secondary_hue="pink")) as app:
|
| 99 |
-
gr.Markdown("## π§ StrongMind Therapist")
|
| 100 |
gr.Markdown(welcome_screen())
|
|
|
|
| 101 |
|
| 102 |
-
# ποΈ Preference tab
|
| 103 |
with gr.Tab("π οΈ Start"):
|
| 104 |
-
name = gr.Textbox(label="
|
| 105 |
-
age = gr.Textbox(label="
|
| 106 |
-
lang = gr.Dropdown(choices=list(lang_codes.keys()), label="
|
| 107 |
-
mood = gr.Textbox(label="
|
| 108 |
start = gr.Button("β
Start Session")
|
| 109 |
-
welcome_msg = gr.Textbox(label="
|
| 110 |
start.click(set_preferences, inputs=[name, age, lang, mood], outputs=welcome_msg)
|
| 111 |
|
| 112 |
-
|
| 113 |
-
with gr.Tab("π¬ Chat"):
|
| 114 |
audio = gr.Audio(type="filepath", label="ποΈ Speak")
|
| 115 |
-
text_input = gr.Textbox(label="β¨οΈ Or type
|
| 116 |
btn = gr.Button("π§ Get Response")
|
| 117 |
response = gr.Textbox(label="π¬ Therapist's Reply")
|
| 118 |
voice_out = gr.Audio(label="π Voice Reply")
|
| 119 |
btn.click(get_therapist_reply, inputs=[audio, text_input], outputs=[response, voice_out])
|
| 120 |
|
| 121 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
with gr.Tab("π Chat History"):
|
| 123 |
def show_history():
|
| 124 |
if not chat_history:
|
| 125 |
return "No chats yet."
|
| 126 |
-
return "\n".join([
|
| 127 |
-
f"π {c['time']}\nYou: {c['user']}\nBot: {c['reply']}\n" for c in chat_history
|
| 128 |
-
])
|
| 129 |
history_btn = gr.Button("π Load Past Chats")
|
| 130 |
-
history_output = gr.Textbox(label="
|
| 131 |
history_btn.click(show_history, outputs=history_output)
|
| 132 |
|
| 133 |
-
# πΏ Background music (optional hidden audio player)
|
| 134 |
-
gr.Audio(value="calm_nature.mp3", autoplay=True, visible=False)
|
| 135 |
-
|
| 136 |
app.launch()
|
|
|
|
| 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 |
+
# Supported languages and codes
|
| 11 |
lang_codes = {
|
| 12 |
"english": "en", "hindi": "hi", "marathi": "mr", "bengali": "bn",
|
| 13 |
"tamil": "ta", "telugu": "te", "malayalam": "ml", "spanish": "es",
|
| 14 |
"french": "fr", "german": "de"
|
| 15 |
}
|
| 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 |
+
greeting = f"Welcome {name}! Let's begin your therapy session in {language}. You said you're feeling {mood}. I'm here to listen."
|
|
|
|
|
|
|
|
|
|
| 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 ("π I believe you're very hurt, but I can't provide help in this situation.\n"
|
| 37 |
+
"Please contact a professional therapist:\n"
|
| 38 |
+
"π Dr. Meera Kapoor β +91 9876543210\n"
|
| 39 |
+
"π Dr. Arjun Das β +91 9123456780\nYou are not alone π")
|
| 40 |
+
|
| 41 |
+
if any(word in user_input for word in ["sad", "anxiety", "angry", "unwell", "hate", "stress"]):
|
| 42 |
+
responses = [
|
| 43 |
+
"Hey, itβs okay to feel this way. Iβm right here. Let's talk. π»",
|
| 44 |
+
"Donβt worry, weβll get through this together. Tell me whatβs bothering you. π",
|
| 45 |
+
"I'm here for you. You can tell me anything. π¬"
|
| 46 |
+
]
|
| 47 |
+
return random.choice(responses)
|
| 48 |
+
|
| 49 |
+
if any(word in user_input for word in ["what should i do", "advice", "guide", "suggest"]):
|
| 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:
|
| 64 |
if audio:
|
|
|
|
| 69 |
elif text_input:
|
| 70 |
user_text = text_input
|
| 71 |
else:
|
| 72 |
+
return "Please say or type something to begin.", None
|
| 73 |
|
| 74 |
reply = generate_reply(user_text)
|
| 75 |
|
|
|
|
| 76 |
chat_entry = {
|
| 77 |
"user": user_text,
|
| 78 |
"reply": reply,
|
|
|
|
| 84 |
with open("chat_history.json", "w") as f:
|
| 85 |
json.dump(chat_history, f, indent=4)
|
| 86 |
|
|
|
|
| 87 |
lang_code = lang_codes.get(user_info["language"], "en")
|
| 88 |
tts = gTTS(text=reply, lang=lang_code)
|
| 89 |
audio_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
|
|
|
|
| 93 |
except Exception as e:
|
| 94 |
return f"β οΈ Error: {str(e)}", None
|
| 95 |
|
| 96 |
+
def save_journal(text):
|
| 97 |
+
journal_entries.append({
|
| 98 |
+
"entry": text,
|
| 99 |
+
"time": datetime.datetime.now().isoformat()
|
| 100 |
+
})
|
| 101 |
+
with open("journal.json", "w") as f:
|
| 102 |
+
json.dump(journal_entries, f, indent=2)
|
| 103 |
+
return "π Journal saved!"
|
| 104 |
+
|
| 105 |
+
def study_tips():
|
| 106 |
+
return "\n".join([
|
| 107 |
+
"π Break tasks into small parts",
|
| 108 |
+
"β° Take a 5-min break every 25 mins (Pomodoro)",
|
| 109 |
+
"π§ Do breathing before starting work",
|
| 110 |
+
"π΄ Keep phone away while studying",
|
| 111 |
+
"β
Reward yourself after completing tasks"
|
| 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 |
+
name = gr.Textbox(label="Your Name")
|
| 122 |
+
age = gr.Textbox(label="Your Age")
|
| 123 |
+
lang = gr.Dropdown(choices=list(lang_codes.keys()), label="Preferred Language")
|
| 124 |
+
mood = gr.Textbox(label="How are you feeling today?")
|
| 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("π¬ Therapist Chat"):
|
|
|
|
| 130 |
audio = gr.Audio(type="filepath", label="ποΈ Speak")
|
| 131 |
+
text_input = gr.Textbox(label="β¨οΈ Or type here")
|
| 132 |
btn = gr.Button("π§ Get Response")
|
| 133 |
response = gr.Textbox(label="π¬ Therapist's Reply")
|
| 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("π Journaling"):
|
| 138 |
+
journal_text = gr.Textbox(label="Write your thoughts", lines=5)
|
| 139 |
+
journal_btn = gr.Button("Save Journal")
|
| 140 |
+
journal_status = gr.Textbox(label="Status")
|
| 141 |
+
journal_btn.click(save_journal, inputs=journal_text, outputs=journal_status)
|
| 142 |
+
|
| 143 |
+
with gr.Tab("π Study Tips"):
|
| 144 |
+
tip_btn = gr.Button("Get Tips")
|
| 145 |
+
tip_output = gr.Textbox(label="Focus & Study Tips")
|
| 146 |
+
tip_btn.click(fn=study_tips, outputs=tip_output)
|
| 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)
|
| 156 |
|
|
|
|
|
|
|
|
|
|
| 157 |
app.launch()
|