|
|
|
|
|
|
|
|
| from fastapi import FastAPI
|
| from fastapi.middleware.cors import CORSMiddleware
|
| from pydantic import BaseModel
|
| import joblib
|
| import numpy as np
|
|
|
|
|
| app = FastAPI(title="Diabetes Prediction API", version="1.0.0")
|
|
|
|
|
| app.add_middleware(
|
| CORSMiddleware,
|
| allow_origins=["*"],
|
| allow_credentials=True,
|
| allow_methods=["*"],
|
| allow_headers=["*"],
|
| )
|
|
|
|
|
|
|
| model = joblib.load('diabetes_model_professional.pkl')
|
|
|
|
|
| class PatientData(BaseModel):
|
| pregnancies: int
|
| glucose: float
|
| blood_pressure: float
|
| skin_thickness: float
|
| insulin: float
|
| bmi: float
|
| pedigree: float
|
| age: int
|
|
|
| @app.get("/")
|
| def home():
|
| return {"status": "online", "message": "Diabetes Prediction API is running"}
|
|
|
| @app.post("/predict")
|
| def predict_diabetes(data: PatientData):
|
|
|
| features = [[
|
| data.pregnancies,
|
| data.glucose,
|
| data.blood_pressure,
|
| data.skin_thickness,
|
| data.insulin,
|
| data.bmi,
|
| data.pedigree,
|
| data.age
|
| ]]
|
|
|
|
|
| probability = model.predict_proba(features)[0][1]
|
|
|
|
|
|
|
| threshold = 0.35
|
| is_diabetic = probability >= threshold
|
|
|
| return {
|
| "prediction": "Diabetic" if is_diabetic else "Healthy",
|
| "risk_score": round(float(probability) * 100, 2),
|
| "risk_level": "High" if probability > 0.6 else "Moderate" if is_diabetic else "Low",
|
| "alert": "Patient requires further clinical screening." if is_diabetic else "Patient appears healthy."
|
| }
|
|
|
|
|
|
|