Repository navigation
Expand file tree
/
Copy pathspacy_install.R
More file actions
390 lines (351 loc) · 14.9 KB
/
Copy pathspacy_install.R
File metadata and controls
390 lines (351 loc) · 14.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#' Install spaCy in a virtual environment
#'
#' @description Install spaCy in a self-contained environment, including
#' specified language models.
#' @param version character; spaCy version to install (see details).
#' @param lang_models character; language models to be installed. Defaults
#' `en_core_web_sm` (English model). A vector of multiple model names can
#' be used (e.g. `c("en_core_web_sm", "de_core_news_sm")`). A list of
#' available language models and their
#' names is available from the [spaCy
#' language models](https://spacy.io/usage/models) page.
#' @param ask logical; ask whether to proceed during the installation. By
#' default, questions are only asked in interactive sessions.
#' @param force ignore if spaCy/the lang_models is already present and install
#' it anyway.
#' @param ... not used.
#' @seealso [spacy_download_langmodel()]
#'
#' @details The function checks whether a suitable installation of Python is
#' present on the system and installs one via
#' [reticulate::install_python()] otherwise. "Suitable" means a version of
#' Python supported by the spaCy release being installed, as declared on
#' PyPI (if PyPI cannot be reached, any Python found is used). It then
#' creates a virtual environment with the necessary packages in the default
#' location chosen by [reticulate::virtualenv_root()].
#' For a spaCy version range, pip selects a release compatible with the
#' environment's Python.
#'
#' An existing environment keeps the Python version it was created with. If
#' that version is not supported by the spaCy release being installed,
#' `spacy_install()` stops with an error; run [spacy_uninstall()] and then
#' `spacy_install()` to recreate the environment with a suitable Python.
#'
#' If you want to install a different version of Python than the default, you
#' should call [reticulate::install_python()] directly. If you want
#' to create or use a different virtual environment, you can use, e.g.,
#' `Sys.setenv(SPACY_PYTHON = "path/to/directory")`.
#'
#'
#' @examples
#' \dontrun{
#' # install the latest version of spaCy
#' spacy_install()
#'
#' # update spaCy
#' spacy_install(force = TRUE)
#'
#' # install an older version
#' spacy_install(version = "3.1.0")
#'
#' # install with GPU enabled
#' spacy_install(version = "cuda-autodetect")
#'
#' # install on Apple ARM processors
#' spacy_install(version = "apple")
#'
#' # install an old custom version
#' spacy_install(version = "[cuda-autodetect]==3.2.0")
#'
#' # install several models with spaCy
#' spacy_install(lang_models = c("en_core_web_sm", "de_core_news_sm"))
#'
#'
#' # install spaCy to an existing virtual environment
#' Sys.setenv(RETICULATE_PYTHON = "path/to/python")
#' spacy_install()
#' }
#'
#' @export
spacy_install <- function(version = "latest",
lang_models = "en_core_web_sm",
ask = interactive(),
force = FALSE,
...) {
if (length(list(...)) > 0)
warning("spacyr 1.3 removed a number of arguments to simplify this function.")
envname <- Sys.getenv("SPACY_PYTHON", unset = "r-spacyr")
spacy_pkg <- spacy_pkg_spec(version)
custom_python <- nzchar(Sys.getenv("RETICULATE_PYTHON"))
create_env <- !custom_python && !reticulate::virtualenv_exists(envname)
python_req <- if (create_env) spacy_python_requirement(version)
if (custom_python) {
message("You provided a custom RETICULATE_PYTHON, so we assume you know what you ",
"are doing managing your virtual environments. Good luck!")
} else if (create_env) {
# this has turned out to be the easiest way to test if a suitable Python
# version is present. All other methods load Python, which creates
# some headache.
t <- try(reticulate::virtualenv_create(envname, version = python_req), silent = TRUE)
if (methods::is(t, "try-error")) {
permission <- TRUE
if (ask) {
permission <- utils::askYesNo(paste0(
"No suitable Python installation was found on your system",
if (!is.null(python_req)) paste0(" (spaCy requires Python ", python_req, ")"),
". Do you want to run `reticulate::install_python()` to install it?"
))
}
if (permission) {
python <- install_spacy_python(python_req)
reticulate::virtualenv_create(envname, python = python)
} else {
stop("Aborted by user")
}
}
}
# installation addresses the environment by name, so only bind the session to
# it if Python is not running yet: once started, reticulate cannot switch
# interpreters and use_virtualenv() errors for a different environment
if (!custom_python && !reticulate::py_available(initialize = FALSE)) {
reticulate::use_virtualenv(envname)
}
if (py_check_installed(spacy_pkg) & !force) {
warning("Skipping installation. Use `force` to force installation or update. ",
"Or use `spacy_download_langmodel()` if you just want to install a model.")
return(invisible(NULL))
}
# an existing environment keeps its Python, which may not suit this spaCy
if (!custom_python) {
if (!create_env) python_req <- spacy_python_requirement(version)
check_env_python(envname, python_req)
}
reticulate::py_install(spacy_pkg, envname = envname)
spacy_download_langmodel(lang_models)
message("Installation of spaCy version ",
py_check_version("spacy", envname = envname),
" complete.")
invisible(NULL)
}
# pip requirement for the spaCy version requested in spacy_install()
spacy_pkg_spec <- function(version) {
if (identical(version, "latest")) {
"spacy"
} else if (grepl("^v*[1-9]\\.\\d{1,2}\\.\\d{1,2}$", version)) {
version <- sub("^v+", "", version)
check_spacy_version(version, installing = TRUE)
paste0("spacy==", version)
} else if (grepl("^[A-z,-]+$", version)) {
paste0("spacy[", version, "]")
} else {
spacy_spec_floor(paste0("spacy", version))
}
}
# Check a pip requirement such as "spacy[cuda102]==3.2.0" or "spacy<3.8"
# against spacyr's minimum spaCy version: stop if it allows no spaCy 3 release
# (a pin, compatible release or upper bound below 3.0.0), and add ">=3.0.0" if
# it sets no lower bound of its own at 3 or above.
spacy_spec_floor <- function(spec) {
name <- regmatches(spec, regexpr("^spacy(\\[[^]]*\\])?", spec))
constraints <- trimws(substring(spec, nchar(name) + 1))
# pip accepts the older form "spacy(>=3.7,<3.8)"; drop the parentheses so
# that the minimum can be added after the range
constraints <- trimws(sub("^\\((.*)\\)$", "\\1", constraints))
if (!nzchar(constraints)) return(name)
spec <- paste0(name, constraints)
# a requirement that does not start with a version operator, such as
# "1.10.1a", is not valid for pip; leave it as it is, so that pip's error
# shows what was given
if (!grepl("^(===|==|~=|<=|>=|!=|<|>)", constraints)) return(spec)
minimum <- numeric_version("3.0.0")
has_floor <- FALSE
for (clause in trimws(strsplit(constraints, ",", fixed = TRUE)[[1]])) {
m <- regmatches(clause, regexec(
"^(===|==|~=|<=|>=|<|>)\\s*v*([0-9]+(\\.[0-9]+)*)(\\.\\*)?$", clause))[[1]]
if (!length(m)) next
op <- m[2]
v <- numeric_version(m[3])
major <- unlist(v)[1]
wildcard <- nzchar(m[5])
# "==2.*" and "~=2.9" allow only 2.x releases
excludes <- switch(op,
"<" = v <= minimum,
"<=" = v < minimum,
"==" = , "===" = if (wildcard) major < 3 else v < minimum,
"~=" = major < 3,
FALSE)
if (excludes) {
stop("spacyr requires spaCy 3.0.0 or later; the version requirement \"",
sub("^spacy", "", spec), "\" allows no such version.", call. = FALSE)
}
if (op %in% c("==", "===", "~=", ">=", ">") && v >= minimum) has_floor <- TRUE
}
if (has_floor) spec else paste0(spec, ",>=3.0.0")
}
# Extract a single pinned release from the version argument.
spacy_pkg_version <- function(version) {
spacy_pkg <- spacy_pkg_spec(version)
exact_pin <- "^spacy(\\[[^]]+\\])?==\\s*([0-9]+(\\.[0-9]+)*)$"
if (grepl(exact_pin, spacy_pkg)) sub(exact_pin, "\\2", spacy_pkg) else NULL
}
# Use PyPI metadata for a single release. Leave version ranges to pip.
spacy_pypi_url <- function(version) {
if (grepl("^spacy(\\[[^]]+\\])?$", spacy_pkg_spec(version)))
return("https://pypi.org/pypi/spacy/json")
pin <- spacy_pkg_version(version)
if (!is.null(pin)) paste0("https://pypi.org/pypi/spacy/", pin, "/json")
}
spacy_python_requirement <- function(version) {
url <- spacy_pypi_url(version)
if (is.null(url)) return(NULL)
json <- tryCatch(
suppressWarnings(paste(readLines(url, warn = FALSE), collapse = "")),
error = function(e) NULL
)
if (is.null(json)) return(NULL)
# the first match is the release's own requirement in the "info" section
req <- regmatches(json, regexpr('"requires_python":\\s*"[^"]*"', json))
if (!length(req)) return(NULL)
req <- sub('^"requires_python":\\s*"([^"]*)"$', "\\1", req)
if (nzchar(req)) req else NULL
}
# does a Python version satisfy a requirement such as "<3.16,>=3.10"?
# specifiers that cannot be parsed are ignored
python_satisfies <- function(python_version, requirement) {
v <- numeric_version(python_version)
for (spec in trimws(strsplit(requirement, ",", fixed = TRUE)[[1]])) {
if (grepl("^(==|!=)\\s*[0-9]+(\\.[0-9]+)*\\.\\*$", spec)) {
prefix <- sub("^(==|!=)\\s*(.*)\\*$", "\\2", spec)
matches <- startsWith(paste0(python_version, "."), prefix)
if (matches != startsWith(spec, "==")) return(FALSE)
next
}
m <- regmatches(spec, regexec("^(>=|<=|==|!=|~=|>|<)\\s*([0-9]+(\\.[0-9]+)*)$", spec))[[1]]
if (!length(m)) next
target <- numeric_version(m[3])
ok <- switch(m[2],
">=" = v >= target, "<=" = v <= target,
">" = v > target, "<" = v < target,
"==" = v == target, "!=" = v != target,
"~=" = v >= target && startsWith(paste0(python_version, "."),
sub("[0-9]+$", "", m[3])))
if (!ok) return(FALSE)
}
TRUE
}
# reticulate's default Python can be outside spaCy's supported range
install_spacy_python <- function(requirement) {
if (is.null(requirement)) return(reticulate::install_python())
versions <- reticulate::install_python(list = TRUE)
versions <- grep("^[0-9]+\\.[0-9]+\\.[0-9]+$", versions, value = TRUE)
compatible <- vapply(versions, python_satisfies, logical(1), requirement = requirement)
versions <- versions[compatible]
if (!length(versions))
stop("No Python version available through reticulate satisfies ", requirement,
". Install a compatible Python and try again.", call. = FALSE)
reticulate::install_python(version = as.character(max(numeric_version(versions))))
}
# stop if the environment's Python does not satisfy spaCy's requirement
check_env_python <- function(envname, python_req) {
if (is.null(python_req)) return(invisible(TRUE))
env_python <- virtualenv_python_version(envname)
if (!is.na(env_python) && !python_satisfies(env_python, python_req)) {
stop("The environment '", envname, "' uses Python ", env_python,
", but this version of spaCy requires Python ", python_req, ". ",
"Run `spacy_uninstall()` and then `spacy_install()` to recreate the ",
"environment with a suitable version of Python.", call. = FALSE)
}
invisible(TRUE)
}
# Python version of a virtual environment, without initialising Python
virtualenv_python_version <- function(envname) {
out <- tryCatch(
suppressWarnings(system2(reticulate::virtualenv_python(envname), "-EV",
stdout = TRUE, stderr = TRUE)),
error = function(e) character()
)
out <- out[grepl("^Python [0-9]", out)]
if (!length(out)) return(NA_character_)
sub("^Python ([0-9.]+).*$", "\\1", out[1])
}
#' Shorthand function to upgrade spaCy
#'
#' Upgrade spaCy (to a specific version).
#'
#' @param ... passed on to [spacy_install()]
#'
#' @inherit spacy_install
#' @export
spacy_upgrade <- function(version = "latest",
lang_models = NULL,
ask = interactive(),
force = TRUE,
...) {
spacy_install(version = version,
lang_models = lang_models,
ask = ask,
force = force,
...)
message("Upgraded to spaCy version ", py_check_version("spacy", envname = Sys.getenv("SPACY_PYTHON", unset = "r-spacyr")), ".")
}
#' @title Install spaCy to a virtual environment
#'
#' @description
#' Deprecated. `spacy_install` now installs to a virtual environment by default.
#'
#' @param ... not used
#'
#' @export
spacy_install_virtualenv <- function(...) {
.Deprecated(msg = "`spacy_install` now installs to a virtual environment by default")
}
#' Uninstall the spaCy environment
#'
#' Removes the virtual environment created by spacy_install()
#'
#' @param confirm logical; confirm before uninstalling spaCy?
#'
#' @export
spacy_uninstall <- function(confirm = interactive()) {
reticulate::virtualenv_remove(Sys.getenv("SPACY_PYTHON", unset = "r-spacyr"),
confirm = confirm)
message("Deinstallation complete.")
invisible(NULL)
}
#' Check whether spaCy is installed
#'
#' Checks, without starting Python, whether spaCy (and optionally one or more
#' language models) is installed in the environment that [spacy_initialize()]
#' will use: the one set by `RETICULATE_PYTHON` if set, or otherwise the
#' virtual environment named by `SPACY_PYTHON` (by default `"r-spacyr"`). This
#' is useful, for instance, to skip examples or tests when spaCy is not
#' available. If a different interpreter is already running in the R session
#' and prevents use of the named virtual environment, returns `FALSE`.
#'
#' @param lang_models character; names of language models that must also be
#' installed, e.g. `"en_core_web_sm"`. If `NULL` (the default), only spaCy
#' itself is checked.
#' @return `TRUE` if spaCy and all `lang_models` are installed, `FALSE`
#' otherwise.
#' @seealso [spacy_install()], [spacy_download_langmodel()]
#' @examples
#' spacy_installed()
#' spacy_installed("en_core_web_sm")
#' @export
spacy_installed <- function(lang_models = NULL) {
if (!nzchar(Sys.getenv("RETICULATE_PYTHON"))) {
envname <- Sys.getenv("SPACY_PYTHON", unset = "r-spacyr")
if (!reticulate::virtualenv_exists(envname)) return(FALSE)
if (reticulate::py_available(initialize = FALSE)) {
active_env <- reticulate::py_config()$prefix
# The interpreter lives in bin/ or Scripts/ inside the virtualenv.
configured_env <- dirname(dirname(reticulate::virtualenv_python(envname)))
if (!identical(normalizePath(active_env, winslash = "/", mustWork = FALSE),
normalizePath(configured_env, winslash = "/", mustWork = FALSE)))
return(FALSE)
}
}
installed <- tryCatch(py_check_installed(c("spacy", lang_models)),
error = function(e) FALSE)
all(installed)
}