2017-03-18 15:01:16 +03:00
|
|
|
# coding: utf8
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2017-05-22 13:28:58 +03:00
|
|
|
import plac
|
2017-03-18 15:01:16 +03:00
|
|
|
import platform
|
|
|
|
from pathlib import Path
|
2019-11-04 04:38:45 +03:00
|
|
|
from wasabi import msg
|
💫 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-18 20:57:45 +03:00
|
|
|
|
2018-12-01 06:55:48 +03:00
|
|
|
from ..compat import path2str, basestring_, unicode_
|
2017-03-18 17:14:48 +03:00
|
|
|
from .. import util
|
2018-04-03 16:50:31 +03:00
|
|
|
from .. import about
|
2017-03-18 15:01:16 +03:00
|
|
|
|
|
|
|
|
2017-05-22 13:28:58 +03:00
|
|
|
@plac.annotations(
|
2018-11-30 22:16:14 +03:00
|
|
|
model=("Optional shortcut link of model", "positional", None, str),
|
|
|
|
markdown=("Generate Markdown for GitHub issues", "flag", "md", str),
|
|
|
|
silent=("Don't print anything (just return)", "flag", "s"),
|
|
|
|
)
|
2018-04-29 02:59:44 +03:00
|
|
|
def info(model=None, markdown=False, silent=False):
|
2018-11-30 22:16:14 +03:00
|
|
|
"""
|
|
|
|
Print info about spaCy installation. If a model shortcut link is
|
2017-05-22 13:28:58 +03:00
|
|
|
speficied as an argument, print model information. Flag --markdown
|
|
|
|
prints details in Markdown for easy copy-pasting to GitHub issues.
|
|
|
|
"""
|
2017-03-18 15:01:16 +03:00
|
|
|
if model:
|
2017-05-28 01:22:00 +03:00
|
|
|
if util.is_package(model):
|
|
|
|
model_path = util.get_package_path(model)
|
|
|
|
else:
|
|
|
|
model_path = util.get_data_path() / model
|
2018-11-30 22:16:14 +03:00
|
|
|
meta_path = model_path / "meta.json"
|
2017-05-28 01:22:00 +03:00
|
|
|
if not meta_path.is_file():
|
2018-12-08 13:49:43 +03:00
|
|
|
msg.fail("Can't find model meta.json", meta_path, exits=1)
|
💫 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)
|
2017-03-18 15:01:16 +03:00
|
|
|
if model_path.resolve() != model_path:
|
2018-11-30 22:16:14 +03:00
|
|
|
meta["link"] = path2str(model_path)
|
|
|
|
meta["source"] = path2str(model_path.resolve())
|
2017-03-18 15:01:16 +03:00
|
|
|
else:
|
2018-11-30 22:16:14 +03:00
|
|
|
meta["source"] = path2str(model_path)
|
2018-04-29 02:59:44 +03:00
|
|
|
if not silent:
|
2018-11-30 22:16:14 +03:00
|
|
|
title = "Info about model '{}'".format(model)
|
|
|
|
model_meta = {
|
|
|
|
k: v for k, v in meta.items() if k not in ("accuracy", "speed")
|
|
|
|
}
|
|
|
|
if markdown:
|
2018-12-01 06:55:48 +03:00
|
|
|
print_markdown(model_meta, title=title)
|
2018-11-30 22:16:14 +03:00
|
|
|
else:
|
|
|
|
msg.table(model_meta, title=title)
|
2018-04-29 02:59:44 +03:00
|
|
|
return meta
|
2018-11-30 22:16:14 +03:00
|
|
|
data = {
|
|
|
|
"spaCy version": about.__version__,
|
|
|
|
"Location": path2str(Path(__file__).parent.parent),
|
|
|
|
"Platform": platform.platform(),
|
|
|
|
"Python version": platform.python_version(),
|
|
|
|
"Models": list_models(),
|
|
|
|
}
|
2018-04-29 02:59:44 +03:00
|
|
|
if not silent:
|
2018-11-30 22:16:14 +03:00
|
|
|
title = "Info about spaCy"
|
|
|
|
if markdown:
|
2018-12-01 06:55:48 +03:00
|
|
|
print_markdown(data, title=title)
|
2018-11-30 22:16:14 +03:00
|
|
|
else:
|
|
|
|
msg.table(data, title=title)
|
2018-04-29 02:59:44 +03:00
|
|
|
return data
|
2017-03-18 15:01:16 +03:00
|
|
|
|
|
|
|
|
|
|
|
def list_models():
|
2017-05-08 00:25:29 +03:00
|
|
|
def exclude_dir(dir_name):
|
|
|
|
# exclude common cache directories and hidden directories
|
2018-11-30 22:16:14 +03:00
|
|
|
exclude = ("cache", "pycache", "__pycache__")
|
|
|
|
return dir_name in exclude or dir_name.startswith(".")
|
|
|
|
|
2017-03-18 15:01:16 +03:00
|
|
|
data_path = util.get_data_path()
|
2017-04-14 17:48:02 +03:00
|
|
|
if data_path:
|
|
|
|
models = [f.parts[-1] for f in data_path.iterdir() if f.is_dir()]
|
2018-11-30 22:16:14 +03:00
|
|
|
return ", ".join([m for m in models if not exclude_dir(m)])
|
|
|
|
return "-"
|
2018-12-01 06:55:48 +03:00
|
|
|
|
|
|
|
|
|
|
|
def print_markdown(data, title=None):
|
|
|
|
"""Print data in GitHub-flavoured Markdown format for issues etc.
|
|
|
|
|
|
|
|
data (dict or list of tuples): Label/value pairs.
|
|
|
|
title (unicode or None): Title, will be rendered as headline 2.
|
|
|
|
"""
|
2018-12-01 06:59:12 +03:00
|
|
|
markdown = []
|
|
|
|
for key, value in data.items():
|
|
|
|
if isinstance(value, basestring_) and Path(value).exists():
|
|
|
|
continue
|
|
|
|
markdown.append("* **{}:** {}".format(key, unicode_(value)))
|
2018-12-01 06:55:48 +03:00
|
|
|
if title:
|
|
|
|
print("\n## {}".format(title))
|
|
|
|
print("\n{}\n".format("\n".join(markdown)))
|