Add rudimentary version checks on model load

This commit is contained in:
Adriane Boyd 2020-06-02 17:23:16 +02:00
parent 925e938570
commit bbc1836581
3 changed files with 70 additions and 0 deletions

View File

@ -115,6 +115,18 @@ class Warnings(object):
"`spacy.gold.biluo_tags_from_offsets(nlp.make_doc(text), entities)`"
" to check the alignment. Misaligned entities ('-') will be "
"ignored during training.")
W031 = ("Model '{model}' ({model_version}) requires spaCy {version} and "
"is incompatible with the current spaCy version ({current}). This "
"may lead to unexpected results or runtime errors. To resolve "
"this, download a newer compatible model or retrain your custom "
"model with the current spaCy version. For more details and "
"available updates, run: python -m spacy validate")
W032 = ("Unable to determine model compatibility for model '{model}' "
"({model_version}) with the current spaCy version ({current}). "
"This may lead to unexpected results or runtime errors. To resolve "
"this, download a newer compatible model or retrain your custom "
"model with the current spaCy version. For more details and "
"available updates, run: python -m spacy validate")
@add_codes

View File

@ -10,6 +10,7 @@ from spacy import prefer_gpu, require_gpu
from spacy.compat import symlink_to, symlink_remove, path2str, is_windows
from spacy._ml import PrecomputableAffine
from subprocess import CalledProcessError
from .util import make_tempdir
@pytest.fixture
@ -146,3 +147,32 @@ def test_load_model_blank_shortcut():
assert nlp.pipeline == []
with pytest.raises(ImportError):
util.load_model("blank:fjsfijsdof")
def test_load_model_version_compat():
"""Test warnings for various spacy_version specifications in meta. Since
this is more of a hack for v2, manually specify the current major.minor
version to simplify test creation."""
nlp = util.load_model("blank:en")
assert nlp.meta["spacy_version"].startswith(">=2.3")
with make_tempdir() as d:
# no change: compatible
nlp.to_disk(d)
nlp2 = util.load_model(d)
# additional compatible upper pin
nlp.meta["spacy_version"] = ">=2.3.0,<2.4.0"
nlp.to_disk(d)
nlp2 = util.load_model(d)
# incompatible older version
nlp.meta["spacy_version"] = ">=2.2.5"
nlp.to_disk(d)
with pytest.warns(UserWarning):
nlp_reloaded = util.load_model(d)
# invalid version specification
nlp.meta["spacy_version"] = ">@#$%_invalid_version"
nlp.to_disk(d)
with pytest.warns(UserWarning):
nlp_reloaded = util.load_model(d)

View File

@ -17,6 +17,7 @@ import srsly
import catalogue
import sys
import warnings
from . import about
try:
import jsonschema
@ -250,6 +251,33 @@ def get_model_meta(path):
for setting in ["lang", "name", "version"]:
if setting not in meta or not meta[setting]:
raise ValueError(Errors.E054.format(setting=setting))
if "spacy_version" in meta:
about_major_minor = ".".join(about.__version__.split(".")[:2])
if about_major_minor is not None and not meta["spacy_version"].startswith(
">=" + about_major_minor
):
# try to simplify version requirements from model meta to vx.x
# for warning message
meta_spacy_version = "v" + ".".join(
meta["spacy_version"].replace(">=", "").split(".")[:2]
)
# if the format is unexpected, supply the full version
if not re.match(r"v\d+\.\d+", meta_spacy_version):
meta_spacy_version = meta["spacy_version"]
warn_msg = Warnings.W031.format(
model=meta["lang"] + "_" + meta["name"],
model_version=meta["version"],
version=meta_spacy_version,
current=about.__version__,
)
warnings.warn(warn_msg)
else:
warn_msg = Warnings.W032.format(
model=meta["lang"] + "_" + meta["name"],
model_version=meta["version"],
current=about.__version__,
)
warnings.warn(warn_msg)
return meta