| import os
|
| from fastapi import FastAPI
|
| from pydantic import BaseModel
|
| import joblib
|
| import numpy as np
|
|
|
| app = FastAPI()
|
|
|
|
|
|
|
| model_path = os.path.join(os.path.dirname(__file__), "diabetes_model_professional.pkl")
|
| model = joblib.load(model_path)
|
|
|
| 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 on Hugging Face!"}
|
|
|
| @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),
|
| "alert": "High Risk - Consult Doctor" if is_diabetic else "Low Risk"
|
| }
|
|
|