コード例 #1
0
    def handle_cli(self, args, func):
        parser = generate_cli_default_parser()
        parser.add_argument('--input_json_orient', default='records')
        parsed_args = parser.parse_args(args)

        # TODO: Add support for parsing cli argument string as input
        file_path = parsed_args.input

        if parsed_args.input_json_orient:
            self.input_json_orient = parsed_args.input_json_orient

        if file_path.endswith('.csv'):
            df = pd.read_csv(file_path)
        elif file_path.endswith('.json'):
            df = pd.read_json(file_path,
                              orient=self.input_json_orient,
                              dtype=False)
        else:
            raise ValueError(
                "BentoML DataframeHandler currently only supports '.csv'"
                "and '.json' files as cli input.")

        if self.input_columns_require:
            check_missing_columns(self.input_columns_require, df.columns)

        result = func(df)

        # TODO: revisit cli handler output format and options
        if isinstance(result, pd.DataFrame):
            print(result.to_json())
        elif isinstance(result, np.ndarray):
            print(json.dumps(result.tolist()))
        else:
            print(result)
コード例 #2
0
ファイル: dataframe_handler.py プロジェクト: yinan98/BentoML
    def handle_cli(args, func, options=None):
        parser = generate_cli_default_parser()
        parser.add_argument('--input_json_orient', default='records')
        parsed_args = parser.parse_args(args)
        options = merge_dicts(default_options, options)
        file_path = parsed_args.input

        if parsed_args.input_json_orient:
            options['input_json_orient'] = parsed_args.input_json_orient

        if file_path.endswith('.csv'):
            df = pd.read_csv(file_path)
        elif file_path.endswith('.json'):
            df = pd.read_json(file_path,
                              orient=options['input_json_orient'],
                              dtype=False)

        if options['input_columns_require']:
            check_missing_columns(options['input_columns_require'], df.columns)

        output = func(df)

        if parsed_args.output == 'json' or not parsed_args.output:
            if isinstance(output, pd.DataFrame):
                result = output.to_json(orient=options['output_json_orient'])
                result = json.loads(result)
                result = json.dumps(result, indent=2)
            else:
                result = json.dumps(output)
            sys.stdout.write(result)
        else:
            raise NotImplementedError
コード例 #3
0
ファイル: image_handler.py プロジェクト: linuxclab/BentoML
    def handle_cli(self, args, func):
        parser = generate_cli_default_parser()
        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:
            import cv2
        except ImportError:
            raise ImportError(
                "opencv-python package is required to use ImageHandler")

        image = cv2.imread(file_path)

        result = func(image)
        # TODO: revisit cli handler output format and options
        if isinstance(result, pd.DataFrame):
            print(result.to_json())
        elif isinstance(result, np.ndarray):
            print(json.dumps(result.tolist()))
        else:
            print(result)
コード例 #4
0
    def handle_cli(args, func, options=None):
        options = merge_dicts(default_options, options)
        parser = generate_cli_default_parser()
        parsed_args = parser.parse_args(args)

        with open(parsed_args.input, 'r') as content_file:
            content = content_file.read()
            input_json = json.loads(content)
            output = func(input_json)

            try:
                result = json.dumps(output)
            except Exception as e:  # pylint:disable=W0703
                if isinstance(e, TypeError):
                    if type(output).__module__ == 'numpy':
                        output = output.tolist()
                        result = json.dumps(output)
                    else:
                        raise e
                else:
                    raise e

            if parsed_args.output == 'json' or not parsed_args.output:
                try:
                    sys.stdout.write(result)
                except Exception as e:
                    raise e
            else:
                raise NotImplementedError
コード例 #5
0
    def handle_cli(self, args, func):
        parser = generate_cli_default_parser()
        parsed_args = parser.parse_args(args)

        with open(parsed_args.input, 'r') as content_file:
            content = content_file.read()

        input_json = json.loads(content)
        result = func(input_json)

        # TODO: revisit cli handler output format and options
        if isinstance(result, pd.DataFrame):
            print(result.to_json())
        elif isinstance(result, np.ndarray):
            print(json.dumps(result.tolist()))
        else:
            print(result)
コード例 #6
0
    def handle_cli(args, func, options=None):
        options = merge_dicts(default_options, options)
        parser = generate_cli_default_parser()
        parsed_args = parser.parse_args(args)
        file_path = parsed_args.input

        check_file_format(file_path, options['accept_file_extensions'])
        if not os.path.isabs(file_path):
            file_path = os.path.abspath(file_path)

        try:
            import cv2
        except ImportError:
            raise ImportError(
                "opencv-python package is required to use ImageHandler")

        image = cv2.imread(file_path)
        output = func(image)

        if options['output_format'] == 'json':
            result = json.dumps(output)
            sys.stdout.write(result)
        else:
            raise NotImplementedError