Upload 3 files
Browse files- Dockerfile +24 -0
- app.py +126 -0
- requirements.txt +5 -0
Dockerfile
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.9-slim
|
| 2 |
+
|
| 3 |
+
# Set up a new user named "user" with user ID 1000
|
| 4 |
+
RUN useradd -m -u 1000 user
|
| 5 |
+
# Switch to the "user" user
|
| 6 |
+
USER user
|
| 7 |
+
# Set home to the user's home directory
|
| 8 |
+
ENV HOME=/home/user \
|
| 9 |
+
PATH=/home/user/.local/bin:$PATH
|
| 10 |
+
|
| 11 |
+
# Set the working directory to the user's home directory
|
| 12 |
+
WORKDIR $HOME/app
|
| 13 |
+
|
| 14 |
+
# Copy the current directory contents into the container at $HOME/app setting the owner to the user
|
| 15 |
+
COPY --chown=user . $HOME/app
|
| 16 |
+
|
| 17 |
+
# Install any needed packages specified in requirements.txt
|
| 18 |
+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
| 19 |
+
|
| 20 |
+
# Expose the default port for Hugging Face Spaces exactly
|
| 21 |
+
EXPOSE 7860
|
| 22 |
+
|
| 23 |
+
# Run FastAPI using uvicorn on port 7860
|
| 24 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
+
from pydantic import BaseModel
|
| 4 |
+
from transformers import pipeline
|
| 5 |
+
import random
|
| 6 |
+
import logging
|
| 7 |
+
|
| 8 |
+
# Set up logging
|
| 9 |
+
logging.basicConfig(level=logging.INFO)
|
| 10 |
+
logger = logging.getLogger(__name__)
|
| 11 |
+
|
| 12 |
+
app = FastAPI(title="Mental Health Chatbot API Backend")
|
| 13 |
+
|
| 14 |
+
# Enable CORS for frontend integration
|
| 15 |
+
app.add_middleware(
|
| 16 |
+
CORSMiddleware,
|
| 17 |
+
allow_origins=["*"], # Adjust this in production to your frontend's actual domain
|
| 18 |
+
allow_credentials=True,
|
| 19 |
+
allow_methods=["*"],
|
| 20 |
+
allow_headers=["*"],
|
| 21 |
+
)
|
| 22 |
+
logger.info("Loading emotion classification model...")
|
| 23 |
+
try:
|
| 24 |
+
classifier = pipeline(
|
| 25 |
+
"text-classification",
|
| 26 |
+
model="bhadresh-savani/distilbert-base-uncased-emotion",
|
| 27 |
+
return_all_scores=True
|
| 28 |
+
)
|
| 29 |
+
logger.info("Model loaded successfully.")
|
| 30 |
+
except Exception as e:
|
| 31 |
+
logger.error(f"Error loading model: {e}")
|
| 32 |
+
classifier = None
|
| 33 |
+
|
| 34 |
+
# Defining our request and response models
|
| 35 |
+
class ChatRequest(BaseModel):
|
| 36 |
+
user_input: str
|
| 37 |
+
|
| 38 |
+
class ChatResponse(BaseModel):
|
| 39 |
+
emotion: str
|
| 40 |
+
response: str
|
| 41 |
+
is_crisis: bool
|
| 42 |
+
|
| 43 |
+
# Critical intervention keywords
|
| 44 |
+
CRISIS_KEYWORDS = [
|
| 45 |
+
"suicide", "kill myself", "want to die", "end my life",
|
| 46 |
+
"hurt myself", "better off dead", "no reason to live",
|
| 47 |
+
"suicidal", "very depressed"
|
| 48 |
+
]
|
| 49 |
+
|
| 50 |
+
# Multiple responses per emotion so it doesn't give the same response twice
|
| 51 |
+
responses_pool = {
|
| 52 |
+
"joy": [
|
| 53 |
+
"I'm so glad to hear you're feeling good! It's wonderful to have moments like this. What’s contributing to your happiness today?",
|
| 54 |
+
"That's wonderful news! It is great to see you experiencing joy. Cherish these moments.",
|
| 55 |
+
"Knowing you are happy makes my day! Keep embracing whatever is bringing you this positivity."
|
| 56 |
+
],
|
| 57 |
+
"sadness": [
|
| 58 |
+
"I'm really sorry to hear you're feeling down. It's okay to feel this way, and I'm here to listen. Remember to be kind to yourself.",
|
| 59 |
+
"I hear the pain in your words. Please know that it's okay to not be okay. Taking it one day at a time is enough.",
|
| 60 |
+
"I'm here for you. Sadness can be heavy, but you don't have to carry it all by yourself. Be gentle with yourself."
|
| 61 |
+
],
|
| 62 |
+
"anger": [
|
| 63 |
+
"It sounds like you're going through a lot of frustration right now. Your feelings are valid. Would it help to talk about what happened?",
|
| 64 |
+
"I can sense the anger in what you're saying, and it's completely understandable to feel that way. I'm here if you want to vent.",
|
| 65 |
+
"That sounds really infuriating. It's okay to be upset when things go wrong. Take a deep breath if you need to."
|
| 66 |
+
],
|
| 67 |
+
"fear": [
|
| 68 |
+
"It sounds like you're feeling quite anxious or worried. Take a slow, deep breath. I'm here with you—what's on your mind?",
|
| 69 |
+
"Fear can be overwhelming, but you're not alone. Let's try to focus on the present moment. I am here for you.",
|
| 70 |
+
"I hear your anxiety. It's a tough feeling to deal with, but you are safe right now. What is it that's making you most afraid?"
|
| 71 |
+
],
|
| 72 |
+
"love": [
|
| 73 |
+
"That's a beautiful sentiment. Feeling connected and full of love is so vital for our well-being.",
|
| 74 |
+
"Love is such a powerful emotion. It's heartwarming to hear you express it so openly.",
|
| 75 |
+
"Holding onto feelings of love and connection can bring so much light into your life. Thank you for sharing that."
|
| 76 |
+
],
|
| 77 |
+
"surprise": [
|
| 78 |
+
"That sounds like a lot to process! Was it a positive surprise, or are you feeling a bit overwhelmed by it?",
|
| 79 |
+
"Wow! Life definitely has a way of throwing unexpected things at us. How are you adjusting to this?",
|
| 80 |
+
"I didn't expect that either! Take your time to figure out how you feel about this surprise."
|
| 81 |
+
],
|
| 82 |
+
"neutral": [
|
| 83 |
+
"I see. How are you holding up otherwise?",
|
| 84 |
+
"Thank you for sharing. Is there anything else on your mind?",
|
| 85 |
+
"I'm listening. Feel free to share as much or as little as you'd like."
|
| 86 |
+
]
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
def detect_crisis(text):
|
| 90 |
+
text_lower = text.lower()
|
| 91 |
+
for keyword in CRISIS_KEYWORDS:
|
| 92 |
+
if keyword in text_lower:
|
| 93 |
+
return True
|
| 94 |
+
return False
|
| 95 |
+
|
| 96 |
+
@app.post("/chat", response_model=ChatResponse)
|
| 97 |
+
def get_chat_response(request: ChatRequest):
|
| 98 |
+
user_input = request.user_input
|
| 99 |
+
|
| 100 |
+
# 1. Severe Depression / Suicidal Check
|
| 101 |
+
if detect_crisis(user_input):
|
| 102 |
+
return ChatResponse(
|
| 103 |
+
emotion="crisis",
|
| 104 |
+
response="I am a chatbot and not a doctor and you need immediate medical support. Please consult a doctor immediately.",
|
| 105 |
+
is_crisis=True
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
+
# 2. Extract Emotion
|
| 109 |
+
if classifier:
|
| 110 |
+
predictions = classifier(user_input)[0]
|
| 111 |
+
# Sort scores highest to lowest
|
| 112 |
+
predictions.sort(key=lambda x: x['score'], reverse=True)
|
| 113 |
+
top_emotion_label = predictions[0]['label']
|
| 114 |
+
else:
|
| 115 |
+
top_emotion_label = "neutral"
|
| 116 |
+
|
| 117 |
+
# 3. Dynamic Response Selection
|
| 118 |
+
emotion_list = responses_pool.get(top_emotion_label, responses_pool["neutral"])
|
| 119 |
+
# Uses random to ensure varied responses (so it doesn't give the same back over and over)
|
| 120 |
+
selected_response = random.choice(emotion_list)
|
| 121 |
+
|
| 122 |
+
return ChatResponse(
|
| 123 |
+
emotion=top_emotion_label,
|
| 124 |
+
response=selected_response,
|
| 125 |
+
is_crisis=False
|
| 126 |
+
)
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
torch
|
| 3 |
+
fastapi
|
| 4 |
+
uvicorn
|
| 5 |
+
pydantic
|