@@ -31,11 +31,11 @@ class BaseDeserializer(abc.ABC):
31
31
"""
32
32
33
33
@abc .abstractmethod
34
- def deserialize (self , data , content_type ):
34
+ def deserialize (self , stream , content_type ):
35
35
"""Deserialize data received from an inference endpoint.
36
36
37
37
Args:
38
- data (object ): Data to be deserialized.
38
+ stream (botocore.response.StreamingBody ): Data to be deserialized.
39
39
content_type (str): The MIME type of the data.
40
40
41
41
Returns:
@@ -61,41 +61,41 @@ def __init__(self, encoding="UTF-8"):
61
61
"""
62
62
self .encoding = encoding
63
63
64
- def deserialize (self , data , content_type ):
64
+ def deserialize (self , stream , content_type ):
65
65
"""Deserialize data from an inference endpoint into a decoded string.
66
66
67
67
Args:
68
- data (object ): Data to be deserialized.
68
+ stream (botocore.response.StreamingBody ): Data to be deserialized.
69
69
content_type (str): The MIME type of the data.
70
70
71
71
Returns:
72
72
str: The data deserialized into a decoded string.
73
73
"""
74
74
try :
75
- return data .read ().decode (self .encoding )
75
+ return stream .read ().decode (self .encoding )
76
76
finally :
77
- data .close ()
77
+ stream .close ()
78
78
79
79
80
80
class BytesDeserializer (BaseDeserializer ):
81
81
"""Deserialize a stream of bytes into a bytes object."""
82
82
83
83
ACCEPT = "*/*"
84
84
85
- def deserialize (self , data , content_type ):
85
+ def deserialize (self , stream , content_type ):
86
86
"""Read a stream of bytes returned from an inference endpoint.
87
87
88
88
Args:
89
- data (object ): A stream of bytes.
89
+ stream (botocore.response.StreamingBody ): A stream of bytes.
90
90
content_type (str): The MIME type of the data.
91
91
92
92
Returns:
93
93
bytes: The bytes object read from the stream.
94
94
"""
95
95
try :
96
- return data .read ()
96
+ return stream .read ()
97
97
finally :
98
- data .close ()
98
+ stream .close ()
99
99
100
100
101
101
class CSVDeserializer (BaseDeserializer ):
@@ -111,22 +111,22 @@ def __init__(self, encoding="utf-8"):
111
111
"""
112
112
self .encoding = encoding
113
113
114
- def deserialize (self , data , content_type ):
114
+ def deserialize (self , stream , content_type ):
115
115
"""Deserialize data from an inference endpoint into a list of lists.
116
116
117
117
Args:
118
- data (botocore.response.StreamingBody): Data to be deserialized.
118
+ stream (botocore.response.StreamingBody): Data to be deserialized.
119
119
content_type (str): The MIME type of the data.
120
120
121
121
Returns:
122
122
list: The data deserialized into a list of lists representing the
123
123
contents of a CSV file.
124
124
"""
125
125
try :
126
- decoded_string = data .read ().decode (self .encoding )
126
+ decoded_string = stream .read ().decode (self .encoding )
127
127
return list (csv .reader (decoded_string .splitlines ()))
128
128
finally :
129
- data .close ()
129
+ stream .close ()
130
130
131
131
132
132
class StreamDeserializer (BaseDeserializer ):
@@ -138,17 +138,17 @@ class StreamDeserializer(BaseDeserializer):
138
138
139
139
ACCEPT = "*/*"
140
140
141
- def deserialize (self , data , content_type ):
141
+ def deserialize (self , stream , content_type ):
142
142
"""Returns a stream of the response body and the MIME type of the data.
143
143
144
144
Args:
145
- data (object ): A stream of bytes.
145
+ stream (botocore.response.StreamingBody ): A stream of bytes.
146
146
content_type (str): The MIME type of the data.
147
147
148
148
Returns:
149
149
tuple: A two-tuple containing the stream and content-type.
150
150
"""
151
- return data , content_type
151
+ return stream , content_type
152
152
153
153
154
154
class NumpyDeserializer (BaseDeserializer ):
@@ -164,11 +164,11 @@ def __init__(self, dtype=None):
164
164
"""
165
165
self .dtype = dtype
166
166
167
- def deserialize (self , data , content_type ):
167
+ def deserialize (self , stream , content_type ):
168
168
"""Deserialize data from an inference endpoint into a NumPy array.
169
169
170
170
Args:
171
- data (botocore.response.StreamingBody): Data to be deserialized.
171
+ stream (botocore.response.StreamingBody): Data to be deserialized.
172
172
content_type (str): The MIME type of the data.
173
173
174
174
Returns:
@@ -177,14 +177,14 @@ def deserialize(self, data, content_type):
177
177
try :
178
178
if content_type == "text/csv" :
179
179
return np .genfromtxt (
180
- codecs .getreader ("utf-8" )(data ), delimiter = "," , dtype = self .dtype
180
+ codecs .getreader ("utf-8" )(stream ), delimiter = "," , dtype = self .dtype
181
181
)
182
182
if content_type == "application/json" :
183
- return np .array (json .load (codecs .getreader ("utf-8" )(data )), dtype = self .dtype )
183
+ return np .array (json .load (codecs .getreader ("utf-8" )(stream )), dtype = self .dtype )
184
184
if content_type == "application/x-npy" :
185
- return np .load (io .BytesIO (data .read ()))
185
+ return np .load (io .BytesIO (stream .read ()))
186
186
finally :
187
- data .close ()
187
+ stream .close ()
188
188
189
189
raise ValueError ("%s cannot read content type %s." % (__class__ .__name__ , content_type ))
190
190
@@ -194,17 +194,17 @@ class JSONDeserializer(BaseDeserializer):
194
194
195
195
ACCEPT = "application/json"
196
196
197
- def deserialize (self , data , content_type ):
197
+ def deserialize (self , stream , content_type ):
198
198
"""Deserialize JSON data from an inference endpoint into a Python object.
199
199
200
200
Args:
201
- data (botocore.response.StreamingBody): Data to be deserialized.
201
+ stream (botocore.response.StreamingBody): Data to be deserialized.
202
202
content_type (str): The MIME type of the data.
203
203
204
204
Returns:
205
205
object: The JSON-formatted data deserialized into a Python object.
206
206
"""
207
207
try :
208
- return json .load (codecs .getreader ("utf-8" )(data ))
208
+ return json .load (codecs .getreader ("utf-8" )(stream ))
209
209
finally :
210
- data .close ()
210
+ stream .close ()
0 commit comments