Automatically install spacy models, if missing
@@ -15,6 +15,7 @@ import sys
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')
+ 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]
