File size: 2,944 Bytes
a18143a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI, HTTPException
import uvicorn
#from typing import List, Literal
from pydantic import BaseModel, Field
import pandas as pd
import pickle, os

 #setup
# Get the directory of the current file (FastAPI application file)
DIRPATH = os.path.dirname(os.path.realpath(__file__))

# Construct the path to ml.pkl relative to the current file using forward slashes
ml_core_fp = os.path.join(DIRPATH, "../model/ml.pkl")


#useful functions
def  load_ml_components(fp):
    "load the ml components to re-use in app"
    with open(fp, 'rb') as file:
        obj = pickle.load(file)
        return obj
    

# Loading: Execute and instantiate ml components
ml_components_dict = load_ml_components(fp = ml_core_fp)

pipeline = ml_components_dict["pipeline"]

encoder =  ml_components_dict["encoder"]


# API
app = FastAPI(
    title= "Sepsis classification API"
)

# Input for Modelling 
class Sepsis_Pred(BaseModel):

    PRG: int = Field(..., description='Plasma glucose')
    PL: int = Field(..., description='Blood Work Result-1 (mu U/ml)')
    PR: int = Field(..., description='Blood Pressure (mm Hg)')
    SK: int = Field(..., description='Blood Work Result-2 (mm)')
    TS: int = Field(..., description='Blood Work Result-3 (mu U/ml)')
    M11: float = Field(..., description='Body mass index (weight in kg/(height in m)^2)')
    BD2: float = Field(..., description='Blood Work Result-4 (mu U/ml)')
    Age: int = Field(..., description='Patient age (years)')
    Insurance: int = Field(..., description='If a patient holds a valid insurance card')


@app.get("/")
def root():
    return {
        "Info": "Sepsis classification API : This API classifies whether a patient will develop sepsis based on various test results"
    }


@app.post("/classify_patient")
def sepsis_classification(sepsis_pred: Sepsis_Pred):

    try:

        #Dataframe creation
        df = pd.DataFrame([sepsis_pred.model_dump()])

        print(f'df: {df}')
       
        # ML prediction
        prediction = pipeline.predict(df)

        # Get the index of the predicted class (0 or 1 in binary classification)
        predicted_class_index = prediction[0]

        confidence_score = pipeline.predict_proba(df)

        # Retrieve the confidence score for the predicted class

        confidence_score_predicted_class = confidence_score[0][predicted_class_index]


        print(f"confidence_score: {confidence_score}")

        execution_message = "Execution successful"


        # encoded prediction
        decoded_prediction = encoder.inverse_transform([prediction])[0]

    
        return {"execution message": execution_message, "patient_diagnosis": decoded_prediction, "confidence_score": confidence_score_predicted_class}
    
    
    except Exception as e: 
        raise HTTPException(status_code=500, detail=f"An error occurred during prediction {str(e)}")

if __name__ == "__main__":
    uvicorn.run("main:app", reload=True)