Spaces:
Sleeping
Sleeping
Upload 5 files
Browse files- Dockerfile +13 -0
- diabetes_model.pkl +3 -0
- main.py +89 -0
- requirements.txt +12 -0
- scaler.pkl +3 -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,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
|
| 11 |
+
# -----------------------------
|
| 12 |
+
# Load Trained Model & Scaler
|
| 13 |
+
# -----------------------------
|
| 14 |
+
# Ensure these files are in the same directory as this script
|
| 15 |
+
scaler = joblib.load("scaler.pkl")
|
| 16 |
+
best_model = joblib.load("diabetes_model.pkl")
|
| 17 |
+
|
| 18 |
+
# -----------------------------
|
| 19 |
+
# Initialize FastAPI app
|
| 20 |
+
# -----------------------------
|
| 21 |
+
app = FastAPI(
|
| 22 |
+
title="Diabetes Prediction API",
|
| 23 |
+
description="Predicts diabetes based on patient data using trained ML model",
|
| 24 |
+
version="1.0"
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
# -----------------------------
|
| 28 |
+
# Enable CORS for all origins (for testing)
|
| 29 |
+
# -----------------------------
|
| 30 |
+
app.add_middleware(
|
| 31 |
+
CORSMiddleware,
|
| 32 |
+
allow_origins=["*"], # Adjust for production to restrict origins
|
| 33 |
+
allow_methods=["*"],
|
| 34 |
+
allow_headers=["*"],
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
# -----------------------------
|
| 38 |
+
# Define Input Data Model
|
| 39 |
+
# -----------------------------
|
| 40 |
+
class InputData(BaseModel):
|
| 41 |
+
Age: float
|
| 42 |
+
Sex: float
|
| 43 |
+
BMI: float
|
| 44 |
+
Glucose: float
|
| 45 |
+
BloodPressure: float
|
| 46 |
+
Insulin: float
|
| 47 |
+
Increased_Thirst: float
|
| 48 |
+
Increased_Hunger: float
|
| 49 |
+
Fatigue_Tiredness: float
|
| 50 |
+
Blurred_Vision: float
|
| 51 |
+
Unexplained_Weight_Loss: float
|
| 52 |
+
|
| 53 |
+
# -----------------------------
|
| 54 |
+
# Prediction Endpoint
|
| 55 |
+
# -----------------------------
|
| 56 |
+
@app.post("/predict")
|
| 57 |
+
def predict(data: InputData):
|
| 58 |
+
# Convert input to numpy array
|
| 59 |
+
features = np.array([[
|
| 60 |
+
data.Age,
|
| 61 |
+
data.Sex,
|
| 62 |
+
data.BMI,
|
| 63 |
+
data.Glucose,
|
| 64 |
+
data.BloodPressure,
|
| 65 |
+
data.Insulin,
|
| 66 |
+
data.Increased_Thirst,
|
| 67 |
+
data.Increased_Hunger,
|
| 68 |
+
data.Fatigue_Tiredness,
|
| 69 |
+
data.Blurred_Vision,
|
| 70 |
+
data.Unexplained_Weight_Loss
|
| 71 |
+
]])
|
| 72 |
+
|
| 73 |
+
# Scale the input features
|
| 74 |
+
features_scaled = scaler.transform(features)
|
| 75 |
+
|
| 76 |
+
# Make prediction
|
| 77 |
+
prediction = best_model.predict(features_scaled)[0]
|
| 78 |
+
|
| 79 |
+
# Map prediction to human-readable label
|
| 80 |
+
result = "Diabetes" if prediction == 1 else "No Diabetes"
|
| 81 |
+
|
| 82 |
+
return {"prediction": result}
|
| 83 |
+
|
| 84 |
+
# -----------------------------
|
| 85 |
+
# Run the API locally (development only)
|
| 86 |
+
# -----------------------------
|
| 87 |
+
if __name__ == "__main__":
|
| 88 |
+
import uvicorn
|
| 89 |
+
uvicorn.run("main:app", host="0.0.0.0", port=7860, reload=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Install Python packages
|
| 2 |
+
pip install fastapi uvicorn scikit-learn numpy joblib pandas
|
| 3 |
+
|
| 4 |
+
# Or create requirements.txt
|
| 5 |
+
echo "fastapi==0.104.0
|
| 6 |
+
uvicorn[standard]==0.24.0
|
| 7 |
+
scikit-learn==1.3.0
|
| 8 |
+
numpy==1.24.0
|
| 9 |
+
joblib==1.3.0
|
| 10 |
+
pandas==2.0.3" > requirements.txt
|
| 11 |
+
|
| 12 |
+
pip install -r requirements.txt
|
scaler.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:e6d4b82ba818bb9a0f1d2e939c36ca625f46e74bd112b9486a84501a6c956afb
|
| 3 |
+
size 1359
|