Sonny4Sonnix commited on
Commit
f349339
1 Parent(s): 33bdf17

Create main.py

Browse files
Files changed (1) hide show
  1. main.py +54 -73
main.py CHANGED
@@ -1,83 +1,64 @@
1
- from fastapi import FastAPI
2
- from fastapi.responses import HTMLResponse
 
3
  from fastapi.templating import Jinja2Templates
4
- from fastapi import FastAPI, Query, Request
5
-
6
- from pydantic import BaseModel
7
- from transformers import pipeline
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
- app = FastAPI()
18
- templates = Jinja2Templates(directory="templates")
19
-
20
- @app.get("/display")
21
- def display_params(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  request: Request,
23
- prg: float = Query(..., description="Plasma glucose"),
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
- #prepare input features for prediction
33
- input_features = [prg,pl,pr,sk,ts,m11,bd2,age]
34
-
35
- #Make predictions using the loaded model
36
- prediction = xgb_model.predict([input_features])[0]
37
-
38
- return templates.TemplateResponse(
39
- "display_params.html",
40
- {
41
- "request": request,
42
- "prg": prg,
43
- "pl": pl,
44
- "pr": pr,
45
- "sk": sk,
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
- # @app.get("/display")
82
- # def display_params(request: Request, item_id: int, q: Union[str, None] = None):
83
- # return templates.TemplateResponse("display_params.html", {"request": request, "item_id": item_id, "q": q})
 
 
 
 
 
 
 
 
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.")