Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
75 lines (62 loc) · 2.5 KB
/
Copy pathmain.py
File metadata and controls
75 lines (62 loc) · 2.5 KB
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
import os
import pandas as pd
import joblib
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel, Field
from starter.ml.data import process_data
from starter.ml.model import inference
# Load the model, encoder, and label binarizer
model_path = os.path.join('model', 'trained_model.joblib')
encoder_path = os.path.join('model', 'encoder.joblib')
lb_path = os.path.join('model', 'label_binarizer.joblib')
model = joblib.load(model_path)
encoder = joblib.load(encoder_path)
lb = joblib.load(lb_path)
# Instantiate the app.
app = FastAPI()
# Define Pydantic model for request body
# Include examples with the `Field` function
class CensusData(BaseModel):
age: int = Field(..., example=39)
workclass: str = Field(..., example="State-gov")
fnlgt: int = Field(..., example=77516)
education: str = Field(..., example="Bachelors")
education_num: int = Field(..., alias="education-num", example=13)
marital_status: str = Field(..., alias="marital-status",
example="Never-married")
occupation: str = Field(..., example="Adm-clerical")
relationship: str = Field(..., example="Not-in-family")
race: str = Field(..., example="White")
sex: str = Field(..., example="Male")
capital_gain: int = Field(..., alias="capital-gain", example=2174)
capital_loss: int = Field(..., alias="capital-loss", example=0)
hours_per_week: int = Field(..., alias="hours-per-week", example=40)
native_country: str = Field(..., alias="native-country",
example="United-States")
@app.get("/")
async def greeting():
return {"greeting": "Welcome to the Census Income Prediction API!"}
@app.post("/predict")
async def predict(data: CensusData):
try:
# Convert Pydantic model to dict and then to dataframe
data_dict = data.dict(by_alias=True)
data_df = pd.DataFrame([data_dict])
# Process the data
cat_features = [
"workclass", "education", "marital-status", "occupation",
"relationship", "race", "sex", "native-country",
]
X, _, _, _ = process_data(
data_df,
categorical_features=cat_features,
encoder=encoder,
lb=lb,
training=False
)
# Make prediction
prediction = inference(model, X)
pred_label = lb.inverse_transform(prediction)[0]
return {"prediction": pred_label}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))