@@ -38,11 +38,11 @@ class BaseDeserializer(abc.ABC):
38
38
"""
39
39
40
40
@abc .abstractmethod
41
- def deserialize (self , data , content_type ):
41
+ def deserialize (self , stream , content_type ):
42
42
"""Deserialize data received from an inference endpoint.
43
43
44
44
Args:
45
- data (object ): Data to be deserialized.
45
+ stream (botocore.response.StreamingBody ): Data to be deserialized.
46
46
content_type (str): The MIME type of the data.
47
47
48
48
Returns:
@@ -68,41 +68,41 @@ def __init__(self, encoding="UTF-8"):
68
68
"""
69
69
self .encoding = encoding
70
70
71
- def deserialize (self , data , content_type ):
71
+ def deserialize (self , stream , content_type ):
72
72
"""Deserialize data from an inference endpoint into a decoded string.
73
73
74
74
Args:
75
- data (object ): Data to be deserialized.
75
+ stream (botocore.response.StreamingBody ): Data to be deserialized.
76
76
content_type (str): The MIME type of the data.
77
77
78
78
Returns:
79
79
str: The data deserialized into a decoded string.
80
80
"""
81
81
try :
82
- return data .read ().decode (self .encoding )
82
+ return stream .read ().decode (self .encoding )
83
83
finally :
84
- data .close ()
84
+ stream .close ()
85
85
86
86
87
87
class BytesDeserializer (BaseDeserializer ):
88
88
"""Deserialize a stream of bytes into a bytes object."""
89
89
90
90
ACCEPT = "*/*"
91
91
92
- def deserialize (self , data , content_type ):
92
+ def deserialize (self , stream , content_type ):
93
93
"""Read a stream of bytes returned from an inference endpoint.
94
94
95
95
Args:
96
- data (object ): A stream of bytes.
96
+ stream (botocore.response.StreamingBody ): A stream of bytes.
97
97
content_type (str): The MIME type of the data.
98
98
99
99
Returns:
100
100
bytes: The bytes object read from the stream.
101
101
"""
102
102
try :
103
- return data .read ()
103
+ return stream .read ()
104
104
finally :
105
- data .close ()
105
+ stream .close ()
106
106
107
107
108
108
class CSVDeserializer (BaseDeserializer ):
@@ -118,22 +118,22 @@ def __init__(self, encoding="utf-8"):
118
118
"""
119
119
self .encoding = encoding
120
120
121
- def deserialize (self , data , content_type ):
121
+ def deserialize (self , stream , content_type ):
122
122
"""Deserialize data from an inference endpoint into a list of lists.
123
123
124
124
Args:
125
- data (botocore.response.StreamingBody): Data to be deserialized.
125
+ stream (botocore.response.StreamingBody): Data to be deserialized.
126
126
content_type (str): The MIME type of the data.
127
127
128
128
Returns:
129
129
list: The data deserialized into a list of lists representing the
130
130
contents of a CSV file.
131
131
"""
132
132
try :
133
- decoded_string = data .read ().decode (self .encoding )
133
+ decoded_string = stream .read ().decode (self .encoding )
134
134
return list (csv .reader (decoded_string .splitlines ()))
135
135
finally :
136
- data .close ()
136
+ stream .close ()
137
137
138
138
139
139
class StreamDeserializer (BaseDeserializer ):
@@ -145,17 +145,17 @@ class StreamDeserializer(BaseDeserializer):
145
145
146
146
ACCEPT = "*/*"
147
147
148
- def deserialize (self , data , content_type ):
148
+ def deserialize (self , stream , content_type ):
149
149
"""Returns a stream of the response body and the MIME type of the data.
150
150
151
151
Args:
152
- data (object ): A stream of bytes.
152
+ stream (botocore.response.StreamingBody ): A stream of bytes.
153
153
content_type (str): The MIME type of the data.
154
154
155
155
Returns:
156
156
tuple: A two-tuple containing the stream and content-type.
157
157
"""
158
- return data , content_type
158
+ return stream , content_type
159
159
160
160
161
161
class NumpyDeserializer (BaseDeserializer ):
@@ -171,11 +171,11 @@ def __init__(self, dtype=None):
171
171
"""
172
172
self .dtype = dtype
173
173
174
- def deserialize (self , data , content_type ):
174
+ def deserialize (self , stream , content_type ):
175
175
"""Deserialize data from an inference endpoint into a NumPy array.
176
176
177
177
Args:
178
- data (botocore.response.StreamingBody): Data to be deserialized.
178
+ stream (botocore.response.StreamingBody): Data to be deserialized.
179
179
content_type (str): The MIME type of the data.
180
180
181
181
Returns:
@@ -184,14 +184,14 @@ def deserialize(self, data, content_type):
184
184
try :
185
185
if content_type == "text/csv" :
186
186
return np .genfromtxt (
187
- codecs .getreader ("utf-8" )(data ), delimiter = "," , dtype = self .dtype
187
+ codecs .getreader ("utf-8" )(stream ), delimiter = "," , dtype = self .dtype
188
188
)
189
189
if content_type == "application/json" :
190
- return np .array (json .load (codecs .getreader ("utf-8" )(data )), dtype = self .dtype )
190
+ return np .array (json .load (codecs .getreader ("utf-8" )(stream )), dtype = self .dtype )
191
191
if content_type == "application/x-npy" :
192
- return np .load (io .BytesIO (data .read ()))
192
+ return np .load (io .BytesIO (stream .read ()))
193
193
finally :
194
- data .close ()
194
+ stream .close ()
195
195
196
196
raise ValueError ("%s cannot read content type %s." % (__class__ .__name__ , content_type ))
197
197
@@ -201,45 +201,45 @@ class JSONDeserializer(BaseDeserializer):
201
201
202
202
ACCEPT = "application/json"
203
203
204
- def deserialize (self , data , content_type ):
204
+ def deserialize (self , stream , content_type ):
205
205
"""Deserialize JSON data from an inference endpoint into a Python object.
206
206
207
207
Args:
208
- data (botocore.response.StreamingBody): Data to be deserialized.
208
+ stream (botocore.response.StreamingBody): Data to be deserialized.
209
209
content_type (str): The MIME type of the data.
210
210
211
211
Returns:
212
212
object: The JSON-formatted data deserialized into a Python object.
213
213
"""
214
214
try :
215
- return json .load (codecs .getreader ("utf-8" )(data ))
215
+ return json .load (codecs .getreader ("utf-8" )(stream ))
216
216
finally :
217
- data .close ()
217
+ stream .close ()
218
218
219
219
220
220
class PandasDeserializer (BaseDeserializer ):
221
221
"""Deserialize CSV or JSON data from an inference endpoint into a pandas dataframe."""
222
222
223
223
ACCEPT = "text/csv"
224
224
225
- def deserialize (self , data , content_type ):
225
+ def deserialize (self , stream , content_type ):
226
226
"""Deserialize CSV or JSON data from an inference endpoint into a pandas
227
227
dataframe.
228
228
229
229
If the data is JSON, the data should be formatted in the 'columns' orient.
230
230
See https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_json.html
231
231
232
232
Args:
233
- data (botocore.response.StreamingBody): Data to be deserialized.
233
+ stream (botocore.response.StreamingBody): Data to be deserialized.
234
234
content_type (str): The MIME type of the data.
235
235
236
236
Returns:
237
237
pandas.DataFrame: The data deserialized into a pandas DataFrame.
238
238
"""
239
239
if content_type == "text/csv" :
240
- return pandas .read_csv (data )
240
+ return pandas .read_csv (stream )
241
241
242
242
if content_type == "application/json" :
243
- return pandas .read_json (data )
243
+ return pandas .read_json (stream )
244
244
245
245
raise ValueError ("%s cannot read content type %s." % (__class__ .__name__ , content_type ))
0 commit comments