-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
213 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#' Queries information about Hub repositories | ||
#' | ||
#' @inheritParams hub_download | ||
#' @param files_metadata Obtain files metadata information when querying repository information. | ||
#' @export | ||
hub_repo_info <- function(repo_id, ..., repo_type = NULL, revision = NULL, files_metadata = FALSE) { | ||
if (is.null(repo_type) || repo_type == "model") { | ||
path <- glue::glue("https://huggingface.co/api/models/{repo_id}") | ||
} else { | ||
path <- glue::glue("https://huggingface.co/api/{repo_type}s/{repo_id}") | ||
} | ||
|
||
if (!is.null(revision)) { | ||
path <- glue::glue("{path}/revision/{revision}") | ||
} | ||
|
||
params <- list() | ||
if (files_metadata) { | ||
params$blobs <- TRUE | ||
} | ||
|
||
results <- httr::GET( | ||
path, | ||
query = params, | ||
httr::add_headers( | ||
"user-agent" = "hfhub/0.0.1" | ||
) | ||
) | ||
|
||
httr::content(results) | ||
} | ||
|
||
#' @describeIn hub_repo_info Query information from a Hub Dataset | ||
#' @export | ||
hub_dataset_info <- function(repo_id, ..., revision = NULL, files_metadata = FALSE) { | ||
hub_repo_info( | ||
repo_id, | ||
revision = revision, | ||
repo_type = "dataset", | ||
files_metadata = files_metadata, | ||
token = token | ||
) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#' Snapshot the entire repository | ||
#' | ||
#' Downloads and stores all files from a Hugging Face Hub repository. | ||
#' @inheritParams hub_download | ||
#' @param allow_patterns A character vector containing patters that are used to | ||
#' filter allowed files to snapshot. | ||
#' @param ignore_patterns A character vector contaitning patterns to reject files | ||
#' from being downloaded. | ||
#' | ||
#' @export | ||
hub_snapshot <- function(repo_id, ..., revision = "main", repo_type = "model", | ||
local_files_only = FALSE, force_download = FALSE, | ||
allow_patterns = NULL, ignore_patterns = NULL) { | ||
info <- hub_repo_info(repo_id, repo_type = repo_type) | ||
all_files <- sapply(info$siblings, function(x) x$rfilename) | ||
|
||
allowed_files <- all_files | ||
if (!is.null(allow_patterns)) { | ||
allowed_files <- lapply(allow_patterns, function(x) { | ||
all_files[grepl(allow_patterns, all_files)] | ||
}) | ||
allowed_files <- unique(unlist(allowed_files)) | ||
} | ||
|
||
files <- allowed_files | ||
if (!is.null(ignore_patterns)) { | ||
for (pattern in ignore_patterns) { | ||
files <- files[!grepl(pattern, files)] | ||
} | ||
} | ||
|
||
id <- cli::cli_progress_bar( | ||
name = "Downloading files", | ||
type = "tasks", | ||
total = length(files), | ||
clear = FALSE | ||
) | ||
|
||
i <- 0 | ||
cli::cli_progress_step("Snapshotting files {i}/{length(files)}") | ||
for (i in seq_along(files)) { | ||
d <- hub_download( | ||
repo_id = repo_id, | ||
filename = files[i], | ||
revision = info$sha, | ||
repo_type = repo_type, | ||
local_files_only = local_files_only, | ||
force_download = force_download | ||
) | ||
} | ||
|
||
attr(d, "snapshot_path") | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# snapshot | ||
|
||
Code | ||
p <- hub_snapshot("dfalbel/cran-packages", repo_type = "dataset", | ||
allow_patterns = "\\.R") | ||
Message <cliMessage> | ||
i Snapshotting files 0/4 | ||
v Snapshotting files 4/4 [0ms] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
skip_on_cran() | ||
|
||
test_that("dataset info", { | ||
info <- hub_dataset_info("dfalbel/cran-packages") | ||
expect_equal(info$author, "dfalbel") | ||
expect_true(length(info$siblings) >= 13) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
skip_on_cran() | ||
|
||
test_that("snapshot", { | ||
expect_snapshot({ | ||
p <- hub_snapshot("dfalbel/cran-packages", repo_type = "dataset", allow_patterns = "\\.R") | ||
}, | ||
transform = function(x) { | ||
sub("\\[[0-9]+[a-z]+\\]", "[0ms]", x = x) | ||
}) | ||
|
||
expect_true(length(fs::dir_ls(p)) >= 4) | ||
}) |