Spaces:
Paused
Paused
Create main.py
Browse files
main.py
ADDED
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Import key libraries and packages
|
2 |
+
from fastapi import FastAPI
|
3 |
+
import pickle
|
4 |
+
import uvicorn
|
5 |
+
import pandas as pd
|
6 |
+
import os
|
7 |
+
import sys
|
8 |
+
|
9 |
+
# Define directory paths
|
10 |
+
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
|
11 |
+
|
12 |
+
DIRPATH = os.path.dirname(os.path.realpath(__file__))
|
13 |
+
ml_component= os.path.join(DIRPATH, "..", "src", "assets", "ml_sepsis.pkl")
|
14 |
+
|
15 |
+
# Function to load pickle file
|
16 |
+
def load_pickle(filename):
|
17 |
+
with open(filename, 'rb') as file:
|
18 |
+
data = pickle.load(file)
|
19 |
+
return data
|
20 |
+
|
21 |
+
# Load pickle file
|
22 |
+
ml_components = load_pickle(ml_component)
|
23 |
+
|
24 |
+
# Components in the pickle file
|
25 |
+
ml_model = ml_components['model']
|
26 |
+
pipeline_processing = ml_components['pipeline']
|
27 |
+
|
28 |
+
# API base configuration
|
29 |
+
app = FastAPI()
|
30 |
+
|
31 |
+
# Endpoints
|
32 |
+
|
33 |
+
@app.get('/Predict_Sepsis')
|
34 |
+
async def predict(Plasma_glucose: int, Blood_Work_Result_1: int,
|
35 |
+
Blood_Pressure: int, Blood_Work_Result_2: int,
|
36 |
+
Blood_Work_Result_3: int, Body_mass_index: float,
|
37 |
+
Blood_Work_Result_4: float,Age: int, Insurance:float):
|
38 |
+
|
39 |
+
data = pd.DataFrame({'Plasma glucose': [Plasma_glucose], 'Blood Work Result-1': [Blood_Work_Result_1],
|
40 |
+
'Blood Pressure': [Blood_Pressure], 'Blood Work Result-2': [Blood_Work_Result_2],
|
41 |
+
'Blood Work Result-3': [Blood_Work_Result_3], 'Body mass index': [Body_mass_index],
|
42 |
+
'Blood Work Result-4': [Blood_Work_Result_4], 'Age': [Age], 'Insurance':[Insurance]})
|
43 |
+
|
44 |
+
data_prepared = pipeline_processing.transform(data)
|
45 |
+
|
46 |
+
model_output = ml_model.predict(data_prepared).tolist()
|
47 |
+
|
48 |
+
prediction = make_prediction(model_output)
|
49 |
+
|
50 |
+
return prediction
|
51 |
+
|
52 |
+
#Prediction endpoints
|
53 |
+
def make_prediction(data_prepared):
|
54 |
+
|
55 |
+
output_pred = data_prepared
|
56 |
+
|
57 |
+
if output_pred == 0:
|
58 |
+
output_pred = "Sepsis status is Negative"
|
59 |
+
else:
|
60 |
+
output_pred = "Sepsis status is Positive"
|
61 |
+
|
62 |
+
return output_pred
|
63 |
+
|
64 |
+
if __name__=='__main__':
|
65 |
+
uvicorn.run('main:app', reload=True)
|