-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththor-wrapper.py
More file actions
165 lines (126 loc) · 5.19 KB
/
thor-wrapper.py
File metadata and controls
165 lines (126 loc) · 5.19 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import json
import redis
import pathlib
import pickle
import logging
import configparser as cp
from flask import Flask, request
from r2r_offer_utils.logging import setup_logger
from thor import tasks
from loader import read_cache
import traceback
service_name = os.path.splitext(os.path.basename(__file__))[0]
app = Flask(service_name)
# config
config = cp.ConfigParser()
config.read(f'{service_name}.conf')
ONE_HOT_CATEGORICAL_COLUMNS = config.get('thor.columns', 'one_hot_categorical_columns').split(', ')
ONE_HOT_CATEGORICAL_LIST_COLUMNS = config.get('thor.columns', 'one_hot_categorical_list_columns').split(', ')
CLASSIFIER_COLUMNS = config.get('thor.columns', 'classifier_columns').split(', ')
TARGET_COLUMN = config.get('thor.columns', 'target_column')
# logging
logger, ch = setup_logger()
logger.setLevel(logging.INFO)
# cache
cache = redis.Redis(host=config.get('cache', 'host'),
port=config.get('cache', 'port'),
decode_responses=False)
execution_mode = config.get('running', 'mode')
@app.route('/train', methods=['GET'])
def train_endpoint():
try:
logger.info('Collecting user data...')
user_dataframes = []
for uid in read_cache.get_all_user_ids(cache):
try:
user_df = read_cache.load_user_requests(uid, cache)
user_dataframes.append(user_df)
except:
traceback.print_exc()
logger.info('Training classifier...')
classifier, classifier_columns_extended = tasks.make_classifier_for_all_users(user_dataframes,
CLASSIFIER_COLUMNS,
TARGET_COLUMN,
ONE_HOT_CATEGORICAL_COLUMNS,
ONE_HOT_CATEGORICAL_LIST_COLUMNS)
cache.set('classifier', pickle.dumps(classifier, protocol=pickle.HIGHEST_PROTOCOL))
cache.set('classifier_columns_extended', pickle.dumps(classifier_columns_extended, protocol=pickle.HIGHEST_PROTOCOL))
logger.info('Classifier trained successfully and stored into cache.')
response = app.response_class(
status=200,
mimetype='application/json'
)
except:
traceback.print_exc()
response = app.response_class(
status=500,
mimetype='application/json'
)
return response
# rank endpoint
@app.route('/rank', methods=['POST'])
def rank_endpoint():
data = request.get_json()
request_id = data['request_id']
try:
request_df = read_cache.load_request_data(request_id, cache, new_request=True)
classifier = pickle.loads(cache.get('classifier'))
classifier_columns_extended = pickle.loads(cache.get('classifier_columns_extended'))
ranked_offers = tasks.sort_offers(request_df,
classifier,
classifier_columns_extended,
ONE_HOT_CATEGORICAL_COLUMNS,
ONE_HOT_CATEGORICAL_LIST_COLUMNS,
CLASSIFIER_COLUMNS)
print('The offers have been ranked!')
result = {}
result["request_id"] = request_id
result["offers"] = ranked_offers
json_object = json.dumps(result)
response = app.response_class(
response=json_object,
status=200,
mimetype='application/json'
)
except:
traceback.print_exc()
response = app.response_class(
status=500,
mimetype='application/json'
)
return response
if __name__ == '__main__':
import argparse
import logging
from r2r_offer_utils.cli_utils import IntRange
FLASK_PORT = 5000
REDIS_HOST = '172.18.0.5' #'localhost'
REDIS_PORT = 6379
parser = argparse.ArgumentParser()
parser.add_argument('--redis-host',
default=REDIS_HOST,
help=f'Redis hostname [default: {REDIS_HOST}].')
parser.add_argument('--redis-port',
default=REDIS_PORT,
type=IntRange(1, 65536),
help=f'Redis port [default: {REDIS_PORT}].')
parser.add_argument('--flask-port',
default=FLASK_PORT,
type=IntRange(1, 65536),
help=f'Flask port [default: {FLASK_PORT}].')
args = parser.parse_args()
# remove default logger
while logger.hasHandlers():
logger.removeHandler(logger.handlers[0])
# create file handler which logs debug messages
fh = logging.FileHandler(f"{service_name}.log", mode='a+')
fh.setLevel(logging.DEBUG)
# set logging level to debug
ch.setLevel(logging.DEBUG)
os.environ["FLASK_ENV"] = "development"
cache = redis.Redis(host=REDIS_HOST, port=REDIS_PORT)
app.run(port=FLASK_PORT, debug=True)
exit(0)