allennlp-demo icon indicating copy to clipboard operation
allennlp-demo copied to clipboard

Include packages for new models

Open ahoho opened this issue 6 years ago • 2 comments

Right now, if a user has developed a task as a library (running it with allennlp train --include-package my_library ...), it will fail when its archive is included as a new model in the demo because its classes won't have been imported. For instance, a registered DatasetReader subclass referenced in config.json will cause a ConfigurationError to be thrown when running python app.py.

I think this can be avoided by adding something like a --include-package-path argument to app.py and then appending the directory to the system path before calling utils.import_submodules. I'm happy to submit a PR if this would be useful.

ahoho avatar Oct 30 '19 19:10 ahoho

hi,I also have a import problem,can you give me some details of how to solve it?Thank you!

vivian-stars avatar Dec 04 '19 06:12 vivian-stars

In app.py, you can add the following lines, then load your package by specifying a path to the folder containing the package with --include-package (the path need not be absolute). It's untested, but I may submit this as a PR along with some tests later in the month.

import pathlib

...

from allennlp.common.util import JsonDict, peak_memory_mb, import_submodules

...

if __name__ == "__main__":
   
    ...
    
    parser.add_argument(
        '--include-package',
        type=str,
        help='Load this package by specifying a path, e.g. ../path/package-name'
    )

    ...
    
    if args.include_package:
        package_path = pathlib.Path(args.include_package)
        sys.path.append(package_path.parent.as_posix())
        import_submodules(package_path.name)

ahoho avatar Dec 05 '19 01:12 ahoho