File size: 1,188 Bytes
49d2e92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI, Query, HTTPException
import joblib
from pydantic import BaseModel
import pandas as pd


pipeline = joblib.load('./sepsis_classification_pipeline.joblib')
encoder = joblib.load('./label_encoder.joblib')
model = joblib.load('./random_forest_model.joblib')

app = FastAPI()


class features(BaseModel):
    Age: int
    Body_Mass_Index_BMI: float
    Diastolic_Blood_Pressure: float
    Plasma_Glucose: float
    Triceps_Skinfold_Thickness: float
    Elevated_Glucose: float
    Diabetes_Pedigree_Function: float
    Insulin_Levels: float


@app.post("/predict")
async def predict_sepsis(item: features):
    try:
        # Convert input data to DataFrame
        input_data = pd.DataFrame([item.dict()])

        # input_data = pipeline.named_steps.preprocessor.transform(input_data)

        # Make predictions using the model
        predictions = pipeline.predict(input_data)

        # Decode predictions using the label encoder
        decoded_predictions = encoder.inverse_transform(predictions)

        return {"prediction": f'Patient is {decoded_predictions[0]}'}

    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))