Spaces:
Sleeping
Sleeping
Commit ·
e0cdea0
1
Parent(s): 56d8d79
host
Browse files- Dockerfile +18 -0
- main.py +35 -0
- nlp_service.py +30 -0
- requirements.txt +5 -0
Dockerfile
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.9-slim
|
| 2 |
+
|
| 3 |
+
WORKDIR /code
|
| 4 |
+
|
| 5 |
+
COPY ./requirements.txt /code/requirements.txt
|
| 6 |
+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
| 7 |
+
|
| 8 |
+
# Create a non-root user for security
|
| 9 |
+
RUN useradd -m -u 1000 user
|
| 10 |
+
USER user
|
| 11 |
+
ENV HOME=/home/user \
|
| 12 |
+
PATH=/home/user/.local/bin:$PATH
|
| 13 |
+
|
| 14 |
+
# Copy scripts into the container
|
| 15 |
+
COPY --chown=user . /code
|
| 16 |
+
|
| 17 |
+
# Run Uvicorn on the port HF expects
|
| 18 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
main.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
from nlp_service import analyse_emergency
|
| 4 |
+
|
| 5 |
+
app = FastAPI()
|
| 6 |
+
|
| 7 |
+
class EmergencyRequest(BaseModel):
|
| 8 |
+
patient_id: str
|
| 9 |
+
message: str
|
| 10 |
+
|
| 11 |
+
class EmergencyResponse(BaseModel):
|
| 12 |
+
patient_id: str
|
| 13 |
+
urgency: str
|
| 14 |
+
confidence: float
|
| 15 |
+
action: str
|
| 16 |
+
message: str
|
| 17 |
+
|
| 18 |
+
@app.post("/api/emergency/analyse", response_model=EmergencyResponse)
|
| 19 |
+
async def analyse(request: EmergencyRequest):
|
| 20 |
+
if not request.message.strip():
|
| 21 |
+
raise HTTPException(status_code=400, detail="Message cannot be empty")
|
| 22 |
+
|
| 23 |
+
result = analyse_emergency(request.message)
|
| 24 |
+
|
| 25 |
+
return EmergencyResponse(
|
| 26 |
+
patient_id=request.patient_id,
|
| 27 |
+
urgency=result["urgency"],
|
| 28 |
+
confidence=result["confidence"],
|
| 29 |
+
action=result["action"],
|
| 30 |
+
message=result["message"]
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
@app.get("/")
|
| 34 |
+
async def health_check():
|
| 35 |
+
return {"status": "BUTH NLP Service is running"}
|
nlp_service.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import pipeline
|
| 2 |
+
|
| 3 |
+
# CHANGE THIS: Use your actual HF username and model name
|
| 4 |
+
MODEL_ID = "firesolami/buth-nlp-model_lean"
|
| 5 |
+
|
| 6 |
+
# Load once when the server starts
|
| 7 |
+
# It will download the weights from the Hub on the first run
|
| 8 |
+
classifier = pipeline(
|
| 9 |
+
"text-classification",
|
| 10 |
+
model=MODEL_ID,
|
| 11 |
+
device=-1
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
ACTIONS = {
|
| 15 |
+
"Critical": {"action": "dispatch_ambulance", "message": "Ambulance has been dispatched."},
|
| 16 |
+
"High": {"action": "alert_admin_dashboard", "message": "Hospital staff have been alerted."},
|
| 17 |
+
"Moderate": {"action": "schedule_appointment", "message": "Your case has been noted. An appointment will be scheduled shortly."},
|
| 18 |
+
"Low": {"action": "provide_guidance", "message": "Your concern has been received. A staff member will provide guidance shortly."}
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
def analyse_emergency(text: str) -> dict:
|
| 22 |
+
result = classifier(text)[0]
|
| 23 |
+
label = result["label"]
|
| 24 |
+
confidence = round(result["score"] * 100, 1)
|
| 25 |
+
return {
|
| 26 |
+
"urgency": label,
|
| 27 |
+
"confidence": confidence,
|
| 28 |
+
"action": ACTIONS[label]["action"],
|
| 29 |
+
"message": ACTIONS[label]["message"]
|
| 30 |
+
}
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
transformers
|
| 4 |
+
torch
|
| 5 |
+
pydantic
|