Sonny4Sonnix commited on
Commit
6b17ed9
·
1 Parent(s): 955fee0

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +47 -34
main.py CHANGED
@@ -2,7 +2,7 @@
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
@@ -23,7 +23,20 @@ class InputFeatures(BaseModel):
23
  age: int
24
 
25
  # Load the pickled XGBoost model
26
- xgb_model = joblib.load("model.joblib")
 
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
 
29
  # @app.get("/")
@@ -33,35 +46,35 @@ xgb_model = joblib.load("model.joblib")
33
  # @app.get("/form/")
34
  # async def show_form():
35
 
36
- @app.post("/predict/")
37
- async def predict_sepsis(
38
- request: Request,
39
- input_data: InputFeatures # Use the Pydantic model for input validation
40
- ):
41
- try:
42
- # Convert Pydantic model to a DataFrame for prediction
43
- input_df = pd.DataFrame([input_data.dict()])
44
-
45
- # Make predictions using the loaded XGBoost model
46
- prediction = xgb_model.predict_proba(xgb.DMatrix(input_df))
47
-
48
- # Create a JSON response
49
- response = {
50
- "input_features": input_data,
51
- "prediction": {
52
- "class_0_probability": prediction[0],
53
- "class_1_probability": prediction[1]
54
- }
55
- }
56
-
57
- return templates.TemplateResponse(
58
- "display_params.html",
59
- {
60
- "request": request,
61
- "input_features": response["input_features"],
62
- "prediction": response["prediction"]
63
- }
64
- )
65
- except Exception as e:
66
- #raise HTTPException(status_code=500, detail="An error occurred while processing the request.")
67
- raise HTTPException(status_code=500, detail=str(e))
 
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
 
23
  age: int
24
 
25
  # Load the pickled XGBoost model
26
+ model_input = joblib.load("model_1.joblib")
27
+
28
+ @app.post("/sepsis_prediction")
29
+ async def predicts(input:model_input):
30
+
31
+ XGB= Pipeline([
32
+ ("col_trans", full_pipeline),
33
+ ("feature_selection", SelectKBest(score_func=f_classif, k='all')),
34
+ ("model", BaggingClassifier(base_estimator=XGBClassifier(random_state=42)))
35
+ ])
36
+ return prediction
37
+
38
+
39
+
40
 
41
 
42
  # @app.get("/")
 
46
  # @app.get("/form/")
47
  # async def show_form():
48
 
49
+ # @app.post("/predict/")
50
+ # async def predict_sepsis(
51
+ # request: Request,
52
+ # input_data: InputFeatures # Use the Pydantic model for input validation
53
+ # ):
54
+ # try:
55
+ # # Convert Pydantic model to a DataFrame for prediction
56
+ # input_df = pd.DataFrame([input_data.dict()])
57
+
58
+ # # Make predictions using the loaded XGBoost model
59
+ # prediction = xgb_model.predict_proba(xgb.DMatrix(input_df))
60
+
61
+ # # Create a JSON response
62
+ # response = {
63
+ # "input_features": input_data,
64
+ # "prediction": {
65
+ # "class_0_probability": prediction[0],
66
+ # "class_1_probability": prediction[1]
67
+ # }
68
+ # }
69
+
70
+ # return templates.TemplateResponse(
71
+ # "display_params.html",
72
+ # {
73
+ # "request": request,
74
+ # "input_features": response["input_features"],
75
+ # "prediction": response["prediction"]
76
+ # }
77
+ # )
78
+ # except Exception as e:
79
+ # #raise HTTPException(status_code=500, detail="An error occurred while processing the request.")
80
+ # raise HTTPException(status_code=500, detail=str(e))