File size: 1,854 Bytes
af50b58
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# import libraries
from pydantic import BaseModel
import pandas as pd
import joblib
import uvicorn
import numpy as np
from fastapi import FastAPI, HTTPException,Query

app = FastAPI()

###create home
@app.get('/')
def home():
    return{'message':'Welcome to Sepsis Prediction Using Fastapi'}

## Load the model
model = joblib.load("src/rf_pipeline.joblib")  
        
# Endpoint for predicting sepsis using a GET request
@app.post("/predict")
def predict_sepsis(
        PRG: int = Query(..., description="Plasma_glucose"),
        PL: int = Query(..., description="Blood_Work_R1"),
        PR: int = Query(..., description="Blood_Pressure"), 
        SK: int = Query(..., description="Blood_Work_R2"),
        TS: int = Query(..., description="Blood_Work_R3"),
        M11: float = Query(..., description="BMI"),
        BD2: float = Query(..., description="Blood_Work_R4"),
        Age: int = Query(..., description="Age")
):
    try:
        # Convert input data to a dictionary
        input_data = {
            'PRG': PRG,
            'PL': PL,
            'PR': PR,
            'SK': SK,
            'TS': TS,
            'M11': M11,
            'BD2': BD2,
            'Age': Age,  
        }


        # Convert input_data to DataFrame
        input_data_df = pd.DataFrame([input_data])

        # Use the loaded model to make predictions

        prediction= model.predict(input_data_df)[0]
        
        sepsis_status = "patient has sepsis" if prediction == 1 else "Patient does not have sepsis"
        
        # Return the prediction
        return {"prediction": sepsis_status}
    
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

if __name__ == "__main__":
    import uvicorn
    import nest_asyncio

    nest_asyncio.apply()

    uvicorn.run(app, host="127.0.0.1", port=8003, log_level="info")