mirror of
				https://github.com/explosion/spaCy.git
				synced 2025-11-04 09:57:26 +03:00 
			
		
		
		
	Add better error for failed model shortcut loading
This commit is contained in:
		
							parent
							
								
									50311a4d37
								
							
						
					
					
						commit
						2a1fa86a0d
					
				| 
						 | 
					@ -7,23 +7,7 @@ import typer
 | 
				
			||||||
from ._util import app, Arg, Opt
 | 
					from ._util import app, Arg, Opt
 | 
				
			||||||
from .. import about
 | 
					from .. import about
 | 
				
			||||||
from ..util import is_package, get_base_version, run_command
 | 
					from ..util import is_package, get_base_version, run_command
 | 
				
			||||||
 | 
					from ..errors import OLD_MODEL_SHORTCUTS
 | 
				
			||||||
# These are the old shortcuts we previously supported in spacy download. As of
 | 
					 | 
				
			||||||
# v3, shortcuts are deprecated so we're not expecting to add anything to this
 | 
					 | 
				
			||||||
# list. It only exists to show users warnings.
 | 
					 | 
				
			||||||
OLD_SHORTCUTS = {
 | 
					 | 
				
			||||||
    "en": "en_core_web_sm",
 | 
					 | 
				
			||||||
    "de": "de_core_news_sm",
 | 
					 | 
				
			||||||
    "es": "es_core_news_sm",
 | 
					 | 
				
			||||||
    "pt": "pt_core_news_sm",
 | 
					 | 
				
			||||||
    "fr": "fr_core_news_sm",
 | 
					 | 
				
			||||||
    "it": "it_core_news_sm",
 | 
					 | 
				
			||||||
    "nl": "nl_core_news_sm",
 | 
					 | 
				
			||||||
    "el": "el_core_news_sm",
 | 
					 | 
				
			||||||
    "nb": "nb_core_news_sm",
 | 
					 | 
				
			||||||
    "lt": "lt_core_news_sm",
 | 
					 | 
				
			||||||
    "xx": "xx_ent_wiki_sm",
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@app.command(
 | 
					@app.command(
 | 
				
			||||||
| 
						 | 
					@ -66,12 +50,12 @@ def download(model: str, direct: bool = False, *pip_args) -> None:
 | 
				
			||||||
        download_model(dl_tpl.format(m=model_name, v=version), pip_args)
 | 
					        download_model(dl_tpl.format(m=model_name, v=version), pip_args)
 | 
				
			||||||
    else:
 | 
					    else:
 | 
				
			||||||
        model_name = model
 | 
					        model_name = model
 | 
				
			||||||
        if model in OLD_SHORTCUTS:
 | 
					        if model in OLD_MODEL_SHORTCUTS:
 | 
				
			||||||
            msg.warn(
 | 
					            msg.warn(
 | 
				
			||||||
                f"As of spaCy v3.0, shortcuts like '{model}' are deprecated. "
 | 
					                f"As of spaCy v3.0, shortcuts like '{model}' are deprecated. Please"
 | 
				
			||||||
                f"Please use the full model name '{OLD_SHORTCUTS[model]}' instead."
 | 
					                f"use the full model name '{OLD_MODEL_SHORTCUTS[model]}' instead."
 | 
				
			||||||
            )
 | 
					            )
 | 
				
			||||||
            model_name = OLD_SHORTCUTS[model]
 | 
					            model_name = OLD_MODEL_SHORTCUTS[model]
 | 
				
			||||||
        compatibility = get_compatibility()
 | 
					        compatibility = get_compatibility()
 | 
				
			||||||
        version = get_version(model_name, compatibility)
 | 
					        version = get_version(model_name, compatibility)
 | 
				
			||||||
        download_model(dl_tpl.format(m=model_name, v=version), pip_args)
 | 
					        download_model(dl_tpl.format(m=model_name, v=version), pip_args)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -482,6 +482,11 @@ class Errors:
 | 
				
			||||||
    E199 = ("Unable to merge 0-length span at doc[{start}:{end}].")
 | 
					    E199 = ("Unable to merge 0-length span at doc[{start}:{end}].")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # TODO: fix numbering after merging develop into master
 | 
					    # TODO: fix numbering after merging develop into master
 | 
				
			||||||
 | 
					    E941 = ("Can't find model '{name}'. It looks like you're trying to load a "
 | 
				
			||||||
 | 
					            "model from a shortcut, which is deprecated as of spaCy v3.0. To "
 | 
				
			||||||
 | 
					            "load the model, use its full name instead. For example:\n\n"
 | 
				
			||||||
 | 
					            "nlp = spacy.load(\"{full}\")\n\nFor more details on the available "
 | 
				
			||||||
 | 
					            "models, see the models directory: https://spacy.io/models")
 | 
				
			||||||
    E942 = ("Executing after_{name} callback failed. Expected the function to "
 | 
					    E942 = ("Executing after_{name} callback failed. Expected the function to "
 | 
				
			||||||
            "return an initialized nlp object but got: {value}. Maybe "
 | 
					            "return an initialized nlp object but got: {value}. Maybe "
 | 
				
			||||||
            "you forgot to return the modified object in your function?")
 | 
					            "you forgot to return the modified object in your function?")
 | 
				
			||||||
| 
						 | 
					@ -635,6 +640,15 @@ class TempErrors:
 | 
				
			||||||
            "issue tracker: http://github.com/explosion/spaCy/issues")
 | 
					            "issue tracker: http://github.com/explosion/spaCy/issues")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Deprecated model shortcuts, only used in errors and warnings
 | 
				
			||||||
 | 
					OLD_MODEL_SHORTCUTS = {
 | 
				
			||||||
 | 
					    "en": "en_core_web_sm", "de": "de_core_news_sm", "es": "es_core_news_sm",
 | 
				
			||||||
 | 
					    "pt": "pt_core_news_sm", "fr": "fr_core_news_sm", "it": "it_core_news_sm",
 | 
				
			||||||
 | 
					    "nl": "nl_core_news_sm", "el": "el_core_news_sm", "nb": "nb_core_news_sm",
 | 
				
			||||||
 | 
					    "lt": "lt_core_news_sm", "xx": "xx_ent_wiki_sm"
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# fmt: on
 | 
					# fmt: on
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -44,7 +44,7 @@ from thinc.api import fix_random_seed, compounding, decaying  # noqa: F401
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from .symbols import ORTH
 | 
					from .symbols import ORTH
 | 
				
			||||||
from .compat import cupy, CudaStream, is_windows
 | 
					from .compat import cupy, CudaStream, is_windows
 | 
				
			||||||
from .errors import Errors, Warnings
 | 
					from .errors import Errors, Warnings, OLD_MODEL_SHORTCUTS
 | 
				
			||||||
from . import about
 | 
					from . import about
 | 
				
			||||||
 | 
					
 | 
				
			||||||
if TYPE_CHECKING:
 | 
					if TYPE_CHECKING:
 | 
				
			||||||
| 
						 | 
					@ -232,6 +232,8 @@ def load_model(
 | 
				
			||||||
            return load_model_from_path(Path(name), **kwargs)
 | 
					            return load_model_from_path(Path(name), **kwargs)
 | 
				
			||||||
    elif hasattr(name, "exists"):  # Path or Path-like to model data
 | 
					    elif hasattr(name, "exists"):  # Path or Path-like to model data
 | 
				
			||||||
        return load_model_from_path(name, **kwargs)
 | 
					        return load_model_from_path(name, **kwargs)
 | 
				
			||||||
 | 
					    if name in OLD_MODEL_SHORTCUTS:
 | 
				
			||||||
 | 
					        raise IOError(Errors.E941.format(name=name, full=OLD_MODEL_SHORTCUTS[name]))
 | 
				
			||||||
    raise IOError(Errors.E050.format(name=name))
 | 
					    raise IOError(Errors.E050.format(name=name))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user