Skip to content

Commit

Permalink
Remove json tricks (#908)
Browse files Browse the repository at this point in the history
  • Loading branch information
vishalbollu authored Mar 25, 2020
1 parent dcc8aef commit 307d767
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 26 deletions.
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

0 comments on commit 307d767

Please sign in to comment.