@@ -248,16 +248,33 @@ Sometimes it is useful to return something different than a JSON file. For such
248
248
cases, you can define an additional argument ``accept `` defining the content
249
249
types that you are able to return as follows::
250
250
251
- def get_predict_args():
251
+ def get_predict_args():
252
252
return {
253
- " accept" : fields.Str(
254
- description="Media type(s) that is/are acceptable for the response." ,
255
- validate=validate.OneOf(["text/plain"]),
253
+ ' accept' : fields.Str(description="Media type(s) that is/are acceptable for the response.",
254
+ missing='application/zip' ,
255
+ validate=validate.OneOf(['application/zip', 'image/png', 'application/json']))
256
256
}
257
257
258
- Consequently, the predict calls will receive an ``accept `` argument containing
259
- the content type requested by the user. Find `here <https://www.iana.org/assignments/media-types/media-types.xhtml >`_
260
- a comprehensive list of possible content types.
258
+ Find `here <https://www.iana.org/assignments/media-types/media-types.xhtml >`_ a comprehensive list of possible
259
+ content types. Then the predict function will have to return the raw bytes of a file according to the user selection.
260
+ For example::
261
+
262
+
263
+ def predict(**args):
264
+ # Run your prediction
265
+
266
+ # Return file according to user selection
267
+ if args['accept'] == 'image/png':
268
+ return open(img_path, 'rb')
269
+
270
+ elif args['accept'] == 'application/json':
271
+ return {'some': 'json'}
272
+
273
+ elif args['accept'] == 'application/zip':
274
+ return open(zip_path, 'rb')
275
+
276
+ If you want to return several content types at the same time (let's say a JSON and an image), the easiest way it to
277
+ return a zip file with all the files.
261
278
262
279
Using classes
263
280
-------------
0 commit comments