Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,57 +1,38 @@
|
|
| 1 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 2 |
-
import torch
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
)
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
# Append to history
|
| 39 |
-
history.append((user_input, response))
|
| 40 |
-
return history, history
|
| 41 |
-
|
| 42 |
-
# Build Gradio Blocks UI
|
| 43 |
-
with gr.Blocks(title="Teen Mental Health Chatbot") as demo:
|
| 44 |
-
gr.Markdown("### 💬 Teen Mental Health Chatbot\n_Not a replacement for therapy, but here to talk._")
|
| 45 |
-
|
| 46 |
-
chatbot = gr.Chatbot()
|
| 47 |
-
msg = gr.Textbox(placeholder="Tell me how you're feeling...")
|
| 48 |
-
state = gr.State([])
|
| 49 |
-
|
| 50 |
-
send = gr.Button("Send")
|
| 51 |
-
|
| 52 |
-
send.click(fn=respond, inputs=[msg, state], outputs=[chatbot, state])
|
| 53 |
-
msg.submit(fn=respond, inputs=[msg, state], outputs=[chatbot, state])
|
| 54 |
-
|
| 55 |
-
# Required to run on Hugging Face
|
| 56 |
if __name__ == "__main__":
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
|