Spaces:
Sleeping
Sleeping
| # app.py | |
| import random | |
| from transformers import pipeline, AutoModelForSequenceClassification, AutoTokenizer | |
| import gradio as gr | |
| # ----------------------------- | |
| # Load empathetic model | |
| # ----------------------------- | |
| MODEL_NAME = "j-hartmann/emotion-english-distilroberta-base" | |
| tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME) | |
| model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME) | |
| pipe = pipeline("text-classification", model=model, tokenizer=tokenizer) | |
| # ----------------------------- | |
| # Empathetic responses | |
| # ----------------------------- | |
| responses = { | |
| "sadness": [ | |
| "I’m really sorry you’re feeling sad. It’s okay to feel this way — do you want to talk about what’s been troubling you?", | |
| "That sounds really hard. Remember, you don’t have to go through this alone.", | |
| "It’s understandable to feel down sometimes. I’m here if you want to share.", | |
| "Sadness can feel heavy — would you like to tell me more about it?", | |
| "You’re not alone in this. Talking can help lighten the load." | |
| ], | |
| "joy": [ | |
| "I’m so glad to hear that! What’s making you feel happy today?", | |
| "That’s wonderful — it’s great to see you in good spirits!", | |
| "Your happiness is contagious! Can you share what made your day?", | |
| "It’s great to feel joy — I’m happy for you!", | |
| "Sounds like a joyful moment! Let’s celebrate it together." | |
| ], | |
| "anger": [ | |
| "It sounds like you’re really upset. Do you want to share what happened?", | |
| "That must have been frustrating. Sometimes expressing it helps ease the anger.", | |
| "Anger can be intense — would you like to talk about it?", | |
| "I hear your frustration. Let’s try to unpack it together.", | |
| "It’s okay to feel angry — letting it out can help." | |
| ], | |
| "fear": [ | |
| "It’s normal to feel scared sometimes. I’m here with you — would you like to talk about what’s worrying you?", | |
| "That sounds frightening. You’re not alone in this.", | |
| "Fear can be overwhelming — want to tell me more about it?", | |
| "It’s okay to feel afraid. Sharing your worries can help.", | |
| "I understand — facing fear is hard, but I’m here for you." | |
| ], | |
| "surprise": [ | |
| "Oh, that sounds unexpected! How are you feeling about it?", | |
| "Wow, that must have caught you off guard. Tell me more!", | |
| "That’s surprising! I’d love to hear more about it.", | |
| "Unexpected events can feel shocking — how are you handling it?", | |
| "I’m here to listen — surprises can bring mixed feelings." | |
| ], | |
| "disgust": [ | |
| "That sounds unpleasant. Want to tell me what caused that feeling?", | |
| "It’s okay to feel grossed out sometimes — that emotion is valid too.", | |
| "Disgust is a natural reaction. Do you want to share why?", | |
| "I hear you — feeling disgusted can be really uncomfortable.", | |
| "It’s understandable to feel that way. Talking can help." | |
| ], | |
| "neutral": [ | |
| "I’m here to listen — what’s on your mind today?", | |
| "How are you feeling right now?", | |
| "I’m ready to hear you out. What would you like to share?", | |
| "It’s okay to just talk about anything. What’s happening?", | |
| "Feel free to tell me whatever’s on your mind." | |
| ] | |
| } | |
| recent_responses = {k: [] for k in responses.keys()} | |
| # ----------------------------- | |
| # FAQ responses with emojis | |
| # ----------------------------- | |
| faq_responses = { | |
| "What is depression? 🧠": "Depression is a common mental health condition characterized by persistent sadness, lack of interest in activities, and changes in sleep or appetite. Talking to a professional can help.", | |
| "How can I sleep better? 😴": "Good sleep hygiene helps: maintain a regular sleep schedule, avoid caffeine or screens before bed, and create a calm sleeping environment.", | |
| "How to deal with stress? 🌿": "Stress can be managed through relaxation techniques like deep breathing, meditation, exercise, and talking to someone you trust.", | |
| "What is anxiety? 😰": "Anxiety is a feeling of worry or fear that can be mild or severe. Breathing exercises, mindfulness, and professional guidance can help manage it.", | |
| "I feel lonely 😔": "Feeling lonely is normal sometimes. Connecting with friends, family, or support groups can help reduce loneliness.", | |
| "How to stay motivated? 💪": "Set small achievable goals, reward yourself, and remember why you started. Motivation can fluctuate — be kind to yourself." | |
| } | |
| # ----------------------------- | |
| # Chat function | |
| # ----------------------------- | |
| def chat(user_input, chat_history): | |
| if not user_input.strip(): | |
| return chat_history, "" # clear input | |
| # Add user's message to chat | |
| chat_history.append({"role": "user", "content": user_input}) | |
| user_lower = user_input.lower() | |
| # FAQ check | |
| for key in faq_responses: | |
| if key.lower().split("?")[0] in user_lower: | |
| reply = faq_responses[key] | |
| chat_history.append({"role": "assistant", "content": reply}) | |
| return chat_history, "" # clear input | |
| # Empathetic fallback | |
| emotions = pipe(user_input, top_k=1) | |
| emotion = emotions[0]['label'].lower() | |
| options = [r for r in responses.get(emotion, responses["neutral"]) if r not in recent_responses[emotion]] | |
| if not options: | |
| recent_responses[emotion] = [] | |
| options = responses.get(emotion, responses["neutral"]) | |
| reply = random.choice(options) | |
| recent_responses[emotion].append(reply) | |
| chat_history.append({"role": "assistant", "content": reply}) | |
| return chat_history, "" # clear input | |
| # ----------------------------- | |
| # FAQ click function (adds to chat) | |
| # ----------------------------- | |
| def faq_click(faq_key, chat_history): | |
| # Show FAQ question as user message | |
| chat_history.append({"role": "user", "content": faq_key}) | |
| # Show answer as bot message | |
| reply = faq_responses[faq_key] | |
| chat_history.append({"role": "assistant", "content": reply}) | |
| return chat_history | |
| # ----------------------------- | |
| # Gradio interface | |
| # ----------------------------- | |
| with gr.Blocks() as demo: | |
| gr.Markdown("<h1 style='text-align:center;'>Empathetic Mental Health Bot + FAQ</h1>") | |
| with gr.Row(): | |
| # FAQ sidebar | |
| with gr.Column(scale=1): | |
| gr.Markdown("### FAQs 💡") | |
| faq_btns = [] | |
| for key in faq_responses.keys(): | |
| btn = gr.Button(key) | |
| faq_btns.append(btn) | |
| # Chat area | |
| with gr.Column(scale=3): | |
| chatbot = gr.Chatbot(type="messages") | |
| msg = gr.Textbox(placeholder="Type your message here...") | |
| send_btn = gr.Button("Send") | |
| clear_btn = gr.Button("Clear") | |
| # Event handlers | |
| send_btn.click(chat, [msg, chatbot], [chatbot, msg]) | |
| msg.submit(chat, [msg, chatbot], [chatbot, msg]) | |
| clear_btn.click(lambda: [], None, chatbot) | |
| # FAQ button click events | |
| for i, btn in enumerate(faq_btns): | |
| key = list(faq_responses.keys())[i] | |
| btn.click(lambda chat_history, k=key: faq_click(k, chat_history), chatbot, chatbot) | |
| # ----------------------------- | |
| # Launch | |
| # ----------------------------- | |
| if __name__ == "__main__": | |
| demo.launch() |