Skip to content

Remove json tricks #908

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

Merged
merged 5 commits into from
Mar 25, 2020
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
6 changes: 3 additions & 3 deletions examples/pytorch/object-detector/predictor.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ def predict(self, payload):
with torch.no_grad():
pred = self.model(img_tensor)

predicted_class = [self.coco_labels[i] for i in list(pred[0]["labels"].cpu().numpy())]
predicted_class = [self.coco_labels[i] for i in pred[0]["labels"].cpu().tolist()]
predicted_boxes = [
[(i[0], i[1]), (i[2], i[3])] for i in list(pred[0]["boxes"].detach().cpu().numpy())
[(i[0], i[1]), (i[2], i[3])] for i in pred[0]["boxes"].detach().cpu().tolist()
]
predicted_score = list(pred[0]["scores"].detach().cpu().numpy())
predicted_score = pred[0]["scores"].detach().cpu().tolist()
predicted_t = [predicted_score.index(x) for x in predicted_score if x > threshold]
if len(predicted_t) == 0:
return [], []
Expand Down
1 change: 0 additions & 1 deletion pkg/workloads/cortex/lib/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,3 @@ pyyaml==5.3
requests==2.22.0

datadog==0.33.0
json_tricks==3.13.5
18 changes: 0 additions & 18 deletions pkg/workloads/cortex/lib/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,10 @@
import msgpack
import pathlib
import inspect
import json_tricks
from inspect import Parameter
from copy import deepcopy


def json_tricks_encoder(*args, **kwargs):
kwargs["primitives"] = True
kwargs["obj_encoders"] = json_tricks.nonp.DEFAULT_ENCODERS
params = list(inspect.signature(json_tricks.TricksEncoder).parameters.values())
params += list(inspect.signature(json.JSONEncoder).parameters.values())
expected_keys = set()
for param in params:
if param.kind == Parameter.POSITIONAL_OR_KEYWORD or param.kind == Parameter.KEYWORD_ONLY:
expected_keys.add(param.name)

for key in list(kwargs.keys()):
if key not in expected_keys:
kwargs.pop(key)

return json_tricks.TricksEncoder(*args, **kwargs)


def extract_zip(zip_path, dest_dir=None, delete_zip_file=False):
if dest_dir is None:
dest_dir = os.path.dirname(zip_path)
Expand Down
12 changes: 8 additions & 4 deletions pkg/workloads/cortex/serve/serve.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
from cortex.lib.type import API
from cortex.lib.log import cx_logger, debug_obj
from cortex.lib.storage import S3

from cortex.lib.exceptions import UserRuntimeException

if os.environ["CORTEX_VERSION"] != consts.CORTEX_VERSION:
errMsg = f"your Cortex operator version ({os.environ['CORTEX_VERSION']}) doesn't match your predictor image version ({consts.CORTEX_VERSION}); please update your cluster by following the instructions at https://www.cortex.dev/cluster-management/update, or update your predictor image by modifying the appropriate `image_*` field(s) in your cluster configuration file (e.g. cluster.yaml) and running `cortex cluster update --config cluster.yaml`"
Expand Down Expand Up @@ -153,12 +153,16 @@ def predict(request: Any = Body(..., media_type="application/json"), debug=False

debug_obj("payload", request, debug)
prediction = predictor_impl.predict(request)
debug_obj("prediction", prediction, debug)

try:
json_string = json.dumps(prediction)
except:
json_string = util.json_tricks_encoder().encode(prediction)
except Exception as e:
raise UserRuntimeException(
f"the return value of predict() or one of its nested values is not JSON serializable",
str(e),
) from e

debug_obj("prediction", json_string, debug)

response = Response(content=json_string, media_type="application/json")

Expand Down