2017-03-18 12:01:16 +00:00
|
|
|
# coding: utf8
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2017-05-22 10:28:58 +00:00
|
|
|
import plac
|
2017-03-18 12:01:16 +00:00
|
|
|
import platform
|
|
|
|
from pathlib import Path
|
2018-11-30 19:16:14 +00:00
|
|
|
from wasabi import Printer
|
💫 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 00:28:22 +00:00
|
|
|
import srsly
|
2017-03-18 17:57:45 +00:00
|
|
|
|
2018-12-01 03:55:48 +00:00
|
|
|
from ..compat import path2str, basestring_, unicode_
|
2017-03-18 14:14:48 +00:00
|
|
|
from .. import util
|
2018-04-03 13:50:31 +00:00
|
|
|
from .. import about
|
2017-03-18 12:01:16 +00:00
|
|
|
|
|
|
|
|
2017-05-22 10:28:58 +00:00
|
|
|
@plac.annotations(
|
2018-11-30 19:16:14 +00: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-28 23:59:44 +00:00
|
|
|
def info(model=None, markdown=False, silent=False):
|
2018-11-30 19:16:14 +00:00
|
|
|
"""
|
|
|
|
Print info about spaCy installation. If a model shortcut link is
|
2017-05-22 10:28:58 +00:00
|
|
|
speficied as an argument, print model information. Flag --markdown
|
|
|
|
prints details in Markdown for easy copy-pasting to GitHub issues.
|
|
|
|
"""
|
2018-11-30 19:16:14 +00:00
|
|
|
msg = Printer()
|
2017-03-18 12:01:16 +00:00
|
|
|
if model:
|
2017-05-27 22:22:00 +00:00
|
|
|
if util.is_package(model):
|
|
|
|
model_path = util.get_package_path(model)
|
|
|
|
else:
|
|
|
|
model_path = util.get_data_path() / model
|
2018-11-30 19:16:14 +00:00
|
|
|
meta_path = model_path / "meta.json"
|
2017-05-27 22:22:00 +00:00
|
|
|
if not meta_path.is_file():
|
2018-12-08 10:49:43 +00: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 00:28:22 +00:00
|
|
|
meta = srsly.read_json(meta_path)
|
2017-03-18 12:01:16 +00:00
|
|
|
if model_path.resolve() != model_path:
|
2018-11-30 19:16:14 +00:00
|
|
|
meta["link"] = path2str(model_path)
|
|
|
|
meta["source"] = path2str(model_path.resolve())
|
2017-03-18 12:01:16 +00:00
|
|
|
else:
|
2018-11-30 19:16:14 +00:00
|
|
|
meta["source"] = path2str(model_path)
|
2018-04-28 23:59:44 +00:00
|
|
|
if not silent:
|
2018-11-30 19:16:14 +00: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 03:55:48 +00:00
|
|
|
print_markdown(model_meta, title=title)
|
2018-11-30 19:16:14 +00:00
|
|
|
else:
|
|
|
|
msg.table(model_meta, title=title)
|
2018-04-28 23:59:44 +00:00
|
|
|
return meta
|
2018-11-30 19:16:14 +00: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-28 23:59:44 +00:00
|
|
|
if not silent:
|
2018-11-30 19:16:14 +00:00
|
|
|
title = "Info about spaCy"
|
|
|
|
if markdown:
|
2018-12-01 03:55:48 +00:00
|
|
|
print_markdown(data, title=title)
|
2018-11-30 19:16:14 +00:00
|
|
|
else:
|
|
|
|
msg.table(data, title=title)
|
2018-04-28 23:59:44 +00:00
|
|
|
return data
|
2017-03-18 12:01:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
def list_models():
|
2017-05-07 21:25:29 +00:00
|
|
|
def exclude_dir(dir_name):
|
|
|
|
# exclude common cache directories and hidden directories
|
2018-11-30 19:16:14 +00:00
|
|
|
exclude = ("cache", "pycache", "__pycache__")
|
|
|
|
return dir_name in exclude or dir_name.startswith(".")
|
|
|
|
|
2017-03-18 12:01:16 +00:00
|
|
|
data_path = util.get_data_path()
|
2017-04-14 14:48:02 +00:00
|
|
|
if data_path:
|
|
|
|
models = [f.parts[-1] for f in data_path.iterdir() if f.is_dir()]
|
2018-11-30 19:16:14 +00:00
|
|
|
return ", ".join([m for m in models if not exclude_dir(m)])
|
|
|
|
return "-"
|
2018-12-01 03:55:48 +00: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 03:59:12 +00: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 03:55:48 +00:00
|
|
|
if title:
|
|
|
|
print("\n## {}".format(title))
|
|
|
|
print("\n{}\n".format("\n".join(markdown)))
|