Upload 3 files
Browse files- Dockerfile +19 -0
- app.py +38 -0
- diabetes_model.pkl +3 -0
Dockerfile
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Minimal image to serve the trained pipeline with FastAPI
|
| 2 |
+
FROM python:3.11-slim
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
|
| 5 |
+
# Pin runtime deps (match training!)
|
| 6 |
+
RUN pip install --no-cache-dir \
|
| 7 |
+
fastapi==0.115.* \
|
| 8 |
+
"uvicorn[standard]==0.30.*" \
|
| 9 |
+
joblib==1.4.2 \
|
| 10 |
+
numpy==1.26.* \
|
| 11 |
+
pandas==2.2.* \
|
| 12 |
+
scikit-learn==1.6.1 # <= match your training version
|
| 13 |
+
|
| 14 |
+
COPY app.py diabetes_model.pkl /app/
|
| 15 |
+
EXPOSE 7860
|
| 16 |
+
|
| 17 |
+
# IMPORTANT: bind to the port Spaces provides
|
| 18 |
+
CMD ["sh", "-c", "uvicorn app:app --host 0.0.0.0 --port ${PORT:-7860} --workers 1 --timeout-keep-alive 120"]
|
| 19 |
+
|
app.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import joblib
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from fastapi import FastAPI
|
| 4 |
+
from pydantic import BaseModel
|
| 5 |
+
|
| 6 |
+
pipe = joblib.load("diabetes_model.pkl")
|
| 7 |
+
|
| 8 |
+
# Columns from Nb3 (Pima Indians Diabetes)
|
| 9 |
+
FEATURE_COLUMNS = [
|
| 10 |
+
"pregnancies","glucose","blood_pressure","skin_thickness",
|
| 11 |
+
"insulin","bmi","diabetes_pedigree","age"
|
| 12 |
+
]
|
| 13 |
+
|
| 14 |
+
class Patient(BaseModel):
|
| 15 |
+
pregnancies: float
|
| 16 |
+
glucose: float
|
| 17 |
+
blood_pressure: float
|
| 18 |
+
skin_thickness: float
|
| 19 |
+
insulin: float
|
| 20 |
+
bmi: float
|
| 21 |
+
diabetes_pedigree: float
|
| 22 |
+
age: float
|
| 23 |
+
|
| 24 |
+
app = FastAPI(title="Diabetes Risk API", version="1.0.0")
|
| 25 |
+
|
| 26 |
+
@app.post("/predict")
|
| 27 |
+
def predict(p: Patient):
|
| 28 |
+
# Build a 1-row DataFrame with correct column names
|
| 29 |
+
row = pd.DataFrame([p.model_dump()])[FEATURE_COLUMNS]
|
| 30 |
+
yhat = int(pipe.predict(row)[0])
|
| 31 |
+
|
| 32 |
+
proba = None
|
| 33 |
+
try:
|
| 34 |
+
proba = float(pipe.predict_proba(row)[0][1])
|
| 35 |
+
except Exception:
|
| 36 |
+
pass
|
| 37 |
+
|
| 38 |
+
return {"prediction": yhat, "positive_probability": proba}
|
diabetes_model.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:eed9a6e5726598c53d39688024bea69bf0fa227c62efb829d9e7490d2eaccd5c
|
| 3 |
+
size 1729691
|