提交 90bca453 编写于 作者: W wuzewu

add command line script

上级 fc3deb58
# 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 *
# 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!")
# 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()
# 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()
# 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)
# 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()
# 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()
# 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()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册