Spaces:
Runtime error
Runtime error
Upload 4 files
Browse files- Dockerfile +13 -0
- diabetes_model.pkl +3 -0
- main.py +97 -0
- requirements.txt +4 -0
Dockerfile
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.10
|
| 2 |
+
|
| 3 |
+
WORKDIR /code
|
| 4 |
+
|
| 5 |
+
COPY requirements.txt .
|
| 6 |
+
|
| 7 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 8 |
+
|
| 9 |
+
COPY . .
|
| 10 |
+
|
| 11 |
+
EXPOSE 7860
|
| 12 |
+
|
| 13 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
diabetes_model.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:3f697fe210ee5bc5907e10f03ec13a084a6e69187ac15d82e6a455e0343d68b6
|
| 3 |
+
size 1127
|
main.py
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ====================================================
|
| 2 |
+
# main.py - Diabetes Prediction API (Production)
|
| 3 |
+
# ====================================================
|
| 4 |
+
|
| 5 |
+
from fastapi import FastAPI
|
| 6 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 7 |
+
from pydantic import BaseModel
|
| 8 |
+
import numpy as np
|
| 9 |
+
import joblib
|
| 10 |
+
import os
|
| 11 |
+
|
| 12 |
+
# -----------------------------
|
| 13 |
+
# Load Trained Model & Scaler
|
| 14 |
+
# -----------------------------
|
| 15 |
+
MODEL_FILE = "diabetes_model.pkl"
|
| 16 |
+
SCALER_FILE = "scaler.pkl"
|
| 17 |
+
|
| 18 |
+
if not os.path.exists(MODEL_FILE) or not os.path.exists(SCALER_FILE):
|
| 19 |
+
raise FileNotFoundError("Model or scaler file not found. Make sure both exist in the same directory.")
|
| 20 |
+
|
| 21 |
+
scaler = joblib.load(SCALER_FILE)
|
| 22 |
+
best_model = joblib.load(MODEL_FILE)
|
| 23 |
+
|
| 24 |
+
# -----------------------------
|
| 25 |
+
# Initialize FastAPI app
|
| 26 |
+
# -----------------------------
|
| 27 |
+
app = FastAPI(
|
| 28 |
+
title="Diabetes Prediction API",
|
| 29 |
+
description="Predicts diabetes based on patient data using trained ML model",
|
| 30 |
+
version="1.0"
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
# -----------------------------
|
| 34 |
+
# Enable CORS for all origins
|
| 35 |
+
# -----------------------------
|
| 36 |
+
app.add_middleware(
|
| 37 |
+
CORSMiddleware,
|
| 38 |
+
allow_origins=["*"], # For production, replace '*' with allowed domains
|
| 39 |
+
allow_methods=["*"],
|
| 40 |
+
allow_headers=["*"],
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
# -----------------------------
|
| 44 |
+
# Define Input Data Model
|
| 45 |
+
# -----------------------------
|
| 46 |
+
class InputData(BaseModel):
|
| 47 |
+
Age: float
|
| 48 |
+
Sex: float
|
| 49 |
+
BMI: float
|
| 50 |
+
Glucose: float
|
| 51 |
+
BloodPressure: float
|
| 52 |
+
Insulin: float
|
| 53 |
+
Increased_Thirst: float
|
| 54 |
+
Increased_Hunger: float
|
| 55 |
+
Fatigue_Tiredness: float
|
| 56 |
+
Blurred_Vision: float
|
| 57 |
+
Unexplained_Weight_Loss: float
|
| 58 |
+
|
| 59 |
+
# -----------------------------
|
| 60 |
+
# Prediction Endpoint
|
| 61 |
+
# -----------------------------
|
| 62 |
+
@app.post("/predict")
|
| 63 |
+
def predict(data: InputData):
|
| 64 |
+
try:
|
| 65 |
+
# Convert input to numpy array
|
| 66 |
+
features = np.array([[
|
| 67 |
+
data.Age,
|
| 68 |
+
data.Sex,
|
| 69 |
+
data.BMI,
|
| 70 |
+
data.Glucose,
|
| 71 |
+
data.BloodPressure,
|
| 72 |
+
data.Insulin,
|
| 73 |
+
data.Increased_Thirst,
|
| 74 |
+
data.Increased_Hunger,
|
| 75 |
+
data.Fatigue_Tiredness,
|
| 76 |
+
data.Blurred_Vision,
|
| 77 |
+
data.Unexplained_Weight_Loss
|
| 78 |
+
]])
|
| 79 |
+
|
| 80 |
+
# Scale features
|
| 81 |
+
features_scaled = scaler.transform(features)
|
| 82 |
+
|
| 83 |
+
# Predict
|
| 84 |
+
prediction = best_model.predict(features_scaled)[0]
|
| 85 |
+
result = "Diabetes" if prediction == 1 else "No Diabetes"
|
| 86 |
+
|
| 87 |
+
return {"prediction": result}
|
| 88 |
+
|
| 89 |
+
except Exception as e:
|
| 90 |
+
return {"error": str(e)}
|
| 91 |
+
|
| 92 |
+
# -----------------------------
|
| 93 |
+
# Run the API locally
|
| 94 |
+
# -----------------------------
|
| 95 |
+
if __name__ == "__main__":
|
| 96 |
+
import uvicorn
|
| 97 |
+
uvicorn.run("main:app", host="0.0.0.0", port=7860, reload=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
numpy
|
| 3 |
+
scikit-learn
|
| 4 |
+
joblib
|