Esempio n. 1
0
 def _detect_format(cls, task: InferenceTask) -> str:
     if task.http_headers.content_type == "application/json":
         return "json"
     if task.http_headers.content_type == "text/csv":
         return "csv"
     file_name = getattr(task.data, "name", "")
     if check_file_extension(file_name, (".csv",)):
         return "csv"
     return "json"
Esempio n. 2
0
    def extract_user_func_args(
            self, tasks: Iterable[InferenceTask[BinaryIO]]) -> ApiFuncArgs:
        img_list = []
        for task in tasks:
            if getattr(task.data, "name", None) and not check_file_extension(
                    task.data.name, self.accept_image_formats):
                task.discard(
                    http_status=400,
                    err_msg=f"Current service only accepts "
                    f"{self.accept_image_formats} formats",
                )
                continue
            try:
                img_array = imageio.imread(task.data, pilmode=self.pilmode)
                img_list.append(img_array)
            except ValueError as e:
                task.discard(http_status=400, err_msg=str(e))

        return (img_list, )
Esempio n. 3
0
 def extract_user_func_args(self,
                            tasks: Iterable[AnnoImgTask]) -> ApiFuncArgs:
     image_arrays = []
     json_objs = []
     for task in tasks:
         try:
             image_file, json_file = task.data
             assert image_file is not None
             assert getattr(image_file, "name", None)
             assert check_file_extension(image_file.name,
                                         self.accept_image_formats)
             image_array = imageio.imread(image_file, pilmode=self.pilmode)
             image_arrays.append(image_array)
             if json_file is not None:
                 json_objs.append(json.load(json_file))
             else:
                 json_objs.append(None)
         except AssertionError:
             task.discard(
                 http_status=400,
                 err_msg=f"BentoML#{self.__class__.__name__} "
                 f"Input image file must be in supported format list: "
                 f"{self.accept_image_formats}",
             )
         except UnicodeDecodeError:
             task.discard(
                 http_status=400,
                 err_msg="JSON must be in unicode",
             )
         except json.JSONDecodeError:
             task.discard(
                 http_status=400,
                 err_msg=f"BentoML#{self.__class__.__name__} "
                 f"received invalid JSON file",
             )
         except Exception:  # pylint: disable=broad-except
             err = traceback.format_exc()
             task.discard(
                 http_status=500,
                 err_msg=f"BentoML#{self.__class__.__name__} "
                 f"Internal Server Error: {err}",
             )
     return tuple(image_arrays), tuple(json_objs)
 def _extract(self, tasks):
     for task in tasks:
         if not all(f is not None for f in task.data):
             task.discard(
                 http_status=400,
                 err_msg=f"BentoML#{self.__class__.__name__} Empty request",
             )
             continue
         try:
             assert all(
                 not getattr(f, "name", None)
                 or check_file_extension(f.name, self.accept_image_formats)
                 for f in task.data)
             image_array_tuple = tuple(
                 fastai.vision.open_image(
                     fn=f,
                     convert_mode=self.convert_mode,
                     div=self.div,
                     after_open=self.after_open,
                     cls=self.cls or fastai.vision.Image,
                 ) for f in task.data)
             yield image_array_tuple
         except ValueError:
             task.discard(
                 http_status=400,
                 err_msg=f"BentoML#{self.__class__.__name__} "
                 f"Input image decode failed, it must be in supported format list: "
                 f"{self.accept_image_formats}",
             )
         except AssertionError:
             task.discard(
                 http_status=400,
                 err_msg=f"BentoML#{self.__class__.__name__} "
                 f"Input image file must be in supported format list: "
                 f"{self.accept_image_formats}",
             )
         except Exception:  # pylint: disable=broad-except
             err = traceback.format_exc()
             task.discard(
                 http_status=500,
                 err_msg=f"BentoML#{self.__class__.__name__} "
                 f"Internal Server Error: {err}",
             )