Commit
·
8c53a41
1
Parent(s):
940c9eb
Update main.py
Browse files
main.py
CHANGED
@@ -1,63 +1,56 @@
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
app = FastAPI()
|
11 |
templates = Jinja2Templates(directory="templates")
|
12 |
|
13 |
-
|
14 |
-
|
15 |
class InputFeatures(BaseModel):
|
16 |
-
# prg: float
|
17 |
-
# pl: float
|
18 |
-
# pr: float
|
19 |
-
# sk: float
|
20 |
-
# ts: float
|
21 |
-
# m11: float
|
22 |
-
# bd2: float
|
23 |
-
# age: int
|
24 |
-
|
25 |
Plasma_glucose: float
|
26 |
-
Blood_Work_Result_1:float
|
27 |
-
Blood_Pressure
|
28 |
-
Blood_Work_Result_2:float
|
29 |
-
Blood_Work_Result_3:float
|
30 |
-
Body_mass_index
|
31 |
-
Blood_Work_Result_4:float
|
32 |
-
patients_age
|
33 |
|
34 |
# Load the pickled XGBoost model
|
35 |
model_input = joblib.load("model_1.joblib")
|
36 |
|
37 |
-
|
38 |
@app.post("/sepsis_prediction")
|
39 |
-
async def predict(input:
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
-
num_features = [['Plasma_glucose','Blood_Work_Result_1','Blood_Pressure',
|
43 |
-
'Blood_Work_Result_2',' Blood_Work_Result_3', 'Body_mass_index',
|
44 |
-
'Blood_Work_Result_4', 'Age']]
|
45 |
-
|
46 |
-
XGB= Pipeline([
|
47 |
-
("col_trans", full_pipeline),
|
48 |
-
("feature_selection", SelectKBest(score_func=f_classif, k='all')),
|
49 |
-
("model", BaggingClassifier(base_estimator=XGBClassifier(random_state=42)))])
|
50 |
-
|
51 |
-
#print(model_input)
|
52 |
df = pd.DataFrame([input])
|
53 |
-
final_input = np.array(predict_input.fit_transform(df), dtype
|
54 |
-
prediction =
|
55 |
|
56 |
return prediction
|
57 |
|
58 |
if __name__ == '__main__':
|
59 |
-
uvicorn.run("Main:app", reload
|
60 |
-
|
61 |
|
62 |
|
63 |
|
|
|
|
|
1 |
from fastapi import FastAPI, Query, Request, HTTPException
|
2 |
from fastapi.responses import JSONResponse, HTMLResponse
|
3 |
from fastapi.templating import Jinja2Templates
|
|
|
4 |
import joblib
|
5 |
import pandas as pd
|
6 |
+
from pydantic import BaseModel
|
7 |
+
from sklearn.pipeline import Pipeline
|
8 |
+
from sklearn.feature_selection import SelectKBest
|
9 |
+
from sklearn.ensemble import BaggingClassifier
|
10 |
+
from xgboost import XGBClassifier
|
11 |
+
from sklearn.preprocessing import StandardScaler
|
12 |
+
from sklearn.compose import ColumnTransformer
|
13 |
+
from sklearn.feature_selection import f_classif
|
14 |
|
15 |
app = FastAPI()
|
16 |
templates = Jinja2Templates(directory="templates")
|
17 |
|
|
|
|
|
18 |
class InputFeatures(BaseModel):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
Plasma_glucose: float
|
20 |
+
Blood_Work_Result_1: float
|
21 |
+
Blood_Pressure: float
|
22 |
+
Blood_Work_Result_2: float
|
23 |
+
Blood_Work_Result_3: float
|
24 |
+
Body_mass_index: float
|
25 |
+
Blood_Work_Result_4: float
|
26 |
+
patients_age: int
|
27 |
|
28 |
# Load the pickled XGBoost model
|
29 |
model_input = joblib.load("model_1.joblib")
|
30 |
|
|
|
31 |
@app.post("/sepsis_prediction")
|
32 |
+
async def predict(input: InputFeatures):
|
33 |
+
# Numeric Features
|
34 |
+
num_features = [
|
35 |
+
['Plasma_glucose', 'Blood_Work_Result_1', 'Blood_Pressure',
|
36 |
+
'Blood_Work_Result_2', 'Blood_Work_Result_3', 'Body_mass_index',
|
37 |
+
'Blood_Work_Result_4', 'patients_age']
|
38 |
+
]
|
39 |
+
|
40 |
+
XGB = Pipeline([
|
41 |
+
("col_trans", full_pipeline), # You need to define full_pipeline
|
42 |
+
("feature_selection", SelectKBest(score_func=f_classif, k='all')),
|
43 |
+
("model", BaggingClassifier(base_estimator=XGBClassifier(random_state=42)))
|
44 |
+
])
|
45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
df = pd.DataFrame([input])
|
47 |
+
final_input = np.array(predict_input.fit_transform(df), dtype=np.str) # Check predict_input, maybe it should be XGB
|
48 |
+
prediction = model_input.predict(np.array([final_input]).reshape(1, -1))
|
49 |
|
50 |
return prediction
|
51 |
|
52 |
if __name__ == '__main__':
|
53 |
+
uvicorn.run("Main:app", reload=True)
|
|
|
54 |
|
55 |
|
56 |
|