2017-03-21 00:50:13 +03:00
|
|
|
# coding: utf8
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2017-05-22 13:28:58 +03:00
|
|
|
import plac
|
2017-03-21 04:06:29 +03:00
|
|
|
import shutil
|
2017-03-21 00:50:13 +03:00
|
|
|
from pathlib import Path
|
2018-11-30 22:16:14 +03:00
|
|
|
from wasabi import Printer, get_raw_input
|
💫 Replace ujson, msgpack and dill/pickle/cloudpickle with srsly (#3003)
Remove hacks and wrappers, keep code in sync across our libraries and move spaCy a few steps closer to only depending on packages with binary wheels 🎉
See here: https://github.com/explosion/srsly
Serialization is hard, especially across Python versions and multiple platforms. After dealing with many subtle bugs over the years (encodings, locales, large files) our libraries like spaCy and Prodigy have steadily grown a number of utility functions to wrap the multiple serialization formats we need to support (especially json, msgpack and pickle). These wrapping functions ended up duplicated across our codebases, so we wanted to put them in one place.
At the same time, we noticed that having a lot of small dependencies was making maintainence harder, and making installation slower. To solve this, we've made srsly standalone, by including the component packages directly within it. This way we can provide all the serialization utilities we need in a single binary wheel.
srsly currently includes forks of the following packages:
ujson
msgpack
msgpack-numpy
cloudpickle
* WIP: replace json/ujson with srsly
* Replace ujson in examples
Use regular json instead of srsly to make code easier to read and follow
* Update requirements
* Fix imports
* Fix typos
* Replace msgpack with srsly
* Fix warning
2018-12-03 03:28:22 +03:00
|
|
|
import srsly
|
2017-03-21 00:50:13 +03:00
|
|
|
|
💫 Replace ujson, msgpack and dill/pickle/cloudpickle with srsly (#3003)
Remove hacks and wrappers, keep code in sync across our libraries and move spaCy a few steps closer to only depending on packages with binary wheels 🎉
See here: https://github.com/explosion/srsly
Serialization is hard, especially across Python versions and multiple platforms. After dealing with many subtle bugs over the years (encodings, locales, large files) our libraries like spaCy and Prodigy have steadily grown a number of utility functions to wrap the multiple serialization formats we need to support (especially json, msgpack and pickle). These wrapping functions ended up duplicated across our codebases, so we wanted to put them in one place.
At the same time, we noticed that having a lot of small dependencies was making maintainence harder, and making installation slower. To solve this, we've made srsly standalone, by including the component packages directly within it. This way we can provide all the serialization utilities we need in a single binary wheel.
srsly currently includes forks of the following packages:
ujson
msgpack
msgpack-numpy
cloudpickle
* WIP: replace json/ujson with srsly
* Replace ujson in examples
Use regular json instead of srsly to make code easier to read and follow
* Update requirements
* Fix imports
* Fix typos
* Replace msgpack with srsly
* Fix warning
2018-12-03 03:28:22 +03:00
|
|
|
from ..compat import path2str
|
2017-03-21 00:50:13 +03:00
|
|
|
from .. import util
|
2017-05-08 00:25:29 +03:00
|
|
|
from .. import about
|
2017-03-21 00:50:13 +03:00
|
|
|
|
|
|
|
|
2017-05-22 13:28:58 +03:00
|
|
|
@plac.annotations(
|
2018-11-30 22:16:14 +03:00
|
|
|
input_dir=("Directory with model data", "positional", None, str),
|
|
|
|
output_dir=("Output parent directory", "positional", None, str),
|
|
|
|
meta_path=("Path to meta.json", "option", "m", str),
|
|
|
|
create_meta=("Create meta.json, even if one exists", "flag", "c", bool),
|
|
|
|
force=("Force overwriting existing model in output directory", "flag", "f", bool),
|
|
|
|
)
|
|
|
|
def package(input_dir, output_dir, meta_path=None, create_meta=False, force=False):
|
2017-05-27 21:01:46 +03:00
|
|
|
"""
|
|
|
|
Generate Python package for model data, including meta and required
|
2017-05-22 13:28:58 +03:00
|
|
|
installation files. A new directory will be created in the specified
|
2018-11-30 22:16:14 +03:00
|
|
|
output directory, and model data will be copied over. If --create-meta is
|
|
|
|
set and a meta.json already exists in the output directory, the existing
|
|
|
|
values will be used as the defaults in the command-line prompt.
|
2017-05-22 13:28:58 +03:00
|
|
|
"""
|
2018-11-30 22:16:14 +03:00
|
|
|
msg = Printer()
|
2017-05-08 00:25:29 +03:00
|
|
|
input_path = util.ensure_path(input_dir)
|
|
|
|
output_path = util.ensure_path(output_dir)
|
2017-08-12 22:44:15 +03:00
|
|
|
meta_path = util.ensure_path(meta_path)
|
2017-05-08 00:25:29 +03:00
|
|
|
if not input_path or not input_path.exists():
|
2018-12-08 13:49:43 +03:00
|
|
|
msg.fail("Can't locate model data", input_path, exits=1)
|
2017-05-08 00:25:29 +03:00
|
|
|
if not output_path or not output_path.exists():
|
2018-12-08 13:49:43 +03:00
|
|
|
msg.fail("Output directory not found", output_path, exits=1)
|
2017-05-08 00:25:29 +03:00
|
|
|
if meta_path and not meta_path.exists():
|
2018-12-08 13:49:43 +03:00
|
|
|
msg.fail("Can't find model meta.json", meta_path, exits=1)
|
2017-03-21 00:50:13 +03:00
|
|
|
|
2018-11-30 22:16:14 +03:00
|
|
|
meta_path = meta_path or input_path / "meta.json"
|
2017-10-30 20:39:38 +03:00
|
|
|
if meta_path.is_file():
|
💫 Replace ujson, msgpack and dill/pickle/cloudpickle with srsly (#3003)
Remove hacks and wrappers, keep code in sync across our libraries and move spaCy a few steps closer to only depending on packages with binary wheels 🎉
See here: https://github.com/explosion/srsly
Serialization is hard, especially across Python versions and multiple platforms. After dealing with many subtle bugs over the years (encodings, locales, large files) our libraries like spaCy and Prodigy have steadily grown a number of utility functions to wrap the multiple serialization formats we need to support (especially json, msgpack and pickle). These wrapping functions ended up duplicated across our codebases, so we wanted to put them in one place.
At the same time, we noticed that having a lot of small dependencies was making maintainence harder, and making installation slower. To solve this, we've made srsly standalone, by including the component packages directly within it. This way we can provide all the serialization utilities we need in a single binary wheel.
srsly currently includes forks of the following packages:
ujson
msgpack
msgpack-numpy
cloudpickle
* WIP: replace json/ujson with srsly
* Replace ujson in examples
Use regular json instead of srsly to make code easier to read and follow
* Update requirements
* Fix imports
* Fix typos
* Replace msgpack with srsly
* Fix warning
2018-12-03 03:28:22 +03:00
|
|
|
meta = srsly.read_json(meta_path)
|
2018-11-30 22:16:14 +03:00
|
|
|
if not create_meta: # only print if user doesn't want to overwrite
|
2018-12-08 13:49:43 +03:00
|
|
|
msg.good("Loaded meta.json from file", meta_path)
|
2017-10-30 20:39:38 +03:00
|
|
|
else:
|
2018-11-30 22:16:14 +03:00
|
|
|
meta = generate_meta(input_dir, meta, msg)
|
|
|
|
for key in ("lang", "name", "version"):
|
|
|
|
if key not in meta or meta[key] == "":
|
2018-12-08 13:49:43 +03:00
|
|
|
msg.fail(
|
|
|
|
"No '{}' setting found in meta.json".format(key),
|
|
|
|
"This setting is required to build your package.",
|
|
|
|
exits=1,
|
|
|
|
)
|
2018-11-30 22:16:14 +03:00
|
|
|
model_name = meta["lang"] + "_" + meta["name"]
|
|
|
|
model_name_v = model_name + "-" + meta["version"]
|
2017-03-21 00:50:13 +03:00
|
|
|
main_path = output_path / model_name_v
|
|
|
|
package_path = main_path / model_name
|
|
|
|
|
2017-03-21 04:06:53 +03:00
|
|
|
if package_path.exists():
|
|
|
|
if force:
|
2017-05-08 00:25:29 +03:00
|
|
|
shutil.rmtree(path2str(package_path))
|
2017-03-21 04:06:53 +03:00
|
|
|
else:
|
2018-11-30 22:16:14 +03:00
|
|
|
msg.fail(
|
2018-12-08 13:49:43 +03:00
|
|
|
"Package directory already exists",
|
|
|
|
"Please delete the directory and try again, or use the "
|
|
|
|
"`--force` flag to overwrite existing "
|
|
|
|
"directories.".format(path=path2str(package_path)),
|
2018-11-30 22:16:14 +03:00
|
|
|
exits=1,
|
|
|
|
)
|
2017-03-21 04:06:53 +03:00
|
|
|
Path.mkdir(package_path, parents=True)
|
2018-11-30 22:16:14 +03:00
|
|
|
shutil.copytree(path2str(input_path), path2str(package_path / model_name_v))
|
💫 Replace ujson, msgpack and dill/pickle/cloudpickle with srsly (#3003)
Remove hacks and wrappers, keep code in sync across our libraries and move spaCy a few steps closer to only depending on packages with binary wheels 🎉
See here: https://github.com/explosion/srsly
Serialization is hard, especially across Python versions and multiple platforms. After dealing with many subtle bugs over the years (encodings, locales, large files) our libraries like spaCy and Prodigy have steadily grown a number of utility functions to wrap the multiple serialization formats we need to support (especially json, msgpack and pickle). These wrapping functions ended up duplicated across our codebases, so we wanted to put them in one place.
At the same time, we noticed that having a lot of small dependencies was making maintainence harder, and making installation slower. To solve this, we've made srsly standalone, by including the component packages directly within it. This way we can provide all the serialization utilities we need in a single binary wheel.
srsly currently includes forks of the following packages:
ujson
msgpack
msgpack-numpy
cloudpickle
* WIP: replace json/ujson with srsly
* Replace ujson in examples
Use regular json instead of srsly to make code easier to read and follow
* Update requirements
* Fix imports
* Fix typos
* Replace msgpack with srsly
* Fix warning
2018-12-03 03:28:22 +03:00
|
|
|
create_file(main_path / "meta.json", srsly.json_dumps(meta))
|
2018-11-30 22:16:14 +03:00
|
|
|
create_file(main_path / "setup.py", TEMPLATE_SETUP)
|
|
|
|
create_file(main_path / "MANIFEST.in", TEMPLATE_MANIFEST)
|
|
|
|
create_file(package_path / "__init__.py", TEMPLATE_INIT)
|
2018-12-08 13:49:43 +03:00
|
|
|
msg.good("Successfully created package '{}'".format(model_name_v), main_path)
|
|
|
|
msg.text("To build the package, run `python setup.py sdist` in this directory.")
|
2017-03-21 04:06:53 +03:00
|
|
|
|
|
|
|
|
2017-03-21 00:50:13 +03:00
|
|
|
def create_file(file_path, contents):
|
|
|
|
file_path.touch()
|
2018-11-30 22:16:14 +03:00
|
|
|
file_path.open("w", encoding="utf-8").write(contents)
|
2017-03-21 00:50:13 +03:00
|
|
|
|
|
|
|
|
2018-11-30 22:16:14 +03:00
|
|
|
def generate_meta(model_path, existing_meta, msg):
|
2017-10-30 20:39:38 +03:00
|
|
|
meta = existing_meta or {}
|
2018-11-30 22:16:14 +03:00
|
|
|
settings = [
|
|
|
|
("lang", "Model language", meta.get("lang", "en")),
|
|
|
|
("name", "Model name", meta.get("name", "model")),
|
|
|
|
("version", "Model version", meta.get("version", "0.0.0")),
|
|
|
|
("spacy_version", "Required spaCy version", ">=%s,<3.0.0" % about.__version__),
|
|
|
|
("description", "Model description", meta.get("description", False)),
|
|
|
|
("author", "Author", meta.get("author", False)),
|
|
|
|
("email", "Author email", meta.get("email", False)),
|
|
|
|
("url", "Author website", meta.get("url", False)),
|
|
|
|
("license", "License", meta.get("license", "CC BY-SA 3.0")),
|
|
|
|
]
|
2017-10-25 17:03:26 +03:00
|
|
|
nlp = util.load_model_from_path(Path(model_path))
|
2018-11-30 22:16:14 +03:00
|
|
|
meta["pipeline"] = nlp.pipe_names
|
|
|
|
meta["vectors"] = {
|
|
|
|
"width": nlp.vocab.vectors_length,
|
|
|
|
"vectors": len(nlp.vocab.vectors),
|
|
|
|
"keys": nlp.vocab.vectors.n_keys,
|
|
|
|
}
|
2018-12-08 13:49:43 +03:00
|
|
|
msg.divider("Generating meta.json")
|
|
|
|
msg.text(
|
|
|
|
"Enter the package settings for your model. The following information "
|
|
|
|
"will be read from your model data: pipeline, vectors."
|
|
|
|
)
|
2017-03-21 00:50:13 +03:00
|
|
|
for setting, desc, default in settings:
|
2018-11-30 22:16:14 +03:00
|
|
|
response = get_raw_input(desc, default)
|
|
|
|
meta[setting] = default if response == "" and default else response
|
|
|
|
if about.__title__ != "spacy":
|
|
|
|
meta["parent_package"] = about.__title__
|
2017-05-27 21:02:01 +03:00
|
|
|
return meta
|
2017-04-16 14:13:17 +03:00
|
|
|
|
|
|
|
|
2017-11-07 14:15:35 +03:00
|
|
|
TEMPLATE_SETUP = """
|
|
|
|
#!/usr/bin/env python
|
|
|
|
# coding: utf8
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
import io
|
|
|
|
import json
|
|
|
|
from os import path, walk
|
|
|
|
from shutil import copy
|
|
|
|
from setuptools import setup
|
|
|
|
|
|
|
|
|
|
|
|
def load_meta(fp):
|
|
|
|
with io.open(fp, encoding='utf8') as f:
|
|
|
|
return json.load(f)
|
|
|
|
|
|
|
|
|
|
|
|
def list_files(data_dir):
|
|
|
|
output = []
|
|
|
|
for root, _, filenames in walk(data_dir):
|
|
|
|
for filename in filenames:
|
|
|
|
if not filename.startswith('.'):
|
|
|
|
output.append(path.join(root, filename))
|
|
|
|
output = [path.relpath(p, path.dirname(data_dir)) for p in output]
|
|
|
|
output.append('meta.json')
|
|
|
|
return output
|
|
|
|
|
|
|
|
|
|
|
|
def list_requirements(meta):
|
|
|
|
parent_package = meta.get('parent_package', 'spacy')
|
2018-05-22 21:50:46 +03:00
|
|
|
requirements = [parent_package + meta['spacy_version']]
|
2017-11-07 14:15:35 +03:00
|
|
|
if 'setup_requires' in meta:
|
|
|
|
requirements += meta['setup_requires']
|
|
|
|
return requirements
|
|
|
|
|
|
|
|
|
|
|
|
def setup_package():
|
|
|
|
root = path.abspath(path.dirname(__file__))
|
|
|
|
meta_path = path.join(root, 'meta.json')
|
|
|
|
meta = load_meta(meta_path)
|
|
|
|
model_name = str(meta['lang'] + '_' + meta['name'])
|
|
|
|
model_dir = path.join(model_name, model_name + '-' + meta['version'])
|
|
|
|
|
|
|
|
copy(meta_path, path.join(model_name))
|
|
|
|
copy(meta_path, model_dir)
|
|
|
|
|
|
|
|
setup(
|
|
|
|
name=model_name,
|
|
|
|
description=meta['description'],
|
|
|
|
author=meta['author'],
|
|
|
|
author_email=meta['email'],
|
|
|
|
url=meta['url'],
|
|
|
|
version=meta['version'],
|
|
|
|
license=meta['license'],
|
|
|
|
packages=[model_name],
|
|
|
|
package_data={model_name: list_files(model_dir)},
|
|
|
|
install_requires=list_requirements(meta),
|
|
|
|
zip_safe=False,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
setup_package()
|
|
|
|
""".strip()
|
|
|
|
|
|
|
|
|
|
|
|
TEMPLATE_MANIFEST = """
|
|
|
|
include meta.json
|
|
|
|
""".strip()
|
|
|
|
|
|
|
|
|
|
|
|
TEMPLATE_INIT = """
|
|
|
|
# coding: utf8
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
from pathlib import Path
|
|
|
|
from spacy.util import load_model_from_init_py, get_model_meta
|
|
|
|
|
|
|
|
|
|
|
|
__version__ = get_model_meta(Path(__file__).parent)['version']
|
|
|
|
|
|
|
|
|
|
|
|
def load(**overrides):
|
|
|
|
return load_model_from_init_py(__file__, **overrides)
|
|
|
|
""".strip()
|