From 1ca0919257c249cc68cf76b1edc163fc007f08f0 Mon Sep 17 00:00:00 2001 From: Zhun Xu Date: Thu, 24 Oct 2019 12:45:53 -0400 Subject: [PATCH 1/5] decode in request json --- python/seldon_core/flask_utils.py | 3 +++ python/tests/test_model_microservice.py | 7 +++---- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/python/seldon_core/flask_utils.py b/python/seldon_core/flask_utils.py index 91f9fd99c5..d66cc1de82 100644 --- a/python/seldon_core/flask_utils.py +++ b/python/seldon_core/flask_utils.py @@ -66,6 +66,9 @@ def get_request() -> Dict: raise SeldonMicroserviceException("Can't find JSON in data") if message is None: raise SeldonMicroserviceException("Invalid Data Format - empty JSON") + if 'binData' in message and message['binData'] is not None: + binData = message['binData'] + message['binData'] = base64.b64decode(binData).decode("utf-8") return message diff --git a/python/tests/test_model_microservice.py b/python/tests/test_model_microservice.py index 086c287a8e..09d22ef344 100644 --- a/python/tests/test_model_microservice.py +++ b/python/tests/test_model_microservice.py @@ -418,9 +418,8 @@ def test_model_bin_data(): bdata_base64 = base64.b64encode(bdata).decode("utf-8") rv = client.get('/predict?json={"binData":"' + bdata_base64 + '"}') j = json.loads(rv.data) - return_data = base64.b64encode(base64.b64encode(bdata)).decode("utf-8") assert rv.status_code == 200 - assert j["binData"] == return_data + assert j["binData"] == bdata_base64 assert j["meta"]["tags"] == {"mytag": 1} assert j["meta"]["metrics"][0]["key"] == user_object.metrics()[0]["key"] assert j["meta"]["metrics"][0]["value"] == user_object.metrics()[0]["value"] @@ -430,8 +429,8 @@ def test_model_bin_data_nparray(): user_object = UserObject(ret_nparray=True) app = get_rest_microservice(user_object) client = app.test_client() - encoded = base64.b64encode(b"1234") - rv = client.get('/predict?json={"binData":"' + str(encoded) + '"}') + encoded = base64.b64encode(b"1234").decode("utf-8") + rv = client.get('/predict?json={"binData":"' + encoded + '"}') j = json.loads(rv.data) print(j) assert rv.status_code == 200 From 429b3108989fb73f9c4061dce1d3c9d5577333c2 Mon Sep 17 00:00:00 2001 From: Zhun Xu Date: Thu, 24 Oct 2019 22:58:49 -0400 Subject: [PATCH 2/5] add decode for proto data too --- python/seldon_core/wrapper.py | 5 +++++ python/tests/test_model_microservice.py | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/python/seldon_core/wrapper.py b/python/seldon_core/wrapper.py index d6942fba5b..395b85abd5 100644 --- a/python/seldon_core/wrapper.py +++ b/python/seldon_core/wrapper.py @@ -16,6 +16,7 @@ ANNOTATION_GRPC_MAX_MSG_SIZE, ) from seldon_core.proto import prediction_pb2_grpc +import base64 import os logger = logging.getLogger(__name__) @@ -113,6 +114,10 @@ def __init__(self, user_model): self.user_model = user_model def Predict(self, request_grpc, context): + data_type = request_grpc.WhichOneof("data_oneof") + if data_type == "binData": + binData = request_grpc.binData + request_grpc.binData = base64.b64decode(binData) return seldon_core.seldon_methods.predict(self.user_model, request_grpc) def SendFeedback(self, feedback_grpc, context): diff --git a/python/tests/test_model_microservice.py b/python/tests/test_model_microservice.py index 09d22ef344..c1ab30aaa8 100644 --- a/python/tests/test_model_microservice.py +++ b/python/tests/test_model_microservice.py @@ -609,7 +609,7 @@ def test_proto_bin_data(): bdata_base64 = base64.b64encode(bdata) request = prediction_pb2.SeldonMessage(binData=bdata_base64) resp = app.Predict(request, None) - assert resp.binData == bdata_base64 + assert resp.binData == bdata def test_proto_bin_data_nparray(): From 71cc0b6047089837150989c6bfce44c66b0445f3 Mon Sep 17 00:00:00 2001 From: Zhun Xu Date: Fri, 25 Oct 2019 09:12:37 -0400 Subject: [PATCH 3/5] revert proto --- python/seldon_core/wrapper.py | 4 ---- python/tests/test_model_microservice.py | 2 +- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/python/seldon_core/wrapper.py b/python/seldon_core/wrapper.py index 395b85abd5..f9f089ec83 100644 --- a/python/seldon_core/wrapper.py +++ b/python/seldon_core/wrapper.py @@ -114,10 +114,6 @@ def __init__(self, user_model): self.user_model = user_model def Predict(self, request_grpc, context): - data_type = request_grpc.WhichOneof("data_oneof") - if data_type == "binData": - binData = request_grpc.binData - request_grpc.binData = base64.b64decode(binData) return seldon_core.seldon_methods.predict(self.user_model, request_grpc) def SendFeedback(self, feedback_grpc, context): diff --git a/python/tests/test_model_microservice.py b/python/tests/test_model_microservice.py index c1ab30aaa8..09d22ef344 100644 --- a/python/tests/test_model_microservice.py +++ b/python/tests/test_model_microservice.py @@ -609,7 +609,7 @@ def test_proto_bin_data(): bdata_base64 = base64.b64encode(bdata) request = prediction_pb2.SeldonMessage(binData=bdata_base64) resp = app.Predict(request, None) - assert resp.binData == bdata + assert resp.binData == bdata_base64 def test_proto_bin_data_nparray(): From f49b6284d0f3a0f2b1b9c78b1cf604a9c786fd58 Mon Sep 17 00:00:00 2001 From: Zhun Xu Date: Fri, 25 Oct 2019 09:13:26 -0400 Subject: [PATCH 4/5] revert proto --- python/seldon_core/wrapper.py | 1 - 1 file changed, 1 deletion(-) diff --git a/python/seldon_core/wrapper.py b/python/seldon_core/wrapper.py index f9f089ec83..d6942fba5b 100644 --- a/python/seldon_core/wrapper.py +++ b/python/seldon_core/wrapper.py @@ -16,7 +16,6 @@ ANNOTATION_GRPC_MAX_MSG_SIZE, ) from seldon_core.proto import prediction_pb2_grpc -import base64 import os logger = logging.getLogger(__name__) From 68adfef54ac88c649760087e1afcd330d6b2a100 Mon Sep 17 00:00:00 2001 From: Zhun Xu Date: Fri, 25 Oct 2019 10:52:11 -0400 Subject: [PATCH 5/5] remove decode utf-8 --- python/seldon_core/flask_utils.py | 2 +- python/seldon_core/utils.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/python/seldon_core/flask_utils.py b/python/seldon_core/flask_utils.py index d66cc1de82..8c7c5679d0 100644 --- a/python/seldon_core/flask_utils.py +++ b/python/seldon_core/flask_utils.py @@ -68,7 +68,7 @@ def get_request() -> Dict: raise SeldonMicroserviceException("Invalid Data Format - empty JSON") if 'binData' in message and message['binData'] is not None: binData = message['binData'] - message['binData'] = base64.b64decode(binData).decode("utf-8") + message['binData'] = base64.b64decode(binData) return message diff --git a/python/seldon_core/utils.py b/python/seldon_core/utils.py index 4f27b8dcd4..9e22f89486 100644 --- a/python/seldon_core/utils.py +++ b/python/seldon_core/utils.py @@ -549,7 +549,7 @@ def extract_request_parts_json( features = request["strData"] elif "binData" in request: data_type = "binData" - features = bytes(request["binData"], "utf8") + features = bytes(request["binData"]) else: raise SeldonMicroserviceException(f"Invalid request data type: {request}")