Spaces:
Sleeping
Sleeping
QuophyDzifa
commited on
Commit
•
622b01e
1
Parent(s):
648e993
Upload main.py
Browse files- src/main.py +109 -0
src/main.py
ADDED
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
# Importations
|
3 |
+
|
4 |
+
from typing import Union
|
5 |
+
from fastapi import FastAPI
|
6 |
+
import pickle
|
7 |
+
from pydantic import BaseModel
|
8 |
+
import pandas as pd
|
9 |
+
import os
|
10 |
+
import uvicorn
|
11 |
+
from fastapi import HTTPException, status
|
12 |
+
from sklearn.preprocessing import StandardScaler
|
13 |
+
from sklearn.preprocessing import LabelEncoder
|
14 |
+
|
15 |
+
# Setup Section
|
16 |
+
|
17 |
+
# Create FastAPI instance
|
18 |
+
app = FastAPI(title="Sepsis Prediction API",
|
19 |
+
description="API for Predicting Sespsis ")
|
20 |
+
# A function to load machine Learning components to re-use
|
21 |
+
|
22 |
+
|
23 |
+
def Ml_loading_components(fp):
|
24 |
+
with open(fp, "rb") as f:
|
25 |
+
object = pickle.load(f)
|
26 |
+
return (object)
|
27 |
+
|
28 |
+
|
29 |
+
# Loading the machine learning components
|
30 |
+
DIRPATH = os.path.dirname(os.path.realpath(__file__))
|
31 |
+
ml_core_fp = os.path.join(DIRPATH, "ML", "ML_Model.pkl")
|
32 |
+
ml_components_dict = Ml_loading_components(fp=ml_core_fp)
|
33 |
+
|
34 |
+
|
35 |
+
# Defining the variables for each component
|
36 |
+
label_encoder = ml_components_dict['label_encoder'] # The label encoder
|
37 |
+
# Loaded scaler component
|
38 |
+
scaler = ml_components_dict['scaler']
|
39 |
+
# Loaded model
|
40 |
+
model = ml_components_dict['model']
|
41 |
+
# Defining our input variables
|
42 |
+
|
43 |
+
|
44 |
+
class InputData(BaseModel):
|
45 |
+
PRG: int
|
46 |
+
PL: int
|
47 |
+
BP: int
|
48 |
+
SK: int
|
49 |
+
TS: int
|
50 |
+
BMI: float
|
51 |
+
BD2: float
|
52 |
+
Age: int
|
53 |
+
|
54 |
+
|
55 |
+
"""
|
56 |
+
* PRG: Plasma glucose
|
57 |
+
|
58 |
+
* PL: Blood Work Result-1 (mu U/ml)
|
59 |
+
|
60 |
+
* PR: Blood Pressure (mmHg)
|
61 |
+
|
62 |
+
* SK: Blood Work Result-2(mm)
|
63 |
+
|
64 |
+
* TS: Blood Work Result-3 (muU/ml)
|
65 |
+
|
66 |
+
* M11: Body mass index (weight in kg/(height in m)^2
|
67 |
+
|
68 |
+
* BD2: Blood Work Result-4 (mu U/ml)
|
69 |
+
|
70 |
+
* Age: patients age(years)
|
71 |
+
|
72 |
+
"""
|
73 |
+
# Index route
|
74 |
+
|
75 |
+
|
76 |
+
@app.get("/")
|
77 |
+
def index():
|
78 |
+
return {'message': 'Hello, Welcome to My Sepsis Prediction FastAPI'}
|
79 |
+
|
80 |
+
|
81 |
+
# Create prediction endpoint
|
82 |
+
@app.post("/predict")
|
83 |
+
def predict(df: InputData):
|
84 |
+
|
85 |
+
# Prepare the feature and structure them like in the notebook
|
86 |
+
df = pd.DataFrame([df.dict().values()], columns=df.dict().keys())
|
87 |
+
|
88 |
+
print(f"[Info] The inputed dataframe is : {df.to_markdown()}")
|
89 |
+
age = df['Age']
|
90 |
+
print(age)
|
91 |
+
# Scaling the inputs
|
92 |
+
df_scaled = scaler.transform(df)
|
93 |
+
|
94 |
+
# Prediction
|
95 |
+
raw_prediction = model.predict(df_scaled)
|
96 |
+
|
97 |
+
if raw_prediction == 0:
|
98 |
+
raise HTTPException(status_code=status.HTTP_200_OK,
|
99 |
+
detail="The patient will Not Develop Sepsis")
|
100 |
+
elif raw_prediction == 1:
|
101 |
+
raise HTTPException(status_code=status.HTTP_200_OK,
|
102 |
+
detail="The patient Will Develop Sepsis")
|
103 |
+
else:
|
104 |
+
raise HTTPException(
|
105 |
+
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="Prediction Error")
|
106 |
+
|
107 |
+
|
108 |
+
if __name__ == "__main__":
|
109 |
+
uvicorn.run("main:app", reload=True)
|