Spaces:
Sleeping
Sleeping
QuophyDzifa
commited on
Commit
•
648e993
1
Parent(s):
d6f2203
Delete main.py
Browse files
main.py
DELETED
@@ -1,135 +0,0 @@
|
|
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 |
-
|
24 |
-
|
25 |
-
|
26 |
-
# def Ml_loading_components(fp):
|
27 |
-
# with open(fp, "rb") as f:
|
28 |
-
# object = pickle.load(f)
|
29 |
-
# return (object)
|
30 |
-
|
31 |
-
|
32 |
-
# # Loading the machine learning components
|
33 |
-
# DIRPATH = os.path.dirname(os.path.realpath(__file__))
|
34 |
-
# ml_core_fp = os.path.join(DIRPATH, "src", "ML", "ML_Model.pkl")
|
35 |
-
# ml_components_dict = Ml_loading_components(fp=ml_core_fp)
|
36 |
-
|
37 |
-
|
38 |
-
# # Defining the variables for each component
|
39 |
-
# label_encoder = ml_components_dict['label_encoder'] # The label encoder
|
40 |
-
# # Loaded scaler component
|
41 |
-
# scaler = ml_components_dict['scaler']
|
42 |
-
# # Loaded model
|
43 |
-
# model = ml_components_dict['model']
|
44 |
-
# # Defining our input variables
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
# Define the path to your pickle file
|
50 |
-
pickle_file_path = "src/ML/ML_Model.pkl"
|
51 |
-
|
52 |
-
# Check if the pickle file exists
|
53 |
-
if not os.path.exists(pickle_file_path):
|
54 |
-
raise Exception(f"Pickle file '{pickle_file_path}' not found. Please check the file path.")
|
55 |
-
|
56 |
-
# Load your pre-trained machine learning model and preprocessing components using pickle
|
57 |
-
with open(pickle_file_path, "rb") as model_file:
|
58 |
-
exported_data = pickle.load(model_file)
|
59 |
-
|
60 |
-
# Extract the pre-fitted preprocessing components and model from the loaded components
|
61 |
-
scaler = exported_data['scaler']
|
62 |
-
label_encoder = exported_data['label_encoder']
|
63 |
-
model = exported_data['best_model']
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
class InputData(BaseModel):
|
71 |
-
PRG: int
|
72 |
-
PL: int
|
73 |
-
BP: int
|
74 |
-
SK: int
|
75 |
-
TS: int
|
76 |
-
BMI: float
|
77 |
-
BD2: float
|
78 |
-
Age: int
|
79 |
-
|
80 |
-
|
81 |
-
"""
|
82 |
-
* PRG: Plasma glucose
|
83 |
-
|
84 |
-
* PL: Blood Work Result-1 (mu U/ml)
|
85 |
-
|
86 |
-
* PR: Blood Pressure (mmHg)
|
87 |
-
|
88 |
-
* SK: Blood Work Result-2(mm)
|
89 |
-
|
90 |
-
* TS: Blood Work Result-3 (muU/ml)
|
91 |
-
|
92 |
-
* M11: Body mass index (weight in kg/(height in m)^2
|
93 |
-
|
94 |
-
* BD2: Blood Work Result-4 (mu U/ml)
|
95 |
-
|
96 |
-
* Age: patients age(years)
|
97 |
-
|
98 |
-
"""
|
99 |
-
# Index route
|
100 |
-
|
101 |
-
|
102 |
-
@app.get("/")
|
103 |
-
def index():
|
104 |
-
return {'message': 'Hello, Welcome to My Sepsis Prediction FastAPI'}
|
105 |
-
|
106 |
-
|
107 |
-
# Create prediction endpoint
|
108 |
-
@app.post("/predict")
|
109 |
-
def predict(df: InputData):
|
110 |
-
|
111 |
-
# Prepare the feature and structure them like in the notebook
|
112 |
-
df = pd.DataFrame([df.dict().values()], columns=df.dict().keys())
|
113 |
-
|
114 |
-
print(f"[Info] The inputed dataframe is : {df.to_markdown()}")
|
115 |
-
age = df['Age']
|
116 |
-
print(age)
|
117 |
-
# Scaling the inputs
|
118 |
-
df_scaled = scaler.transform(df)
|
119 |
-
|
120 |
-
# Prediction
|
121 |
-
raw_prediction = model.predict(df_scaled)
|
122 |
-
|
123 |
-
if raw_prediction == 0:
|
124 |
-
raise HTTPException(status_code=status.HTTP_200_OK,
|
125 |
-
detail="The patient will Not Develop Sepsis")
|
126 |
-
elif raw_prediction == 1:
|
127 |
-
raise HTTPException(status_code=status.HTTP_200_OK,
|
128 |
-
detail="The patient Will Develop Sepsis")
|
129 |
-
else:
|
130 |
-
raise HTTPException(
|
131 |
-
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="Prediction Error")
|
132 |
-
|
133 |
-
|
134 |
-
if __name__ == "__main__":
|
135 |
-
uvicorn.run("src.main:app", reload=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|