Kwasiasomani
commited on
Commit
•
23f70cf
1
Parent(s):
de98f4b
Create API_app
Browse files
API_app
ADDED
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
FastAPI script for Sepssis and model prediction
|
3 |
+
Author: Equity
|
4 |
+
Date: May.30th 2023
|
5 |
+
"""
|
6 |
+
|
7 |
+
|
8 |
+
# The library for the API Code
|
9 |
+
from fastapi import FastAPI
|
10 |
+
import pickle
|
11 |
+
import uvicorn
|
12 |
+
from pydantic import BaseModel
|
13 |
+
import pandas as pd
|
14 |
+
|
15 |
+
|
16 |
+
|
17 |
+
# Declare the data with its components and their type
|
18 |
+
class model_input(BaseModel):
|
19 |
+
|
20 |
+
PRG: int
|
21 |
+
PL: int
|
22 |
+
PR: int
|
23 |
+
SK: int
|
24 |
+
TS: int
|
25 |
+
M11: float
|
26 |
+
BD2: float
|
27 |
+
Age: int
|
28 |
+
Insurance:int
|
29 |
+
|
30 |
+
|
31 |
+
app = FastAPI(title = 'Sepssis API',
|
32 |
+
description = 'An API that takes input and display the predictions',
|
33 |
+
version = '0.1.0')
|
34 |
+
|
35 |
+
# Load the saved data
|
36 |
+
toolkit = "P6_toolkit"
|
37 |
+
|
38 |
+
def load_toolkit(filepath = toolkit):
|
39 |
+
with open(toolkit, "rb") as file:
|
40 |
+
loaded_toolkit = pickle.load(file)
|
41 |
+
return loaded_toolkit
|
42 |
+
|
43 |
+
toolkit = load_toolkit()
|
44 |
+
scaler = toolkit["scaler"]
|
45 |
+
model = toolkit["model"]
|
46 |
+
|
47 |
+
|
48 |
+
@app.get("/")
|
49 |
+
async def hello():
|
50 |
+
return "Welcome to our model API"
|
51 |
+
|
52 |
+
|
53 |
+
|
54 |
+
@app.post("/Sepssis")
|
55 |
+
async def prediction(input:model_input):
|
56 |
+
data = {
|
57 |
+
'PRG': input.PRG,
|
58 |
+
'PL': input.PL,
|
59 |
+
'PR': input.PR,
|
60 |
+
'SK': input.SK,
|
61 |
+
'TS': input.TS,
|
62 |
+
'M11': input.M11,
|
63 |
+
'BD2': input.BD2,
|
64 |
+
'Age': input.Age,
|
65 |
+
'Insurance': input.Insurance,
|
66 |
+
}
|
67 |
+
|
68 |
+
# prepare the data as a dataframe
|
69 |
+
df = pd.DataFrame(data, index=[0])
|
70 |
+
|
71 |
+
|
72 |
+
#numerical features
|
73 |
+
numeric_columns = [ 'PRG', 'PL', 'PR', 'SK', 'TS', 'M11', 'BD2', 'Age','Insurance']
|
74 |
+
|
75 |
+
#scaling
|
76 |
+
Scaler = scaler.transform(df[numeric_columns])
|
77 |
+
Scaled = pd.DataFrame(Scaler)
|
78 |
+
prediction = model.predict(Scaled).tolist()
|
79 |
+
probability = model.predict_proba(Scaled)
|
80 |
+
|
81 |
+
|
82 |
+
# Labelling Model output
|
83 |
+
if (prediction[0] < 0.5):
|
84 |
+
prediction = "Negative. This person has no Sepssis"
|
85 |
+
else:
|
86 |
+
prediction = "Positive. This person has Sepssis"
|
87 |
+
data['prediction'] = prediction
|
88 |
+
return data
|
89 |
+
|
90 |
+
|
91 |
+
|
92 |
+
|
93 |
+
|
94 |
+
# Launch the app
|
95 |
+
if __name__ == "__main__":
|
96 |
+
uvicorn.run("API_app:app",host = '127.0.0.1', port = 7860)
|