How to use the bentoml.exceptions.BadInput function in bentoml

To help you get started, we’ve selected a few bentoml examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github bentoml / BentoML / tests / handlers / test_dataframe_handler.py View on Github external
test_content = '[{"name": "john","game": "mario","city": "sf"}]'

    def test_func(df):
        return df["name"][0]

    handler = DataframeHandler()
    success_event_obj = {
        "headers": {"Content-Type": "application/json"},
        "body": test_content,
    }
    success_response = handler.handle_aws_lambda_event(success_event_obj, test_func)

    assert success_response["statusCode"] == 200
    assert success_response["body"] == '"john"'

    with pytest.raises(BadInput):
        error_event_obj = {
            "headers": {"Content-Type": "this_will_fail"},
            "body": test_content,
        }
        handler.handle_aws_lambda_event(error_event_obj, test_func)
github bentoml / BentoML / bentoml / handlers / dataframe_handler.py View on Github external
if os.path.isfile(cli_input) or is_s3_url(cli_input) or is_url(cli_input):
            if cli_input.endswith(".csv"):
                df = pd.read_csv(cli_input)
            elif cli_input.endswith(".json"):
                df = pd.read_json(cli_input, orient=orient, typ=self.typ, dtype=False)
            else:
                raise BadInput(
                    "Input file format not supported, BentoML cli only accepts .json "
                    "and .csv file"
                )
        else:
            # Assuming input string is JSON format
            try:
                df = pd.read_json(cli_input, orient=orient, typ=self.typ, dtype=False)
            except ValueError as e:
                raise BadInput(
                    "Unexpected input format, BentoML DataframeHandler expects json "
                    "string as input: {}".format(e)
                )

        if self.typ == "frame" and self.input_dtypes is not None:
            check_dataframe_column_contains(self.input_dtypes, df)

        result = func(df)
        result = get_output_str(result, parsed_args.output, output_orient)
        print(result)
github bentoml / BentoML / bentoml / handlers / image_handler.py View on Github external
def verify_image_format_or_raise(file_name, accept_format_list):
    """
    Raise error if file's extension is not in the accept_format_list
    """
    if accept_format_list:
        _, extension = os.path.splitext(file_name)
        if extension.lower() not in accept_format_list:
            raise BadInput(
                "Input file not in supported format list: {}".format(accept_format_list)
            )
github bentoml / BentoML / bentoml / handlers / dataframe_handler.py View on Github external
)
        parser.add_argument("--orient", default=self.orient)
        parser.add_argument("--output_orient", default=self.output_orient)
        parsed_args = parser.parse_args(args)

        orient = parsed_args.orient
        output_orient = parsed_args.output_orient
        cli_input = parsed_args.input

        if os.path.isfile(cli_input) or is_s3_url(cli_input) or is_url(cli_input):
            if cli_input.endswith(".csv"):
                df = pd.read_csv(cli_input)
            elif cli_input.endswith(".json"):
                df = pd.read_json(cli_input, orient=orient, typ=self.typ, dtype=False)
            else:
                raise BadInput(
                    "Input file format not supported, BentoML cli only accepts .json "
                    "and .csv file"
                )
        else:
            # Assuming input string is JSON format
            try:
                df = pd.read_json(cli_input, orient=orient, typ=self.typ, dtype=False)
            except ValueError as e:
                raise BadInput(
                    "Unexpected input format, BentoML DataframeHandler expects json "
                    "string as input: {}".format(e)
                )

        if self.typ == "frame" and self.input_dtypes is not None:
            check_dataframe_column_contains(self.input_dtypes, df)
github bentoml / BentoML / bentoml / handlers / json_handler.py View on Github external
def handle_aws_lambda_event(self, event, func):
        if event["headers"]["Content-Type"] == "application/json":
            parsed_json = json.loads(event["body"])
        else:
            raise BadInput(
                "Request content-type must be 'application/json' for this "
                "BentoService API lambda endpoint"
            )

        result = func(parsed_json)
        result = get_output_str(result, event["headers"].get("output", "json"))
        return {"statusCode": 200, "body": result}
github bentoml / BentoML / bentoml / handlers / image_handler.py View on Github external
request.files.get(form_input_name)
                for form_input_name in self.input_names
                if form_input_name in request.files
            ]

        if input_files:
            file_names = [secure_filename(file.filename) for file in input_files]
            for file_name in file_names:
                verify_image_format_or_raise(file_name, self.accept_image_formats)
            input_streams = [BytesIO(input_file.read()) for input_file in input_files]
        else:
            data = request.get_data()
            if data:
                input_streams = (data,)
            else:
                raise BadInput("BentoML#ImageHandler unexpected HTTP request format")

        input_data = tuple(
            self.imread(input_stream, pilmode=self.pilmode)
            for input_stream in input_streams
        )
        result = func(*input_data)
        result = get_output_str(result, request.headers.get("output", "json"))
        return Response(response=result, status=200, mimetype="application/json")
github bentoml / BentoML / bentoml / handlers / fastai_image_handler.py View on Github external
def handle_request(self, request, func):
        input_streams = []
        for filename in self.input_names:
            file = request.files.get(filename)
            if file is not None:
                file_name = secure_filename(file.filename)
                verify_image_format_or_raise(file_name, self.accept_image_formats)
                input_streams.append(BytesIO(file.read()))

        if len(input_streams) == 0:
            data = request.get_data()
            if data:
                input_streams = (data,)
            else:
                raise BadInput(
                    "BentoML#ImageHandler unexpected HTTP request: %s" % request
                )

        input_data = []
        for input_stream in input_streams:
            data = self.imread(input_stream, pilmode=self.convert_mode)

            if self.after_open:
                data = self.after_open(data)

            data = self.fastai_vision.pil2tensor(data, np.float32)

            if self.div:
                data = data.div_(255)

            if self.cls:
github bentoml / BentoML / bentoml / handlers / dataframe_handler.py View on Github external
def check_dataframe_column_contains(required_column_names, df):
    df_columns = set(map(str, df.columns))
    for col in required_column_names:
        if col not in df_columns:
            raise BadInput(
                "Missing columns: {}, required_column:{}".format(
                    ",".join(set(required_column_names) - df_columns), df_columns
                )