-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
158 lines (135 loc) · 3.96 KB
/
main.py
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
'''
Script for defining a RESTful API using FastAPI
Author: Paulo Souza
Date: Feb. 2023
'''
import pickle
from fastapi import FastAPI
from pydantic.main import BaseModel
import pandas as pd
from ml.data import process_data
app = FastAPI(
title="Census API",
description="An API for serving predictions over the US census dataset.",
version="0.0.1")
class DataInput(BaseModel):
'''
Input data class for feeding into the API.
'''
age: int
workclass: str
fnlgt: int
education: str
education_num: int
marital_status: str
occupation: str
relationship: str
race: str
sex: str
capital_gain: int
capital_loss: int
hours_per_week: int
native_country: str
class Config:
'''
DataInput example schema class
'''
schema_extra = {
"example": {
'age': 39,
'workclass': "State-gov",
'fnlgt': 77516,
'education': "Bachelors",
'education_num': 13,
'marital_status': "Never-married",
'occupation': "Adm-clerical",
'relationship': "Not-in-family",
'race': "White",
'sex': "Male",
'capital_gain': 2174,
'capital_loss': 0,
'hours_per_week': 40,
'native_country': "United-States"
}
}
@app.on_event("startup")
async def startup_event():
'''
Function to load global objects, speeding up startup process.
'''
global RF_MODEL, DATA_ENCODER, LIN_BINARIZER
try:
with open('./model/rf_model.pkl', 'rb') as file:
RF_MODEL = pickle.load(file)
with open('./model/encoder.pkl', 'rb') as file:
DATA_ENCODER = pickle.load(file)
with open('./model/lb.pkl', 'rb') as file:
LIN_BINARIZER = pickle.load(file)
except Exception as err:
print(
'Some or all of the model prediction objects could not be loaded.'
)
raise err
@app.get("/")
async def welcome():
'''
Displays welcome message on API's root page.
'''
return "Welcome to the FastAPI model app"
@app.post("/inference/")
async def inference(inference: DataInput):
'''
API's main function. Performs inference over the passed data.
'''
try:
with open('./model/rf_model.pkl', 'rb') as file:
RF_MODEL = pickle.load(file)
with open('./model/encoder.pkl', 'rb') as file:
DATA_ENCODER = pickle.load(file)
with open('./model/lb.pkl', 'rb') as file:
LIN_BINARIZER = pickle.load(file)
except Exception as err:
print(
'Some or all of the model prediction objects could not be loaded.'
)
raise err
data = {
'age': inference.age,
'workclass': inference.workclass,
'fnlgt': inference.fnlgt,
'education': inference.education,
'education-num': inference.education_num,
'marital-status': inference.marital_status,
'occupation': inference.occupation,
'relationship': inference.relationship,
'race': inference.race,
'sex': inference.sex,
'capital-gain': inference.capital_gain,
'capital-loss': inference.capital_loss,
'hours-per-week': inference.hours_per_week,
'native-country': inference.native_country,
}
obs = pd.DataFrame(data, index=[0])
cat_features = [
"workclass",
"education",
"marital-status",
"occupation",
"relationship",
"race",
"sex",
"native-country",
]
treated_obs, _, _, _ = process_data(
obs,
categorical_features=cat_features,
training=False,
encoder=DATA_ENCODER,
lb=LIN_BINARIZER
)
pred = RF_MODEL.predict(treated_obs)
pred = '<=50k' if pred[0] < .5 else '>50k'
data['prediction'] = pred
return data
if __name__ == '__main__':
pass