This repository was archived by the owner on Apr 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathapplication.py
More file actions
105 lines (76 loc) · 2.71 KB
/
application.py
File metadata and controls
105 lines (76 loc) · 2.71 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
# -*- coding: utf-8 -*-
# Standard Libs
from flask import Flask, request, jsonify
from pandas import DataFrame
from pickle import load
import logging as lg
from datetime import datetime as dt
from pytz import timezone
# Custom Libs
def dummy_encode(in_df, dummies):
out_df = in_df.copy()
for feature, values in dummies.items():
for value in values:
dummy_name = '{}__{}'.format(feature, value)
out_df[dummy_name] = (out_df[feature] == value).astype(int)
del out_df[feature]
return out_df
def minmax_scale(in_df, boundaries):
out_df = in_df.copy()
for feature, (min_val, max_val) in boundaries.items():
col_name = '{}__norm'.format(feature)
out_df[col_name] = round((out_df[feature] - min_val)/(max_val - min_val), 3)
out_df.loc[out_df[col_name] < 0, col_name] = 0
out_df.loc[out_df[col_name] > 1, col_name] = 1
del out_df[feature]
# print('MinMax Scaled feature\t\t{}'.format(feature))
return out_df
# ******************************************************************************
# Conf
current_time = dt.now(timezone('UTC')).astimezone(timezone('Europe/Paris'))
log_file_name = 'logs/run.{}.log'.format(current_time.strftime("%Y-%m-%d"))
lg.basicConfig(
filename=log_file_name,
level=lg.INFO,
filemode='a',
format='%(asctime)s\t%(levelname)s\t%(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
)
# ******************************************************************************
# Init
app = Flask(__name__)
model = load(open("./pickles/model_v1.pkl", 'rb'))
DUMMIES = {'sex': ['M', 'F', 'I']}
BOUNDARIES = {
'length': (0.075000, 0.815000),
'diameter': (0.055000, 0.650000),
'height': (0.000000, 1.130000),
'whole_weight': (0.002000, 2.825500),
'shucked_weight': (0.001000, 1.488000),
'viscera_weight': (0.000500, 0.760000),
'shell_weight': (0.001500, 1.005000)
}
# ******************************************************************************
# Core
@app.route('/api/v1.0/aballone', methods=['POST'])
def index():
# Fetching inputs
query = request.get_json(silent=True, force=True)['inputs']
input_df = DataFrame(query)
# Preparing features
X_tmp = dummy_encode(input_df, DUMMIES)
X = minmax_scale(X_tmp, BOUNDARIES)
# Computing predictions
y_pred = model.predict(X)
y_prob = model.predict_proba(X)
# Building output
output = [
{"label": int(y), "prob": round(float(p[0]), 3)}
for (y, p) in zip(y_pred, y_prob)
]
# Logging predictions
for (i, o) in zip(query, output):
lg.info('IN | {} || OUT | {}'.format(i, o))
return jsonify({'outputs': output})
if __name__ == '__main__':
app.run(host='0.0.0.0')