tests/testthat/test-hub_download.R

skip_on_cran()

test_that("hub_download", {
  file <- hub_download("gpt2", filename = "config.json")

  expect_equal(
    jsonlite::fromJSON(file)$architectures,
    "GPT2LMHeadModel"
  )

  file <- hub_download("gpt2", filename = "config.json", force_download = TRUE)
  expect_equal(
    jsonlite::fromJSON(file)$architectures,
    "GPT2LMHeadModel"
  )

  file <- hub_download("gpt2", filename = "config.json", local_files_only = TRUE)
  expect_equal(
    jsonlite::fromJSON(file)$architectures,
    "GPT2LMHeadModel"
  )

  tmp <- tempfile()
  dir.create(tmp)
  withr::with_envvar(c(HUGGINGFACE_HUB_CACHE = tmp), {
    file <- hub_download("gpt2", filename = "config.json")
  })
  expect_equal(list.files(tmp), "models--gpt2")
})

test_that("can download from private repo", {

  skip_if_no_token()

  expect_error(regexp = NA, {
    hub_download(
      repo_id = "dfalbel/test-hfhub",
      filename = ".gitattributes",
      force_download = TRUE
    )
  })

  expect_error(regexp = NA, {
    hub_download(
      repo_id = "dfalbel/test-hfhub",
      filename = "hello.safetensors",
      force_download = TRUE
    )
  })

})

Try the hfhub package in your browser

Any scripts or data that you put into this service are public.

hfhub documentation built on Aug. 19, 2023, 1:07 a.m.