Skip to content

Commit 307d767

Browse files
authored
Remove json tricks (#908)
1 parent dcc8aef commit 307d767

File tree

4 files changed

+11
-26
lines changed

4 files changed

+11
-26
lines changed

examples/pytorch/object-detector/predictor.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ def predict(self, payload):
3232
with torch.no_grad():
3333
pred = self.model(img_tensor)
3434

35-
predicted_class = [self.coco_labels[i] for i in list(pred[0]["labels"].cpu().numpy())]
35+
predicted_class = [self.coco_labels[i] for i in pred[0]["labels"].cpu().tolist()]
3636
predicted_boxes = [
37-
[(i[0], i[1]), (i[2], i[3])] for i in list(pred[0]["boxes"].detach().cpu().numpy())
37+
[(i[0], i[1]), (i[2], i[3])] for i in pred[0]["boxes"].detach().cpu().tolist()
3838
]
39-
predicted_score = list(pred[0]["scores"].detach().cpu().numpy())
39+
predicted_score = pred[0]["scores"].detach().cpu().tolist()
4040
predicted_t = [predicted_score.index(x) for x in predicted_score if x > threshold]
4141
if len(predicted_t) == 0:
4242
return [], []

pkg/workloads/cortex/lib/requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,3 @@ pyyaml==5.3
66
requests==2.22.0
77

88
datadog==0.33.0
9-
json_tricks==3.13.5

pkg/workloads/cortex/lib/util.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,10 @@
2020
import msgpack
2121
import pathlib
2222
import inspect
23-
import json_tricks
2423
from inspect import Parameter
2524
from copy import deepcopy
2625

2726

28-
def json_tricks_encoder(*args, **kwargs):
29-
kwargs["primitives"] = True
30-
kwargs["obj_encoders"] = json_tricks.nonp.DEFAULT_ENCODERS
31-
params = list(inspect.signature(json_tricks.TricksEncoder).parameters.values())
32-
params += list(inspect.signature(json.JSONEncoder).parameters.values())
33-
expected_keys = set()
34-
for param in params:
35-
if param.kind == Parameter.POSITIONAL_OR_KEYWORD or param.kind == Parameter.KEYWORD_ONLY:
36-
expected_keys.add(param.name)
37-
38-
for key in list(kwargs.keys()):
39-
if key not in expected_keys:
40-
kwargs.pop(key)
41-
42-
return json_tricks.TricksEncoder(*args, **kwargs)
43-
44-
4527
def extract_zip(zip_path, dest_dir=None, delete_zip_file=False):
4628
if dest_dir is None:
4729
dest_dir = os.path.dirname(zip_path)

pkg/workloads/cortex/serve/serve.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
from cortex.lib.type import API
3636
from cortex.lib.log import cx_logger, debug_obj
3737
from cortex.lib.storage import S3
38-
38+
from cortex.lib.exceptions import UserRuntimeException
3939

4040
if os.environ["CORTEX_VERSION"] != consts.CORTEX_VERSION:
4141
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`"
@@ -153,12 +153,16 @@ def predict(request: Any = Body(..., media_type="application/json"), debug=False
153153

154154
debug_obj("payload", request, debug)
155155
prediction = predictor_impl.predict(request)
156-
debug_obj("prediction", prediction, debug)
157156

158157
try:
159158
json_string = json.dumps(prediction)
160-
except:
161-
json_string = util.json_tricks_encoder().encode(prediction)
159+
except Exception as e:
160+
raise UserRuntimeException(
161+
f"the return value of predict() or one of its nested values is not JSON serializable",
162+
str(e),
163+
) from e
164+
165+
debug_obj("prediction", json_string, debug)
162166

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

0 commit comments

Comments
 (0)