Spaces:
No application file
No application file
Upload 4 files
Browse files- DockerFile +17 -0
- main.py +67 -0
- requirements.txt +7 -0
- xgb_model_reg.pkl +3 -0
DockerFile
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.10
|
| 2 |
+
|
| 3 |
+
# Set working directory
|
| 4 |
+
WORKDIR /code
|
| 5 |
+
|
| 6 |
+
# Install dependencies
|
| 7 |
+
COPY requirements.txt .
|
| 8 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 9 |
+
|
| 10 |
+
# Copy all files
|
| 11 |
+
COPY . .
|
| 12 |
+
|
| 13 |
+
# Expose API port
|
| 14 |
+
EXPOSE 7860
|
| 15 |
+
|
| 16 |
+
# Start FastAPI app
|
| 17 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
main.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from fastapi.responses import JSONResponse
|
| 3 |
+
from pydantic import BaseModel, Field, computed_field
|
| 4 |
+
from typing import Literal, Annotated
|
| 5 |
+
import pickle
|
| 6 |
+
import pandas as pd
|
| 7 |
+
import joblib
|
| 8 |
+
import traceback
|
| 9 |
+
|
| 10 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 11 |
+
|
| 12 |
+
# Path to your saved pickle file
|
| 13 |
+
model_path = "xgb_model_reg.pkl"
|
| 14 |
+
|
| 15 |
+
# Load the model
|
| 16 |
+
model = joblib.load(model_path)
|
| 17 |
+
|
| 18 |
+
app=FastAPI()
|
| 19 |
+
|
| 20 |
+
app.add_middleware(
|
| 21 |
+
CORSMiddleware,
|
| 22 |
+
allow_origins=["*"], # In production, use specific domains
|
| 23 |
+
allow_methods=["GET", "POST"],
|
| 24 |
+
allow_headers=["*"],
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
#pydantic Model to validate data
|
| 28 |
+
class UserInput(BaseModel):
|
| 29 |
+
age: Annotated[int, Field(gt=0,description='Age of the Patient')]
|
| 30 |
+
albumin_gL: Annotated[float, Field(gt=0,description='Quantity of Albumin in gL')]
|
| 31 |
+
creat_umol: Annotated[float, Field(gt=0,description='Quantity of Creatnine in umol')]
|
| 32 |
+
glucose_mmol: Annotated[float, Field(gt=0,description='Qunatity of Glucose in mmol')]
|
| 33 |
+
lncrp:Annotated[float, Field(gt=0,description='Log of Crp')]
|
| 34 |
+
lymph: Annotated[float, Field(gt=0,description='lym ph')]
|
| 35 |
+
mcv: Annotated[float, Field(gt=0,description='mcv')]
|
| 36 |
+
rdw: Annotated[float, Field(gt=0,description='rdw')]
|
| 37 |
+
alp: Annotated[float, Field(gt=0,description='alp')]
|
| 38 |
+
wbc: Annotated[float, Field(gt=0,description='white blood cell')]
|
| 39 |
+
|
| 40 |
+
@app.post('/predict')
|
| 41 |
+
def predict_premium(data: UserInput):
|
| 42 |
+
try:
|
| 43 |
+
input_df = pd.DataFrame(
|
| 44 |
+
[
|
| 45 |
+
{
|
| 46 |
+
'age': data.age,
|
| 47 |
+
'albumin_gL': data.albumin_gL,
|
| 48 |
+
'creat_umol': data.creat_umol,
|
| 49 |
+
'glucose_mmol': data.glucose_mmol,
|
| 50 |
+
'lncrp': data.lncrp,
|
| 51 |
+
'lymph': data.lymph,
|
| 52 |
+
'mcv': data.mcv,
|
| 53 |
+
'rdw': data.rdw,
|
| 54 |
+
'alp': data.alp,
|
| 55 |
+
'wbc': data.wbc
|
| 56 |
+
}
|
| 57 |
+
]
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
prediction_value = float(model.predict(input_df)[0]) # <-- FIXED HERE
|
| 61 |
+
|
| 62 |
+
return JSONResponse(
|
| 63 |
+
status_code=200,
|
| 64 |
+
content={"Predicted Biological Age of Patient": prediction_value}
|
| 65 |
+
)
|
| 66 |
+
except Exception as e:
|
| 67 |
+
return JSONResponse(status_code=500, content={"error": str(e)})
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
pydantic
|
| 4 |
+
pandas
|
| 5 |
+
joblib
|
| 6 |
+
scikit-learn
|
| 7 |
+
xgboost
|
xgb_model_reg.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:3c5645af0edb995d8d995921a3454e9ffa1df735d8c60c89d4d5075afea3f200
|
| 3 |
+
size 4417070
|