Upload 5 files
Browse files- lr_model.joblib +3 -0
- main.py +82 -0
- numerical_imputer.joblib +3 -0
- requirements.txt +8 -0
- scaler.joblib +3 -0
lr_model.joblib
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:b4e6945f0cda3e51575762750345b994c1a0e00422f495f6e20140075b3754dc
|
3 |
+
size 1193
|
main.py
ADDED
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
import joblib
|
3 |
+
from fastapi import FastAPI
|
4 |
+
import uvicorn
|
5 |
+
import numpy as np
|
6 |
+
import os
|
7 |
+
|
8 |
+
app = FastAPI()
|
9 |
+
|
10 |
+
def load_model():
|
11 |
+
cwd = os.getcwd()
|
12 |
+
destination = os.path.join(cwd, "Assets")
|
13 |
+
|
14 |
+
imputer_filepath = os.path.join(destination, "numerical_imputer.joblib")
|
15 |
+
scaler_filepath = os.path.join(destination, "scaler.joblib")
|
16 |
+
model_filepath = os.path.join(destination, "lr_model.joblib")
|
17 |
+
|
18 |
+
num_imputer = joblib.load(imputer_filepath)
|
19 |
+
scaler = joblib.load(scaler_filepath)
|
20 |
+
model = joblib.load(model_filepath)
|
21 |
+
|
22 |
+
return num_imputer, scaler, model
|
23 |
+
|
24 |
+
|
25 |
+
def preprocess_input_data(input_data, num_imputer, scaler):
|
26 |
+
input_data_df = pd.DataFrame([input_data])
|
27 |
+
num_columns = [col for col in input_data_df.columns if input_data_df[col].dtype != 'object']
|
28 |
+
input_data_imputed_num = num_imputer.transform(input_data_df[num_columns])
|
29 |
+
input_scaled_df = pd.DataFrame(scaler.transform(input_data_imputed_num), columns=num_columns)
|
30 |
+
return input_scaled_df
|
31 |
+
|
32 |
+
@app.get("/")
|
33 |
+
def read_root():
|
34 |
+
return "Sepsis Prediction App"
|
35 |
+
|
36 |
+
@app.get("/sepsis/predict")
|
37 |
+
def predict_sepsis_endpoint(PRG: float, PL: float, PR: float, SK: float, TS: float,
|
38 |
+
M11: float, BD2: float, Age: float, Insurance: int):
|
39 |
+
num_imputer, scaler, model = load_model()
|
40 |
+
|
41 |
+
input_data = {
|
42 |
+
'PRG': [PRG],
|
43 |
+
'PL': [PL],
|
44 |
+
'PR': [PR],
|
45 |
+
'SK': [SK],
|
46 |
+
'TS': [TS],
|
47 |
+
'M11': [M11],
|
48 |
+
'BD2': [BD2],
|
49 |
+
'Age': [Age],
|
50 |
+
'Insurance': [Insurance]
|
51 |
+
}
|
52 |
+
|
53 |
+
input_scaled_df = preprocess_input_data(input_data, num_imputer, scaler)
|
54 |
+
|
55 |
+
probabilities = model.predict_proba(input_scaled_df)[0]
|
56 |
+
prediction = np.argmax(probabilities)
|
57 |
+
|
58 |
+
sepsis_status = "Positive" if prediction == 1 else "Negative"
|
59 |
+
|
60 |
+
probability = probabilities[1] if prediction == 1 else probabilities[0]
|
61 |
+
|
62 |
+
#statement = f"The patient is {sepsis_status}. There is a {'high' if prediction == 1 else 'low'} probability ({probability:.2f}) that the patient is susceptible to developing sepsis."
|
63 |
+
|
64 |
+
if prediction == 1:
|
65 |
+
status_icon = "β" # Red 'X' icon for positive sepsis prediction
|
66 |
+
sepsis_explanation = "Sepsis is a life-threatening condition caused by an infection. A positive prediction suggests that the patient might be exhibiting sepsis symptoms and requires immediate medical attention."
|
67 |
+
else:
|
68 |
+
status_icon = "β" # Green checkmark icon for negative sepsis prediction
|
69 |
+
sepsis_explanation = "Sepsis is a life-threatening condition caused by an infection. A negative prediction suggests that the patient is not currently exhibiting sepsis symptoms."
|
70 |
+
|
71 |
+
statement = f"The patient's sepsis status is {sepsis_status} {status_icon} with a probability of {probability:.2f}. {sepsis_explanation}"
|
72 |
+
|
73 |
+
user_input_statement = "Please note this is the user-inputted data: "
|
74 |
+
|
75 |
+
output_df = pd.DataFrame([input_data])
|
76 |
+
|
77 |
+
result = {'predicted_sepsis': sepsis_status, 'statement': statement, 'user_input_statement': user_input_statement, 'input_data_df': output_df.to_dict('records')}
|
78 |
+
|
79 |
+
return result
|
80 |
+
|
81 |
+
if __name__ == "__main__":
|
82 |
+
uvicorn.run(app, host="0.0.0.0", port=8080, reload=True)
|
numerical_imputer.joblib
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:345add991409813d7429e1a0ad969dfc0c770aab4c6029dabb2231c450ba0cc1
|
3 |
+
size 913
|
requirements.txt
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
joblib==1.2.0
|
2 |
+
matplotlib==3.7.1
|
3 |
+
numpy==1.24.2
|
4 |
+
pandas==1.5.3
|
5 |
+
scikit-learn==1.2.2
|
6 |
+
fastapi==0.95.1
|
7 |
+
uvicorn==0.22.0
|
8 |
+
pydantic==1.10.7
|
scaler.joblib
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:d2e124383ce5c05bbb777499908c299b05fda0c87445d602b601794d6261f209
|
3 |
+
size 752
|