def test_add_predict_subparser(self): parser = argparse.ArgumentParser(description=u"Testing") subparsers = parser.add_subparsers(title=u'Commands', metavar=u'') Predict().add_subparser(u'predict', subparsers) kebab_args = [ u"predict", # command u"/path/to/archive", # archive u"/dev/null", # input_file u"--output-file", u"/dev/null", u"--batch-size", u"10", u"--cuda-device", u"0", u"--silent" ] args = parser.parse_args(kebab_args) assert args.func.__name__ == u'_predict' assert args.archive_file == u"/path/to/archive" assert args.output_file == u"/dev/null" assert args.batch_size == 10 assert args.cuda_device == 0 assert args.silent
def test_add_predict_subparser(self): parser = argparse.ArgumentParser(description="Testing") subparsers = parser.add_subparsers(title="Commands", metavar="") Predict().add_subparser("predict", subparsers) kebab_args = [ "predict", # command "/path/to/archive", # archive "/dev/null", # input_file "--output-file", "/dev/null", "--batch-size", "10", "--cuda-device", "0", "--silent", ] args = parser.parse_args(kebab_args) assert args.func.__name__ == "_predict" assert args.archive_file == "/path/to/archive" assert args.output_file == "/dev/null" assert args.batch_size == 10 assert args.cuda_device == 0 assert args.silent
def test_add_predict_subparser(self): parser = argparse.ArgumentParser(description="Testing") subparsers = parser.add_subparsers(title='Commands', metavar='') Predict(DEFAULT_PREDICTORS).add_subparser('predict', subparsers) snake_args = ["predict", # command "/path/to/archive", # archive "/dev/null", # input_file "--output-file", "/dev/null", # this one was always kebab-case "--batch_size", "10", "--cuda_device", "0", "--silent"] kebab_args = ["predict", # command "/path/to/archive", # archive "/dev/null", # input_file "--output-file", "/dev/null", "--batch-size", "10", "--cuda-device", "0", "--silent"] for raw_args in [snake_args, kebab_args]: args = parser.parse_args(raw_args) assert args.func.__name__ == 'predict_inner' assert args.archive_file == "/path/to/archive" assert args.output_file.name == "/dev/null" assert args.batch_size == 10 assert args.cuda_device == 0 assert args.silent
def main(prog: str = None, subcommand_overrides: Dict[str, Subcommand] = {}) -> None: """ The :mod:`~allennlp.run` command only knows about the registered classes in the ``allennlp`` codebase. In particular, once you start creating your own ``Model`` s and so forth, it won't work for them, unless you use the ``--include-package`` flag. """ parser = ArgumentParserWithDefaults(description="Run AllenNLP", usage="%(prog)s", prog=prog) parser.add_argument("--version", action="version", version="%(prog)s " + __version__) subparsers = parser.add_subparsers(title="Commands", metavar="") subcommands = { # Default commands "configure": Configure(), "train": Train(), "evaluate": Evaluate(), "predict": Predict(), "make-vocab": MakeVocab(), "elmo": Elmo(), "fine-tune": FineTune(), "dry-run": DryRun(), "test-install": TestInstall(), "find-lr": FindLearningRate(), "print-results": PrintResults(), # Superseded by overrides **subcommand_overrides, } for name, subcommand in subcommands.items(): subparser = subcommand.add_subparser(name, subparsers) # configure doesn't need include-package because it imports # whatever classes it needs. if name != "configure": subparser.add_argument( "--include-package", type=str, action="append", default=[], help="additional packages to include", ) args = parser.parse_args() # If a subparser is triggered, it adds its work as `args.func`. # So if no such attribute has been added, no subparser was triggered, # so give the user some help. if "func" in dir(args): # Import any additional modules needed (to register custom classes). for package_name in getattr(args, "include_package", ()): import_submodules(package_name) args.func(args) else: parser.print_help()
def main(prog: str = None, model_overrides: Dict[str, DemoModel] = {}, predictor_overrides: Dict[str, str] = {}, subcommand_overrides: Dict[str, Subcommand] = {}) -> None: """ The :mod:`~allennlp.run` command only knows about the registered classes in the ``allennlp`` codebase. In particular, once you start creating your own ``Model`` s and so forth, it won't work for them, unless you use the ``--include-package`` flag available for most commands. """ # pylint: disable=dangerous-default-value # TODO(mattg): document and/or remove the `predictor_overrides` and `model_overrides` commands. # The `--predictor` option for the `predict` command largely removes the need for # `predictor_overrides`, and I think the simple server largely removes the need for # `model_overrides`, and maybe the whole `serve` command as a public API (we only need that # path for demo.allennlp.org, and it's not likely anyone else would host that particular demo). parser = argparse.ArgumentParser(description="Run AllenNLP", usage='%(prog)s', prog=prog) subparsers = parser.add_subparsers(title='Commands', metavar='') subcommands = { # Default commands "train": Train(), "evaluate": Evaluate(), "predict": Predict(predictor_overrides), "serve": Serve(model_overrides), "make-vocab": MakeVocab(), "elmo": Elmo(), "fine-tune": FineTune(), # Superseded by overrides **subcommand_overrides } for name, subcommand in subcommands.items(): subparser = subcommand.add_subparser(name, subparsers) subparser.add_argument('--include-package', type=str, action='append', default=[], help='additional packages to include') args = parser.parse_args() # Import any additional modules needed (to register custom classes). for package_name in args.include_package: import_submodules(package_name) # If a subparser is triggered, it adds its work as `args.func`. # So if no such attribute has been added, no subparser was triggered, # so give the user some help. if 'func' in dir(args): args.func(args) else: parser.print_help()
def main(prog: str = None, subcommand_overrides: Dict[str, Subcommand] = {}) -> None: """ The :mod:`~allennlp.run` command only knows about the registered classes in the ``allennlp`` codebase. In particular, once you start creating your own ``Model`` s and so forth, it won't work for them, unless you use the ``--include-package`` flag. """ # pylint: disable=dangerous-default-value parser = argparse.ArgumentParser(description="Run AllenNLP", usage='%(prog)s', prog=prog) parser.add_argument('--version', action='version', version='%(prog)s ' + __version__) subparsers = parser.add_subparsers(title='Commands', metavar='') subcommands = { # Default commands "configure": Configure(), "train": Train(), "train_multitask": TrainMultiTask(), # includes the auxillary citation section prediction task "train_multitask_2": TrainMultiTask2(), # includes both tasks "evaluate": Evaluate(), "predict": Predict(), "make-vocab": MakeVocab(), "elmo": Elmo(), "fine-tune": FineTune(), "dry-run": DryRun(), "test-install": TestInstall(), # Superseded by overrides **subcommand_overrides } for name, subcommand in subcommands.items(): subparser = subcommand.add_subparser(name, subparsers) # configure doesn't need include-package because it imports # whatever classes it needs. if name != "configure": subparser.add_argument('--include-package', type=str, action='append', default=[], help='additional packages to include') args = parser.parse_args() # If a subparser is triggered, it adds its work as `args.func`. # So if no such attribute has been added, no subparser was triggered, # so give the user some help. if 'func' in dir(args): # Import any additional modules needed (to register custom classes). for package_name in getattr(args, 'include_package', ()): try: import_submodules(package_name) except TypeError: import pdb; pdb.set_trace() args.func(args) else: parser.print_help()
def main(prog: str = None, model_overrides: Dict[str, DemoModel] = {}, predictor_overrides: Dict[str, str] = {}, subcommand_overrides: Dict[str, Subcommand] = {}) -> None: """ The :mod:`~allennlp.run` command only knows about the registered classes in the ``allennlp`` codebase. In particular, once you start creating your own ``Model`` s and so forth, it won't work for them. However, ``allennlp.run`` is simply a wrapper around this function. To use the command line interface with your own custom classes, just create your own script that imports all of the classes you want and then calls ``main()``. The default models for ``serve`` and the default predictors for ``predict`` are defined above. If you'd like to add more or use different ones, the ``model_overrides`` and ``predictor_overrides`` arguments will take precedence over the defaults. """ # pylint: disable=dangerous-default-value parser = argparse.ArgumentParser(description="Run AllenNLP", usage='%(prog)s [command]', prog=prog) subparsers = parser.add_subparsers(title='Commands', metavar='') subcommands = { # Default commands "train": Train(), "evaluate": Evaluate(), "predict": Predict(predictor_overrides), "serve": Serve(model_overrides), "make-vocab": MakeVocab(), "elmo": Elmo(), # Superseded by overrides **subcommand_overrides } for name, subcommand in subcommands.items(): subcommand.add_subparser(name, subparsers) # Check and warn for deprecated args. for arg in sys.argv[1:]: if arg in DEPRECATED_FLAGS: logger.warning("Argument name %s is deprecated (and will likely go away at some point), " "please use %s instead", arg, DEPRECATED_FLAGS[arg]) args = parser.parse_args() # If a subparser is triggered, it adds its work as `args.func`. # So if no such attribute has been added, no subparser was triggered, # so give the user some help. if 'func' in dir(args): args.func(args) else: parser.print_help()
def create_parser( prog: str = None, subcommand_overrides: Dict[str, Subcommand] = None) -> argparse.ArgumentParser: """ Creates the argument parser for the main program. """ if subcommand_overrides is None: subcommand_overrides = {} parser = ArgumentParserWithDefaults(description="Run AllenNLP", usage="%(prog)s", prog=prog) parser.add_argument("--version", action="version", version="%(prog)s " + __version__) subparsers = parser.add_subparsers(title="Commands", metavar="") subcommands = { # Default commands "train": Train(), "evaluate": Evaluate(), "predict": Predict(), "elmo": Elmo(), "fine-tune": FineTune(), "dry-run": DryRun(), "make-vocab": DryRun(), # deprecated, but keeping for backward compatibility. "test-install": TestInstall(), "find-lr": FindLearningRate(), "print-results": PrintResults(), # Superseded by overrides **subcommand_overrides, } for name, subcommand in subcommands.items(): subparser = subcommand.add_subparser(name, subparsers) # configure doesn't need include-package because it imports # whatever classes it needs. if name != "configure": subparser.add_argument( "--include-package", type=str, action="append", default=[], help="additional packages to include", ) return parser
def main(prog=None, subcommand_overrides={}): u""" The :mod:`~allennlp.run` command only knows about the registered classes in the ``allennlp`` codebase. In particular, once you start creating your own ``Model`` s and so forth, it won't work for them, unless you use the ``--include-package`` flag. """ # pylint: disable=dangerous-default-value parser = argparse.ArgumentParser(description=u"Run AllenNLP", usage=u'%(prog)s', prog=prog) parser.add_argument(u'--version', action=u'version', version=u'%(prog)s ') subparsers = parser.add_subparsers(title=u'Commands', metavar=u'') subcommands = { # Default commands u"configure": Configure(), u"train": Train(), u"evaluate": Evaluate(), u"predict": Predict(), u"make-vocab": MakeVocab(), u"elmo": Elmo(), u"fine-tune": FineTune(), u"dry-run": DryRun(), u"test-install": TestInstall(), } for name, subcommand in list(subcommands.items()): subparser = subcommand.add_subparser(name, subparsers) # configure doesn't need include-package because it imports # whatever classes it needs. if name != u"configure": subparser.add_argument(u'--include-package', type=unicode, action=u'append', default=[], help=u'additional packages to include') args = parser.parse_args() # If a subparser is triggered, it adds its work as `args.func`. # So if no such attribute has been added, no subparser was triggered, # so give the user some help. if u'func' in dir(args): # Import any additional modules needed (to register custom classes). for package_name in getattr(args, u'include_package', ()): import_submodules(package_name) args.func(args) else: parser.print_help()
def main(prog: str = None, subcommand_overrides: Dict[str, Subcommand] = {}) -> None: """ The :mod:`~allennlp.run` command only knows about the registered classes in the ``allennlp`` codebase. In particular, once you start creating your own ``Model`` s and so forth, it won't work for them, unless you use the ``--include-package`` flag. """ # pylint: disable=dangerous-default-value parser = argparse.ArgumentParser(description="Run AllenNLP", usage='%(prog)s', prog=prog) subparsers = parser.add_subparsers(title='Commands', metavar='') subcommands = { # Default commands "train": Train(), "evaluate": Evaluate(), "predict": Predict(), "make-vocab": MakeVocab(), "elmo": Elmo(), "fine-tune": FineTune(), "dry-run": DryRun(), # Superseded by overrides **subcommand_overrides } for name, subcommand in subcommands.items(): subparser = subcommand.add_subparser(name, subparsers) subparser.add_argument('--include-package', type=str, action='append', default=[], help='additional packages to include') args = parser.parse_args() # If a subparser is triggered, it adds its work as `args.func`. # So if no such attribute has been added, no subparser was triggered, # so give the user some help. if 'func' in dir(args): # Import any additional modules needed (to register custom classes). for package_name in args.include_package: import_submodules(package_name) # Run a command args.func(args) else: parser.print_help()
def main(prog: str = None, model_overrides: Dict[str, str] = {}, predictor_overrides: Dict[str, str] = {}, subcommand_overrides: Dict[str, Subcommand] = {}) -> None: """ The :mod:`~allennlp.run` command only knows about the registered classes in the ``allennlp`` codebase. In particular, once you start creating your own ``Model`` s and so forth, it won't work for them. However, ``allennlp.run`` is simply a wrapper around this function. To use the command line interface with your own custom classes, just create your own script that imports all of the classes you want and then calls ``main()``. The default models for ``serve`` and the default predictors for ``predict`` are defined above. If you'd like to add more or use different ones, the ``model_overrides`` and ``predictor_overrides`` arguments will take precedence over the defaults. """ # pylint: disable=dangerous-default-value parser = argparse.ArgumentParser(description="Run AllenNLP", usage='%(prog)s [command]', prog=prog) subparsers = parser.add_subparsers(title='Commands', metavar='') trained_models = {**DEFAULT_MODELS, **model_overrides} predictors = {**DEFAULT_PREDICTORS, **predictor_overrides} subcommands = { # Default commands "train": Train(), "evaluate": Evaluate(), "predict": Predict(predictors), "serve": Serve(trained_models), # Superseded by overrides **subcommand_overrides } for name, subcommand in subcommands.items(): subcommand.add_subparser(name, subparsers) args = parser.parse_args() # If a subparser is triggered, it adds its work as `args.func`. # So if no such attribute has been added, no subparser was triggered, # so give the user some help. if 'func' in dir(args): args.func(args) else: parser.print_help()
def test_add_predict_subparser(self): parser = argparse.ArgumentParser(description="Testing") subparsers = parser.add_subparsers(title='Commands', metavar='') Predict(DEFAULT_PREDICTORS).add_subparser('predict', subparsers) raw_args = ["predict", # command "/path/to/archive", # archive "/dev/null", # input_file "--output-file", "/dev/null", "--silent"] args = parser.parse_args(raw_args) assert args.func.__name__ == 'predict_inner' assert args.archive_file == "/path/to/archive" assert args.silent
subparser.add_argument("--beam-size", type=int, default=1) subparser.add_argument("--line-limit", type=int, default=None) subparser.set_defaults(func=_predict) return subparser if __name__ == "__main__": parser = ArgumentParserWithDefaults(description="Run AllenNLP") subparsers = parser.add_subparsers(title='Commands', metavar='') subcommands = { # Default commands "predict": Predict(), } for name, subcommand in subcommands.items(): subparser = subcommand.add_subparser(name, subparsers) # configure doesn't need include-package because it imports # whatever classes it needs. if name != "configure": subparser.add_argument('--include-package', type=str, action='append', default=[], help='additional packages to include') args = parser.parse_args() if 'func' in dir(args):