-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
74 lines (64 loc) · 2.46 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
import os
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 inference, 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")
path = # TODO: enter the path for the saved encoder
encoder = load_model(path)
path = # TODO: enter the path for the saved model
model = load_model(path)
# TODO: create a RESTful API using FastAPI
app = # your code here
# TODO: create a GET on the root giving a welcome message
@app.get("/")
async def get_root():
""" Say hello!"""
# your code here
pass
# TODO: 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.
# The data has names with hyphens and Python does not allow those as variable names.
# Here it uses the functionality of FastAPI/Pydantic/etc to deal with this.
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(
# your code here
# use data as data input
# use training = False
# do not need to pass lb as input
)
_inference = # your code here to predict the result using data_processed
return {"result": apply_label(_inference)}