def handle_aws_lambda_event(self, event, func): orient = event['headers'].get('orient', self.orient) output_orient = event['headers'].get('output_orient', self.output_orient) if event['headers']['Content-Type'] == 'application/json': df = pd.read_json(event['body'], orient=orient, typ=self.typ, dtype=False) elif event['headers']['Content-Type'] == 'text/csv': df = pd.read_csv(event['body']) else: return { "statusCode": 400, "body": "Request content-type not supported, only application/json and text/csv" " are supported" } if self.typ == 'frame' and self.input_columns is not None: check_dataframe_column_contains(self.input_columns, df) result = func(df) result = get_output_str(result, event['headers'].get('output', 'json'), output_orient) return {"statusCode": 200, "body": result}
def handle_request(self, request, func): orient = request.headers.get('orient', self.orient) output_orient = request.headers.get('output_orient', self.output_orient) if request.content_type == 'application/json': df = pd.read_json(request.data.decode('utf-8'), orient=orient, typ=self.typ, dtype=False) elif request.content_type == 'text/csv': df = pd.read_csv(request.data.decode('utf-8')) else: return make_response( jsonify(message="Request content-type not supported, " "only application/json and text/csv are " "supported"), 400) if self.typ == 'frame' and self.input_columns is not None: check_dataframe_column_contains(self.input_columns, df) result = func(df) result = get_output_str(result, request.headers.get('output', 'json'), output_orient) return Response(response=result, status=200, mimetype="application/json")
def handle_aws_lambda_event(self, event, func): if event["headers"].get("Content-Type", "").startswith("images/"): # decodebytes introduced at python3.1 try: image_data = self.imread(base64.decodebytes(event["body"]), pilmode=self.pilmode) except AttributeError: image_data = self.imread( base64.decodestring(event["body"]), # pylint: disable=W1505 pilmode=self.convert_mode, ) else: raise BentoMLException( "BentoML currently doesn't support Content-Type: {content_type} for " "AWS Lambda".format( content_type=event["headers"]["Content-Type"])) if self.after_open: image_data = self.after_open(image_data) image_data = self.fastai_vision.pil2tensor(image_data, np.float32) if self.div: image_data = image_data.div_(255) if self.cls: image_data = self.cls(image_data) else: image_data = self.fastai_vision.Image(image_data) result = func(image_data) result = get_output_str(result, event["headers"].get("output", "json")) return {"statusCode": 200, "body": result}
def handle_request(self, request, func): """Handle http request that has image file/s. It will convert image into a ndarray for the function to consume. Args: request: incoming request object. func: function that will take ndarray as its arg. options: configuration for handling request object. Return: response object """ if request.method != 'POST': return Response(response="Only accept POST request", status=400) if not self.accept_multiple_files: input_file = request.files[self.input_names[0]] file_name = secure_filename(input_file.filename) check_file_format(file_name, self.accept_file_extensions) input_data_string = input_file.read() input_data = np.fromstring(input_data_string, np.uint8) else: return Response(response="Only support single file input", status=400) result = func(input_data) result = get_output_str(result, request.headers.get('output', 'json')) return Response(response=result, status=200, mimetype="application/json")
def handle_request(self, request, func): """Handle http request that has image file/s. It will convert image into a ndarray for the function to consume. Args: request: incoming request object. func: function that will take ndarray as its arg. options: configuration for handling request object. Return: response object """ try: from imageio import imread except ImportError: raise ImportError("imageio package is required to use ImageHandler") if request.method != "POST": return Response(response="Only accept POST request", status=400) if not self.accept_multiple_files: input_file = request.files.get(self.input_names) file_name = secure_filename(input_file.filename) check_file_format(file_name, self.accept_file_extensions) input_stream = BytesIO() input_file.save(input_stream) input_data = imread(input_file, pilmode=self.pilmode) else: return Response(response="Only support single file input", status=400) result = func(input_data) result = get_output_str(result, request.headers.get("output", "json")) return Response(response=result, status=200, mimetype="application/json")
def handle_cli(self, args, func): parser = argparse.ArgumentParser() parser.add_argument("--input", required=True) parser.add_argument("-o", "--output", default="str", choices=["str", "json", "yaml"]) parsed_args = parser.parse_args(args) file_path = parsed_args.input check_file_format(file_path, self.accept_file_extensions) if not os.path.isabs(file_path): file_path = os.path.abspath(file_path) try: from fastai.vision import open_image, Image except ImportError: raise ImportError("fastai package is required to use") image_array = open_image( fn=file_path, convert_mode=self.convert_mode, div=self.div, after_open=self.after_open, cls=self.cls or Image, ) result = func(image_array) result = get_output_str(result, output_format=parsed_args.output) print(result)
def handle_cli(self, args, func): parser = argparse.ArgumentParser() parser.add_argument("--input", required=True) parser.add_argument("-o", "--output", default="str", choices=["str", "json", "yaml"]) parsed_args = parser.parse_args(args) file_path = parsed_args.input check_file_format(file_path, self.accept_file_extensions) if not os.path.isabs(file_path): file_path = os.path.abspath(file_path) try: from imageio import imread except ImportError: raise ImportError( "imageio package is required to use ImageHandler") image_array = imread(file_path, pilmode=self.pilmode) result = func(image_array) result = get_output_str(result, output_format=parsed_args.output) print(result)
def handle_aws_lambda_event(self, event, func): try: from imageio import imread except ImportError: raise ImportError( "imageio package is required to use ImageHandler") if event["headers"].get("Content-Type", None) in ACCEPTED_CONTENT_TYPES: # decodebytes introduced at python3.1 try: image = imread(base64.decodebytes(event["body"]), pilmode=self.pilmode) except AttributeError: image = imread( base64.decodestring(event["body"]), # pylint: disable=W1505 pilmode=self.pilmode, ) else: raise BentoMLException( "BentoML currently doesn't support Content-Type: {content_type} for " "AWS Lambda".format( content_type=event["headers"]["Content-Type"])) result = func(image) result = get_output_str(result, event["headers"].get("output", "json")) return {"statusCode": 200, "body": result}
def handle_aws_lambda_event(self, event, func): if event["headers"].get("Content-Type", "").startswith("images/"): image_data = self.imread( base64.decodebytes(event["body"]), pilmode=self.pilmode ) else: raise BadInput( "BentoML currently doesn't support Content-Type: {content_type} for " "AWS Lambda".format(content_type=event["headers"]["Content-Type"]) ) if self.after_open: image_data = self.after_open(image_data) image_data = self.fastai_vision.pil2tensor(image_data, np.float32) if self.div: image_data = image_data.div_(255) if self.cls: image_data = self.cls(image_data) else: image_data = self.fastai_vision.Image(image_data) result = func(image_data) result = get_output_str(result, event["headers"].get("output", "json")) return {"statusCode": 200, "body": result}
def handle_request(self, request, func): orient = request.headers.get("orient", self.orient) output_orient = request.headers.get("output_orient", self.output_orient) if request.content_type == "application/json": df = pd.read_json(request.data.decode("utf-8"), orient=orient, typ=self.typ, dtype=False) elif request.content_type == "text/csv": df = pd.read_csv(request.data.decode("utf-8")) else: return make_response( jsonify( message= "Request content-type not supported, only application/json " "and text/csv are supported"), 400, ) 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, request.headers.get("output", "json"), output_orient) return Response(response=result, status=200, mimetype="application/json")
def handle_cli(self, args, func): parser = argparse.ArgumentParser() parser.add_argument("--input", required=True) parser.add_argument("-o", "--output", default="str", choices=["str", "json", "yaml"]) parsed_args = parser.parse_args(args) file_path = parsed_args.input verify_image_format_or_raise(file_path, self.accept_image_formats) if not os.path.isabs(file_path): file_path = os.path.abspath(file_path) image_array = self.fastai_vision.open_image( fn=file_path, convert_mode=self.convert_mode, div=self.div, after_open=self.after_open, cls=self.cls or self.fastai_vision.Image, ) result = func(image_array) result = get_output_str(result, output_format=parsed_args.output) print(result)
def handle_aws_lambda_event(self, event, func): orient = event["headers"].get("orient", self.orient) output_orient = event["headers"].get("output_orient", self.output_orient) if event["headers"]["Content-Type"] == "application/json": df = pd.read_json(event["body"], orient=orient, typ=self.typ, dtype=False) elif event["headers"]["Content-Type"] == "text/csv": df = pd.read_csv(event["body"]) else: return { "statusCode": 400, "body": "Request content-type not supported, only application/json and " "text/csv are supported", } 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, event["headers"].get("output", "json"), output_orient) return {"statusCode": 200, "body": result}
def handle_aws_lambda_event(self, event, func): if event["headers"]["Content-Type"] == "application/json": parsed_json = json.loads(event["body"]) else: return {"statusCode": 400, "body": "Only accept json as content type"} result = func(parsed_json) result = get_output_str(result, event["headers"].get("output", "json")) return {"statusCode": 200, "body": result}
def handle_request(self, request, func): """Handle http request that has image file/s. It will convert image into a ndarray for the function to consume. Args: request: incoming request object. func: function that will take ndarray as its arg. options: configuration for handling request object. Return: response object """ if request.method != "POST": return Response( response="BentoML#ImageHandler only accept POST request", status=405) if len(self.input_names) == 1 and len(request.files) == 1: # Ignore multipart form input name when ImageHandler is intended to accept # only one image file at a time input_files = [file for _, file in iteritems(request.files)] else: input_files = [ 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 ] try: for file_name in file_names: verify_image_format_or_raise(file_name, self.accept_image_formats) except ValueError as e: return Response(response=str(e), status=400) input_streams = [ BytesIO(input_file.read()) for input_file in input_files ] else: data = request.get_data() if data: input_streams = (data, ) else: return Response( "BentoML#ImageHandler unexpected HTTP request format", status=400) 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")
def handle_request(self, request, func): """Handle http request that has image file/s. It will convert image into a ndarray for the function to consume. Args: request: incoming request object. func: function that will take ndarray as its arg. options: configuration for handling request object. Return: response object """ try: from imageio import imread except ImportError: raise ImportError("imageio package is required to use ImageHandler") if request.method != "POST": return Response(response="Only accept POST request", status=400) if not self.accept_multiple_files: input_file = request.files.get(self.input_name) if input_file: file_name = secure_filename(input_file.filename) check_file_format(file_name, self.accept_file_extensions) input_stream = BytesIO(input_file.read()) elif request.data: input_stream = request.data else: raise ValueError( "BentoML#ImageHandler unexpected HTTP request: %s" % request ) input_data = imread(input_stream, pilmode=self.pilmode) result = func(input_data) else: input_files = [request.files.get(filename) for filename in self.input_name] if input_files: file_names = [secure_filename(file.filename) for file in input_files] for file_name in file_names: check_file_format(file_name, self.accept_file_extensions) input_streams = [ BytesIO(input_file.read()) for input_file in input_files ] else: raise ValueError( "BentoML#ImageHandler unexpected HTTP request: %s" % request ) input_data = tuple( 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")
def handle_aws_lambda_event(self, event, func): if event['headers']['Content-Type'] == 'application/json': parsed_json = json.loads(event['body']) else: return {"statusCode": 400, "body": 'Only accept json as content type'} result = func(parsed_json) result = get_output_str(result, event['headers'].get('output', 'json')) return {"statusCode": 200, "body": result}
def handle_request(self, request, func): try: from fastai.vision import Image, pil2tensor except ImportError: raise ImportError( "fastai package is required to use FastaiImageHandler") try: from imageio import imread except ImportError: raise ImportError( "imageio package is required to use FastaiImageHandler") if request.method != "POST": return Response(response="Only accept POST request", status=400) input_streams = [] for filename in self.input_names: file = request.files.get(filename) if file is not None: file_name = secure_filename(file.filename) check_file_format(file_name, self.accept_file_extensions) input_streams.append(BytesIO(file.read())) if len(input_streams) == 0: if request.data: input_streams = (request.data, ) else: raise ValueError( "BentoML#ImageHandler unexpected HTTP request: %s" % request) input_data = [] for input_stream in input_streams: data = imread(input_stream, pilmode=self.convert_mode) if self.after_open: data = self.after_open(data) data = pil2tensor(data, np.float32) if self.div: data = data.div_(255) if self.cls: data = self.cls(data) else: data = Image(data) input_data.append(data) result = func(*input_data) result = get_output_str(result, request.headers.get("output", "json")) return Response(response=result, status=200, mimetype="application/json")
def handle_request(self, request, func): if request.content_type == 'application/json': parsed_json = json.loads(request.data.decode('utf-8')) else: return make_response( jsonify(message="Request content-type must be 'application/json'" "for this BentoService API"), 400) result = func(parsed_json) result = get_output_str(result, request.headers.get('output', 'json')) return Response(response=result, status=200, mimetype="application/json")
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}
def handle_aws_lambda_event(self, event, func): if event["headers"].get("Content-Type", "").startswith("images/"): image = self.imread(base64.decodebytes(event["body"]), pilmode=self.pilmode) else: raise BadInput( "BentoML currently doesn't support Content-Type: {content_type} for " "AWS Lambda".format(content_type=event["headers"]["Content-Type"]) ) result = func(image) result = get_output_str(result, event["headers"].get("output", "json")) return {"statusCode": 200, "body": result}
def handle_request(self, request, func): if request.content_type == "application/json": parsed_json = json.loads(request.data.decode("utf-8")) else: raise BadInput( "Request content-type must be 'application/json' for this " "BentoService API") result = func(parsed_json) result = get_output_str(result, request.headers.get("output", "json")) return Response(response=result, status=200, mimetype="application/json")
def handle_cli(self, args, func): parser = argparse.ArgumentParser() parser.add_argument('--input', required=True) parser.add_argument('-o', '--output', default="str", choices=['str', 'json', 'yaml']) parsed_args = parser.parse_args(args) if os.path.isfile(parsed_args.input): with open(parsed_args.input, 'r') as content_file: content = content_file.read() else: content = parsed_args.input input_json = json.loads(content) result = func(input_json) result = get_output_str(result, parsed_args.output) print(result)
def handle_cli(self, args, func): parser = argparse.ArgumentParser() parser.add_argument("--input", required=True) parser.add_argument("-o", "--output", default="str", choices=["str", "json", "yaml"]) 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 ValueError( "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 ValueError( "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)
def handle_request(self, request, func): if request.method != "POST": return Response(response="Only accept POST request", status=400) 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 ValueError( "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: data = self.cls(data) else: data = self.fastai_vision.Image(data) input_data.append(data) result = func(*input_data) result = get_output_str(result, request.headers.get("output", "json")) return Response(response=result, status=200, mimetype="application/json")
def handle_request(self, request, func): """Handle http request that has image file/s. It will convert image into a ndarray for the function to consume. Args: request: incoming request object. func: function that will take ndarray as its arg. options: configuration for handling request object. Return: response object """ if request.method != "POST": return Response(response="Only accept POST request", status=400) input_files = [ request.files.get(filename) for filename in self.input_names ] if len(input_files) == 1 and input_files[0] is None: data = request.get_data() if data: input_streams = (data, ) else: raise ValueError( "BentoML#ImageHandler unexpected HTTP request: %s" % request) else: 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 ] input_data = tuple( 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")
def handle_cli(self, args, func): parser = argparse.ArgumentParser() parser.add_argument("--input", required=True) parser.add_argument( "-o", "--output", default="str", choices=["str", "json", "yaml"] ) parsed_args = parser.parse_args(args) file_path = parsed_args.input verify_image_format_or_raise(file_path, self.accept_image_formats) if not os.path.isabs(file_path): file_path = os.path.abspath(file_path) image_array = self.imread(file_path, pilmode=self.pilmode) result = func(image_array) result = get_output_str(result, output_format=parsed_args.output) print(result)
def handle_aws_lambda_event(self, event, func): try: from imageio import imread except ImportError: raise ImportError( "imageio package is required to use ImageHandler") if event["headers"].get("Content-Type", None) in ACCEPTED_CONTENT_TYPES: # decodebytes introduced at python3.1 try: image_data = imread(base64.decodebytes(event["body"]), pilmode=self.pilmode) except AttributeError: image_data = imread( base64.decodestring(event["body"]), # pylint: disable=W1505 pilmode=self.convert_mode, ) else: raise BentoMLException( "BentoML currently doesn't support Content-Type: {content_type} for " "AWS Lambda".format( content_type=event["headers"]["Content-Type"])) try: from fastai.vision import pil2tensor, Image import numpy as np except ImportError: raise ImportError("fastai package is required") if self.after_open: image_data = self.after_open(image_data) image_data = pil2tensor(image_data, np.float32) if self.div: image_data = image_data.div_(255) if self.cls: image_data = self.cls(image_data) else: image_data = Image(image_data) result = func(image_data) result = get_output_str(result, event["headers"].get("output", "json")) return {"statusCode": 200, "body": result}
def handle_request(self, request, func): try: from fastai.vision import Image, pil2tensor import numpy as np except ImportError: raise ImportError( "fastai package is required to use FastaiImageHandler") try: from imageio import imread except ImportError: raise ImportError( "imageio package is required to use FastaiImageHandler") return if request.method != "POST": return Response(response="Only accept POST request", status=400) input_file = request.files.get(self.input_name) file_name = secure_filename(input_file.filename) check_file_format(file_name, self.accept_file_extensions) input_stream = BytesIO(input_file.read()) input_data = imread(input_stream, pilmode=self.convert_mode) if self.after_open: input_data = self.after_open(input_data) input_data = pil2tensor(input_data, np.float32) if self.div: input_data = input_data.div_(255) if self.cls: input_data = self.cls(input_data) else: input_data = Image(input_data) result = func(input_data) result = get_output_str(result, request.headers.get("output", "json")) return Response(response=result, status=200, mimetype="application/json")
def handle_aws_lambda_event(self, event, func): try: import cv2 except ImportError: raise ImportError( "opencv-python package is required to use ImageHandler") if event['headers'].get('Content-Type', None) in ACCEPTED_CONTENT_TYPES: nparr = np.fromstring(base64.b64decode(event['body']), np.uint8) image = cv2.imdecode(nparr, cv2.IMREAD_COLOR) else: raise BentoMLException( "BentoML currently doesn't support Content-Type: {content_type} for AWS Lambda" .format(content_type=event['headers']['Content-Type'])) result = func(image) result = get_output_str(result, event['headers'].get('output', 'json')) return {'statusCode': 200, 'body': result}
def handle_aws_lambda_event(self, event, func): if event["headers"].get("Content-Type", "").startswith("images/"): # decodebytes introduced at python3.1 try: image = imread(base64.decodebytes(event["body"]), pilmode=self.pilmode) except AttributeError: image = imread( base64.decodestring(event["body"]), # pylint: disable=W1505 pilmode=self.pilmode, ) else: raise BentoMLException( "BentoML currently doesn't support Content-Type: {content_type} for " "AWS Lambda".format( content_type=event["headers"]["Content-Type"])) result = func(image) result = get_output_str(result, event["headers"].get("output", "json")) return {"statusCode": 200, "body": result}