2023-06-14 15:48:41 +00:00
|
|
|
import sys
|
2020-07-08 12:00:07 +00:00
|
|
|
from typing import Optional, Sequence
|
2024-02-20 12:17:51 +00:00
|
|
|
from urllib.parse import urljoin
|
2023-06-14 15:48:41 +00:00
|
|
|
|
2018-05-20 18:26:56 +00:00
|
|
|
import requests
|
2020-06-21 19:35:01 +00:00
|
|
|
import typer
|
2023-06-14 15:48:41 +00:00
|
|
|
from wasabi import msg
|
2017-03-18 18:29:36 +00:00
|
|
|
|
2017-03-18 14:14:48 +00:00
|
|
|
from .. import about
|
2020-08-05 21:10:29 +00:00
|
|
|
from ..errors import OLD_MODEL_SHORTCUTS
|
2023-11-10 07:05:07 +00:00
|
|
|
from ..util import (
|
|
|
|
get_minor_version,
|
|
|
|
is_in_interactive,
|
|
|
|
is_in_jupyter,
|
|
|
|
is_package,
|
|
|
|
is_prerelease_version,
|
|
|
|
run_command,
|
|
|
|
)
|
2023-06-14 15:48:41 +00:00
|
|
|
from ._util import SDIST_SUFFIX, WHEEL_SUFFIX, Arg, Opt, app
|
2020-07-08 12:00:07 +00:00
|
|
|
|
2016-03-24 10:19:43 +00:00
|
|
|
|
2020-06-21 11:44:00 +00:00
|
|
|
@app.command(
|
|
|
|
"download",
|
|
|
|
context_settings={"allow_extra_args": True, "ignore_unknown_options": True},
|
|
|
|
)
|
2020-06-21 19:35:01 +00:00
|
|
|
def download_cli(
|
2020-06-21 11:44:00 +00:00
|
|
|
# fmt: off
|
2020-06-21 19:35:01 +00:00
|
|
|
ctx: typer.Context,
|
2020-09-03 11:13:03 +00:00
|
|
|
model: str = Arg(..., help="Name of pipeline package to download"),
|
2020-06-21 22:57:28 +00:00
|
|
|
direct: bool = Opt(False, "--direct", "-d", "-D", help="Force direct download of name + version"),
|
2022-09-02 09:58:21 +00:00
|
|
|
sdist: bool = Opt(False, "--sdist", "-S", help="Download sdist (.tar.gz) archive instead of pre-built binary wheel"),
|
2020-06-21 11:44:00 +00:00
|
|
|
# fmt: on
|
2020-01-01 12:15:46 +00:00
|
|
|
):
|
2017-05-27 18:01:46 +00:00
|
|
|
"""
|
2020-09-03 11:13:03 +00:00
|
|
|
Download compatible trained pipeline from the default download path using
|
|
|
|
pip. If --direct flag is set, the command expects the full package name with
|
|
|
|
version. For direct downloads, the compatibility check will be skipped. All
|
2020-06-21 19:35:01 +00:00
|
|
|
additional arguments provided to this command will be passed to `pip install`
|
2020-09-03 11:13:03 +00:00
|
|
|
on package installation.
|
|
|
|
|
2021-01-30 09:09:38 +00:00
|
|
|
DOCS: https://spacy.io/api/cli#download
|
2020-09-03 11:13:03 +00:00
|
|
|
AVAILABLE PACKAGES: https://spacy.io/models
|
2017-05-22 10:28:58 +00:00
|
|
|
"""
|
2021-02-01 12:18:43 +00:00
|
|
|
download(model, direct, sdist, *ctx.args)
|
2020-06-21 19:35:01 +00:00
|
|
|
|
|
|
|
|
2022-09-02 09:58:21 +00:00
|
|
|
def download(
|
|
|
|
model: str,
|
|
|
|
direct: bool = False,
|
|
|
|
sdist: bool = False,
|
|
|
|
*pip_args,
|
|
|
|
) -> None:
|
2021-01-05 02:41:53 +00:00
|
|
|
if (
|
|
|
|
not (is_package("spacy") or is_package("spacy-nightly"))
|
|
|
|
and "--no-deps" not in pip_args
|
|
|
|
):
|
2019-09-16 21:32:41 +00:00
|
|
|
msg.warn(
|
2020-09-03 11:13:03 +00:00
|
|
|
"Skipping pipeline package dependencies and setting `--no-deps`. "
|
2019-09-16 21:32:41 +00:00
|
|
|
"You don't seem to have the spaCy package itself installed "
|
|
|
|
"(maybe because you've built from source?), so installing the "
|
2020-09-03 11:13:03 +00:00
|
|
|
"package dependencies would cause spaCy to be downloaded, which "
|
|
|
|
"probably isn't what you want. If the pipeline package has other "
|
2019-09-16 21:32:41 +00:00
|
|
|
"dependencies, you'll have to install them manually."
|
|
|
|
)
|
|
|
|
pip_args = pip_args + ("--no-deps",)
|
2017-03-15 16:37:18 +00:00
|
|
|
if direct:
|
2024-02-20 12:17:51 +00:00
|
|
|
# Reject model names with '/', in order to prevent shenanigans.
|
|
|
|
if "/" in model:
|
|
|
|
msg.fail(
|
|
|
|
title="Model download rejected",
|
|
|
|
text=f"Cannot download model '{model}'. Models are expected to be file names, not URLs or fragments",
|
|
|
|
exits=True,
|
|
|
|
)
|
2019-03-07 20:07:19 +00:00
|
|
|
components = model.split("-")
|
|
|
|
model_name = "".join(components[:-1])
|
|
|
|
version = components[-1]
|
2017-03-15 16:37:18 +00:00
|
|
|
else:
|
2020-07-08 12:00:07 +00:00
|
|
|
model_name = model
|
2020-08-05 21:10:29 +00:00
|
|
|
if model in OLD_MODEL_SHORTCUTS:
|
2020-07-08 12:00:07 +00:00
|
|
|
msg.warn(
|
2021-03-09 12:04:22 +00:00
|
|
|
f"As of spaCy v3.0, shortcuts like '{model}' are deprecated. Please "
|
2020-09-03 11:13:03 +00:00
|
|
|
f"use the full pipeline package name '{OLD_MODEL_SHORTCUTS[model]}' instead."
|
2020-07-08 12:00:07 +00:00
|
|
|
)
|
2020-08-05 21:10:29 +00:00
|
|
|
model_name = OLD_MODEL_SHORTCUTS[model]
|
2018-05-20 18:26:56 +00:00
|
|
|
compatibility = get_compatibility()
|
2017-03-16 18:54:51 +00:00
|
|
|
version = get_version(model_name, compatibility)
|
2022-09-02 09:58:21 +00:00
|
|
|
|
|
|
|
filename = get_model_filename(model_name, version, sdist)
|
|
|
|
|
|
|
|
download_model(filename, pip_args)
|
2020-06-21 19:35:01 +00:00
|
|
|
msg.good(
|
|
|
|
"Download and installation successful",
|
2020-09-03 11:13:03 +00:00
|
|
|
f"You can now load the package via spacy.load('{model_name}')",
|
2020-06-21 19:35:01 +00:00
|
|
|
)
|
2023-11-10 07:05:07 +00:00
|
|
|
if is_in_jupyter():
|
|
|
|
reload_deps_msg = (
|
|
|
|
"If you are in a Jupyter or Colab notebook, you may need to "
|
|
|
|
"restart Python in order to load all the package's dependencies. "
|
|
|
|
"You can do this by selecting the 'Restart kernel' or 'Restart "
|
|
|
|
"runtime' option."
|
|
|
|
)
|
|
|
|
msg.warn(
|
|
|
|
"Restart to reload dependencies",
|
|
|
|
reload_deps_msg,
|
|
|
|
)
|
|
|
|
elif is_in_interactive():
|
|
|
|
reload_deps_msg = (
|
|
|
|
"If you are in an interactive Python session, you may need to "
|
|
|
|
"exit and restart Python to load all the package's dependencies. "
|
|
|
|
"You can exit with Ctrl-D (or Ctrl-Z and Enter on Windows)."
|
|
|
|
)
|
|
|
|
msg.warn(
|
|
|
|
"Restart to reload dependencies",
|
|
|
|
reload_deps_msg,
|
|
|
|
)
|
2017-03-16 22:23:26 +00:00
|
|
|
|
2018-01-03 19:14:50 +00:00
|
|
|
|
2022-09-02 09:58:21 +00:00
|
|
|
def get_model_filename(model_name: str, version: str, sdist: bool = False) -> str:
|
|
|
|
dl_tpl = "{m}-{v}/{m}-{v}{s}"
|
|
|
|
suffix = SDIST_SUFFIX if sdist else WHEEL_SUFFIX
|
|
|
|
filename = dl_tpl.format(m=model_name, v=version, s=suffix)
|
|
|
|
return filename
|
|
|
|
|
|
|
|
|
2020-07-08 12:00:07 +00:00
|
|
|
def get_compatibility() -> dict:
|
2022-08-04 13:14:19 +00:00
|
|
|
if is_prerelease_version(about.__version__):
|
|
|
|
version: Optional[str] = about.__version__
|
|
|
|
else:
|
|
|
|
version = get_minor_version(about.__version__)
|
2020-07-08 12:00:07 +00:00
|
|
|
r = requests.get(about.__compatibility__)
|
2018-05-20 18:26:56 +00:00
|
|
|
if r.status_code != 200:
|
2018-11-30 19:16:14 +00:00
|
|
|
msg.fail(
|
2019-12-22 00:53:56 +00:00
|
|
|
f"Server error ({r.status_code})",
|
2020-09-03 11:13:03 +00:00
|
|
|
f"Couldn't fetch compatibility table. Please find a package for your spaCy "
|
2019-12-22 00:53:56 +00:00
|
|
|
f"installation (v{about.__version__}), and download it manually. "
|
|
|
|
f"For more details, see the documentation: "
|
2021-01-30 09:09:38 +00:00
|
|
|
f"https://spacy.io/usage/models",
|
2018-11-30 19:16:14 +00:00
|
|
|
exits=1,
|
|
|
|
)
|
2020-07-08 12:00:07 +00:00
|
|
|
comp_table = r.json()
|
2018-11-30 19:16:14 +00:00
|
|
|
comp = comp_table["spacy"]
|
2017-03-15 16:37:18 +00:00
|
|
|
if version not in comp:
|
2020-09-03 11:13:03 +00:00
|
|
|
msg.fail(f"No compatible packages found for v{version} of spaCy", exits=1)
|
2017-04-26 16:00:28 +00:00
|
|
|
return comp[version]
|
2017-03-15 16:37:18 +00:00
|
|
|
|
|
|
|
|
2020-06-21 19:35:01 +00:00
|
|
|
def get_version(model: str, comp: dict) -> str:
|
2017-03-15 16:37:18 +00:00
|
|
|
if model not in comp:
|
2018-11-30 19:16:14 +00:00
|
|
|
msg.fail(
|
2020-09-03 11:13:03 +00:00
|
|
|
f"No compatible package found for '{model}' (spaCy v{about.__version__})",
|
2018-11-30 19:16:14 +00:00
|
|
|
exits=1,
|
|
|
|
)
|
2017-03-15 16:37:18 +00:00
|
|
|
return comp[model][0]
|
|
|
|
|
|
|
|
|
2022-09-02 09:58:21 +00:00
|
|
|
def get_latest_version(model: str) -> str:
|
|
|
|
comp = get_compatibility()
|
|
|
|
return get_version(model, comp)
|
|
|
|
|
|
|
|
|
2020-06-21 19:35:01 +00:00
|
|
|
def download_model(
|
|
|
|
filename: str, user_pip_args: Optional[Sequence[str]] = None
|
|
|
|
) -> None:
|
2024-02-20 12:17:51 +00:00
|
|
|
# Construct the download URL carefully. We need to make sure we don't
|
|
|
|
# allow relative paths or other shenanigans to trick us into download
|
|
|
|
# from outside our own repo.
|
|
|
|
base_url = about.__download_url__
|
|
|
|
# urljoin requires that the path ends with /, or the last path part will be dropped
|
|
|
|
if not base_url.endswith("/"):
|
|
|
|
base_url = about.__download_url__ + "/"
|
|
|
|
download_url = urljoin(base_url, filename)
|
|
|
|
if not download_url.startswith(about.__download_url__):
|
|
|
|
raise ValueError(f"Download from {filename} rejected. Was it a relative path?")
|
2021-02-01 12:15:00 +00:00
|
|
|
pip_args = list(user_pip_args) if user_pip_args is not None else []
|
2018-11-30 19:16:14 +00:00
|
|
|
cmd = [sys.executable, "-m", "pip", "install"] + pip_args + [download_url]
|
2020-06-21 19:35:01 +00:00
|
|
|
run_command(cmd)
|