2022-04-18 20:24:04 +00:00
|
|
|
from concurrent.futures import ThreadPoolExecutor
|
2022-03-09 02:02:03 +00:00
|
|
|
from typing import Any, List
|
|
|
|
|
|
|
|
|
|
|
|
# Notice Preprocess class Must be named "Preprocess"
|
|
|
|
class Preprocess(object):
|
|
|
|
def __init__(self):
|
|
|
|
# set internal state, this will be called only once. (i.e. not per request)
|
2022-04-18 20:24:04 +00:00
|
|
|
self.executor = ThreadPoolExecutor(max_workers=32)
|
2022-03-09 02:02:03 +00:00
|
|
|
|
2022-03-20 23:00:19 +00:00
|
|
|
def postprocess(self, data: List[dict], collect_custom_statistics_fn=None) -> dict:
|
2022-03-09 02:02:03 +00:00
|
|
|
# we will here average the results and return the new value
|
|
|
|
# assume data is a list of dicts greater than 1
|
|
|
|
|
|
|
|
# average result
|
|
|
|
return dict(y=0.5 * data[0]['y'][0] + 0.5 * data[1]['y'][0])
|
|
|
|
|
2022-03-20 23:00:19 +00:00
|
|
|
def process(self, data: Any, collect_custom_statistics_fn=None) -> Any:
|
2022-03-09 02:02:03 +00:00
|
|
|
"""
|
|
|
|
do something with the actual data, return any type of object.
|
|
|
|
The returned object will be passed as is to the postprocess function engine
|
|
|
|
"""
|
2022-04-18 20:24:04 +00:00
|
|
|
predict_a = self.executor.submit(self.send_request, endpoint="/test_model_sklearn_a/", version=None, data=data)
|
|
|
|
predict_b = self.executor.submit(self.send_request, endpoint="/test_model_sklearn_b/", version=None, data=data)
|
|
|
|
|
|
|
|
predict_a = predict_a.result()
|
|
|
|
predict_b = predict_b.result()
|
|
|
|
|
2022-03-09 02:02:03 +00:00
|
|
|
if not predict_b or not predict_a:
|
|
|
|
raise ValueError("Error requesting inference endpoint test_model_sklearn a/b")
|
|
|
|
|
|
|
|
return [predict_a, predict_b]
|
|
|
|
|
|
|
|
def send_request(self, endpoint, version, data) -> List[dict]:
|
|
|
|
# Mock Function!
|
|
|
|
# replaced by real send request function when constructed by the inference service
|
|
|
|
pass
|