-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
159 lines (141 loc) · 4.95 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
159
'''
This script contains the main codes of the FAST API app, including functions for
POST and GET, the POST input schema, as well as prediction using the previously
trained XGBoost model.
Author: Gian Atmaja
Created: 6 May 2023
'''
# Import required libraries
import os
from fastapi import FastAPI
from typing import Literal
import pandas as pd
import numpy as np
import uvicorn
from pydantic import BaseModel
from src.model_runner import process_inference_data
from src.utils import load_model
# Create app
app = FastAPI()
# POST Input Schema
class ModelInput(BaseModel):
age: int
workclass: Literal['State-gov',
'Self-emp-not-inc',
'Private',
'Federal-gov',
'Local-gov',
'Self-emp-inc',
'Without-pay']
fnlgt: int
education: Literal[
'Bachelors', 'HS-grad', '11th', 'Masters', '9th',
'Some-college',
'Assoc-acdm', '7th-8th', 'Doctorate', 'Assoc-voc', 'Prof-school',
'5th-6th', '10th', 'Preschool', '12th', '1st-4th']
education_num: int
marital_status: Literal["Never-married",
"Married-civ-spouse",
"Divorced",
"Married-spouse-absent",
"Separated",
"Married-AF-spouse",
"Widowed"]
occupation: Literal["Tech-support",
"Craft-repair",
"Other-service",
"Sales",
"Exec-managerial",
"Prof-specialty",
"Handlers-cleaners",
"Machine-op-inspct",
"Adm-clerical",
"Farming-fishing",
"Transport-moving",
"Priv-house-serv",
"Protective-serv",
"Armed-Forces"]
relationship: Literal["Wife", "Own-child", "Husband",
"Not-in-family", "Other-relative", "Unmarried"]
race: Literal["White", "Asian-Pac-Islander",
"Amer-Indian-Eskimo", "Other", "Black"]
sex: Literal[" Female", " Male"]
capital_gain: int
capital_loss: int
hours_per_week: int
native_country: Literal[
'United-States', 'Cuba', 'Jamaica', 'India', 'Mexico',
'Puerto-Rico', 'Honduras', 'England', 'Canada', 'Germany', 'Iran',
'Philippines', 'Poland', 'Columbia', 'Cambodia', 'Thailand',
'Ecuador', 'Laos', 'Taiwan', 'Haiti', 'Portugal',
'Dominican-Republic', 'El-Salvador', 'France', 'Guatemala',
'Italy', 'China', 'South', 'Japan', 'Yugoslavia', 'Peru',
'Outlying-US(Guam-USVI-etc)', 'Scotland', 'Trinadad&Tobago',
'Greece', 'Nicaragua', 'Vietnam', 'Hong', 'Ireland', 'Hungary',
'Holand-Netherlands']
class Config:
schema_extra = {
"example": {
"age": 32,
"workclass": 'Self-emp-not-inc',
"fnlgt": 83311,
"education": 'Bachelors',
"education_num": 13,
"marital_status": "Married-civ-spouse",
"occupation": "Sales",
"relationship": "Husband",
"race": "White",
"sex": " Male",
"capital_gain": 2500,
"capital_loss": 0,
"hours_per_week": 40,
"native_country": 'United-States'
}
}
# Load model
model = load_model('model/xgb_model.pkl')
# Root path
@app.get("/")
async def root():
return {
"Hi": "This app predicts whether the input person's annual income exceeds $50 000."}
# Prediction path
@app.post("/predict-income")
async def predict(input: ModelInput):
input_data = np.array([[
input.age,
input.workclass,
input.fnlgt,
input.education,
input.education_num,
input.marital_status,
input.occupation,
input.relationship,
input.race,
input.sex,
input.capital_gain,
input.capital_loss,
input.hours_per_week,
input.native_country]])
original_col_names = [
'age', 'workclass', 'fnlgt', 'education', 'education-num',
'marital-status', 'occupation', 'relationship', 'race','sex',
'capital-gain', 'capital-loss', 'hours-per-week', 'native-country'
]
# Get df of input data
input_df = pd.DataFrame(data=input_data, columns=original_col_names)
# Process data and predict y
X = process_inference_data(input_df)
y = model.predict(X)
# Map y to income category, then return output
if y == 0:
pred = '<=50K'
elif y == 1:
pred = '>50K'
else:
pred = 'Check model output'
return {"Income prediction": pred}
'''
if __name__ == "__main__":
uvicorn.run("main:app", host="127.0.0.1", port=8000, reload=True)
'''