Created
June 7, 2019 19:28
-
-
Save tibaes/752171db3d1db85d0958e255203af84d to your computer and use it in GitHub Desktop.
Google functions example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import numpy as np | |
import logging | |
from google.cloud import storage | |
from lightgbm import LGBMRegressor | |
from sklearn.externals import joblib | |
from sklearn.impute import SimpleImputer | |
from sklearn.preprocessing import OrdinalEncoder | |
num_feat = ['MSSubClass', 'LotFrontage', 'LotArea', | |
'OverallQual', 'OverallCond', 'YearBuilt', | |
'YearRemodAdd', 'MasVnrArea', 'BsmtFinSF1', | |
'BsmtFinSF2', 'BsmtUnfSF', 'TotalBsmtSF', | |
'1stFlrSF', '2ndFlrSF', 'LowQualFinSF', 'GrLivArea', | |
'BsmtFullBath', 'BsmtHalfBath', 'FullBath', | |
'HalfBath', 'BedroomAbvGr', 'KitchenAbvGr', | |
'TotRmsAbvGrd', 'Fireplaces', 'GarageYrBlt', | |
'GarageCars', 'GarageArea', 'WoodDeckSF', | |
'OpenPorchSF', 'EnclosedPorch', '3SsnPorch', | |
'ScreenPorch', 'PoolArea', 'MiscVal', 'MoSold', 'YrSold' | |
] | |
cat_feat = ['MSZoning', 'Street', 'Alley', 'LotShape', | |
'LandContour', 'Utilities', 'LotConfig', | |
'LandSlope', 'Neighborhood', 'Condition1', | |
'Condition2', 'BldgType', 'HouseStyle', 'RoofStyle', | |
'RoofMatl', 'Exterior1st', 'Exterior2nd', 'MasVnrType', | |
'ExterQual', 'ExterCond', 'Foundation', 'BsmtQual', | |
'BsmtCond', 'BsmtExposure', 'BsmtFinType1', 'BsmtFinType2', | |
'Heating', 'HeatingQC', 'CentralAir', 'Electrical', | |
'KitchenQual', 'Functional', 'FireplaceQu', 'GarageType', | |
'GarageFinish', 'GarageQual', 'GarageCond', 'PavedDrive', | |
'PoolQC', 'Fence', 'MiscFeature', 'SaleType', 'SaleCondition' | |
] | |
def download_model(): | |
logging.info('Downloading resources GCloud -> /tmp...') | |
client = storage.Client() | |
bucket = client.get_bucket('up_teaching_2019') | |
# download encoder | |
blob_enc = storage.Blob('model/house_pricing/enc.joblib', bucket) | |
blob_enc.download_to_filename('/tmp/enc.joblib') | |
# download imputer | |
blob_imp = storage.Blob('model/house_pricing/imp.joblib', bucket) | |
blob_imp.download_to_filename('/tmp/imp.joblib') | |
# download model | |
blob_model = storage.Blob('model/house_pricing/model.joblib', bucket) | |
blob_model.download_to_filename('/tmp/model.joblib') | |
def predict(data): | |
logging.info('Making predictions...') | |
logging.info(data) | |
# load models | |
imp = joblib.load('/tmp/imp.joblib') | |
enc = joblib.load('/tmp/enc.joblib') | |
model = joblib.load('/tmp/model.joblib') | |
# convert json to pandas | |
data = pd.DataFrame([pd.Series(data)]) | |
data = data.replace(['None', 'none', 'null', 'Null', None], np.nan) | |
# preprocessing | |
data[cat_feat] = imp.transform(data[cat_feat]) | |
data[cat_feat] = enc.transform(data[cat_feat]) | |
# predict | |
return str(model.predict(data[num_feat+cat_feat])[0]) | |
def proba(request): | |
"""Responds to any HTTP request. | |
Args: | |
request (flask.Request): HTTP request object. | |
Returns: | |
The response text or any set of values that can be turned into a | |
Response object using | |
`make_response <http://flask.pocoo.org/docs/1.0/api/#flask.Flask.make_response>`. | |
""" | |
logging.info('Starting a new request...') | |
download_model() | |
request_json = request.get_json() | |
if request.args and 'message' in request.args: | |
return predict(request.args.get('message')) | |
elif request_json and 'message' in request_json: | |
return predict(request_json['message']) | |
else: | |
return f'Failed to read data to predict' | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"message": { | |
"Id": 1461, | |
"MSSubClass": 20, | |
"MSZoning": "RH", | |
"LotFrontage": 80.0, | |
"LotArea": 11622, | |
"Street": "Pave", | |
"Alley": null, | |
"LotShape": "Reg", | |
"LandContour": "Lvl", | |
"Utilities": "AllPub", | |
"LotConfig": "Inside", | |
"LandSlope": "Gtl", | |
"Neighborhood": "NAmes", | |
"Condition1": "Feedr", | |
"Condition2": "Norm", | |
"BldgType": "1Fam", | |
"HouseStyle": "1Story", | |
"OverallQual": 5, | |
"OverallCond": 6, | |
"YearBuilt": 1961, | |
"YearRemodAdd": 1961, | |
"RoofStyle": "Gable", | |
"RoofMatl": "CompShg", | |
"Exterior1st": "VinylSd", | |
"Exterior2nd": "VinylSd", | |
"MasVnrType": "None", | |
"MasVnrArea": 0.0, | |
"ExterQual": "TA", | |
"ExterCond": "TA", | |
"Foundation": "CBlock", | |
"BsmtQual": "TA", | |
"BsmtCond": "TA", | |
"BsmtExposure": "No", | |
"BsmtFinType1": "Rec", | |
"BsmtFinSF1": 468.0, | |
"BsmtFinType2": "LwQ", | |
"BsmtFinSF2": 144.0, | |
"BsmtUnfSF": 270.0, | |
"TotalBsmtSF": 882.0, | |
"Heating": "GasA", | |
"HeatingQC": "TA", | |
"CentralAir": "Y", | |
"Electrical": "SBrkr", | |
"1stFlrSF": 896, | |
"2ndFlrSF": 0, | |
"LowQualFinSF": 0, | |
"GrLivArea": 896, | |
"BsmtFullBath": 0.0, | |
"BsmtHalfBath": 0.0, | |
"FullBath": 1, | |
"HalfBath": 0, | |
"BedroomAbvGr": 2, | |
"KitchenAbvGr": 1, | |
"KitchenQual": "TA", | |
"TotRmsAbvGrd": 5, | |
"Functional": "Typ", | |
"Fireplaces": 0, | |
"FireplaceQu": null, | |
"GarageType": "Attchd", | |
"GarageYrBlt": 1961.0, | |
"GarageFinish": "Unf", | |
"GarageCars": 1.0, | |
"GarageArea": 730.0, | |
"GarageQual": "TA", | |
"GarageCond": "TA", | |
"PavedDrive": "Y", | |
"WoodDeckSF": 140, | |
"OpenPorchSF": 0, | |
"EnclosedPorch": 0, | |
"3SsnPorch": 0, | |
"ScreenPorch": 120, | |
"PoolArea": 0, | |
"PoolQC": null, | |
"Fence": "MnPrv", | |
"MiscFeature": null, | |
"MiscVal": 0, | |
"MoSold": 6, | |
"YrSold": 2010, | |
"SaleType": "WD", | |
"SaleCondition": "Normal" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment