File tree Expand file tree Collapse file tree 5 files changed +28
-6
lines changed Expand file tree Collapse file tree 5 files changed +28
-6
lines changed Original file line number Diff line number Diff line change 20
20
class MXNetPredictor (RealTimePredictor ):
21
21
"""A RealTimePredictor for inference against MXNet Endpoints.
22
22
23
- This is able to serialize Python lists and numpy arrays to multidimensional tensors for MXNet inference."""
23
+ This is able to serialize Python lists, dictionaries, and numpy arrays to multidimensional tensors for MXNet
24
+ inference."""
24
25
25
26
def __init__ (self , endpoint_name , sagemaker_session = None ):
26
27
"""Initialize an ``MXNetPredictor``.
Original file line number Diff line number Diff line change @@ -240,7 +240,12 @@ def __call__(self, data):
240
240
if isinstance (data , list ):
241
241
if not len (data ) > 0 :
242
242
raise ValueError ("empty array can't be serialized" )
243
- return _json_serialize_python_array (data )
243
+ return _json_serialize_python_object (data )
244
+
245
+ if isinstance (data , dict ):
246
+ if not len (data .keys ()) > 0 :
247
+ raise ValueError ("empty dictionary can't be serialized" )
248
+ return _json_serialize_python_object (data )
244
249
245
250
# files and buffers
246
251
if hasattr (data , 'read' ):
@@ -254,10 +259,10 @@ def __call__(self, data):
254
259
255
260
def _json_serialize_numpy_array (data ):
256
261
# numpy arrays can't be serialized but we know they have uniform type
257
- return _json_serialize_python_array (data .tolist ())
262
+ return _json_serialize_python_object (data .tolist ())
258
263
259
264
260
- def _json_serialize_python_array (data ):
265
+ def _json_serialize_python_object (data ):
261
266
return _json_serialize_object (data )
262
267
263
268
Original file line number Diff line number Diff line change 19
19
20
20
21
21
class TensorFlowPredictor (RealTimePredictor ):
22
- """A ``RealTimePredictor`` for inference against MXNet ``Endpoint``s."""
22
+ """A ``RealTimePredictor`` for inference against TensorFlow ``Endpoint``s.
23
23
24
+ This is able to serialize Python lists, dictionaries, and numpy arrays to multidimensional tensors for MXNet
25
+ inference"""
24
26
def __init__ (self , endpoint_name , sagemaker_session = None ):
25
27
"""Initialize an ``TensorFlowPredictor``.
26
28
Original file line number Diff line number Diff line change @@ -32,7 +32,7 @@ def __init__(self):
32
32
self .content_type = CONTENT_TYPE_OCTET_STREAM
33
33
34
34
def __call__ (self , data ):
35
- # isintance does not work here because a same protobuf message can be imported from a different module.
35
+ # isinstance does not work here because a same protobuf message can be imported from a different module.
36
36
# for example sagemaker.tensorflow.tensorflow_serving.regression_pb2 and tensorflow_serving.apis.regression_pb2
37
37
predict_type = data .__class__ .__name__
38
38
Original file line number Diff line number Diff line change @@ -51,12 +51,26 @@ def test_json_serializer_python_array():
51
51
assert result == '[1, 2, 3]'
52
52
53
53
54
+ def test_json_serializer_python_dictionary ():
55
+ d = {"gender" : "m" , "age" : 22 , "city" : "Paris" }
56
+
57
+ result = json_serializer (d )
58
+
59
+ assert json .loads (result ) == d
60
+
61
+
54
62
def test_json_serializer_python_invalid_empty ():
55
63
with pytest .raises (ValueError ) as error :
56
64
json_serializer ([])
57
65
assert "empty array" in str (error )
58
66
59
67
68
+ def test_json_serializer_python_dictionary_invalid_empty ():
69
+ with pytest .raises (ValueError ) as error :
70
+ json_serializer ({})
71
+ assert "empty dictionary" in str (error )
72
+
73
+
60
74
def test_json_serializer_csv_buffer ():
61
75
csv_file_path = os .path .join (DATA_DIR , "with_integers.csv" )
62
76
with open (csv_file_path ) as csv_file :
You can’t perform that action at this time.
0 commit comments