Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,38 +1,61 @@
|
|
1 |
import gradio as gr
|
|
|
2 |
|
3 |
-
#
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
for keyword in responses:
|
19 |
-
if keyword in message:
|
20 |
-
return responses[keyword]
|
21 |
-
|
22 |
-
# Default fallback
|
23 |
-
return "I'm here for you. Want to talk more about how you're feeling?"
|
24 |
-
|
25 |
-
# Gradio interface setup
|
26 |
-
chat_interface = gr.Interface(
|
27 |
-
fn=mental_health_bot,
|
28 |
-
inputs=gr.Textbox(lines=2, placeholder="How are you feeling today?"),
|
29 |
-
outputs="text",
|
30 |
-
title="Teen Mental Health Chatbot 💬",
|
31 |
-
description="A kind and simple chatbot to support your feelings. You're not alone. Let's talk.",
|
32 |
-
theme="soft"
|
33 |
)
|
34 |
|
35 |
-
#
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
|
|
1 |
import gradio as gr
|
2 |
+
import datetime
|
3 |
|
4 |
+
# Keywords and professional responses
|
5 |
+
topic_responses = {
|
6 |
+
"lonely": "It's completely natural to feel lonely at times. You're not alone — many people experience this. Would you like to talk about what's been going on recently?",
|
7 |
+
"heartbroken": "Heartbreak can be deeply painful. It's okay to grieve. Healing takes time, and I'm here to listen if you’d like to share more about it.",
|
8 |
+
"anxious": "Anxiety can feel overwhelming, but recognizing it is a strong first step. Would you like to explore grounding techniques or simply talk about what’s making you anxious?",
|
9 |
+
"depressed": "Feeling low can be incredibly hard. If you're feeling overwhelmed, speaking to a mental health professional can help. I’m also here to talk if you'd like.",
|
10 |
+
"not able to study": "Lack of focus can stem from stress, fatigue, or emotional strain. Do you want to break down your routine or talk about what's affecting your concentration?",
|
11 |
+
"lackin in physics": "Academic challenges are common, and it's okay to seek help. Would you like a study tip or a motivational push to get back on track?",
|
12 |
+
"suicidal": "I'm really sorry you're feeling this way. You're important and your life matters. Please seek immediate help from a professional or call a helpline in your area. You're not alone."
|
13 |
+
}
|
14 |
+
|
15 |
+
# Fallback generic emotional support
|
16 |
+
fallback_response = (
|
17 |
+
"Thank you for sharing that with me. Emotions can be difficult to navigate, but I'm here to listen and support you."
|
18 |
+
" Would you like to talk more about what's been on your mind lately?"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
)
|
20 |
|
21 |
+
# Logging (for future upgrade with secure storage, optional)
|
22 |
+
def log_interaction(user_input, response):
|
23 |
+
timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
24 |
+
print(f"[{timestamp}] User: {user_input}\nResponse: {response}\n")
|
25 |
+
|
26 |
+
# Chatbot response function
|
27 |
+
def mental_health_bot(user_input):
|
28 |
+
user_input_lower = user_input.lower()
|
29 |
+
for keyword, response in topic_responses.items():
|
30 |
+
if keyword in user_input_lower:
|
31 |
+
log_interaction(user_input, response)
|
32 |
+
return response
|
33 |
+
log_interaction(user_input, fallback_response)
|
34 |
+
return fallback_response
|
35 |
+
|
36 |
+
# Gradio interface
|
37 |
+
def build_interface():
|
38 |
+
with gr.Blocks(theme=gr.themes.Base(), title="Teen Mental Health Chatbot") as demo:
|
39 |
+
gr.Markdown("""
|
40 |
+
# 💬 Teen Mental Health Chatbot
|
41 |
+
|
42 |
+
This chatbot provides a safe, non-judgmental space for teenagers to talk about their feelings.
|
43 |
+
While it does not replace professional help, it can offer emotional support and understanding.
|
44 |
+
""")
|
45 |
+
|
46 |
+
chatbot = gr.Textbox(placeholder="How are you feeling today?", label="Your Message", lines=3)
|
47 |
+
output = gr.Textbox(label="Chatbot Response")
|
48 |
+
|
49 |
+
submit_btn = gr.Button("Send")
|
50 |
+
submit_btn.click(fn=mental_health_bot, inputs=chatbot, outputs=output)
|
51 |
+
|
52 |
+
gr.Markdown("""
|
53 |
+
> ⚠️ This is not a substitute for professional care. If you're in crisis, please contact a licensed therapist or a suicide prevention helpline in your region.
|
54 |
+
""")
|
55 |
+
return demo
|
56 |
+
|
57 |
+
# Run the chatbot
|
58 |
+
demo_app = build_interface()
|
59 |
+
|
60 |
+
demo_app.launch()
|
61 |
|