2020-06-21 22:35:01 +03:00
|
|
|
from typing import Tuple
|
2017-10-12 21:05:06 +03:00
|
|
|
from pathlib import Path
|
2018-01-03 23:20:35 +03:00
|
|
|
import sys
|
2018-05-20 21:26:56 +03:00
|
|
|
import requests
|
2020-06-21 22:35:01 +03:00
|
|
|
from wasabi import msg, Printer
|
2017-10-12 21:05:06 +03:00
|
|
|
|
2020-06-21 14:44:00 +03:00
|
|
|
from ._app import app
|
2017-10-12 21:05:06 +03:00
|
|
|
from .. import about
|
2020-05-30 16:01:58 +03:00
|
|
|
from ..util import get_package_version, get_installed_models, get_base_version
|
2020-05-30 16:18:53 +03:00
|
|
|
from ..util import get_package_path, get_model_meta, is_compatible_version
|
2017-10-12 21:05:06 +03:00
|
|
|
|
|
|
|
|
2020-06-21 14:44:00 +03:00
|
|
|
@app.command("validate")
|
2020-06-21 22:35:01 +03:00
|
|
|
def validate_cli():
|
2018-11-30 22:16:14 +03:00
|
|
|
"""
|
|
|
|
Validate that the currently installed version of spaCy is compatible
|
2017-10-12 21:05:06 +03:00
|
|
|
with the installed models. Should be run after `pip install -U spacy`.
|
|
|
|
"""
|
2020-06-21 22:35:01 +03:00
|
|
|
validate()
|
|
|
|
|
|
|
|
|
|
|
|
def validate() -> None:
|
2020-02-18 19:20:17 +03:00
|
|
|
model_pkgs, compat = get_model_pkgs()
|
2020-05-30 16:01:58 +03:00
|
|
|
spacy_version = get_base_version(about.__version__)
|
2020-02-18 19:20:17 +03:00
|
|
|
current_compat = compat.get(spacy_version, {})
|
2018-02-01 00:06:28 +03:00
|
|
|
if not current_compat:
|
2020-02-18 19:20:17 +03:00
|
|
|
msg.warn(f"No compatible models found for v{spacy_version} of spaCy")
|
2018-11-30 22:16:14 +03:00
|
|
|
incompat_models = {d["name"] for _, d in model_pkgs.items() if not d["compat"]}
|
2017-10-12 21:05:06 +03:00
|
|
|
na_models = [m for m in incompat_models if m not in current_compat]
|
|
|
|
update_models = [m for m in incompat_models if m in current_compat]
|
2018-11-30 22:16:14 +03:00
|
|
|
spacy_dir = Path(__file__).parent.parent
|
|
|
|
|
2019-12-22 03:53:56 +03:00
|
|
|
msg.divider(f"Installed models (spaCy v{about.__version__})")
|
|
|
|
msg.info(f"spaCy installation: {spacy_dir}")
|
2017-10-12 21:05:06 +03:00
|
|
|
|
2020-02-18 19:20:17 +03:00
|
|
|
if model_pkgs:
|
2020-05-22 16:42:46 +03:00
|
|
|
header = ("NAME", "SPACY", "VERSION", "")
|
2018-11-30 22:16:14 +03:00
|
|
|
rows = []
|
2017-10-12 21:05:06 +03:00
|
|
|
for name, data in model_pkgs.items():
|
2020-02-18 19:20:17 +03:00
|
|
|
if data["compat"]:
|
|
|
|
comp = msg.text("", color="green", icon="good", no_print=True)
|
|
|
|
version = msg.text(data["version"], color="green", no_print=True)
|
|
|
|
else:
|
|
|
|
version = msg.text(data["version"], color="red", no_print=True)
|
|
|
|
comp = f"--> {compat.get(data['name'], ['n/a'])[0]}"
|
2020-05-22 16:42:46 +03:00
|
|
|
rows.append((data["name"], data["spacy"], version, comp))
|
2018-11-30 22:16:14 +03:00
|
|
|
msg.table(rows, header=header)
|
2017-10-12 21:05:06 +03:00
|
|
|
else:
|
2018-12-08 13:49:43 +03:00
|
|
|
msg.text("No models found in your current environment.", exits=0)
|
2017-10-12 21:05:06 +03:00
|
|
|
if update_models:
|
2018-11-30 22:16:14 +03:00
|
|
|
msg.divider("Install updates")
|
2018-12-08 13:49:43 +03:00
|
|
|
msg.text("Use the following commands to update the model packages:")
|
2018-11-30 22:16:14 +03:00
|
|
|
cmd = "python -m spacy download {}"
|
|
|
|
print("\n".join([cmd.format(pkg) for pkg in update_models]) + "\n")
|
2017-10-12 21:05:06 +03:00
|
|
|
if na_models:
|
2020-05-22 16:42:46 +03:00
|
|
|
msg.info(
|
|
|
|
f"The following models are custom spaCy models or not "
|
|
|
|
f"available for spaCy v{about.__version__}:",
|
2020-02-18 19:20:17 +03:00
|
|
|
", ".join(na_models),
|
2018-11-30 22:16:14 +03:00
|
|
|
)
|
2020-02-18 19:20:17 +03:00
|
|
|
if incompat_models:
|
2018-01-03 23:20:35 +03:00
|
|
|
sys.exit(1)
|
|
|
|
|
2017-10-12 21:05:06 +03:00
|
|
|
|
2020-06-21 22:35:01 +03:00
|
|
|
def get_model_pkgs(silent: bool = False) -> Tuple[dict, dict]:
|
|
|
|
msg = Printer(no_print=silent, pretty=not silent)
|
2020-02-18 19:20:17 +03:00
|
|
|
with msg.loading("Loading compatibility table..."):
|
|
|
|
r = requests.get(about.__compatibility__)
|
|
|
|
if r.status_code != 200:
|
|
|
|
msg.fail(
|
|
|
|
f"Server error ({r.status_code})",
|
|
|
|
"Couldn't fetch compatibility table.",
|
|
|
|
exits=1,
|
|
|
|
)
|
|
|
|
msg.good("Loaded compatibility table")
|
|
|
|
compat = r.json()["spacy"]
|
|
|
|
all_models = set()
|
2020-05-22 16:42:46 +03:00
|
|
|
installed_models = get_installed_models()
|
2020-02-18 19:20:17 +03:00
|
|
|
for spacy_v, models in dict(compat).items():
|
|
|
|
all_models.update(models.keys())
|
|
|
|
for model, model_vs in models.items():
|
|
|
|
compat[spacy_v][model] = [reformat_version(v) for v in model_vs]
|
2017-10-12 21:05:06 +03:00
|
|
|
pkgs = {}
|
2020-05-22 16:42:46 +03:00
|
|
|
for pkg_name in installed_models:
|
2018-11-30 22:16:14 +03:00
|
|
|
package = pkg_name.replace("-", "_")
|
2020-05-22 16:42:46 +03:00
|
|
|
version = get_package_version(pkg_name)
|
|
|
|
if package in compat:
|
|
|
|
is_compat = version in compat[package]
|
2020-05-30 16:01:58 +03:00
|
|
|
spacy_version = about.__version__
|
2020-05-22 16:42:46 +03:00
|
|
|
else:
|
|
|
|
model_path = get_package_path(package)
|
|
|
|
model_meta = get_model_meta(model_path)
|
|
|
|
spacy_version = model_meta.get("spacy_version", "n/a")
|
2020-05-30 16:18:53 +03:00
|
|
|
is_compat = is_compatible_version(about.__version__, spacy_version)
|
2020-05-22 16:42:46 +03:00
|
|
|
pkgs[pkg_name] = {
|
|
|
|
"name": package,
|
|
|
|
"version": version,
|
|
|
|
"spacy": spacy_version,
|
|
|
|
"compat": is_compat,
|
|
|
|
}
|
2020-02-18 19:20:17 +03:00
|
|
|
return pkgs, compat
|
2017-10-12 21:05:06 +03:00
|
|
|
|
|
|
|
|
2020-06-21 22:35:01 +03:00
|
|
|
def reformat_version(version: str) -> str:
|
2017-10-27 15:38:39 +03:00
|
|
|
"""Hack to reformat old versions ending on '-alpha' to match pip format."""
|
2018-11-30 22:16:14 +03:00
|
|
|
if version.endswith("-alpha"):
|
|
|
|
return version.replace("-alpha", "a0")
|
|
|
|
return version.replace("-alpha", "a")
|