mirror of
https://github.com/explosion/spaCy.git
synced 2024-12-26 18:06:29 +03:00
Merge pull request #1792 from explosion/feature-improve-model-download
💫 Improve model downloading and linking
This commit is contained in:
commit
a2a06dce24
|
@ -31,24 +31,28 @@ def download(model, direct=False):
|
||||||
version = get_version(model_name, compatibility)
|
version = get_version(model_name, compatibility)
|
||||||
dl = download_model('{m}-{v}/{m}-{v}.tar.gz'.format(m=model_name,
|
dl = download_model('{m}-{v}/{m}-{v}.tar.gz'.format(m=model_name,
|
||||||
v=version))
|
v=version))
|
||||||
if dl == 0:
|
if dl != 0:
|
||||||
try:
|
# if download subprocess doesn't return 0, exit with the respective
|
||||||
# Get package path here because link uses
|
# exit code before doing anything else
|
||||||
# pip.get_installed_distributions() to check if model is a
|
sys.exit(dl)
|
||||||
# package, which fails if model was just installed via
|
try:
|
||||||
# subprocess
|
# Get package path here because link uses
|
||||||
package_path = get_package_path(model_name)
|
# pip.get_installed_distributions() to check if model is a
|
||||||
link(model_name, model, force=True, model_path=package_path)
|
# package, which fails if model was just installed via
|
||||||
except:
|
# subprocess
|
||||||
# Dirty, but since spacy.download and the auto-linking is
|
package_path = get_package_path(model_name)
|
||||||
# mostly a convenience wrapper, it's best to show a success
|
link(None, model_name, model, force=True,
|
||||||
# message and loading instructions, even if linking fails.
|
model_path=package_path)
|
||||||
prints(
|
except:
|
||||||
"Creating a shortcut link for 'en' didn't work (maybe "
|
# Dirty, but since spacy.download and the auto-linking is
|
||||||
"you don't have admin permissions?), but you can still "
|
# mostly a convenience wrapper, it's best to show a success
|
||||||
"load the model via its full package name:",
|
# message and loading instructions, even if linking fails.
|
||||||
"nlp = spacy.load('%s')" % model_name,
|
prints(
|
||||||
title="Download successful")
|
"Creating a shortcut link for 'en' didn't work (maybe "
|
||||||
|
"you don't have admin permissions?), but you can still "
|
||||||
|
"load the model via its full package name:",
|
||||||
|
"nlp = spacy.load('%s')" % model_name,
|
||||||
|
title="Download successful but linking failed")
|
||||||
|
|
||||||
|
|
||||||
def get_json(url, desc):
|
def get_json(url, desc):
|
||||||
|
@ -84,5 +88,5 @@ def get_version(model, comp):
|
||||||
def download_model(filename):
|
def download_model(filename):
|
||||||
download_url = about.__download_url__ + '/' + filename
|
download_url = about.__download_url__ + '/' + filename
|
||||||
return subprocess.call(
|
return subprocess.call(
|
||||||
[sys.executable, '-m', 'pip', 'install', '--no-cache-dir',
|
[sys.executable, '-m', 'pip', 'install', '--no-cache-dir', '--no-deps',
|
||||||
download_url], env=os.environ.copy())
|
download_url], env=os.environ.copy())
|
||||||
|
|
|
@ -34,11 +34,18 @@ def link(origin, link_name, force=False, model_path=None):
|
||||||
"located here:", path2str(spacy_loc), exits=1,
|
"located here:", path2str(spacy_loc), exits=1,
|
||||||
title="Can't find the spaCy data path to create model symlink")
|
title="Can't find the spaCy data path to create model symlink")
|
||||||
link_path = util.get_data_path() / link_name
|
link_path = util.get_data_path() / link_name
|
||||||
if link_path.exists() and not force:
|
if link_path.is_symlink() and not force:
|
||||||
prints("To overwrite an existing link, use the --force flag.",
|
prints("To overwrite an existing link, use the --force flag.",
|
||||||
title="Link %s already exists" % link_name, exits=1)
|
title="Link %s already exists" % link_name, exits=1)
|
||||||
elif link_path.exists():
|
elif link_path.is_symlink(): # does a symlink exist?
|
||||||
|
# NB: It's important to check for is_symlink here and not for exists,
|
||||||
|
# because invalid/outdated symlinks would return False otherwise.
|
||||||
link_path.unlink()
|
link_path.unlink()
|
||||||
|
elif link_path.exists(): # does it exist otherwise?
|
||||||
|
# NB: Check this last because valid symlinks also "exist".
|
||||||
|
prints("This can happen if your data directory contains a directory "
|
||||||
|
"or file of the same name.", link_path,
|
||||||
|
title="Can't overwrite symlink %s" % link_name, exits=1)
|
||||||
try:
|
try:
|
||||||
symlink_to(link_path, model_path)
|
symlink_to(link_path, model_path)
|
||||||
except:
|
except:
|
||||||
|
|
|
@ -4,6 +4,7 @@ from __future__ import unicode_literals, print_function
|
||||||
import requests
|
import requests
|
||||||
import pkg_resources
|
import pkg_resources
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
import sys
|
||||||
|
|
||||||
from ..compat import path2str, locale_escape
|
from ..compat import path2str, locale_escape
|
||||||
from ..util import prints, get_data_path, read_json
|
from ..util import prints, get_data_path, read_json
|
||||||
|
@ -62,6 +63,9 @@ def validate():
|
||||||
"them from the data directory. Data path: {}"
|
"them from the data directory. Data path: {}"
|
||||||
.format(path2str(get_data_path())))
|
.format(path2str(get_data_path())))
|
||||||
|
|
||||||
|
if incompat_models or incompat_links:
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
def get_model_links(compat):
|
def get_model_links(compat):
|
||||||
links = {}
|
links = {}
|
||||||
|
|
|
@ -17,6 +17,17 @@ p
|
||||||
| Direct downloads don't perform any compatibility checks and require the
|
| Direct downloads don't perform any compatibility checks and require the
|
||||||
| model name to be specified with its version (e.g., #[code en_core_web_sm-1.2.0]).
|
| model name to be specified with its version (e.g., #[code en_core_web_sm-1.2.0]).
|
||||||
|
|
||||||
|
+aside("Downloading best practices")
|
||||||
|
| The #[code download] command is mostly intended as a convenient,
|
||||||
|
| interactive wrapper – it performs compatibility checks and prints
|
||||||
|
| detailed messages in case things go wrong. It's #[strong not recommended]
|
||||||
|
| to use this command as part of an automated process. If you know which
|
||||||
|
| model your project needs, you should consider a
|
||||||
|
| #[+a("/usage/models#download-pip") direct download via pip], or
|
||||||
|
| uploading the model to a local PyPi installation and fetching it straight
|
||||||
|
| from there. This will also allow you to add it as a versioned package
|
||||||
|
| dependency to your project.
|
||||||
|
|
||||||
+code(false, "bash", "$").
|
+code(false, "bash", "$").
|
||||||
python -m spacy download [model] [--direct]
|
python -m spacy download [model] [--direct]
|
||||||
|
|
||||||
|
@ -43,17 +54,6 @@ p
|
||||||
| The installed model package in your #[code site-packages]
|
| The installed model package in your #[code site-packages]
|
||||||
| directory and a shortcut link as a symlink in #[code spacy/data].
|
| directory and a shortcut link as a symlink in #[code spacy/data].
|
||||||
|
|
||||||
+aside("Downloading best practices")
|
|
||||||
| The #[code download] command is mostly intended as a convenient,
|
|
||||||
| interactive wrapper – it performs compatibility checks and prints
|
|
||||||
| detailed messages in case things go wrong. It's #[strong not recommended]
|
|
||||||
| to use this command as part of an automated process. If you know which
|
|
||||||
| model your project needs, you should consider a
|
|
||||||
| #[+a("/usage/models#download-pip") direct download via pip], or
|
|
||||||
| uploading the model to a local PyPi installation and fetching it straight
|
|
||||||
| from there. This will also allow you to add it as a versioned package
|
|
||||||
| dependency to your project.
|
|
||||||
|
|
||||||
+h(3, "link") Link
|
+h(3, "link") Link
|
||||||
|
|
||||||
p
|
p
|
||||||
|
@ -144,8 +144,14 @@ p
|
||||||
| #[code pip install -U spacy] to ensure that all installed models are
|
| #[code pip install -U spacy] to ensure that all installed models are
|
||||||
| can be used with the new version. The command is also useful to detect
|
| can be used with the new version. The command is also useful to detect
|
||||||
| out-of-sync model links resulting from links created in different virtual
|
| out-of-sync model links resulting from links created in different virtual
|
||||||
| environments. Prints a list of models, the installed versions, the latest
|
| environments. It will a list of models, the installed versions, the
|
||||||
| compatible version (if out of date) and the commands for updating.
|
| latest compatible version (if out of date) and the commands for updating.
|
||||||
|
|
||||||
|
+aside("Automated validation")
|
||||||
|
| You can also use the #[code validate] command as part of your build
|
||||||
|
| process or test suite, to ensure all models are up to date before
|
||||||
|
| proceeding. If incompatible models or shortcut links are found, it will
|
||||||
|
| return #[code 1].
|
||||||
|
|
||||||
+code(false, "bash", "$").
|
+code(false, "bash", "$").
|
||||||
python -m spacy validate
|
python -m spacy validate
|
||||||
|
|
Loading…
Reference in New Issue
Block a user