Skip to content

Commit

Permalink
Decode binData before sending it to Predict function (#993)
Browse files Browse the repository at this point in the history
* decode in request json

* add decode for proto data too

* revert proto

* revert proto

* remove decode utf-8
  • Loading branch information
lennon310 authored and axsaucedo committed Oct 25, 2019
1 parent 87b4c53 commit f2d2aef
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 5 deletions.
3 changes: 3 additions & 0 deletions python/seldon_core/flask_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
return message


Expand Down
2 changes: 1 addition & 1 deletion python/seldon_core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")

Expand Down
7 changes: 3 additions & 4 deletions python/tests/test_model_microservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand All @@ -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
Expand Down

0 comments on commit f2d2aef

Please sign in to comment.