-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.py
47 lines (33 loc) · 1.21 KB
/
handler.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
import os
import pickle
import pandas as pd
from flask import Flask, request, Response
from rossmann.Rossmann import Rossmann
# load model
model = pickle.load(open('model/model_rossmann.pkl', 'rb'))
# initialize API
app = Flask(__name__)
@app.route('/rossmann/predict', methods=['GET', 'POST'])
def rossmann_predict():
test_json = request.get_json()
if test_json: # there is data
if isinstance(test_json, dict): # unique example
test_raw = pd.DataFrame(test_json, index=[0])
else: # multiple examples
test_raw = pd.DataFrame(test_json, columns=test_json[0].keys())
# instantiate rossmann class
pipeline = Rossmann()
# data cleaning
df1 = pipeline.data_cleaning(test_raw)
# feature engineering
df2 = pipeline.feature_engineering(df1)
# data preparation
df3 = pipeline.data_preparation(df2)
# prediction
df_response = pipeline.get_prediction(model, test_raw, df3)
return df_response
else: # there isn't data
return Response('{}', status=200, mimetype='application/json')
if __name__ == '__main__':
port = os.environ.get('PORT', 5000)
app.run(host='0.0.0.0', port=port)