Sonny4Sonnix
commited on
Commit
•
f349339
1
Parent(s):
33bdf17
Create main.py
Browse files
main.py
CHANGED
@@ -1,83 +1,64 @@
|
|
1 |
-
|
2 |
-
from fastapi
|
|
|
3 |
from fastapi.templating import Jinja2Templates
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
from
|
8 |
-
from typing import Union
|
9 |
-
import pickle
|
10 |
-
import joblib
|
11 |
|
|
|
|
|
12 |
|
13 |
# Load the pickled XGBoost model
|
14 |
xgb_model = joblib.load("xgb_model.joblib")
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
request: Request,
|
23 |
-
|
24 |
-
pl: float = Query(..., description="Blood Work Result-1 (mu U/ml)"),
|
25 |
-
pr: float = Query(..., description="Blood Pressure (mm Hg)"),
|
26 |
-
sk: float = Query(..., description="Blood Work Result-2 (mm)"),
|
27 |
-
ts: float = Query(..., description="Blood Work Result-3 (mu U/ml)"),
|
28 |
-
m11: float = Query(..., description="Body mass index (weight in kg/(height in m)^2"),
|
29 |
-
bd2: float = Query(..., description="Blood Work Result-4 (mu U/ml)"),
|
30 |
-
age: int = Query(..., description="Patient's age (years)")
|
31 |
):
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
{
|
41 |
-
"
|
42 |
-
"
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
"ts": ts,
|
47 |
-
"m11": m11,
|
48 |
-
"bd2": bd2,
|
49 |
-
"age": age
|
50 |
-
"prediction": prediction # Include the prediction in the response
|
51 |
}
|
52 |
-
)
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
# class Item(BaseModel):
|
62 |
-
# name: str
|
63 |
-
# price: float
|
64 |
-
# is_offer: Union[bool, None] = None
|
65 |
-
|
66 |
-
|
67 |
-
# @app.get("/")
|
68 |
-
# def read_root():
|
69 |
-
# return {"Hello": "World"}
|
70 |
-
|
71 |
-
|
72 |
-
# @app.get("/items/{item_id}")
|
73 |
-
# def read_item(item_id: int, q: Union[str, None] = None):
|
74 |
-
# return {"item_id": item_id, "q": q}
|
75 |
-
|
76 |
-
|
77 |
-
# @app.put("/items/{item_id}")
|
78 |
-
# def update_item(item_id: int, item: Item):
|
79 |
-
# return {"item_name": item.name, "item_id": item_id}
|
80 |
|
81 |
-
|
82 |
-
|
83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# main.py
|
2 |
+
from fastapi import FastAPI, Query, Request, HTTPException
|
3 |
+
from fastapi.responses import JSONResponse, HTMLResponse
|
4 |
from fastapi.templating import Jinja2Templates
|
5 |
+
import xgboost as xgb
|
6 |
+
import joblib
|
7 |
+
import pandas as pd
|
8 |
+
from pydantic import BaseModel # Import Pydantic's BaseModel
|
|
|
|
|
|
|
9 |
|
10 |
+
app = FastAPI()
|
11 |
+
templates = Jinja2Templates(directory="templates")
|
12 |
|
13 |
# Load the pickled XGBoost model
|
14 |
xgb_model = joblib.load("xgb_model.joblib")
|
15 |
|
16 |
+
class InputFeatures(BaseModel):
|
17 |
+
prg: float
|
18 |
+
pl: float
|
19 |
+
pr: float
|
20 |
+
sk: float
|
21 |
+
ts: float
|
22 |
+
m11: float
|
23 |
+
bd2: float
|
24 |
+
age: int
|
25 |
+
|
26 |
+
@app.get("/")
|
27 |
+
async def read_root():
|
28 |
+
return {"message": "Welcome to the XGBoost Diabetes Prediction API"}
|
29 |
+
|
30 |
+
@app.get("/form/")
|
31 |
+
async def show_form():
|
32 |
+
return templates.TemplateResponse("input_form.html", {"request": None})
|
33 |
+
|
34 |
+
@app.post("/predict/")
|
35 |
+
async def predict_diabetes(
|
36 |
request: Request,
|
37 |
+
input_data: InputFeatures # Use the Pydantic model for input validation
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
):
|
39 |
+
try:
|
40 |
+
# Convert Pydantic model to a DataFrame for prediction
|
41 |
+
input_df = pd.DataFrame([input_data.dict()])
|
42 |
+
|
43 |
+
# Make predictions using the loaded XGBoost model
|
44 |
+
prediction = xgb_model.predict_proba(xgb.DMatrix(input_df))
|
45 |
+
|
46 |
+
# Create a JSON response
|
47 |
+
response = {
|
48 |
+
"input_features": input_data,
|
49 |
+
"prediction": {
|
50 |
+
"class_0_probability": prediction[0],
|
51 |
+
"class_1_probability": prediction[1]
|
52 |
+
}
|
|
|
|
|
|
|
|
|
|
|
53 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
|
55 |
+
return templates.TemplateResponse(
|
56 |
+
"display_params.html",
|
57 |
+
{
|
58 |
+
"request": request,
|
59 |
+
"input_features": response["input_features"],
|
60 |
+
"prediction": response["prediction"]
|
61 |
+
}
|
62 |
+
)
|
63 |
+
except Exception as e:
|
64 |
+
raise HTTPException(status_code=500, detail="An error occurred while processing the request.")
|