Sanchit2207 commited on
Commit
3b85244
·
verified ·
1 Parent(s): 2ee01de

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -33
app.py CHANGED
@@ -1,38 +1,61 @@
1
  import gradio as gr
 
2
 
3
- # Custom supportive chatbot function
4
- def mental_health_bot(message):
5
- message = message.lower().strip()
6
-
7
- responses = {
8
- "lonely": "You're not alone. I'm here to listen. Would you like to talk about what's making you feel this way?",
9
- "heartbroken": "Heartbreak can feel really heavy. Want to share what happened? I'm here for you.",
10
- "not able to study": "It’s okay to feel stuck. Maybe take a short break, or we can talk about what’s bothering you?",
11
- "lackin in physics": "Struggling is part of learning. Maybe I can help you with a study tip or we can just talk about what’s making it tough.",
12
- "anxious": "Anxiety can be overwhelming. Want to talk through it together?",
13
- "depressed": "You matter. Even if it doesn’t feel like it right now, things can get better. I'm here for you.",
14
- "suicidal": "I'm really sorry you're feeling this way. You're not alone. Please consider reaching out to a mental health professional or a helpline right away. Your life is important."
15
- }
16
-
17
- # Check if any keyword is matched
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
- # Launch the app
36
- if __name__ == "__main__":
37
- chat_interface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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