Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files
app.py
CHANGED
|
@@ -1,29 +1,37 @@
|
|
| 1 |
-
|
| 2 |
import gradio as gr
|
| 3 |
from gtts import gTTS
|
| 4 |
import tempfile
|
| 5 |
import json
|
| 6 |
import datetime
|
| 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 |
user_info = {"name": "", "age": "", "language": "english", "mood": ""}
|
| 15 |
chat_history = []
|
| 16 |
|
|
|
|
| 17 |
def welcome_screen():
|
| 18 |
-
return "π§ Welcome to StrongMind Therapist!\nYour
|
|
|
|
|
|
|
| 19 |
def set_preferences(name, age, language, mood):
|
| 20 |
user_info["name"] = name
|
| 21 |
user_info["age"] = age
|
| 22 |
user_info["language"] = language.lower()
|
| 23 |
user_info["mood"] = mood
|
| 24 |
-
greeting =
|
|
|
|
|
|
|
|
|
|
| 25 |
return greeting
|
| 26 |
|
|
|
|
| 27 |
def get_therapist_reply(audio, text_input):
|
| 28 |
try:
|
| 29 |
if audio:
|
|
@@ -34,10 +42,11 @@ def get_therapist_reply(audio, text_input):
|
|
| 34 |
elif text_input:
|
| 35 |
user_text = text_input
|
| 36 |
else:
|
| 37 |
-
return "Please
|
| 38 |
|
| 39 |
reply = generate_reply(user_text)
|
| 40 |
|
|
|
|
| 41 |
chat_entry = {
|
| 42 |
"user": user_text,
|
| 43 |
"reply": reply,
|
|
@@ -49,6 +58,7 @@ def get_therapist_reply(audio, text_input):
|
|
| 49 |
with open("chat_history.json", "w") as f:
|
| 50 |
json.dump(chat_history, f, indent=4)
|
| 51 |
|
|
|
|
| 52 |
lang_code = lang_codes.get(user_info["language"], "en")
|
| 53 |
tts = gTTS(text=reply, lang=lang_code)
|
| 54 |
audio_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
|
|
@@ -58,58 +68,69 @@ def get_therapist_reply(audio, text_input):
|
|
| 58 |
except Exception as e:
|
| 59 |
return f"β οΈ Error: {str(e)}", None
|
| 60 |
|
|
|
|
| 61 |
def generate_reply(user_input):
|
| 62 |
user_input = user_input.lower()
|
|
|
|
| 63 |
if any(word in user_input for word in ["suicide", "kill", "die", "harm", "cut"]):
|
| 64 |
return (
|
| 65 |
-
"π I believe you're very hurt, but I can't provide help in this situation.
|
| 66 |
-
"
|
| 67 |
-
"
|
| 68 |
-
"
|
| 69 |
-
"π Dr. Meera Kapoor β +91 9876543210
|
| 70 |
-
"
|
| 71 |
-
"π Dr. Arjun Das β +91 9123456780
|
| 72 |
-
You are not alone π"
|
| 73 |
)
|
|
|
|
| 74 |
if any(word in user_input for word in ["sad", "anxiety", "angry", "unwell", "hate", "stress"]):
|
| 75 |
-
return "
|
|
|
|
| 76 |
if any(word in user_input for word in ["what should i do", "advice", "guide", "suggest"]):
|
| 77 |
return (
|
| 78 |
-
"
|
| 79 |
-
"Would you like help listing
|
| 80 |
)
|
| 81 |
-
if "joke" in user_input or "make me happy" in user_input:
|
| 82 |
-
return "π Why don't scientists trust atoms? Because they make up everything!"
|
| 83 |
-
return "Thank you for sharing. I'm here with you. You can keep talking β Iβm listening."
|
| 84 |
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
|
|
|
| 88 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
with gr.Tab("π οΈ Start"):
|
| 90 |
-
name = gr.Textbox(label="Your Name")
|
| 91 |
-
age = gr.Textbox(label="Your Age")
|
| 92 |
-
lang = gr.Dropdown(choices=list(lang_codes.keys()), label="Preferred Language")
|
| 93 |
-
mood = gr.Textbox(label="How are you feeling today?")
|
| 94 |
start = gr.Button("β
Start Session")
|
| 95 |
-
welcome_msg = gr.Textbox(label="System Message")
|
| 96 |
start.click(set_preferences, inputs=[name, age, lang, mood], outputs=welcome_msg)
|
| 97 |
|
|
|
|
| 98 |
with gr.Tab("π¬ Chat"):
|
| 99 |
audio = gr.Audio(type="filepath", label="ποΈ Speak")
|
| 100 |
-
text_input = gr.Textbox(label="β¨οΈ Or type
|
| 101 |
btn = gr.Button("π§ Get Response")
|
| 102 |
response = gr.Textbox(label="π¬ Therapist's Reply")
|
| 103 |
voice_out = gr.Audio(label="π Voice Reply")
|
| 104 |
btn.click(get_therapist_reply, inputs=[audio, text_input], outputs=[response, voice_out])
|
| 105 |
|
|
|
|
| 106 |
with gr.Tab("π Chat History"):
|
| 107 |
def show_history():
|
| 108 |
if not chat_history:
|
| 109 |
return "No chats yet."
|
| 110 |
-
return "\n".join([
|
|
|
|
|
|
|
| 111 |
history_btn = gr.Button("π Load Past Chats")
|
| 112 |
-
history_output = gr.Textbox(label="
|
| 113 |
history_btn.click(show_history, outputs=history_output)
|
| 114 |
|
|
|
|
|
|
|
|
|
|
| 115 |
app.launch()
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from gtts import gTTS
|
| 3 |
import tempfile
|
| 4 |
import json
|
| 5 |
import datetime
|
| 6 |
|
| 7 |
+
# π Language support dictionary
|
| 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!\nYour peaceful space to talk, heal, and grow π"
|
| 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 |
+
# π¬ Therapist reply generator
|
| 35 |
def get_therapist_reply(audio, text_input):
|
| 36 |
try:
|
| 37 |
if audio:
|
|
|
|
| 42 |
elif text_input:
|
| 43 |
user_text = text_input
|
| 44 |
else:
|
| 45 |
+
return "Please speak or type something to begin.", None
|
| 46 |
|
| 47 |
reply = generate_reply(user_text)
|
| 48 |
|
| 49 |
+
# πΎ Save chat
|
| 50 |
chat_entry = {
|
| 51 |
"user": user_text,
|
| 52 |
"reply": reply,
|
|
|
|
| 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 |
except Exception as e:
|
| 69 |
return f"β οΈ Error: {str(e)}", None
|
| 70 |
|
| 71 |
+
# π€ Smart response generator
|
| 72 |
def generate_reply(user_input):
|
| 73 |
user_input = user_input.lower()
|
| 74 |
+
|
| 75 |
if any(word in user_input for word in ["suicide", "kill", "die", "harm", "cut"]):
|
| 76 |
return (
|
| 77 |
+
"π I believe you're very hurt, but I can't provide help in this situation.\n"
|
| 78 |
+
"Please contact a professional therapist:\n"
|
| 79 |
+
"π Dr. Meera Kapoor β +91 9876543210\n"
|
| 80 |
+
"π Dr. Arjun Das β +91 9123456780\nYou are not alone π"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
)
|
| 82 |
+
|
| 83 |
if any(word in user_input for word in ["sad", "anxiety", "angry", "unwell", "hate", "stress"]):
|
| 84 |
+
return "π I hear you. Let's take a deep breath together. Youβre not alone β tell me more, I'm here for you."
|
| 85 |
+
|
| 86 |
if any(word in user_input for word in ["what should i do", "advice", "guide", "suggest"]):
|
| 87 |
return (
|
| 88 |
+
"π§ When you're unsure, take a moment to pause and reflect. Try breaking your problem into small steps. "
|
| 89 |
+
"Would you like help listing ideas?"
|
| 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="π€ Your Name")
|
| 105 |
+
age = gr.Textbox(label="π Your Age")
|
| 106 |
+
lang = gr.Dropdown(choices=list(lang_codes.keys()), label="π Preferred Language")
|
| 107 |
+
mood = gr.Textbox(label="π§ How are you feeling today?")
|
| 108 |
start = gr.Button("β
Start Session")
|
| 109 |
+
welcome_msg = gr.Textbox(label="π System Message")
|
| 110 |
start.click(set_preferences, inputs=[name, age, lang, mood], outputs=welcome_msg)
|
| 111 |
|
| 112 |
+
# ποΈ Chat tab
|
| 113 |
with gr.Tab("π¬ Chat"):
|
| 114 |
audio = gr.Audio(type="filepath", label="ποΈ Speak")
|
| 115 |
+
text_input = gr.Textbox(label="β¨οΈ Or type your feelings")
|
| 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 |
+
# π Chat history
|
| 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="π Your Past Conversations", lines=20)
|
| 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()
|