diff --git a/paddle_hub/commands/__init__.py b/paddle_hub/commands/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..4778ecfb071d47fa18ada1b5f90e8f468a1acff1 --- /dev/null +++ b/paddle_hub/commands/__init__.py @@ -0,0 +1,15 @@ +# Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License" +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from . import * diff --git a/paddle_hub/commands/base_command.py b/paddle_hub/commands/base_command.py new file mode 100644 index 0000000000000000000000000000000000000000..149d3fe704cdd3102f06d0aee1749792c77df02d --- /dev/null +++ b/paddle_hub/commands/base_command.py @@ -0,0 +1,49 @@ +# Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License" +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function +from paddle_hub.tools.arg_helper import add_argument, print_arguments +from paddle_hub.tools.logger import logger +import argparse + + +class BaseCommand: + @classmethod + def instance(cls): + if not hasattr(cls, '_instance'): + cls._instance = cls() + return cls._instance + + def __init__(self): + assert not hasattr( + self.__class__, + '_instance'), 'Please use `instance()` to get Command object!' + self.parser = argparse.ArgumentParser(description=__doc__) + self.args = None + + def add_arg(self, argument, type="str", default=None, help=None): + add_argument( + argument=argument, + type=type, + default=default, + help=help, + argparser=self.parser) + + def print_args(self): + print_arguments(self.args) + + def exec(self, argv): + raise NotImplementedError("Base Command should not be execute!") diff --git a/paddle_hub/commands/download.py b/paddle_hub/commands/download.py new file mode 100644 index 0000000000000000000000000000000000000000..b4e3c369c507742319b4bc931b3974f1eb274a63 --- /dev/null +++ b/paddle_hub/commands/download.py @@ -0,0 +1,33 @@ +# Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License" +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function +from paddle_hub.tools.logger import logger +from paddle_hub.commands.base_command import BaseCommand + + +class DownloadCommand(BaseCommand): + def __init__(self): + super(DownloadCommand, self).__init__() + + def help(self): + pass + + def exec(self, argv): + pass + + +command = DownloadCommand.instance() diff --git a/paddle_hub/commands/help.py b/paddle_hub/commands/help.py new file mode 100644 index 0000000000000000000000000000000000000000..53c592407f687a18652baca7dc5206d5aae2f3a5 --- /dev/null +++ b/paddle_hub/commands/help.py @@ -0,0 +1,33 @@ +# Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License" +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function +from paddle_hub.tools.logger import logger +from paddle_hub.commands.base_command import BaseCommand + + +class HelpCommand(BaseCommand): + def __init__(self): + super(HelpCommand, self).__init__() + + def help(self): + pass + + def exec(self, argv): + pass + + +command = HelpCommand.instance() diff --git a/paddle_hub/commands/hub.py b/paddle_hub/commands/hub.py new file mode 100644 index 0000000000000000000000000000000000000000..76a215813e98c2a51d5f6e28caa6eddc465e974e --- /dev/null +++ b/paddle_hub/commands/hub.py @@ -0,0 +1,56 @@ +# Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License" +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function +from paddle_hub.tools.logger import logger +from paddle_hub.commands.base_command import BaseCommand +from paddle_hub.commands import show +from paddle_hub.commands import help +from paddle_hub.commands import version +from paddle_hub.commands import run +from paddle_hub.commands import download +import sys + + +class HubCommand(BaseCommand): + def __init__(self): + super(HubCommand, self).__init__() + # yapf: disable + self.add_arg('command', str, None, "command to run" ) + # yapf: enable + + def exec(self, argv): + args = self.parser.parse_args(argv[1:2]) + + command_dict = { + 'show': show.command.exec, + 'run': run.command.exec, + 'download': download.command.exec, + 'help': help.command.exec, + 'version': version.command.exec + } + + if not args.command in command_dict: + logger.critical("command %s not supported!" % args.command) + exit(1) + + command = command_dict[args.command] + command(argv[2:]) + + +if __name__ == "__main__": + hub_command = HubCommand.instance() + hub_command.exec(sys.argv) diff --git a/paddle_hub/commands/run.py b/paddle_hub/commands/run.py new file mode 100644 index 0000000000000000000000000000000000000000..ec4866b72769ceae7e4dccf0a98bd7a38ef52b5b --- /dev/null +++ b/paddle_hub/commands/run.py @@ -0,0 +1,102 @@ +# Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License" +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function +from paddle_hub.tools.logger import logger +from paddle_hub.commands.base_command import BaseCommand +import paddle_hub as hub +from paddle_hub.data.reader import csv_reader, yaml_reader +from paddle_hub.tools import utils +from paddle_hub.tools.arg_helper import add_argument, print_arguments + + +class RunCommand(BaseCommand): + def __init__(self): + super(RunCommand, self).__init__() + # yapf: disable + self.add_arg('--config', str, None, "config file in yaml format" ) + self.add_arg('--dataset', str, None, "dataset be used" ) + self.add_arg('--module', str, None, "module to run" ) + self.add_arg('--signature', str, None, "signature to run" ) + # yapf: enable + + def _check_module(self): + if not self.args.module: + logger.critical("lack of module") + self.help() + exit(1) + + def _check_dataset(self): + if not self.args.dataset: + logger.critical("lack of dataset file") + self.help() + exit(1) + if not utils.is_csv_file(self.args.dataset): + logger.critical("dataset file should in csv format") + self.help() + exit(1) + + def _check_config(self): + if not self.args.config: + logger.critical("lack of config file") + self.help() + exit(1) + if not utils.is_yaml_file(self.args.config): + logger.critical("config file should in yaml format") + self.help() + exit(1) + + def help(self): + self.parser.print_help() + + def exec(self, argv): + self.args = self.parser.parse_args(argv) + self.print_args() + + self._check_module() + self._check_dataset() + self._check_config() + + module = hub.Module(module_dir=self.args.module) + yaml_config = yaml_reader.read(self.args.config) + + if not self.args.signature: + self.args.signature = module.default_signature().name + + # data_format check + expect_data_format = module.processor.data_format(self.args.signature) + input_data_format = yaml_config['input_data'] + assert len(input_data_format) == len(expect_data_format) + for key, value in expect_data_format.items(): + assert key in input_data_format + assert value == hub.DataType.type(input_data_format[key]['type']) + + # get data dict + origin_data = csv_reader.read(self.args.dataset) + input_data = {} + for key, value in yaml_config['input_data'].items(): + type_reader = hub.DataType.type_reader(value['type']) + input_data[key] = list( + map(type_reader.read, origin_data[value['key']])) + + # run module with data + module( + sign_name=self.args.signature, + data=input_data, + config=yaml_config['config']) + + +command = RunCommand.instance() diff --git a/paddle_hub/commands/show.py b/paddle_hub/commands/show.py new file mode 100644 index 0000000000000000000000000000000000000000..1d3b823362ac9ed10bc4c37f3de44b178ce140e8 --- /dev/null +++ b/paddle_hub/commands/show.py @@ -0,0 +1,33 @@ +# Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License" +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function +from paddle_hub.tools.logger import logger +from paddle_hub.commands.base_command import BaseCommand + + +class ShowCommand(BaseCommand): + def __init__(self): + super(ShowCommand, self).__init__() + + def help(self): + pass + + def exec(self, argv): + pass + + +command = ShowCommand.instance() diff --git a/paddle_hub/commands/version.py b/paddle_hub/commands/version.py new file mode 100644 index 0000000000000000000000000000000000000000..6b670f9c27929040fe5e0f19d85e9b6e18874ece --- /dev/null +++ b/paddle_hub/commands/version.py @@ -0,0 +1,33 @@ +# Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License" +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function +from paddle_hub.tools.logger import logger +from paddle_hub.commands.base_command import BaseCommand + + +class VersionCommand(BaseCommand): + def __init__(self): + super(VersionCommand, self).__init__() + + def help(self): + pass + + def exec(self, argv): + pass + + +command = VersionCommand.instance()