| import joblib |
| import pandas as pd |
| from fastapi import FastAPI |
| from pydantic import BaseModel |
|
|
| pipe = joblib.load("diabetes_model.pkl") |
|
|
| |
| FEATURE_COLUMNS = [ |
| "pregnancies","glucose","blood_pressure","skin_thickness", |
| "insulin","bmi","diabetes_pedigree","age" |
| ] |
|
|
| class Patient(BaseModel): |
| pregnancies: float |
| glucose: float |
| blood_pressure: float |
| skin_thickness: float |
| insulin: float |
| bmi: float |
| diabetes_pedigree: float |
| age: float |
|
|
| app = FastAPI(title="Diabetes Risk API", version="1.0.0") |
|
|
| @app.post("/predict") |
| def predict(p: Patient): |
| |
| row = pd.DataFrame([p.model_dump()])[FEATURE_COLUMNS] |
| yhat = int(pipe.predict(row)[0]) |
|
|
| proba = None |
| try: |
| proba = float(pipe.predict_proba(row)[0][1]) |
| except Exception: |
| pass |
|
|
| return {"prediction": yhat, "positive_probability": proba} |
|
|