FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
mlrequest-python/mlrequest/mlrequest.py at master · mlrequest/mlrequest-python · GitHub
mlrequest
mlrequest-python
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
mlrequest-python
/
mlrequest
/
mlrequest.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
429 lines (333 loc) · 16.6 KB
Breadcrumbs
mlrequest-python
/
mlrequest
/
mlrequest.py
Copy path
File metadata and controls
429 lines (333 loc) · 16.6 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
from
mlrequest
import
regions
from
requests_futures
.
sessions
import
FuturesSession
import
json
import
requests
import
sklearn_json
as
skljson
import
sys
import
logging
class
Classifier
:
session
=
FuturesSession
()
base_url
=
'https://api.mlrequest.com'
model_path
=
'/v1/classifier/'
batch_model_path
=
'/v1/classifier/batch/'
predict_path
=
'predict'
learn_path
=
'learn'
batch_size
=
100
def
__init__
(
self
,
api_key
=
None
):
if
not
api_key
:
raise
MissingAPIKey
(
'An API Key is required. Visit https://mlrequest.com for a free or paid API Key.'
)
self
.
session
.
headers
[
'MLREQ-API-KEY'
]
=
api_key
def
predict
(
self
,
features
,
model_name
,
class_count
):
self
.
_validate_predict_fields
(
features
,
model_name
,
class_count
)
if
isinstance
(
features
,
list
):
return
self
.
batch_predict
(
features
,
model_name
,
class_count
)
else
:
payload
=
{
'features'
:
features
,
'model_name'
:
model_name
,
'class_count'
:
class_count
}
future
=
self
.
session
.
post
(
f'
{
self
.
base_url
}
{
self
.
model_path
}
{
self
.
predict_path
}
'
,
json
=
payload
)
response
=
json
.
loads
(
future
.
result
().
content
)
if
(
'predict_result'
in
response
)
and
(
'class_scores'
in
response
):
return
Response
({
'predict_result'
:
response
[
'predict_result'
],
'class_scores'
:
response
[
'class_scores'
],
'content'
:
response
})
else
:
return
Response
({
'predict_result'
:
None
,
'content'
:
response
})
def
batch_predict
(
self
,
features
,
model_name
,
class_count
):
class_scores
=
[]
predict_results
=
[]
errors
=
[]
for
i
in
range
(
0
,
len
(
features
),
self
.
batch_size
):
futures
=
[]
feature_batch
=
features
[
i
:
i
+
self
.
batch_size
]
payload
=
{
'features'
:
feature_batch
,
'model_name'
:
model_name
,
'class_count'
:
class_count
}
futures
.
append
(
self
.
session
.
post
(
f'
{
self
.
base_url
}
{
self
.
batch_model_path
}
{
self
.
predict_path
}
'
,
json
=
payload
))
for
future
in
futures
:
response
=
json
.
loads
(
future
.
result
().
content
)
if
(
'class_scores'
in
response
)
and
(
'predict_result'
in
response
):
class_scores
+=
response
[
'class_scores'
]
predict_results
+=
response
[
'predict_result'
]
else
:
class_scores
+=
[
None
]
predict_results
+=
[
None
]
errors
.
append
(
response
)
return
Response
({
'predict_result'
:
predict_results
,
'class_scores'
:
class_scores
,
'errors'
:
errors
,
'content'
:
response
})
def
learn
(
self
,
training_data
,
model_name
,
class_count
):
self
.
_validate_learn_fields
(
training_data
,
model_name
,
class_count
)
if
isinstance
(
training_data
,
list
):
return
self
.
batch_learn
(
training_data
,
model_name
,
class_count
)
else
:
payload
=
{
'features'
:
training_data
[
'features'
],
'model_name'
:
model_name
,
'class_count'
:
class_count
,
'label'
:
training_data
[
'label'
]
}
future
=
self
.
session
.
post
(
f'
{
self
.
base_url
}
{
self
.
model_path
}
{
self
.
learn_path
}
'
,
json
=
payload
)
return
Response
({
'content'
:
json
.
loads
(
future
.
result
().
content
)})
def
batch_learn
(
self
,
training_data
,
model_name
,
class_count
):
responses
=
[]
for
i
in
range
(
0
,
len
(
training_data
),
self
.
batch_size
):
futures
=
[]
training_batch
=
training_data
[
i
:
i
+
self
.
batch_size
]
payload
=
{
'training_data'
:
training_batch
,
'model_name'
:
model_name
,
'class_count'
:
class_count
,
}
futures
.
append
(
self
.
session
.
post
(
f'
{
self
.
base_url
}
{
self
.
batch_model_path
}
{
self
.
learn_path
}
'
,
json
=
payload
))
for
future
in
futures
:
responses
.
append
(
json
.
loads
(
future
.
result
().
content
))
return
Response
({
'content'
:
responses
})
def
_validate_predict_fields
(
self
,
features
,
model_name
,
class_count
):
if
not
(
isinstance
(
features
,
dict
)
or
isinstance
(
features
,
list
)):
raise
ValueError
(
f'features must be a dict or a list of dicts'
)
if
len
(
features
)
==
0
:
raise
ValueError
(
f'features must not be empty'
)
if
not
isinstance
(
model_name
,
str
):
raise
ValueError
(
f'model_name must be a string'
)
if
not
isinstance
(
class_count
,
int
):
raise
ValueError
(
f'class_count must be an integer'
)
def
_validate_learn_fields
(
self
,
training_data
,
model_name
,
class_count
):
if
not
(
isinstance
(
training_data
,
dict
)
or
isinstance
(
training_data
,
list
)):
raise
ValueError
(
f'training_data must be a dict or a list of dicts containing features and an associated label.'
)
if
len
(
training_data
)
==
0
:
raise
ValueError
(
f'training data must not be empty'
)
if
not
isinstance
(
model_name
,
str
):
raise
ValueError
(
f'model_name must be a string'
)
if
not
isinstance
(
class_count
,
int
):
raise
ValueError
(
f'class_count must be an integer'
)
class
Regression
:
session
=
FuturesSession
()
base_url
=
'https://api.mlrequest.com'
model_path
=
'/v1/regression/'
batch_model_path
=
'/v1/regression/batch/'
predict_path
=
'predict'
learn_path
=
'learn'
batch_size
=
100
def
__init__
(
self
,
api_key
=
None
):
if
not
api_key
:
raise
MissingAPIKey
(
'An API Key is required. Visit https://mlrequest.com for a free or paid API Key.'
)
self
.
session
.
headers
[
'MLREQ-API-KEY'
]
=
api_key
def
predict
(
self
,
features
,
model_name
):
self
.
_validate_predict_fields
(
features
,
model_name
)
if
isinstance
(
features
,
list
):
return
self
.
batch_predict
(
features
,
model_name
)
else
:
payload
=
{
'features'
:
features
,
'model_name'
:
model_name
}
future
=
self
.
session
.
post
(
f'
{
self
.
base_url
}
{
self
.
model_path
}
{
self
.
predict_path
}
'
,
json
=
payload
)
response
=
json
.
loads
(
future
.
result
().
content
)
if
'predict_result'
in
response
:
return
Response
({
'predict_result'
:
response
[
'predict_result'
],
'content'
:
response
})
else
:
return
Response
({
'predict_result'
:
None
,
'content'
:
response
})
def
batch_predict
(
self
,
features
,
model_name
):
predict_results
=
[]
errors
=
[]
for
i
in
range
(
0
,
len
(
features
),
self
.
batch_size
):
futures
=
[]
feature_batch
=
features
[
i
:
i
+
self
.
batch_size
]
payload
=
{
'features'
:
feature_batch
,
'model_name'
:
model_name
}
futures
.
append
(
self
.
session
.
post
(
f'
{
self
.
base_url
}
{
self
.
batch_model_path
}
{
self
.
predict_path
}
'
,
json
=
payload
))
for
future
in
futures
:
response
=
json
.
loads
(
future
.
result
().
content
)
if
'predict_result'
in
response
:
predict_results
+=
response
[
'predict_result'
]
else
:
predict_results
+=
[
None
]
errors
.
append
(
response
)
return
Response
({
'predict_result'
:
predict_results
,
'errors'
:
errors
,
'content'
:
response
})
def
learn
(
self
,
training_data
,
model_name
):
self
.
_validate_learn_fields
(
training_data
,
model_name
)
if
isinstance
(
training_data
,
list
):
return
self
.
batch_learn
(
training_data
,
model_name
)
else
:
payload
=
{
'features'
:
training_data
[
'features'
],
'model_name'
:
model_name
,
'label'
:
training_data
[
'label'
]
}
future
=
self
.
session
.
post
(
f'
{
self
.
base_url
}
{
self
.
model_path
}
{
self
.
learn_path
}
'
,
json
=
payload
)
return
Response
({
'content'
:
json
.
loads
(
future
.
result
().
content
)})
def
batch_learn
(
self
,
training_data
,
model_name
):
responses
=
[]
for
i
in
range
(
0
,
len
(
training_data
),
self
.
batch_size
):
futures
=
[]
training_batch
=
training_data
[
i
:
i
+
self
.
batch_size
]
payload
=
{
'training_data'
:
training_batch
,
'model_name'
:
model_name
}
futures
.
append
(
self
.
session
.
post
(
f'
{
self
.
base_url
}
{
self
.
batch_model_path
}
{
self
.
learn_path
}
'
,
json
=
payload
))
for
future
in
futures
:
responses
.
append
(
json
.
loads
(
future
.
result
().
content
))
return
Response
({
'content'
:
responses
})
def
_validate_predict_fields
(
self
,
features
,
model_name
):
if
not
(
isinstance
(
features
,
dict
)
or
isinstance
(
features
,
list
)):
raise
ValueError
(
f'features must be a dict or a list of dicts'
)
if
len
(
features
)
==
0
:
raise
ValueError
(
f'features must not be empty'
)
if
not
isinstance
(
model_name
,
str
):
raise
ValueError
(
f'model_name must be a string'
)
def
_validate_learn_fields
(
self
,
training_data
,
model_name
):
if
not
(
isinstance
(
training_data
,
dict
)
or
isinstance
(
training_data
,
list
)):
raise
ValueError
(
f'training_data must be a dict or a list of dicts containing features and an associated label.'
)
if
len
(
training_data
)
==
0
:
raise
ValueError
(
f'training data must not be empty'
)
if
not
isinstance
(
model_name
,
str
):
raise
ValueError
(
f'model_name must be a string'
)
class
RL
:
session
=
FuturesSession
()
base_url
=
'https://api.mlrequest.com'
model_path
=
'/v1/rl/'
predict_path
=
'predict'
learn_path
=
'reward'
def
__init__
(
self
,
api_key
=
None
):
if
not
api_key
:
raise
MissingAPIKey
(
'An API Key is required. Visit https://mlrequest.com for a free or paid API Key.'
)
self
.
session
.
headers
[
'MLREQ-API-KEY'
]
=
api_key
def
predict
(
self
,
features
,
model_name
,
session_id
,
negative_reward
,
action_count
,
epsilon
=
0.2
,
action_list
=
None
):
self
.
_validate_predict_fields
(
features
,
model_name
,
session_id
,
negative_reward
,
action_count
,
epsilon
,
action_list
)
payload
=
{
'features'
:
features
,
'model_name'
:
model_name
,
'session_id'
:
session_id
,
'negative_reward'
:
negative_reward
,
'action_count'
:
action_count
,
'epsilon'
:
epsilon
,
'action_list'
:
action_list
}
future
=
self
.
session
.
post
(
f'
{
self
.
base_url
}
{
self
.
model_path
}
{
self
.
predict_path
}
'
,
json
=
payload
)
response
=
json
.
loads
(
future
.
result
().
content
)
if
'predict_result'
in
response
:
return
Response
({
'predict_result'
:
response
[
'predict_result'
],
'content'
:
response
})
else
:
return
Response
({
'predict_result'
:
None
,
'content'
:
response
})
def
reward
(
self
,
reward
,
model_name
,
session_id
):
self
.
_validate_reward_fields
(
model_name
,
session_id
,
reward
)
payload
=
{
'model_name'
:
model_name
,
'session_id'
:
session_id
,
'reward'
:
reward
}
future
=
self
.
session
.
post
(
f'
{
self
.
base_url
}
{
self
.
model_path
}
{
self
.
learn_path
}
'
,
json
=
payload
)
return
Response
({
'content'
:
json
.
loads
(
future
.
result
().
content
)})
def
_validate_predict_fields
(
self
,
features
,
model_name
,
session_id
,
negative_reward
,
action_count
,
epsilon
,
action_list
):
if
not
isinstance
(
features
,
dict
):
raise
ValueError
(
f'features must be a dict'
)
if
len
(
features
)
==
0
:
raise
ValueError
(
f'features must not be empty'
)
if
not
isinstance
(
model_name
,
str
):
raise
ValueError
(
f'model_name must be a string'
)
if
not
isinstance
(
session_id
,
str
):
raise
ValueError
(
f'session_id must be a string'
)
if
not
isinstance
(
negative_reward
, (
int
,
float
)):
raise
ValueError
(
f'negative_reward must be a number'
)
if
(
not
isinstance
(
action_count
,
int
))
or
action_count
<
0
:
raise
ValueError
(
f'negative_reward must be an int greater than 0'
)
if
not
isinstance
(
epsilon
,
float
)
or
epsilon
<
0
or
epsilon
>
1
:
raise
ValueError
(
f'negative_reward must be an float between 0 and 1'
)
if
(
action_list
is
not
None
)
and
(
not
isinstance
(
action_list
,
list
)):
raise
ValueError
(
f'action_list must be None or a list'
)
def
_validate_reward_fields
(
self
,
model_name
,
session_id
,
reward
):
if
not
isinstance
(
model_name
,
str
):
raise
ValueError
(
f'model_name must be a string'
)
if
not
isinstance
(
session_id
,
str
):
raise
ValueError
(
f'session_id must be a string'
)
if
not
isinstance
(
reward
, (
int
,
float
)):
raise
ValueError
(
f'model_name must be a number'
)
class
Account
:
session
=
FuturesSession
()
base_url
=
'https://api.mlrequest.com'
def
__init__
(
self
,
api_key
=
None
):
self
.
log
=
logging
.
getLogger
(
__name__
+
'.Account'
)
if
not
api_key
:
raise
MissingAPIKey
(
'An API Key is required. Visit https://mlrequest.com for a free or paid API Key.'
)
self
.
session
.
headers
[
'MLREQ-API-KEY'
]
=
api_key
def
delete_model
(
self
,
model_name
):
payload
=
{
'model_name'
:
model_name
}
future
=
self
.
session
.
post
(
f'
{
self
.
base_url
}
/v1/model/delete'
,
json
=
payload
)
response
=
json
.
loads
(
future
.
result
().
content
)
return
Response
({
'content'
:
response
})
def
get_details
(
self
):
future
=
self
.
session
.
get
(
f'
{
self
.
base_url
}
/v1/account'
)
response
=
json
.
loads
(
future
.
result
().
content
)
return
Response
({
'content'
:
response
})
class
SKLearn
:
def
__init__
(
self
,
api_key
=
None
):
self
.
log
=
logging
.
getLogger
(
__name__
+
'.SKLearn'
)
if
not
api_key
:
raise
MissingAPIKey
(
'An API Key is required. Visit https://mlrequest.com for a free or paid API Key.'
)
self
.
headers
=
{
'MLREQ-API-KEY'
:
api_key
}
def
deploy
(
self
,
sklearn_model
,
model_name
,
region_url
=
regions
.
ALL
):
payload
=
{
'model_name'
:
model_name
}
try
:
r
=
requests
.
post
(
f'
{
region_url
}
/v1/sklearn/deploy/url'
,
json
=
payload
,
headers
=
self
.
headers
)
response
=
r
.
json
()
except
Exception
as
e
:
return
Response
({
'content'
:
f'There was an error getting the deploy URL
{
r
.
text
}
'
})
if
'url'
in
response
and
'tier'
in
response
:
url
=
response
[
'url'
]
tier
=
response
[
'tier'
]
model_dict
=
skljson
.
to_dict
(
sklearn_model
)
model_size_bytes
=
sys
.
getsizeof
(
json
.
dumps
(
model_dict
))
model_size_mb
=
model_size_bytes
/
1000
**
2
if
(
model_size_mb
>
1
)
and
(
tier
==
0
):
raise
ModelSizeExceeded
(
f'Model size is
{
model_size_mb
}
MB. Upgrade to a paid plan for model sizes greater than 1 MB.'
)
if
model_size_mb
>
100
:
raise
ModelSizeExceeded
(
f'Model size is
{
model_size_mb
}
MB and cannot exceed 100 MB.'
)
requests
.
put
(
url
,
json
=
model_dict
,
headers
=
{
'content-type'
:
'application/json'
})
return
Response
({
'content'
:
'Model deployed.'
})
elif
'message'
in
response
:
return
Response
({
'content'
:
response
[
'message'
]})
else
:
return
Response
({
'content'
:
response
})
def
predict
(
self
,
features
,
model_name
,
region_url
=
regions
.
ALL
):
self
.
_validate_predict_fields
(
features
,
model_name
)
payload
=
{
'features'
:
features
,
'model_name'
:
model_name
}
try
:
r
=
requests
.
post
(
f'
{
region_url
}
/v1/sklearn/predict'
,
json
=
payload
,
headers
=
self
.
headers
)
response
=
r
.
json
()
except
Exception
as
e
:
return
f'Error getting scikit-learn prediction:
{
e
}
,
{
r
.
text
}
'
if
'predict_result'
in
response
:
return
Response
({
'predict_result'
:
response
[
'predict_result'
],
'content'
:
response
})
else
:
return
Response
({
'predict_result'
:
None
,
'content'
:
response
})
def
_validate_predict_fields
(
self
,
features
,
model_name
):
if
not
(
isinstance
(
features
,
list
)
and
isinstance
(
features
[
0
],
list
)):
raise
ValueError
(
f'Features must be a list of lists. '
f'Example of one prediction: [[1, 2, 3]]. '
f'Example of multiple predictions: [[1, 2, 3], [5, 6, 7], ...]'
)
if
len
(
features
)
==
0
:
raise
ValueError
(
f'features must not be empty'
)
if
not
isinstance
(
model_name
,
str
):
raise
ValueError
(
f'model_name must be a string'
)
class
Response
(
dict
):
def
__init__
(
self
,
*
args
,
**
kwargs
):
super
(
Response
,
self
).
__init__
(
*
args
,
**
kwargs
)
self
.
__dict__
=
self
class
MissingAPIKey
(
Exception
):
pass
class
ModelSizeExceeded
(
Exception
):
pass
Back
|
FazBrowse Home
|
New Git URL