| Title: | Convert Hugging Face Datasets into mlr3 Tasks |
|---|---|
| Description: | Provides a simple interface to download tabular datasets from the Hugging Face Hub and convert them into mlr3 Task objects. |
| Authors: | Toby Hocking [aut], Sebastian Fischer [cre, aut], Anjani Nandan [aut] |
| Maintainer: | Sebastian Fischer <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.0.0.9000 |
| Built: | 2026-09-27 09:45:04 UTC |
| Source: | https://github.com/mlr-org/mlr3hf |
Converts an HFData object into an mlr3 DataBackend,
triggering download and parsing of the underlying dataset if this has not
already happened.
## S3 method for class 'HFData' as_data_backend(data, ...)## S3 method for class 'HFData' as_data_backend(data, ...)
data |
( |
... |
Additional arguments (currently unused). |
An mlr3 DataBackend.
Converts an HFData object into an mlr3 Task, either a
TaskClassif or a TaskRegr. The
target column and task type can be taken from the HFData object or
overridden explicitly.
When task_type = "auto", the task type is inferred from the class of
the target column: factor and logical columns become classification
tasks, numeric and integer columns become regression tasks.
## S3 method for class 'HFData' as_task(x, target_names = NULL, task_type = NULL, ...)## S3 method for class 'HFData' as_task(x, target_names = NULL, task_type = NULL, ...)
x |
( |
target_names |
( |
task_type |
( |
... |
Additional arguments (currently unused). |
An mlr3::TaskClassif or mlr3::TaskRegr object.
Downloads and caches a specific file from the canonical (original)
version of a dataset repository on the Hugging Face Hub — that is,
the dataset as published by its authors, rather than the
auto-generated Parquet conversion (see cache_parquet
for that). If the file has already been cached locally, it is
reused instead of being downloaded again.
cache_hfhub( repo_id, file_name, local_files_only = FALSE, revision = "main", ... )cache_hfhub( repo_id, file_name, local_files_only = FALSE, revision = "main", ... )
repo_id |
Character string. The repository ID of the dataset on
the Hugging Face Hub, in the form |
file_name |
Character string. The name of the file to download
from the repository, e.g. |
local_files_only |
Logical. If |
revision |
Character string. The branch, tag, or commit hash to
download the file from. Defaults to |
... |
Additional arguments for future use. |
Files are cached in a local directory following the Hugging Face
Hub caching convention, keyed by repo_id, revision,
and file_name. Subsequent calls with the same arguments will
return the cached path without re-downloading, unless the remote
file has changed.
Character string giving the local file path where the cached file is stored.
cache_parquet for caching the auto-converted Parquet
version of a dataset instead.
https://huggingface.co/docs/huggingface_hub/en/guides/manage-cache
cache_hfhub( repo_id = "scikit-learn/iris", file_name = "iris.csv" ) # Use a cached copy only, without attempting a network request cache_hfhub( repo_id = "scikit-learn/iris", file_name = "iris.csv", local_files_only = TRUE )cache_hfhub( repo_id = "scikit-learn/iris", file_name = "iris.csv" ) # Use a cached copy only, without attempting a network request cache_hfhub( repo_id = "scikit-learn/iris", file_name = "iris.csv", local_files_only = TRUE )
Downloads and caches the Parquet files for a dataset hosted on the
Hugging Face Hub, using the auto-generated refs/convert/parquet
branch. This branch contains a Parquet conversion of the dataset that
Hugging Face maintains automatically, which avoids the need to load
the dataset via its original (often script-based) loading format.
cache_parquet( repo_id, config, split = NULL, revision = "refs%2Fconvert%2Fparquet", ... )cache_parquet( repo_id, config, split = NULL, revision = "refs%2Fconvert%2Fparquet", ... )
repo_id |
Character string. The repository ID of the dataset on
the Hugging Face Hub, in the form |
config |
Character string. The dataset configuration name. Many
datasets expose only one configuration, in which case
|
split |
Character string. The dataset split to cache, such as
|
revision |
Character string. A specific commit hash to pin the
download to, instead of using the latest revision of the
|
... |
Additional arguments, reserved for future use. |
Files are downloaded from the
refs/convert/parquet branch maintained by Hugging Face, not
from the dataset's default branch. Large datasets may be split
across multiple Parquet shards; all matching shards for the
requested split are downloaded and cached.
A named list with one entry per cached split. Each
entry is a character vector of local file paths to the cached
Parquet shard(s) for that split (a single path if the split has
one shard, multiple paths if it was sharded across several
files).
https://huggingface.co/docs/datasets-server/en/parquet
cache_parquet( repo_id = "scikit-learn/iris", config = "default" ) cache_parquet( repo_id = "fancyzhx/amazon_polarity", config = "amazon_polarity", split = "train" )cache_parquet( repo_id = "scikit-learn/iris", config = "default" ) cache_parquet( repo_id = "fancyzhx/amazon_polarity", config = "amazon_polarity", split = "train" )
it helps in downloading the description
download_desc(repo_id, revision = "main", ..., files_metadata = FALSE)download_desc(repo_id, revision = "main", ..., files_metadata = FALSE)
repo_id |
repo id of the dataset |
revision |
revision of the dataset, default is "main" |
... |
additional arguments passed to httr::GET |
files_metadata |
whether to include files metadata, default is FALSE |
list containing description and downloads of the dataset
download_desc("scikit-learn/iris")download_desc("scikit-learn/iris")
A single call to /size per dataset is enough. First the configs[]
array is checked: if the dataset does not have exactly one config,
nrows/ncols are both NA. If it does have exactly one config, nrows is
computed by summing num_rows across all of that config's entries in
the splits[] array (rather than trusting the config-level aggregate
directly), and ncols is read from num_columns on one of those
splits (column count doesn't vary across splits of the same config).
fetch_dataset_meta(ids, max_concurrent = 10, timeout = 30)fetch_dataset_meta(ids, max_concurrent = 10, timeout = 30)
ids |
Character vector of dataset ids, e.g. "google/dataset-one". |
max_concurrent |
Integer. Max simultaneous connections in the pool. |
timeout |
Numeric. Per-request timeout in seconds. |
If a dataset has zero or more than one config, both nrows and ncols are set to NA – there's no single unambiguous row/column count to report in that case.
A data.table with columns: id, nrows, ncols.
An R6::R6Class that wraps a dataset hosted on the Hugging Face Hub and
exposes it as an mlr3 compatible data backend / task. Data is loaded
lazily: no download or parsing happens until fields that require access
to the underlying data (e.g. data, nrow, ncol, colnames) are
accessed for the first time. Results are then cached for the lifetime of
the object.
A dataset can be identified either by a config (one of the dataset's
predefined configurations, loaded via its parquet export) or by a single
file_name within the repository. Exactly one of the two must be
supplied before data can be retrieved.
All active bindings are read-only; attempting to assign to them raises an error. See the Fields section below for details on each one.
Note on clone(): like every R6 object, HFData objects have an
inherited clone(deep = FALSE) method for copying the object (deep
controls whether nested R6 fields are also cloned). It is not
re-documented here since it isn't defined in this class's source, but
behaves exactly as described in R6::R6Class.
repo_id(character(1))
The Hugging Face repository id, e.g. "user/dataset". Read-only.
config(character(1) | NULL)
The dataset configuration name, if specified. Read-only.
file_name(character(1) | NULL)
The specific file name within the repository, if specified. Read-only.
split(character() | NULL)
The dataset split (e.g. "train", "test"), if specified. Read-only.
target(character() | NULL)
The name of the target column. Read-only.
desc(list())
The repository description/metadata, downloaded from the Hub on first access. Read-only.
storage(character(1))
Human-readable size of the storage used by the whole repository (not a single file/config). Read-only.
data(data.frame())
The full dataset as a data frame. Triggers backend creation on first access. Read-only.
nrow(integer(1))
Number of rows in the dataset. Read-only.
ncol(integer(1))
Number of columns in the dataset. Read-only.
colnames(character())
Column names of the dataset. Read-only.
siblings(character())
File names of all sibling files in the repository. Read-only.
repo_link(character(1))
URL to the dataset page on the Hugging Face Hub. Read-only.
configs(character())
Available dataset configurations, queried from the datasets-server API. Read-only.
splits(character())
Available splits for the selected configuration. Read-only.
coltypes(character())
Named vector of the (first) class of each column. Read-only.
task_type(character())
One of "auto", "classif", or "regr". Read-only.
feature_names(character())
Column names excluding the target column. Read-only.
new()
Create a new HFData object.
HFData$new(
repo_id,
config = NULL,
file_name = NULL,
split = NULL,
target = NULL,
primary_key = NULL,
task_type = c("auto", "classif", "regr"),
...
)repo_id(character(1)) Repository id on the Hugging Face Hub.
config(character(1)|NULL) Dataset configuration name. Defaults to NULL, meaning no configuration is selected.
file_name(character(1)|NULL) Specific file name within the repository. Defaults to NULL, meaning no configuration is selected.
split(character()|NULL) Dataset split to load. Defaults to NULL, meaning no configuration is selected.
target(character()) Name of the target column. Defaults to NULL, meaning no configuration is selected.
primary_key(character(1)|NULL) Name of the primary key column. Defaults to NULL, meaning no configuration is selected.
task_type(character(1)|NULL) One of "auto", "classif", "regr".
...Additional arguments, stored for later use.
A new HFData object.
print()
Print a short summary of the HFData object, including its
dimensions, target column, and repository storage size.
HFData$print()
self, invisibly (called for its side effect).
clone()
The objects of this class are cloneable with this method.
HFData$clone(deep = FALSE)
deepWhether to make a deep clone.
Creates an HFData object for a dataset hosted on the Hugging Face Hub.
The returned object can be converted to an mlr3 task using
mlr3::as_task() or accessed through its active bindings.
hfdt( repo_id, config = NULL, file_name = NULL, target = NULL, primary_key = NULL, split = NULL, revision = NULL, task_type = c("auto", "classif", "regr"), ... )hfdt( repo_id, config = NULL, file_name = NULL, target = NULL, primary_key = NULL, split = NULL, revision = NULL, task_type = c("auto", "classif", "regr"), ... )
repo_id |
( |
config |
( |
file_name |
( |
target |
( |
primary_key |
( |
split |
( |
revision |
( |
task_type |
( |
... |
Additional arguments passed to |
An HFData object.
Query datasets hosted on https://huggingface.co/datasets via the Hugging Face Hub API, returning metadata such as dataset id, gated status, and download counts.
list_datasets( num_of_dataset = 100, chunk_size = 100, search = NULL, author = NULL, fetch_meta = TRUE, max_concurrent = 10, ... )list_datasets( num_of_dataset = 100, chunk_size = 100, search = NULL, author = NULL, fetch_meta = TRUE, max_concurrent = 10, ... )
num_of_dataset |
( |
chunk_size |
( |
search |
( |
author |
( |
fetch_meta |
( |
max_concurrent |
( |
... |
Additional arguments passed on to |
(data.table()) of results with columns id, nrows, and
ncols or an empty data.table() if no datasets are found.
https://huggingface.co/docs/hub/api
## Not run: dat <- list_datasets(num_of_dataset = 50) dat <- list_datasets(num_of_dataset = 500, chunk_size = 100) dat <- list_datasets(author = "a4n9i") ## End(Not run)## Not run: dat <- list_datasets(num_of_dataset = 50) dat <- list_datasets(num_of_dataset = 500, chunk_size = 100) dat <- list_datasets(author = "a4n9i") ## End(Not run)
Provides integration between Hugging Face datasets and the mlr3 ecosystem. Datasets can be downloaded from the Hugging Face Hub and converted directly into mlr3 Tasks.
The package registers the "hf" task in
mlr3::mlr_tasks.
task = tsk( "hf", repo_id = "scikit-learn/iris", config = "default", target = "species" )
Alternatively, users can create an HFData object and convert it
using as_task().
Downloaded datasets are cached locally to avoid repeated downloads.
See the package vignettes for examples of loading datasets, converting them to mlr3 tasks, and working with large datasets.
Maintainer: Sebastian Fischer [email protected]
Authors:
Toby Hocking [email protected]
Anjani Nandan [email protected]
Useful links:
Report bugs at https://github.com/mlr-org/mlr3hf/issues