Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Functionality for model wrapper to return JSON on any Dict return #1174

Merged
merged 3 commits into from
Dec 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion python/seldon_core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
20 changes: 19 additions & 1 deletion python/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@


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):
Expand All @@ -34,6 +37,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)
Expand Down Expand Up @@ -190,6 +195,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]])
Expand Down