From 459e0ca738a92df4d8256b81738ccf78af6da144 Mon Sep 17 00:00:00 2001 From: TroyHernandez Date: Wed, 14 Jan 2026 18:12:57 -0600 Subject: [PATCH 1/2] Add HF_TOKEN as primary env var for authentication HF_TOKEN is the standard environment variable used by HuggingFace CLI and Python libraries. Check it first before falling back to the longer HUGGING_FACE_HUB_TOKEN and HUGGINGFACE_HUB_TOKEN variants. This fixes authentication for gated repos when users have HF_TOKEN set in their .Renviron or environment. Co-Authored-By: Claude Opus 4.5 --- R/hub_download.R | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/R/hub_download.R b/R/hub_download.R index 714c2a5..a34d878 100644 --- a/R/hub_download.R +++ b/R/hub_download.R @@ -188,7 +188,10 @@ repo_folder_name <- function(repo_id, repo_type = "model") { hub_headers <- function() { headers <- c("user-agent" = "hfhub/0.0.1") - token <- Sys.getenv("HUGGING_FACE_HUB_TOKEN", unset = "") + # Check multiple common env var names for HuggingFace token + token <- Sys.getenv("HF_TOKEN", unset = "") + if (!nzchar(token)) + token <- Sys.getenv("HUGGING_FACE_HUB_TOKEN", unset = "") if (!nzchar(token)) token <- Sys.getenv("HUGGINGFACE_HUB_TOKEN", unset = "") From 2df5137958629444c6bf39fdfe8ec451c7a6ae14 Mon Sep 17 00:00:00 2001 From: TroyHernandez Date: Wed, 14 Jan 2026 18:20:03 -0600 Subject: [PATCH 2/2] Add helpful error messages for gated repos and auth failures When accessing gated repos without proper authorization, provide clear instructions: - GatedRepo error: Link to the model page to accept terms - 401 Unauthorized: Instructions to set HF_TOKEN - Other errors: Show the error message from HuggingFace Co-Authored-By: Claude Opus 4.5 --- R/hub_download.R | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/R/hub_download.R b/R/hub_download.R index a34d878..36c5c03 100644 --- a/R/hub_download.R +++ b/R/hub_download.R @@ -42,6 +42,29 @@ hub_download <- function(repo_id, filename, ..., revision = "main", repo_type = tryCatch({ metadata <- get_file_metadata(url) + # Check for HTTP errors with helpful messages + if (!is.null(metadata$status_code) && metadata$status_code >= 400) { + if (metadata$error_code == "GatedRepo") { + cli::cli_abort(c( + "Access denied to gated repository.", + "i" = "This model requires accepting a license agreement.", + "i" = "Visit {.url https://huggingface.co/{repo_id}} to accept the terms.", + "i" = "Make sure you are logged in with the account that owns your HF_TOKEN." + )) + } else if (metadata$status_code == 401) { + cli::cli_abort(c( + "Authentication required.", + "i" = "Set the {.envvar HF_TOKEN} environment variable with your HuggingFace token.", + "i" = "Get a token at {.url https://huggingface.co/settings/tokens}" + )) + } else if (!is.null(metadata$error_message)) { + cli::cli_abort(c( + "HuggingFace API error ({metadata$status_code}).", + "x" = metadata$error_message + )) + } + } + commit_hash <- metadata$commit_hash if (is.null(commit_hash)) { cli::cli_abort(gettext("Distant resource does not seem to be on huggingface.co (missing commit header).")) @@ -215,10 +238,13 @@ get_file_metadata <- function(url) { follow_relative_redirects = TRUE ) list( + status_code = req$status_code, location = grab_from_headers(req, "location") %||% req$url, commit_hash = grab_from_headers(req, "x-repo-commit"), etag = normalize_etag(grab_from_headers(req, c(HUGGINGFACE_HEADER_X_LINKED_ETAG(), "etag"))), - size = as.integer(grab_from_headers(req, "content-length")) + size = as.integer(grab_from_headers(req, "content-length")), + error_code = grab_from_headers(req, "x-error-code"), + error_message = grab_from_headers(req, "x-error-message") ) }