Spaces:
Sleeping
Sleeping
import gradio as gr | |
import datetime | |
# Keywords and professional responses | |
topic_responses = { | |
"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?", | |
"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.", | |
"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?", | |
"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.", | |
"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?", | |
"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?", | |
"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." | |
} | |
# Fallback generic emotional support | |
fallback_response = ( | |
"Thank you for sharing that with me. Emotions can be difficult to navigate, but I'm here to listen and support you." | |
" Would you like to talk more about what's been on your mind lately?" | |
) | |
# Logging (for future upgrade with secure storage, optional) | |
def log_interaction(user_input, response): | |
timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") | |
print(f"[{timestamp}] User: {user_input}\nResponse: {response}\n") | |
# Chatbot response function | |
def mental_health_bot(user_input): | |
user_input_lower = user_input.lower() | |
for keyword, response in topic_responses.items(): | |
if keyword in user_input_lower: | |
log_interaction(user_input, response) | |
return response | |
log_interaction(user_input, fallback_response) | |
return fallback_response | |
# Gradio interface | |
def build_interface(): | |
with gr.Blocks(theme=gr.themes.Base(), title="Teen Mental Health Chatbot") as demo: | |
gr.Markdown(""" | |
# 💬 Teen Mental Health Chatbot | |
This chatbot provides a safe, non-judgmental space for teenagers to talk about their feelings. | |
While it does not replace professional help, it can offer emotional support and understanding. | |
""") | |
chatbot = gr.Textbox(placeholder="How are you feeling today?", label="Your Message", lines=3) | |
output = gr.Textbox(label="Chatbot Response") | |
submit_btn = gr.Button("Send") | |
submit_btn.click(fn=mental_health_bot, inputs=chatbot, outputs=output) | |
gr.Markdown(""" | |
> ⚠️ 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. | |
""") | |
return demo | |
# Run the chatbot | |
demo_app = build_interface() | |
demo_app.launch() | |