|
|
|
from fastapi import FastAPI |
|
import uvicorn |
|
from typing import Union, Literal |
|
import pandas as pd |
|
import pickle, os |
|
from pydantic import BaseModel |
|
|
|
|
|
additional_info = """ |
|
- PRG: Plasma Glucose |
|
- PL: Blood Work Result-1 (mu U/ml) |
|
- PR: Blood Pressure (mm Hg) |
|
- SK: Blood Work Result-2 (mm) |
|
- TS: Blood Work Result-3 (mu U/ml) |
|
- M11: Body Mass Index (weight in kg/(height in m)^2) |
|
- BD2: Blood Work Result-4 (mu U/ml) |
|
- Age: Patient's Age (years) |
|
- Insurance: If a patient holds a valid insurance card |
|
|
|
Output: |
|
- Sepsis: Positive if a patient in ICU will develop sepsis, Negative if a patient in ICU will not develop sepsis. |
|
""" |
|
|
|
|
|
app = FastAPI( |
|
title='Sepsis Prediction App', |
|
description='This app was built to predict if patients are Sepsis Positive or Sepsis Negative. ' |
|
'It uses a machine learning model to make predictions based on patient data. ' |
|
'Below is the kind of patient data required for the prediction, and the meaning of the prediction output.' |
|
+ additional_info |
|
) |
|
|
|
@app.get('/') |
|
async def root(): |
|
return { |
|
'This app was built to predict if patients are Sepsis Positive or Sepsis Negative.' |
|
} |
|
|
|
|
|
def load_ml_components(fp): |
|
'Load the ML component to re-use in app' |
|
with open(fp, 'rb') as f: |
|
object = pickle.load(f) |
|
return object |
|
|
|
|
|
DIRPATH = os.path.dirname(os.path.realpath(__file__)) |
|
ml_core_fp = os.path.join(DIRPATH, 'export', 'ml_components.pkl') |
|
|
|
|
|
ml_components_dict = load_ml_components(fp=ml_core_fp) |
|
|
|
|
|
|
|
print(f'\n[Info] ML components loaded: {list(ml_components_dict.keys())}') |
|
|
|
|
|
model = ml_components_dict['model'] |
|
scaler = ml_components_dict['scaler'] |
|
|
|
|
|
class Sepsis(BaseModel): |
|
|
|
PRG: float |
|
PL: float |
|
PR: float |
|
SK: float |
|
TS: float |
|
M11: float |
|
BD2: float |
|
Age: float |
|
Insurance: float |
|
|
|
@app.post('/classify') |
|
async def sepsis_prediction(sepsis: Sepsis): |
|
try: |
|
|
|
df = pd.DataFrame( |
|
{ |
|
'PRG': [sepsis.PRG], |
|
'PL': [sepsis.PL], |
|
'PR': [sepsis.PR], |
|
'SK': [sepsis.SK], |
|
'TS': [sepsis.TS], |
|
'M11': [sepsis.M11], |
|
'BD2': [sepsis.BD2], |
|
'Age': [sepsis.Age], |
|
'Insurance': [sepsis.Insurance] |
|
} |
|
) |
|
print(f'[Info] Input data as DataFrame:\n{df.to_markdown()}') |
|
|
|
|
|
scaled_df = scaler.transform(df) |
|
|
|
|
|
prediction = model.predict(scaled_df) |
|
|
|
|
|
df['Sepsis'] = prediction |
|
|
|
|
|
mapping = {0: 'Negative', 1: 'Positive'} |
|
|
|
|
|
df['Sepsis'] = [mapping[x] for x in df['Sepsis']] |
|
|
|
|
|
print(f"The Patient has been predicted as : {df['Sepsis'].values[0]}") |
|
msg = 'Execution went fine' |
|
code = 1 |
|
pred = df.to_dict('records') |
|
|
|
except Exception as e: |
|
print(f'Something went wrong during the Sepsis prediction: {str(e)}') |
|
msg = 'Execution went wrong' |
|
code = 0 |
|
pred = None |
|
|
|
result = {'execution_msg': msg, 'execution_code': code, 'predictions': pred} |
|
return result |
|
|
|
if __name__ == '__main__': |
|
uvicorn.run('main:app', reload=True) |