forked from jashwantraj92/cocktail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2
executable file
·249 lines (203 loc) · 9.03 KB
/
2
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import asyncio
import json
import logging
import threading
import time
from base64 import b64encode
from collections import deque
import requests
from .constants import *
from . import utils
TYPE_CONFING = {
'c5.large' : 'nginxc5large.conf',
'c5.xlarge' : 'nginxc5xlarge.conf',
'c5.2xlarge' : 'nginxc52xlarge.conf',
'c5.4xlarge' : 'nginxc54xlarge.conf',
'p2.xlarge8' : 'nginxp2xlarge_8.conf',
'p2.xlarge16' : 'nginxp2xlarge_16.conf'
}
KR_CONFING = {
'c5.xlarge' : 'nginx_c5xlarge.conf',
'c5.2xlarge' : 'nginx_c52xlarge.conf',
'c5.4xlarge' : 'nginx_c54xlarge.conf'
}
NMT_CONFING = {
'c5.xlarge' : 'nginx_c5xlarge.conf',
'c5.2xlarge' : 'nginx_c52xlarge.conf',
'c5.4xlarge' : 'nginx_c54xlarge.conf'
}
class _ModelSource():
def get_request(self, data, ip):
pass
def setup_config(self, ins, region, typ):
pass
def get_lambda_req(self):
return 'https://example'
def collect_result(self, res):
return res['predictions']
class MXNetSource(_ModelSource):
def get_request(self, data, ip):
url = f'http://{ip}:8080/invocations'
return dct
def setup_config(self, ins, region, typ):
for i in ins:
ses = utils.get_session(i.ip)
utils.check_command(ses, 'docker run -p 8080:8080 mxnet-lstm-local &> /dev/null &')
test_data = "[{'input_sentence': 'on the exchange floor as soon as ual stopped trading we <unk> for a panic said one top floor trader'}]"
for i in ins:
while True:
try:
start_time = utils.now()
response = requests.post(url=f'http://{i.ip}:8080/invocations',data={'data': test_data})
if response.status_code == 200:
latency = utils.gap_time(start_time)
if latency <= 30:
logging.info(f'Successful warm up for ip: {i.ip}; Latency: {latency}')
break
else:
logging.info(f'Warming up for ip: {i.ip}; Latency: {latency}')
else:
logging.info(f'Warming up for ip: {i.ip}')
except requests.exceptions.RequestException as e:
print(f'Serving request exception for ip: {i.ip}')
time.sleep(10)
def get_lambda_req(self):
return
def collect_result(self, res):
return [res['prediction']]
class TensorFlowSource(_ModelSource):
def __init__(self):
test_file = f'{utils.upper_folder}/resources/test.jpg'
with open(test_file, 'rb') as f:
raw_data = f.read()
base64_bytes = b64encode(raw_data)
self.test_data = base64_bytes.decode('utf-8')
def get_request(self, data, ip):
url = f'http://{ip}:8000/predict'
headers = "{\"Content-type\": \"application/json\"}"
#image_data=json.dumps({
# 'instances': data
# })
url_data = json.dumps({'data':data[0]
})
dct = {'url': url, 'data': url_data, 'timeout': 2, 'headers':headers}
return dct
def setup_config(self, ins, region, typ):
#self. _start_nginx(ins)
#logging.info('Nginx started')
#cmd = TF_DEPLOY_CMD['GPU'] if typ.startswith('p2') else TF_DEPLOY_CMD['CPU']
models = "\"MobileNet InceptionV3\""
for i in ins:
cmd = f'nohup python3.6 sanic-server.py 0.0.0.0 8000 4 {models} > server.log 2>&1 &'
utils.check_command(utils.get_session(i.ip), cmd, debug=True)
#self._deploy_model(region, [ i.ip for i in ins ], cmd)
logging.info('Models are Deployed now')
image_data = [{ "b64": self.test_data},]
if typ.startswith('p2'):
image_data = image_data * HANDLE_SIZE_P2
"""for i in ins:
while True:
try:
response = requests.post(url=f'http://{i.ip}:8501/v1/models/resnet:predict',
data=image_data,
timeout=3)
if response.status_code == 200:
logging.info(f'Successful warm up for ip: {i.ip}')
break
logging.info(f'Warming up for ip: {i.ip}')
logging.info("response received",response)
except requests.exceptions.RequestException as e:
logging.info(f'no response: {e}')
time.sleep(5)"""
def get_lambda_req(self):
cmd = "aws lambda invoke --invocation-type RequestResponse --function-name lambda-resnet --region us-east-1 --payload \'{\"url\": \"https://s3.amazonaws.com/mxnet-tests/images/dog3.jpg\"}\' output_file "
return cmd
def _start_nginx(self, instances):
for i in instances:
nginx_conf = ''
if i.typ.startswith('p2'):
nginx_conf = TYPE_CONFING[f'{i.typ}{HANDLE_SIZE}']
else:
nginx_conf = TYPE_CONFING[i.typ] if i.typ in TYPE_CONFING else TYPE_CONFING['c5.large']
ses = utils.get_session(i.ip)
utils.check_command(ses, f'sudo cp /etc/nginx/{nginx_conf} /etc/nginx/nginx.conf && sudo systemctl restart nginx')
def _deploy_model(self, region, ips, cmd):
return all([utils.check_command(utils.get_session(i), cmd, debug=True) for i in ips])
class KerasSource(_ModelSource):
def __init__(self):
test_file = f'{utils.upper_folder}/keras/SageMaker/cat.jpg'
with open(test_file, "rb") as f:
raw_data = f.read()
self.test_data = raw_data
def get_request(self, data, ip):
url = f'http://{ip}:8301/invocations'
files = payload
dct = {'url': url, 'data': files, 'timeout': 2.5}
return dct
def _get_data(self):
test_file = f'{utils.upper_folder}/keras/SageMaker/cat.jpg'
with open(test_file, "rb") as f:
raw_data = f.read()
return raw_data
def setup_config(self, ins, region, typ):
for i in ins:
ses = utils.get_session(i.ip)
utils.check_command(ses, 'sudo service supervisor stop && sudo service nginx stop')
if i.typ.startswith('c5'):
nginx_conf = KR_CONFING[i.typ]
utils.check_command(ses, f'sudo cp /etc/nginx/{nginx_conf} /etc/nginx/nginx.conf')
utils.check_command(ses, 'sudo supervisord -c /etc/supervisor/conf.d/supervisord.conf -n &> /dev/null &')
test_file = f'{utils.upper_folder}/keras/SageMaker/cat.jpg'
with open(test_file, "rb") as f:
raw_data = f.read()
payload = {"image": raw_data}
for i in ins:
while True:
try:
response = requests.post(url=f'http://{i.ip}:8301/invocations',files=payload, timeout=3)
if response.status_code == 200:
logging.info(f'Successful warm up for ip: {i.ip}')
break
logging.info(f'Warming up for ip: {i.ip}')
except requests.exceptions.RequestException as e:
print(e)
time.sleep(5)
def get_lambda_req(self):
return 'https://example'
class NMTSource(_ModelSource):
def get_request(self, data, ip):
url = f'http://{ip}:8301/invocations'
return dct
def setup_config(self, ins, region, typ):
for i in ins:
ses = utils.get_session(i.ip)
utils.check_command(ses, 'sudo service supervisor stop && sudo service nginx stop')
if i.typ.startswith('c5'):
nginx_conf = KR_CONFING[i.typ]
utils.check_command(ses, f'sudo cp /etc/nginx/{nginx_conf} /etc/nginx/nginx.conf')
utils.check_command(ses, 'sudo supervisord -c /etc/supervisor/conf.d/supervisord.conf -n &> /dev/null &')
for i in ins:
while True:
try:
response = requests.post(url=f'http://{i.ip}:8301/invocations',
headers = {"Content-type": "application/json"},
data=json.dumps({
'instances': ["hellow"]
}),
timeout=3)
if response.status_code == 200:
logging.info(f'Successful warm up for ip: {i.ip}')
break
logging.info(f'Warming up for ip: {i.ip}')
except requests.exceptions.RequestException as e:
print(e)
time.sleep(5)
def get_lambda_req(self):
return 'exaple'
all_source = {
'kr': KerasSource(),
'tf': TensorFlowSource(),
'mx': MXNetSource(),
'nmt': NMTSource()
}
mdl_source = all_source['tf']