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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# from fastapi import FastAPI,Form, Body,Path
|
| 2 |
+
# from typing import Annotated
|
| 3 |
+
# from pydantic import BaseModel, Field
|
| 4 |
+
# import joblib
|
| 5 |
+
# import pandas as pd
|
| 6 |
+
# import numpy as np
|
| 7 |
+
# import uvicorn
|
| 8 |
+
# from fastapi.responses import JSONResponse
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
# app = FastAPI()
|
| 12 |
+
|
| 13 |
+
# # Load the numerical imputer, scaler, and model
|
| 14 |
+
# num_imputer_filepath = "joblib_files/numerical_imputer.joblib"
|
| 15 |
+
# scaler_filepath = "joblib_files/scaler.joblib"
|
| 16 |
+
# model_filepath = "joblib_files/lr_model.joblib"
|
| 17 |
+
|
| 18 |
+
# num_imputer = joblib.load(num_imputer_filepath)
|
| 19 |
+
# scaler = joblib.load(scaler_filepath)
|
| 20 |
+
# model = joblib.load(model_filepath)
|
| 21 |
+
|
| 22 |
+
# class PatientData(BaseModel):
|
| 23 |
+
# PRG: float
|
| 24 |
+
# PL: float
|
| 25 |
+
# PR: float
|
| 26 |
+
# SK: float
|
| 27 |
+
# TS: float
|
| 28 |
+
# M11: float
|
| 29 |
+
# BD2: float
|
| 30 |
+
# Age: float
|
| 31 |
+
# Insurance: int
|
| 32 |
+
|
| 33 |
+
# def preprocess_input_data(user_input):
|
| 34 |
+
# input_data_df = pd.DataFrame([user_input])
|
| 35 |
+
# num_columns = [col for col in input_data_df.columns if input_data_df[col].dtype != 'object']
|
| 36 |
+
# input_data_imputed_num = num_imputer.transform(input_data_df[num_columns])
|
| 37 |
+
# input_scaled_df = pd.DataFrame(scaler.transform(input_data_imputed_num), columns=num_columns)
|
| 38 |
+
# return input_scaled_df
|
| 39 |
+
|
| 40 |
+
# @app.get("/")
|
| 41 |
+
# def read_root():
|
| 42 |
+
# return "Sepsis Prediction App"
|
| 43 |
+
# @app.post("/sepsis/predict")
|
| 44 |
+
# def get_data_from_user(data:PatientData):
|
| 45 |
+
# user_input = data.dict()
|
| 46 |
+
# input_scaled_df = preprocess_input_data(user_input)
|
| 47 |
+
# probabilities = model.predict_proba(input_scaled_df)[0]
|
| 48 |
+
# prediction = np.argmax(probabilities)
|
| 49 |
+
|
| 50 |
+
# sepsis_status = "Positive" if prediction == 1 else "Negative"
|
| 51 |
+
# probability = probabilities[1] if prediction == 1 else probabilities[0]
|
| 52 |
+
|
| 53 |
+
# if prediction == 1:
|
| 54 |
+
# sepsis_explanation = "A positive prediction suggests that the patient might be exhibiting sepsis symptoms and requires immediate medical attention."
|
| 55 |
+
# else:
|
| 56 |
+
# sepsis_explanation = "A negative prediction suggests that the patient is not currently exhibiting sepsis symptoms."
|
| 57 |
+
|
| 58 |
+
# statement = f"The patient's sepsis status is {sepsis_status} with a probability of {probability:.2f}. {sepsis_explanation}"
|
| 59 |
+
|
| 60 |
+
# user_input_statement = "user-inputted data: "
|
| 61 |
+
# output_df = pd.DataFrame([user_input])
|
| 62 |
+
|
| 63 |
+
# result = {'predicted_sepsis': sepsis_status, 'statement': statement, 'user_input_statement': user_input_statement, 'input_data_df': output_df.to_dict('records')}
|
| 64 |
+
# return result
|
| 65 |
+
|
| 66 |
+
# from fastapi import FastAPI, Form
|
| 67 |
+
# from pydantic import BaseModel
|
| 68 |
+
# import joblib
|
| 69 |
+
# import pandas as pd
|
| 70 |
+
# import numpy as np
|
| 71 |
+
# import uvicorn
|
| 72 |
+
# from fastapi.responses import JSONResponse
|
| 73 |
+
|
| 74 |
+
# app = FastAPI()
|
| 75 |
+
|
| 76 |
+
# # Load the entire pipeline
|
| 77 |
+
# pipeline_filepath = "pipeline.joblib"
|
| 78 |
+
# pipeline = joblib.load(pipeline_filepath)
|
| 79 |
+
|
| 80 |
+
# class PatientData(BaseModel):
|
| 81 |
+
# PRG: float
|
| 82 |
+
# PL: float
|
| 83 |
+
# PR: float
|
| 84 |
+
# SK: float
|
| 85 |
+
# TS: float
|
| 86 |
+
# M11: float
|
| 87 |
+
# BD2: float
|
| 88 |
+
# Age: float
|
| 89 |
+
# Insurance: int
|
| 90 |
+
|
| 91 |
+
# @app.get("/")
|
| 92 |
+
# def read_root():
|
| 93 |
+
# explanation = {
|
| 94 |
+
# 'message': "Welcome to the Sepsis Prediction App",
|
| 95 |
+
# 'description': "This API allows you to predict sepsis based on patient data.",
|
| 96 |
+
# 'usage': "Submit a POST request to /predict with patient data to make predictions.",
|
| 97 |
+
# 'input_fields': {
|
| 98 |
+
# 'PRG': 'Plasma_glucose',
|
| 99 |
+
# 'PL': 'Blood_Work_Result_1',
|
| 100 |
+
# 'PR': 'Blood_Pressure',
|
| 101 |
+
# 'SK': 'Blood_Work_Result_2',
|
| 102 |
+
# 'TS': 'Blood_Work_Result_3',
|
| 103 |
+
# 'M11': 'Body_mass_index',
|
| 104 |
+
# 'BD2': 'Blood_Work_Result_4',
|
| 105 |
+
# 'Insurance': 'Sepsis (Positive = 1, Negative = 0)'
|
| 106 |
+
# }
|
| 107 |
+
# }
|
| 108 |
+
# return explanation
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
# @app.post("/predict")
|
| 112 |
+
# def get_data_from_user(data: PatientData):
|
| 113 |
+
# user_input = data.model_dump()
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
# input_df = pd.DataFrame([user_input])
|
| 117 |
+
# # Make predictions using the loaded pipeline
|
| 118 |
+
# # Make predictions using the loaded pipeline
|
| 119 |
+
# predictions = pipeline.predict(user_input)
|
| 120 |
+
# probabilities = pipeline.decision_function(user_input)
|
| 121 |
+
|
| 122 |
+
# # Assuming the pipeline uses a Logistic Regression model
|
| 123 |
+
# probability_of_positive_class = probabilities[0]
|
| 124 |
+
|
| 125 |
+
# # Calculate the prediction
|
| 126 |
+
# prediction = 1 if probability_of_positive_class >= 0.5 else 0
|
| 127 |
+
|
| 128 |
+
# sepsis_status = "Positive" if prediction == 1 else "Negative"
|
| 129 |
+
# sepsis_explanation = "A positive prediction suggests that the patient might be exhibiting sepsis symptoms and requires immediate medical attention." if prediction == 1 else "A negative prediction suggests that the patient is not currently exhibiting sepsis symptoms."
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
# if prediction == 1:
|
| 133 |
+
# sepsis_status = "Positive"
|
| 134 |
+
# sepsis_explanation = "A positive prediction suggests that the patient might be exhibiting sepsis symptoms and requires immediate medical attention."
|
| 135 |
+
# else:
|
| 136 |
+
# sepsis_status = "Negative"
|
| 137 |
+
# sepsis_explanation = "A negative prediction suggests that the patient is not currently exhibiting sepsis symptoms."
|
| 138 |
+
|
| 139 |
+
# result = {
|
| 140 |
+
# 'predicted_sepsis': sepsis_status,
|
| 141 |
+
# 'sepsis_explanation': sepsis_explanation
|
| 142 |
+
# }
|
| 143 |
+
# return result
|
| 144 |
+
|
| 145 |
+
from fastapi import FastAPI
|
| 146 |
+
from pydantic import BaseModel
|
| 147 |
+
import joblib
|
| 148 |
+
import pandas as pd
|
| 149 |
+
import numpy as np
|
| 150 |
+
from sklearn.preprocessing import StandardScaler
|
| 151 |
+
from sklearn.impute import SimpleImputer
|
| 152 |
+
from sklearn.compose import ColumnTransformer
|
| 153 |
+
from sklearn.pipeline import Pipeline
|
| 154 |
+
from sklearn.linear_model import LogisticRegression
|
| 155 |
+
|
| 156 |
+
app = FastAPI()
|
| 157 |
+
|
| 158 |
+
# Load the entire pipeline
|
| 159 |
+
pipeline_filepath = "pipeline.joblib"
|
| 160 |
+
pipeline = joblib.load(pipeline_filepath)
|
| 161 |
+
|
| 162 |
+
class PatientData(BaseModel):
|
| 163 |
+
Plasma_glucose : float
|
| 164 |
+
Blood_Work_Result_1: float
|
| 165 |
+
Blood_Pressure : float
|
| 166 |
+
Blood_Work_Result_2 : float
|
| 167 |
+
Blood_Work_Result_3 : float
|
| 168 |
+
Body_mass_index : float
|
| 169 |
+
Blood_Work_Result_4: float
|
| 170 |
+
Age: float
|
| 171 |
+
Insurance: int
|
| 172 |
+
|
| 173 |
+
@app.get("/")
|
| 174 |
+
def read_root():
|
| 175 |
+
explanation = {
|
| 176 |
+
'message': "Welcome to the Sepsis Prediction App",
|
| 177 |
+
'description': "This API allows you to predict sepsis based on patient data.",
|
| 178 |
+
'usage': "Submit a POST request to /predict with patient data to make predictions.",
|
| 179 |
+
|
| 180 |
+
}
|
| 181 |
+
return explanation
|
| 182 |
+
|
| 183 |
+
@app.post("/predict")
|
| 184 |
+
def get_data_from_user(data: PatientData):
|
| 185 |
+
user_input = data.dict()
|
| 186 |
+
|
| 187 |
+
input_df = pd.DataFrame([user_input])
|
| 188 |
+
|
| 189 |
+
# Make predictions using the loaded pipeline
|
| 190 |
+
prediction = pipeline.predict(input_df)
|
| 191 |
+
probabilities = pipeline.predict_proba(input_df)
|
| 192 |
+
|
| 193 |
+
|
| 194 |
+
probability_of_positive_class = probabilities[0][1]
|
| 195 |
+
|
| 196 |
+
# Calculate the prediction
|
| 197 |
+
sepsis_status = "Positive" if prediction[0] == 1 else "Negative"
|
| 198 |
+
sepsis_explanation = "A positive prediction suggests that the patient might be exhibiting sepsis symptoms and requires immediate medical attention." if prediction[0] == 1 else "A negative prediction suggests that the patient is not currently exhibiting sepsis symptoms."
|
| 199 |
+
|
| 200 |
+
result = {
|
| 201 |
+
'predicted_sepsis': sepsis_status,
|
| 202 |
+
'probability': probability_of_positive_class,
|
| 203 |
+
'sepsis_explanation': sepsis_explanation
|
| 204 |
+
}
|
| 205 |
+
return result
|