diff --git a/allennlp/common/util.py b/allennlp/common/util.py index 161f1ff9934..ed4ca26a794 100644 --- a/allennlp/common/util.py +++ b/allennlp/common/util.py @@ -15,6 +15,7 @@ import torch import numpy import spacy +from spacy.cli.download import download as spacy_download from spacy.language import Language as SpacyModelType from allennlp.common.checks import log_pytorch_version_info @@ -189,6 +190,7 @@ def get_spacy_model(spacy_model_name: str, pos_tags: bool, parse: bool, ner: boo keyed by the options we used to create the spacy model, so any particular configuration only gets loaded once. """ + options = (spacy_model_name, pos_tags, parse, ner) if options not in LOADED_SPACY_MODELS: disable = ['vectors', 'textcat'] @@ -198,7 +200,13 @@ def get_spacy_model(spacy_model_name: str, pos_tags: bool, parse: bool, ner: boo disable.append('parser') if not ner: disable.append('ner') - spacy_model = spacy.load(spacy_model_name, disable=disable) + try: + spacy_model = spacy.load(spacy_model_name, disable=disable) + except OSError: + logger.warning(f"Spacy models '{spacy_model_name}' not found. Downloading and installing.") + spacy_download(spacy_model_name) + spacy_model = spacy.load(spacy_model_name, disable=disable) + LOADED_SPACY_MODELS[options] = spacy_model return LOADED_SPACY_MODELS[options]