File size: 7,122 Bytes
64c643e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# from fastapi import FastAPI,Form, Body,Path
# from typing import Annotated
# from pydantic import BaseModel, Field
# import joblib
# import pandas as pd
# import numpy as np
# import uvicorn
# from fastapi.responses import JSONResponse


# app = FastAPI()

# # Load the numerical imputer, scaler, and model
# num_imputer_filepath = "joblib_files/numerical_imputer.joblib"
# scaler_filepath = "joblib_files/scaler.joblib"
# model_filepath = "joblib_files/lr_model.joblib"

# num_imputer = joblib.load(num_imputer_filepath)
# scaler = joblib.load(scaler_filepath)
# model = joblib.load(model_filepath)

# class PatientData(BaseModel):
#     PRG: float 
#     PL: float
#     PR: float
#     SK: float
#     TS: float
#     M11: float
#     BD2: float
#     Age: float
#     Insurance: int

# def preprocess_input_data(user_input):
#     input_data_df = pd.DataFrame([user_input])
#     num_columns = [col for col in input_data_df.columns if input_data_df[col].dtype != 'object']
#     input_data_imputed_num = num_imputer.transform(input_data_df[num_columns])
#     input_scaled_df = pd.DataFrame(scaler.transform(input_data_imputed_num), columns=num_columns)
#     return input_scaled_df

# @app.get("/")
# def read_root():
#         return "Sepsis Prediction App"
# @app.post("/sepsis/predict")
# def get_data_from_user(data:PatientData):
#     user_input = data.dict()
#     input_scaled_df = preprocess_input_data(user_input)
#     probabilities = model.predict_proba(input_scaled_df)[0]
#     prediction = np.argmax(probabilities)

#     sepsis_status = "Positive" if prediction == 1 else "Negative"
#     probability = probabilities[1] if prediction == 1 else probabilities[0]

#     if prediction == 1:
#         sepsis_explanation = "A positive prediction suggests that the patient might be exhibiting sepsis symptoms and requires immediate medical attention."
#     else:
#         sepsis_explanation = "A negative prediction suggests that the patient is not currently exhibiting sepsis symptoms."

#     statement = f"The patient's sepsis status is {sepsis_status} with a probability of {probability:.2f}. {sepsis_explanation}"

#     user_input_statement = "user-inputted data: "
#     output_df = pd.DataFrame([user_input])

#     result = {'predicted_sepsis': sepsis_status, 'statement': statement, 'user_input_statement': user_input_statement, 'input_data_df': output_df.to_dict('records')}
#     return result

# from fastapi import FastAPI, Form
# from pydantic import BaseModel
# import joblib
# import pandas as pd
# import numpy as np
# import uvicorn
# from fastapi.responses import JSONResponse

# app = FastAPI()

# # Load the entire pipeline
# pipeline_filepath = "pipeline.joblib"
# pipeline = joblib.load(pipeline_filepath)

# class PatientData(BaseModel):
#     PRG: float
#     PL: float
#     PR: float
#     SK: float
#     TS: float
#     M11: float
#     BD2: float
#     Age: float
#     Insurance: int

# @app.get("/")
# def read_root():
#     explanation = {
#         'message': "Welcome to the Sepsis Prediction App",
#         'description': "This API allows you to predict sepsis based on patient data.",
#         'usage': "Submit a POST request to /predict with patient data to make predictions.",
#         'input_fields': {
#             'PRG': 'Plasma_glucose',
#             'PL': 'Blood_Work_Result_1',
#             'PR': 'Blood_Pressure',
#             'SK': 'Blood_Work_Result_2',
#             'TS': 'Blood_Work_Result_3',
#             'M11': 'Body_mass_index',
#             'BD2': 'Blood_Work_Result_4',
#             'Insurance': 'Sepsis (Positive = 1, Negative = 0)'
#         }
#     }
#     return explanation


# @app.post("/predict")
# def get_data_from_user(data: PatientData):
#     user_input = data.model_dump()

    
#     input_df = pd.DataFrame([user_input])
#      # Make predictions using the loaded pipeline
#     # Make predictions using the loaded pipeline
#     predictions = pipeline.predict(user_input)
#     probabilities = pipeline.decision_function(user_input)

#     # Assuming the pipeline uses a Logistic Regression model
#     probability_of_positive_class = probabilities[0]

#      # Calculate the prediction
#     prediction = 1 if probability_of_positive_class >= 0.5 else 0

#     sepsis_status = "Positive" if prediction == 1 else "Negative"
#     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."

   
#     if prediction == 1:
#         sepsis_status = "Positive"
#         sepsis_explanation = "A positive prediction suggests that the patient might be exhibiting sepsis symptoms and requires immediate medical attention."
#     else:
#         sepsis_status = "Negative"
#         sepsis_explanation = "A negative prediction suggests that the patient is not currently exhibiting sepsis symptoms."

#     result = {
#         'predicted_sepsis': sepsis_status,
#         'sepsis_explanation': sepsis_explanation
#     }
#     return result

from fastapi import FastAPI
from pydantic import BaseModel
import joblib
import pandas as pd
import numpy as np
from sklearn.preprocessing import StandardScaler
from sklearn.impute import SimpleImputer
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
from sklearn.linear_model import LogisticRegression

app = FastAPI()

# Load the entire pipeline
pipeline_filepath = "pipeline.joblib"
pipeline = joblib.load(pipeline_filepath)

class PatientData(BaseModel):
    Plasma_glucose : float
    Blood_Work_Result_1: float
    Blood_Pressure : float
    Blood_Work_Result_2 : float
    Blood_Work_Result_3 : float
    Body_mass_index  : float
    Blood_Work_Result_4: float
    Age: float
    Insurance: int

@app.get("/")
def read_root():
    explanation = {
        'message': "Welcome to the Sepsis Prediction App",
        'description': "This API allows you to predict sepsis based on patient data.",
        'usage': "Submit a POST request to /predict with patient data to make predictions.",
        
    }
    return explanation

@app.post("/predict")
def get_data_from_user(data: PatientData):
    user_input = data.dict()

    input_df = pd.DataFrame([user_input])

    # Make predictions using the loaded pipeline
    prediction = pipeline.predict(input_df)
    probabilities = pipeline.predict_proba(input_df)

    
    probability_of_positive_class = probabilities[0][1]

    # Calculate the prediction
    sepsis_status = "Positive" if prediction[0] == 1 else "Negative"
    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."

    result = {
        'predicted_sepsis': sepsis_status,
        'probability': probability_of_positive_class,
        'sepsis_explanation': sepsis_explanation
    }
    return result