forked from skp1919/HousePricePredictorHackathon
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
123 lines (114 loc) · 3.02 KB
/
main.py
File metadata and controls
123 lines (114 loc) · 3.02 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
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
import uvicorn
from fastapi import FastAPI
from pydantic import BaseModel
from ml_utils import load_model, predict
from typing import List
import datetime
# defining the main app
app = FastAPI(title="House Price Predictor", docs_url="/")
# calling the load_model during startup.
# this will train the model and keep it loaded for prediction.
app.add_event_handler("startup", load_model)
# class which is expected in the payload
class QueryIn(BaseModel):
MSSubClass: float
MSZoning: str
LotFrontage: float
LotArea: float
Street: str
Alley: str
LotShape: str
LandContour: str
Utilities: str
LotConfig: str
LandSlope: str
Neighborhood: str
Condition1: str
Condition2: str
BldgType: str
HouseStyle: str
OverallQual: float
OverallCond: float
YearBuilt: float
YearRemodAdd: float
RoofStyle: str
RoofMatl: str
Exterior1st: str
Exterior2nd: str
MasVnrType: str
MasVnrArea: float
ExterQual: str
ExterCond: str
Foundation: str
BsmtQual: str
BsmtCond: str
BsmtExposure: str
BsmtFinType1: str
BsmtFinSF1: float
BsmtFinType2: str
BsmtFinSF2: float
BsmtUnfSF: float
TotalBsmtSF: float
Heating: str
HeatingQC: str
CentralAir: str
Electrical: str
FirstFlrSF: float
SecondFlrSF: float
LowQualFinSF: float
GrLivArea: float
BsmtFullBath: float
BsmtHalfBath: float
FullBath: float
HalfBath: float
BedroomAbvGr: float
KitchenAbvGr: float
KitchenQual: str
TotRmsAbvGrd: float
Functional: str
Fireplaces: float
FireplaceQu: str
GarageType: str
GarageYrBlt: float
GarageFinish: str
GarageCars: float
GarageArea: float
GarageQual: str
GarageCond: str
PavedDrive: str
WoodDeckSF: float
OpenPorchSF: float
EnclosedPorch: float
ThreeSsnPorch: float
ScreenPorch: float
PoolArea: float
PoolQC: str
Fence: str
MiscFeature: str
MiscVal: float
MoSold: float
YrSold: float
SaleType: str
SaleCondition: str
# class which is returned in the response
class QueryOut(BaseModel):
saleprice: str
timestamp: str
# Route definitions
@app.get("/ping")
# Healthcheck route to ensure that the API is up and running
def ping():
ct = datetime.datetime.now().strftime('%d-%B-%y %H:%M')
return {"ping": "pong", "timestamp": ct}
@app.post("/predict_house_price", response_model=QueryOut, status_code=200)
# Route to do the prediction using the ML model defined.
# Payload: QueryIn containing the parameters
# Response: QueryOut containing the saleprice predicted (200)
def predict_house_price(query_data: QueryIn):
ct = datetime.datetime.now().strftime('%d-%B-%y %H:%M')
output = {"saleprice": predict(query_data), "timestamp": ct}
return output
# Main function to start the app when main.py is called
if __name__ == "__main__":
# Uvicorn is used to run the server and listen for incoming API requests on 0.0.0.0:8887
uvicorn.run("main:app", host="0.0.0.0", port=8887, reload=True)