Commit
·
9ebc0c2
1
Parent(s):
ab7726e
updated
Browse files
main.py
ADDED
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
#app.mount("/static", StaticFiles(directory="static"), name="static")
|
10 |
+
#templates = Jinja2Templates(directory="templates")
|
11 |
+
|
12 |
+
def load_model():
|
13 |
+
cwd = os.getcwd()
|
14 |
+
destination = os.path.join(cwd, "Assets")
|
15 |
+
|
16 |
+
imputer_filepath = os.path.join(destination, "numerical_imputer.joblib")
|
17 |
+
scaler_filepath = os.path.join(destination, "scaler.joblib")
|
18 |
+
model_filepath = os.path.join(destination, "lr_model.joblib")
|
19 |
+
|
20 |
+
num_imputer = joblib.load(imputer_filepath)
|
21 |
+
scaler = joblib.load(scaler_filepath)
|
22 |
+
model = joblib.load(model_filepath)
|
23 |
+
|
24 |
+
return num_imputer, scaler, model
|
25 |
+
|
26 |
+
|
27 |
+
def preprocess_input_data(input_data, num_imputer, scaler):
|
28 |
+
input_data_df = pd.DataFrame([input_data])
|
29 |
+
num_columns = [col for col in input_data_df.columns if input_data_df[col].dtype != 'object']
|
30 |
+
input_data_imputed_num = num_imputer.transform(input_data_df[num_columns])
|
31 |
+
input_scaled_df = pd.DataFrame(scaler.transform(input_data_imputed_num), columns=num_columns)
|
32 |
+
return input_scaled_df
|
33 |
+
|
34 |
+
@app.get("/")
|
35 |
+
def read_root():
|
36 |
+
return "Sepsis Prediction App"
|
37 |
+
|
38 |
+
@app.get("/sepsis/predict")
|
39 |
+
def predict_sepsis_endpoint(PRG: float, PL: float, PR: float, SK: float, TS: float,
|
40 |
+
M11: float, BD2: float, Age: float, Insurance: int):
|
41 |
+
num_imputer, scaler, model = load_model()
|
42 |
+
|
43 |
+
input_data = {
|
44 |
+
'PRG': [PRG],
|
45 |
+
'PL': [PL],
|
46 |
+
'PR': [PR],
|
47 |
+
'SK': [SK],
|
48 |
+
'TS': [TS],
|
49 |
+
'M11': [M11],
|
50 |
+
'BD2': [BD2],
|
51 |
+
'Age': [Age],
|
52 |
+
'Insurance': [Insurance]
|
53 |
+
}
|
54 |
+
|
55 |
+
input_scaled_df = preprocess_input_data(input_data, num_imputer, scaler)
|
56 |
+
|
57 |
+
probabilities = model.predict_proba(input_scaled_df)[0]
|
58 |
+
prediction = np.argmax(probabilities)
|
59 |
+
|
60 |
+
sepsis_status = "Positive" if prediction == 1 else "Negative"
|
61 |
+
|
62 |
+
probability = probabilities[1] if prediction == 1 else probabilities[0]
|
63 |
+
|
64 |
+
#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."
|
65 |
+
|
66 |
+
if prediction == 1:
|
67 |
+
status_icon = "✔" # Red 'X' icon for positive sepsis prediction
|
68 |
+
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."
|
69 |
+
else:
|
70 |
+
status_icon = "✘" # Green checkmark icon for negative sepsis prediction
|
71 |
+
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."
|
72 |
+
|
73 |
+
statement = f"The patient's sepsis status is {sepsis_status} {status_icon} with a probability of {probability:.2f}. {sepsis_explanation}"
|
74 |
+
|
75 |
+
user_input_statement = "Please note this is the user-inputted data: "
|
76 |
+
|
77 |
+
output_df = pd.DataFrame([input_data])
|
78 |
+
|
79 |
+
result = {'predicted_sepsis': sepsis_status, 'statement': statement, 'user_input_statement': user_input_statement, 'input_data_df': output_df.to_dict('records')}
|
80 |
+
|
81 |
+
return result
|
82 |
+
|
83 |
+
if __name__ == "__main__":
|
84 |
+
uvicorn.run(app, host="0.0.0.0", port=7860, reload=True)
|