File size: 1,506 Bytes
10b1799
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI
from pydantic import BaseModel
import uvicorn
import pandas as pd
import joblib

app = FastAPI()


#Load your saved model and components
def load_model():
  num_imputer = joblib.load('numerical_imputer.joblib')
  scaler = joblib.load('scaler.joblib')
  model = joblib.load('sepsis_model.joblib')
  return num_imputer, scaler, model

#Create a class for taking inputs
class UserInput(BaseModel):
    PRG: int
    PL: int
    PR: int
    SK: int
    TS: int
    M11: float
    BD2: float
    Age: int
    Insurance:int

@app.get('/')
async def index():
    return {"Sepsis API": "Sepsis Prediction"}

#get data and make predictions
@app.post('/predict/')
async def predict(UserInput: UserInput):

  data = {
           'PRG': UserInput.PRG, 
           'PL': UserInput.PL,
           'PR': UserInput.PR,
           'SK': UserInput.SK,
           'TS': UserInput.TS,
           'M11': UserInput.M11,
           'BD2': UserInput.BD2,
           'Age': UserInput.Age,
           'Insurance': UserInput.Insurance,
                }
  df = pd.DataFrame(data, index=[0])
  num_col =  [ 'PRG', 'PL', 'PR', 'SK', 'TS', 'M11', 'BD2', 'Age','Insurance']
  num_imputer, scaler, model = load_model()
     #Scale numerical colums
  scaled_col = scaler.transform(df[num_col])
  df2 = pd.DataFrame(scaled_col)                  
  prediction = model.predict(df2).tolist()

  if (prediction[0] == 1):
    result = "Positive Sepsis"
  else:
    result = "Negative Sepsis"

  return{"result":result}