File size: 2,062 Bytes
23f70cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6d42868
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
"""
FastAPI script for Sepssis and model prediction
Author: Equity
Date: May.30th 2023
"""


# The library for the API Code
from fastapi import FastAPI
import pickle
import uvicorn
from pydantic import BaseModel
import pandas as pd



# Declare the data with its components and their type
class model_input(BaseModel):
    
    PRG: int
    PL: int
    PR: int
    SK: int
    TS: int
    M11: float
    BD2: float
    Age: int
    Insurance:int


app = FastAPI(title = 'Sepssis API',
              description = 'An API that takes input and display the predictions',
              version = '0.1.0')

# Load the saved data
toolkit = "P6_toolkit"

def load_toolkit(filepath = toolkit):
    with open(toolkit, "rb") as file:
        loaded_toolkit = pickle.load(file)
    return loaded_toolkit

toolkit = load_toolkit()
scaler = toolkit["scaler"]
model = toolkit["model"]


@app.get("/")
async def hello():
    return "Welcome to our model API"



@app.post("/Sepssis")
async def prediction(input:model_input):
   data = {
           'PRG': input.PRG, 
           'PL': input.PL,
           'PR': input.PR,
           'SK': input.SK,
           'TS': input.TS,
           'M11': input.M11,
           'BD2': input.BD2,
           'Age': input.Age,
           'Insurance': input.Insurance,
                }
   
# prepare the data as a dataframe
   df = pd.DataFrame(data, index=[0])


   #numerical features
   numeric_columns =  [ 'PRG', 'PL', 'PR', 'SK', 'TS', 'M11', 'BD2', 'Age','Insurance']
   
   #scaling
   Scaler = scaler.transform(df[numeric_columns])
   Scaled = pd.DataFrame(Scaler)                  
   prediction = model.predict(Scaled).tolist()
   probability = model.predict_proba(Scaled)


    # Labelling Model output
   if (prediction[0] < 0.5):
         prediction = "Negative. This person has no Sepssis"
   else: 
      prediction = "Positive. This person has Sepssis"
   data['prediction'] = prediction  
   return data
   
   
        


# Launch the app
if __name__ == "__main__":
 uvicorn.run("API_app:app",host = '0.0.0.0', port = 7860)