2023-06-14 18:48:41 +03:00
|
|
|
import sys
|
2020-07-08 15:00:07 +03:00
|
|
|
from typing import Optional, Sequence
|
2023-06-14 18:48:41 +03:00
|
|
|
|
2018-05-20 21:26:56 +03:00
|
|
|
import requests
|
2020-06-21 22:35:01 +03:00
|
|
|
import typer
|
2023-06-14 18:48:41 +03:00
|
|
|
from wasabi import msg
|
2017-03-18 21:29:36 +03:00
|
|
|
|
2017-03-18 17:14:48 +03:00
|
|
|
from .. import about
|
2020-08-06 00:10:29 +03:00
|
|
|
from ..errors import OLD_MODEL_SHORTCUTS
|
2023-06-14 18:48:41 +03:00
|
|
|
from ..util import get_minor_version, is_package, is_prerelease_version, run_command
|
|
|
|
from ._util import SDIST_SUFFIX, WHEEL_SUFFIX, Arg, Opt, app
|
2020-07-08 15:00:07 +03:00
|
|
|
|
2016-03-24 13:19:43 +03:00
|
|
|
|
2020-06-21 14:44:00 +03:00
|
|
|
@app.command(
|
|
|
|
"download",
|
|
|
|
context_settings={"allow_extra_args": True, "ignore_unknown_options": True},
|
|
|
|
)
|
2020-06-21 22:35:01 +03:00
|
|
|
def download_cli(
|
2020-06-21 14:44:00 +03:00
|
|
|
# fmt: off
|
2020-06-21 22:35:01 +03:00
|
|
|
ctx: typer.Context,
|
2020-09-03 14:13:03 +03:00
|
|
|
model: str = Arg(..., help="Name of pipeline package to download"),
|
2020-06-22 01:57:28 +03:00
|
|
|
direct: bool = Opt(False, "--direct", "-d", "-D", help="Force direct download of name + version"),
|
2022-09-02 12:58:21 +03:00
|
|
|
sdist: bool = Opt(False, "--sdist", "-S", help="Download sdist (.tar.gz) archive instead of pre-built binary wheel"),
|
2020-06-21 14:44:00 +03:00
|
|
|
# fmt: on
|
2020-01-01 15:15:46 +03:00
|
|
|
):
|
2017-05-27 21:01:46 +03:00
|
|
|
"""
|
2020-09-03 14:13:03 +03: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 22:35:01 +03:00
|
|
|
additional arguments provided to this command will be passed to `pip install`
|
2020-09-03 14:13:03 +03:00
|
|
|
on package installation.
|
|
|
|
|
2021-01-30 12:09:38 +03:00
|
|
|
DOCS: https://spacy.io/api/cli#download
|
2020-09-03 14:13:03 +03:00
|
|
|
AVAILABLE PACKAGES: https://spacy.io/models
|
2017-05-22 13:28:58 +03:00
|
|
|
"""
|
2021-02-01 15:18:43 +03:00
|
|
|
download(model, direct, sdist, *ctx.args)
|
2020-06-21 22:35:01 +03:00
|
|
|
|
|
|
|
|
2022-09-02 12:58:21 +03:00
|
|
|
def download(
|
|
|
|
model: str,
|
|
|
|
direct: bool = False,
|
|
|
|
sdist: bool = False,
|
|
|
|
*pip_args,
|
|
|
|
) -> None:
|
2021-01-05 05:41:53 +03:00
|
|
|
if (
|
|
|
|
not (is_package("spacy") or is_package("spacy-nightly"))
|
|
|
|
and "--no-deps" not in pip_args
|
|
|
|
):
|
2019-09-17 00:32:41 +03:00
|
|
|
msg.warn(
|
2020-09-03 14:13:03 +03:00
|
|
|
"Skipping pipeline package dependencies and setting `--no-deps`. "
|
2019-09-17 00:32:41 +03: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 14:13:03 +03:00
|
|
|
"package dependencies would cause spaCy to be downloaded, which "
|
|
|
|
"probably isn't what you want. If the pipeline package has other "
|
2019-09-17 00:32:41 +03:00
|
|
|
"dependencies, you'll have to install them manually."
|
|
|
|
)
|
|
|
|
pip_args = pip_args + ("--no-deps",)
|
2017-03-15 19:37:18 +03:00
|
|
|
if direct:
|
2019-03-07 23:07:19 +03:00
|
|
|
components = model.split("-")
|
|
|
|
model_name = "".join(components[:-1])
|
|
|
|
version = components[-1]
|
2017-03-15 19:37:18 +03:00
|
|
|
else:
|
2020-07-08 15:00:07 +03:00
|
|
|
model_name = model
|
2020-08-06 00:10:29 +03:00
|
|
|
if model in OLD_MODEL_SHORTCUTS:
|
2020-07-08 15:00:07 +03:00
|
|
|
msg.warn(
|
2021-03-09 15:04:22 +03:00
|
|
|
f"As of spaCy v3.0, shortcuts like '{model}' are deprecated. Please "
|
2020-09-03 14:13:03 +03:00
|
|
|
f"use the full pipeline package name '{OLD_MODEL_SHORTCUTS[model]}' instead."
|
2020-07-08 15:00:07 +03:00
|
|
|
)
|
2020-08-06 00:10:29 +03:00
|
|
|
model_name = OLD_MODEL_SHORTCUTS[model]
|
2018-05-20 21:26:56 +03:00
|
|
|
compatibility = get_compatibility()
|
2017-03-16 21:54:51 +03:00
|
|
|
version = get_version(model_name, compatibility)
|
2022-09-02 12:58:21 +03:00
|
|
|
|
|
|
|
filename = get_model_filename(model_name, version, sdist)
|
|
|
|
|
|
|
|
download_model(filename, pip_args)
|
2020-06-21 22:35:01 +03:00
|
|
|
msg.good(
|
|
|
|
"Download and installation successful",
|
2020-09-03 14:13:03 +03:00
|
|
|
f"You can now load the package via spacy.load('{model_name}')",
|
2020-06-21 22:35:01 +03:00
|
|
|
)
|
2017-03-17 01:23:26 +03:00
|
|
|
|
2018-01-03 22:14:50 +03:00
|
|
|
|
2022-09-02 12:58:21 +03: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 15:00:07 +03:00
|
|
|
def get_compatibility() -> dict:
|
2022-08-04 16:14:19 +03:00
|
|
|
if is_prerelease_version(about.__version__):
|
|
|
|
version: Optional[str] = about.__version__
|
|
|
|
else:
|
|
|
|
version = get_minor_version(about.__version__)
|
2020-07-08 15:00:07 +03:00
|
|
|
r = requests.get(about.__compatibility__)
|
2018-05-20 21:26:56 +03:00
|
|
|
if r.status_code != 200:
|
2018-11-30 22:16:14 +03:00
|
|
|
msg.fail(
|
2019-12-22 03:53:56 +03:00
|
|
|
f"Server error ({r.status_code})",
|
2020-09-03 14:13:03 +03:00
|
|
|
f"Couldn't fetch compatibility table. Please find a package for your spaCy "
|
2019-12-22 03:53:56 +03:00
|
|
|
f"installation (v{about.__version__}), and download it manually. "
|
|
|
|
f"For more details, see the documentation: "
|
2021-01-30 12:09:38 +03:00
|
|
|
f"https://spacy.io/usage/models",
|
2018-11-30 22:16:14 +03:00
|
|
|
exits=1,
|
|
|
|
)
|
2020-07-08 15:00:07 +03:00
|
|
|
comp_table = r.json()
|
2018-11-30 22:16:14 +03:00
|
|
|
comp = comp_table["spacy"]
|
2017-03-15 19:37:18 +03:00
|
|
|
if version not in comp:
|
2020-09-03 14:13:03 +03:00
|
|
|
msg.fail(f"No compatible packages found for v{version} of spaCy", exits=1)
|
2017-04-26 19:00:28 +03:00
|
|
|
return comp[version]
|
2017-03-15 19:37:18 +03:00
|
|
|
|
|
|
|
|
2020-06-21 22:35:01 +03:00
|
|
|
def get_version(model: str, comp: dict) -> str:
|
2017-03-15 19:37:18 +03:00
|
|
|
if model not in comp:
|
2018-11-30 22:16:14 +03:00
|
|
|
msg.fail(
|
2020-09-03 14:13:03 +03:00
|
|
|
f"No compatible package found for '{model}' (spaCy v{about.__version__})",
|
2018-11-30 22:16:14 +03:00
|
|
|
exits=1,
|
|
|
|
)
|
2017-03-15 19:37:18 +03:00
|
|
|
return comp[model][0]
|
|
|
|
|
|
|
|
|
2022-09-02 12:58:21 +03:00
|
|
|
def get_latest_version(model: str) -> str:
|
|
|
|
comp = get_compatibility()
|
|
|
|
return get_version(model, comp)
|
|
|
|
|
|
|
|
|
2020-06-21 22:35:01 +03:00
|
|
|
def download_model(
|
|
|
|
filename: str, user_pip_args: Optional[Sequence[str]] = None
|
|
|
|
) -> None:
|
2018-11-30 22:16:14 +03:00
|
|
|
download_url = about.__download_url__ + "/" + filename
|
2021-02-01 15:15:00 +03:00
|
|
|
pip_args = list(user_pip_args) if user_pip_args is not None else []
|
2018-11-30 22:16:14 +03:00
|
|
|
cmd = [sys.executable, "-m", "pip", "install"] + pip_args + [download_url]
|
2020-06-21 22:35:01 +03:00
|
|
|
run_command(cmd)
|