|
from fastapi import FastAPI, Query, Request, HTTPException |
|
from fastapi.responses import JSONResponse, HTMLResponse |
|
from fastapi.templating import Jinja2Templates |
|
import joblib |
|
import pandas as pd |
|
import numpy as np |
|
from pydantic import BaseModel |
|
from sklearn.pipeline import Pipeline |
|
from sklearn.feature_selection import SelectKBest |
|
from sklearn.ensemble import BaggingClassifier |
|
from xgboost import XGBClassifier |
|
from sklearn.preprocessing import StandardScaler |
|
from sklearn.compose import ColumnTransformer |
|
from sklearn.feature_selection import f_classif |
|
from sklearn.impute import SimpleImputer |
|
|
|
|
|
|
|
app = FastAPI() |
|
templates = Jinja2Templates(directory="templates") |
|
|
|
class InputFeatures(BaseModel): |
|
Plasma_glucose: float |
|
Blood_Work_Result_1: float |
|
Blood_Pressure: float |
|
Blood_Work_Result_2: float |
|
Blood_Work_Result_3: float |
|
Body_mass_index: float |
|
Blood_Work_Result_4: float |
|
patients_age: int |
|
|
|
|
|
model_input = joblib.load("model_1.joblib") |
|
|
|
@app.post("/sepsis_prediction") |
|
async def predict(input: InputFeatures): |
|
|
|
num_attr = [ |
|
['Plasma_glucose', 'Blood_Work_Result_1', 'Blood_Pressure', |
|
'Blood_Work_Result_2', 'Blood_Work_Result_3', 'Body_mass_index', |
|
'Blood_Work_Result_4', 'patients_age']] |
|
|
|
|
|
num_pipeline= Pipeline([('imputer', SimpleImputer()),('scaler', StandardScaler())]) |
|
|
|
|
|
full_pipeline=ColumnTransformer([('num_pipe',num_pipeline,num_attr)]) |
|
|
|
XGB = Pipeline([ |
|
("col_trans", full_pipeline), |
|
("feature_selection", SelectKBest(score_func=f_classif, k='all')), |
|
("model", BaggingClassifier(base_estimator=XGBClassifier(random_state=42))) |
|
]) |
|
|
|
df = pd.DataFrame([input]) |
|
final_input = np.array(predict_input.fit_transform(df), dtype=np.str) |
|
prediction = model_input.predict(np.array([final_input]).reshape(1, -1)) |
|
|
|
return prediction |
|
|
|
if __name__ == '__main__': |
|
uvicorn.run("Main:app", reload=True) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|