From 6fb72974145ca467361fcf7e61a3d210068afa96 Mon Sep 17 00:00:00 2001 From: Alejandro Saucedo Date: Mon, 2 Dec 2019 12:28:44 +0000 Subject: [PATCH 1/3] Added logic for JSON response to be created whenever model wrapper predict method returns Dict --- python/seldon_core/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/seldon_core/utils.py b/python/seldon_core/utils.py index 53f260b3a2..62049e9740 100644 --- a/python/seldon_core/utils.py +++ b/python/seldon_core/utils.py @@ -345,7 +345,7 @@ def construct_response_json( """ response = {} - if "jsonData" in client_request_raw: + if isinstance(client_raw_response, dict): response["jsonData"] = client_raw_response elif isinstance(client_raw_response, (bytes, bytearray)): base64_data = base64.b64encode(client_raw_response) From 382af7dfbbe88449be23c213ffa97d253c6b346b Mon Sep 17 00:00:00 2001 From: Alejandro Saucedo Date: Mon, 2 Dec 2019 16:58:05 +0000 Subject: [PATCH 2/3] Added test that makes sure json is still returned if a dict is passed even if a datadef is provided as input --- python/tests/test_utils.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/python/tests/test_utils.py b/python/tests/test_utils.py index f277bb4710..bee4be5525 100644 --- a/python/tests/test_utils.py +++ b/python/tests/test_utils.py @@ -15,10 +15,11 @@ class UserObject(object): - def __init__(self, metrics_ok=True, ret_nparray=False, ret_meta=False): + def __init__(self, metrics_ok=True, ret_nparray=False, ret_meta=False, ret_dict=False): self.metrics_ok = metrics_ok self.ret_nparray = ret_nparray self.nparray = np.array([1, 2, 3]) + self.dict = {"output": "data"} self.ret_meta = ret_meta def predict(self, X, features_names, **kwargs): @@ -34,6 +35,8 @@ def predict(self, X, features_names, **kwargs): self.inc_meta = kwargs.get("meta") if self.ret_nparray: return self.nparray + elif self.ret_dict: + return self.dict else: print("Predict called - will run identity function") print(X) @@ -190,6 +193,19 @@ def test_create_rest_response_jsondata(): assert json_response["jsonData"] != emptyValue +def test_create_rest_response_jsondata_with_array_input(): + user_model = UserObject(ret_dict=True) + request_data = np.array([[5, 6, 7]]) + datadef = scu.array_to_rest_datadef("ndarray", request_data) + json_request = {"data": datadef } + raw_response = {"output": "data"} + json_response = scu.construct_response_json( + user_model, True, json_request, raw_response + ) + assert "data" not in json_response + assert json_response["jsonData"] == user_model.dict + + def test_symmetric_json_conversion(): user_model = UserObject() request_data = np.array([[5, 6, 7]]) From 23a3bde976f9a790352c3c65a053e323df466fe6 Mon Sep 17 00:00:00 2001 From: Alejandro Saucedo Date: Mon, 2 Dec 2019 16:59:19 +0000 Subject: [PATCH 3/3] Ran linter on files --- python/tests/test_utils.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/python/tests/test_utils.py b/python/tests/test_utils.py index bee4be5525..3bbe394c57 100644 --- a/python/tests/test_utils.py +++ b/python/tests/test_utils.py @@ -15,7 +15,9 @@ class UserObject(object): - def __init__(self, metrics_ok=True, ret_nparray=False, ret_meta=False, ret_dict=False): + def __init__( + self, metrics_ok=True, ret_nparray=False, ret_meta=False, ret_dict=False + ): self.metrics_ok = metrics_ok self.ret_nparray = ret_nparray self.nparray = np.array([1, 2, 3]) @@ -197,7 +199,7 @@ def test_create_rest_response_jsondata_with_array_input(): user_model = UserObject(ret_dict=True) request_data = np.array([[5, 6, 7]]) datadef = scu.array_to_rest_datadef("ndarray", request_data) - json_request = {"data": datadef } + json_request = {"data": datadef} raw_response = {"output": "data"} json_response = scu.construct_response_json( user_model, True, json_request, raw_response