-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
78 lines (65 loc) · 2.26 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
import pandas as pd
from fastapi import FastAPI
from pydantic import BaseModel, Field
from ml.data import apply_label, process_data
from ml.model import load_model
# DO NOT MODIFY
class Data(BaseModel):
age: int = Field(..., example=37)
workclass: str = Field(..., example="Private")
fnlgt: int = Field(..., example=178356)
education: str = Field(..., example="HS-grad")
education_num: int = Field(..., example=10, alias="education-num")
marital_status: str = Field(
..., example="Married-civ-spouse", alias="marital-status"
)
occupation: str = Field(..., example="Prof-specialty")
relationship: str = Field(..., example="Husband")
race: str = Field(..., example="White")
sex: str = Field(..., example="Male")
capital_gain: int = Field(..., example=0, alias="capital-gain")
capital_loss: int = Field(..., example=0, alias="capital-loss")
hours_per_week: int = Field(
..., example=40, alias="hours-per-week"
)
native_country: str = Field(
..., example="United-States", alias="native-country"
)
# Load the encoder and model
path_encoder = "./model/encoder.pkl"
encoder = load_model(path_encoder)
path_model = "./model/model.pkl"
model = load_model(path_model)
# Create a RESTful API using FastAPI
app = FastAPI()
# Create a GET on the root giving a welcome message
@app.get("/")
async def get_root():
"""Say hello!"""
return {"message": "Welcome to the ML model API!"}
# Create a POST on a different path that does model inference
@app.post("/data/")
async def post_inference(data: Data):
# DO NOT MODIFY: turn the Pydantic model into a dict.
data_dict = data.dict()
# DO NOT MODIFY: clean up the dict to turn it into a Pandas DataFrame.
data = {k.replace("_", "-"): [v] for k, v in data_dict.items()}
data = pd.DataFrame.from_dict(data)
cat_features = [
"workclass",
"education",
"marital-status",
"occupation",
"relationship",
"race",
"sex",
"native-country",
]
data_processed, _, _, _ = process_data(
data,
categorical_features=cat_features,
encoder=encoder,
training=False,
)
_inference = model.predict(data_processed)
return {"result": apply_label(_inference)}