Builder classes
Last updated
Last updated
🤗 Datasets relies on two main classes during the dataset building process: and .
( cache_dir: typing.Optional[str] = Nonedataset_name: typing.Optional[str] = Noneconfig_name: typing.Optional[str] = Nonehash: typing.Optional[str] = Nonebase_path: typing.Optional[str] = Noneinfo: typing.Optional[datasets.info.DatasetInfo] = Nonefeatures: typing.Optional[datasets.features.features.Features] = Nonetoken: typing.Union[bool, str, NoneType] = Noneuse_auth_token = 'deprecated'repo_id: typing.Optional[str] = Nonedata_files: typing.Union[str, list, dict, datasets.data_files.DataFilesDict, NoneType] = Nonedata_dir: typing.Optional[str] = Nonestorage_options: typing.Optional[dict] = Nonewriter_batch_size: typing.Optional[int] = Nonename = 'deprecated'**config_kwargs )
Parameters
cache_dir (str
, optional) — Directory to cache data. Defaults to "~/.cache/huggingface/datasets"
.
dataset_name (str
, optional) — Name of the dataset, if different from the builder name. Useful for packaged builders like csv, imagefolder, audiofolder, etc. to reflect the difference between datasets that use the same packaged builder.
config_name (str
, optional) — Name of the dataset configuration. It affects the data generated on disk. Different configurations will have their own subdirectories and versions. If not provided, the default configuration is used (if it exists).
Added in 2.3.0
Parameter name
was renamed to config_name
.
hash (str
, optional) — Hash specific to the dataset code. Used to update the caching directory when the dataset loading script code is updated (to avoid reusing old data). The typical caching directory (defined in self._relative_data_dir
) is name/version/hash/
.
base_path (str
, optional) — Base path for relative paths that are used to download files. This can be a remote URL.
features (, optional) — Features types to use with this dataset. It can be used to change the types of a dataset, for example.
token (str
or bool
, optional) — String or boolean to use as Bearer token for remote files on the Datasets Hub. If True
, will get token from "~/.huggingface"
.
repo_id (str
, optional) — ID of the dataset repository. Used to distinguish builders with the same name but not coming from the same namespace, for example “squad” and “lhoestq/squad” repo IDs. In the latter, the builder name would be “lhoestq___squad”.
data_files (str
or Sequence
or Mapping
, optional) — Path(s) to source data file(s). For builders like “csv” or “json” that need the user to specify data files. They can be either local or remote files. For convenience, you can use a DataFilesDict
.
data_dir (str
, optional) — Path to directory containing source data file(s). Use only if data_files
is not passed, in which case it is equivalent to passing os.path.join(data_dir, "**")
as data_files
. For builders that require manual download, it must be the path to the local directory containing the manually downloaded data.
storage_options (dict
, optional) — Key/value pairs to be passed on to the dataset file-system backend, if any.
writer_batch_size (int
, optional) — Batch size used by the ArrowWriter. It defines the number of samples that are kept in memory before writing them and also the length of the arrow chunks. None means that the ArrowWriter will use its default value.
name (str
) — Configuration name for the dataset.
Deprecated in 2.3.0
Use config_name
instead.
**config_kwargs (additional keyword arguments) — Keyword arguments to be passed to the corresponding builder configuration class, set on the class attribute . The builder configuration class is or a subclass of it.
Abstract base class for all datasets.
DatasetBuilder
has 3 key methods:
DatasetBuilder.info
: Documents the dataset, including feature names, types, shapes, version, splits, citation, etc.
as_dataset
( split: typing.Optional[datasets.splits.Split] = Nonerun_post_process = Trueverification_mode: typing.Union[datasets.utils.info_utils.VerificationMode, str, NoneType] = Noneignore_verifications = 'deprecated'in_memory = False )
Parameters
split (datasets.Split
) — Which subset of the data to return.
run_post_process (bool
, defaults to True
) — Whether to run post-processing dataset transforms and/or add indexes.
Added in 2.9.1
ignore_verifications (bool
, defaults to False
) — Whether to ignore the verifications of the downloaded/processed dataset information (checksums/size/splits/…).
Deprecated in 2.9.1
ignore_verifications
was deprecated in version 2.9.1 and will be removed in 3.0.0. Please use verification_mode
instead.
in_memory (bool
, defaults to False
) — Whether to copy the data in-memory.
Return a Dataset for the specified split.
Example:
Copied
download_and_prepare
( output_dir: typing.Optional[str] = Nonedownload_config: typing.Optional[datasets.download.download_config.DownloadConfig] = Nonedownload_mode: typing.Union[datasets.download.download_manager.DownloadMode, str, NoneType] = Noneverification_mode: typing.Union[datasets.utils.info_utils.VerificationMode, str, NoneType] = Noneignore_verifications = 'deprecated'try_from_hf_gcs: bool = Truedl_manager: typing.Optional[datasets.download.download_manager.DownloadManager] = Nonebase_path: typing.Optional[str] = Noneuse_auth_token = 'deprecated'file_format: str = 'arrow'max_shard_size: typing.Union[str, int, NoneType] = Nonenum_proc: typing.Optional[int] = Nonestorage_options: typing.Optional[dict] = None**download_and_prepare_kwargs )
Parameters
output_dir (str
, optional) — Output directory for the dataset. Default to this builder’s cache_dir
, which is inside ~/.cache/huggingface/datasets
by default.
Added in 2.5.0
download_config (DownloadConfig
, optional) — Specific download configuration parameters.
Added in 2.9.1
ignore_verifications (bool
, defaults to False
) — Ignore the verifications of the downloaded/processed dataset information (checksums/size/splits/…).
Deprecated in 2.9.1
ignore_verifications
was deprecated in version 2.9.1 and will be removed in 3.0.0. Please use verification_mode
instead.
try_from_hf_gcs (bool
) — If True
, it will try to download the already prepared dataset from the HF Google cloud storage.
dl_manager (DownloadManager
, optional) — Specific DownloadManger
to use.
base_path (str
, optional) — Base path for relative paths that are used to download files. This can be a remote url. If not specified, the value of the base_path
attribute (self.base_path
) will be used instead.
use_auth_token (Union[str, bool]
, optional) — Optional string or boolean to use as Bearer token for remote files on the Datasets Hub. If True, or not specified, will get token from ~/.huggingface.
Deprecated in 2.7.1
Pass use_auth_token
to load_dataset_builder
instead.
file_format (str
, optional) — Format of the data files in which the dataset will be written. Supported formats: “arrow”, “parquet”. Default to “arrow” format. If the format is “parquet”, then image and audio data are embedded into the Parquet files instead of pointing to local files.
Added in 2.5.0
max_shard_size (Union[str, int]
, optional) — Maximum number of bytes written per shard, default is “500MB”. The size is based on uncompressed data size, so in practice your shard files may be smaller than max_shard_size
thanks to Parquet compression for example.
Added in 2.5.0
num_proc (int
, optional, defaults to None
) — Number of processes when downloading and generating the dataset locally. Multiprocessing is disabled by default.
Added in 2.7.0
storage_options (dict
, optional) — Key/value pairs to be passed on to the caching file-system backend, if any.
Added in 2.5.0
**download_and_prepare_kwargs (additional keyword arguments) — Keyword arguments.
Downloads and prepares dataset for reading.
Example:
Download and prepare the dataset as Arrow files that can be loaded as a Dataset using builder.as_dataset()
:
Copied
Download and prepare the dataset as sharded Parquet files locally:
Copied
Download and prepare the dataset as sharded Parquet files in a cloud storage:
Copied
get_all_exported_dataset_infos
( )
Empty dict if doesn’t exist
Example:
Copied
get_exported_dataset_info
( )
Empty DatasetInfo
if doesn’t exist
Example:
Copied
get_imported_module_dir
( )
Return the path of the module of this class or subclass.
( cache_dir: typing.Optional[str] = Nonedataset_name: typing.Optional[str] = Noneconfig_name: typing.Optional[str] = Nonehash: typing.Optional[str] = Nonebase_path: typing.Optional[str] = Noneinfo: typing.Optional[datasets.info.DatasetInfo] = Nonefeatures: typing.Optional[datasets.features.features.Features] = Nonetoken: typing.Union[bool, str, NoneType] = Noneuse_auth_token = 'deprecated'repo_id: typing.Optional[str] = Nonedata_files: typing.Union[str, list, dict, datasets.data_files.DataFilesDict, NoneType] = Nonedata_dir: typing.Optional[str] = Nonestorage_options: typing.Optional[dict] = Nonewriter_batch_size: typing.Optional[int] = Nonename = 'deprecated'**config_kwargs )
Base class for datasets with data generation based on dict generators.
GeneratorBasedBuilder
is a convenience class that abstracts away much of the data writing and reading of DatasetBuilder
. It expects subclasses to implement generators of feature dictionaries across the dataset splits (_split_generators
). See the method docstrings for details.
( *argsbeam_runner = Nonebeam_options = None**kwargs )
Beam-based Builder.
( cache_dir: typing.Optional[str] = Nonedataset_name: typing.Optional[str] = Noneconfig_name: typing.Optional[str] = Nonehash: typing.Optional[str] = Nonebase_path: typing.Optional[str] = Noneinfo: typing.Optional[datasets.info.DatasetInfo] = Nonefeatures: typing.Optional[datasets.features.features.Features] = Nonetoken: typing.Union[bool, str, NoneType] = Noneuse_auth_token = 'deprecated'repo_id: typing.Optional[str] = Nonedata_files: typing.Union[str, list, dict, datasets.data_files.DataFilesDict, NoneType] = Nonedata_dir: typing.Optional[str] = Nonestorage_options: typing.Optional[dict] = Nonewriter_batch_size: typing.Optional[int] = Nonename = 'deprecated'**config_kwargs )
Base class for datasets with data generation based on Arrow loading functions (CSV/JSON/Parquet).
( name: str = 'default'version: typing.Union[str, datasets.utils.version.Version, NoneType] = 0.0.0data_dir: typing.Optional[str] = Nonedata_files: typing.Optional[datasets.data_files.DataFilesDict] = Nonedescription: typing.Optional[str] = None )
Parameters
name (str
, defaults to default
) — The name of the configuration.
version (Version
or str
, defaults to 0.0.0
) — The version of the configuration.
data_dir (str
, optional) — Path to the directory containing the source data.
data_files (str
or Sequence
or Mapping
, optional) — Path(s) to source data file(s).
description (str
, optional) — A human description of the configuration.
Base class for DatasetBuilder
data configuration.
DatasetBuilder
subclasses with data configuration options should subclass BuilderConfig
and add their own properties.
create_config_id
( config_kwargs: dictcustom_features: typing.Optional[datasets.features.features.Features] = None )
The config id is used to build the cache directory. By default it is equal to the config name. However the name of a config is not sufficient to have a unique identifier for the dataset being generated since it doesn’t take into account:
the config kwargs that can be used to overwrite attributes
the custom features used to write the dataset
the data_files for json/text/csv/pandas datasets
Therefore the config id is just the config name with an optional suffix based on these.
( dataset_name: typing.Optional[str] = Nonedata_dir: typing.Optional[str] = Nonedownload_config: typing.Optional[datasets.download.download_config.DownloadConfig] = Nonebase_path: typing.Optional[str] = Nonerecord_checksums = True )
download
( url_or_urls ) → str
or list
or dict
Parameters
url_or_urls (str
or list
or dict
) — URL or list
or dict
of URLs to download. Each URL is a str
.
Returns
str
or list
or dict
The downloaded paths matching the given input url_or_urls
.
Download given URL(s).
By default, only one process is used for download. Pass customized download_config.num_proc
to change this behavior.
Example:
Copied
download_and_extract
( url_or_urls ) → extracted_path(s)
Parameters
url_or_urls (str
or list
or dict
) — URL or list
or dict
of URLs to download and extract. Each URL is a str
.
Returns
extracted_path(s)
str
, extracted paths of given URL(s).
Download and extract given url_or_urls
.
Is roughly equivalent to:
Copied
download_custom
( url_or_urlscustom_download ) → downloaded_path(s)
Parameters
url_or_urls (str
or list
or dict
) — URL or list
or dict
of URLs to download and extract. Each URL is a str
.
custom_download (Callable[src_url, dst_path]
) — The source URL and destination path. For example tf.io.gfile.copy
, that lets you download from Google storage.
Returns
downloaded_path(s)
str
, The downloaded paths matching the given input url_or_urls
.
Download given urls(s) by calling custom_download
.
Example:
Copied
extract
( path_or_pathsnum_proc = 'deprecated' ) → extracted_path(s)
Parameters
path_or_paths (path or list
or dict
) — Path of file to extract. Each path is a str
.
num_proc (int
) — Use multi-processing if num_proc
> 1 and the length of path_or_paths
is larger than num_proc
.
Deprecated in 2.6.2
Pass DownloadConfig(num_proc=<num_proc>)
to the initializer instead.
Returns
extracted_path(s)
str
, The extracted paths matching the given input path_or_paths.
Extract given path(s).
Example:
Copied
iter_archive
( path_or_buf: typing.Union[str, _io.BufferedReader] ) → tuple[str, io.BufferedReader]
Parameters
path_or_buf (str
or io.BufferedReader
) — Archive path or archive binary file object.
Yields
tuple[str, io.BufferedReader]
Iterate over files within an archive.
Example:
Copied
iter_files
( paths: typing.Union[str, typing.List[str]] ) → str
Parameters
paths (str
or list
of str
) — Root paths.
Yields
str
Iterate over file paths.
Example:
Copied
ship_files_with_pipeline
( downloaded_path_or_pathspipeline )
Parameters
downloaded_path_or_paths (str
or list[str]
or dict[str, str]
) — Nested structure containing the downloaded path(s).
pipeline (utils.beam_utils.BeamPipeline
) — Apache Beam Pipeline.
Ship the files using Beam FileSystems to the pipeline temp dir.
( dataset_name: typing.Optional[str] = Nonedata_dir: typing.Optional[str] = Nonedownload_config: typing.Optional[datasets.download.download_config.DownloadConfig] = Nonebase_path: typing.Optional[str] = None )
Download manager that uses the ”::” separator to navigate through (possibly remote) compressed archives. Contrary to the regular DownloadManager
, the download
and extract
methods don’t actually download nor extract data, but they rather return the path or url that could be opened using the xopen
function which extends the built-in open
function to stream data from remote files.
download
( url_or_urls ) → url(s)
Parameters
url_or_urls (str
or list
or dict
) — URL(s) of files to stream data from. Each url is a str
.
Returns
url(s)
(str
or list
or dict
), URL(s) to stream data from matching the given input url_or_urls.
Normalize URL(s) of files to stream data from. This is the lazy version of DownloadManager.download
for streaming.
Example:
Copied
download_and_extract
( url_or_urls ) → url(s)
Parameters
url_or_urls (str
or list
or dict
) — URL(s) to stream from data from. Each url is a str
.
Returns
url(s)
(str
or list
or dict
), URL(s) to stream data from matching the given input url_or_urls
.
Prepare given url_or_urls
for streaming (add extraction protocol).
This is the lazy version of DownloadManager.download_and_extract
for streaming.
Is equivalent to:
Copied
extract
( url_or_urls ) → url(s)
Parameters
url_or_urls (str
or list
or dict
) — URL(s) of files to stream data from. Each url is a str
.
Returns
url(s)
(str
or list
or dict
), URL(s) to stream data from matching the given input url_or_urls
.
Add extraction protocol for given url(s) for streaming.
This is the lazy version of DownloadManager.extract
for streaming.
Example:
Copied
iter_archive
( urlpath_or_buf: typing.Union[str, _io.BufferedReader] ) → tuple[str, io.BufferedReader]
Parameters
urlpath_or_buf (str
or io.BufferedReader
) — Archive path or archive binary file object.
Yields
tuple[str, io.BufferedReader]
Iterate over files within an archive.
Example:
Copied
iter_files
( urlpaths: typing.Union[str, typing.List[str]] ) → str
Parameters
urlpaths (str
or list
of str
) — Root paths.
Yields
str
Iterate over files.
Example:
Copied
( cache_dir: typing.Union[str, pathlib.Path, NoneType] = Noneforce_download: bool = Falseresume_download: bool = Falselocal_files_only: bool = Falseproxies: typing.Optional[typing.Dict] = Noneuser_agent: typing.Optional[str] = Noneextract_compressed_file: bool = Falseforce_extract: bool = Falsedelete_extracted: bool = Falseuse_etag: bool = Truenum_proc: typing.Optional[int] = Nonemax_retries: int = 1token: typing.Union[str, bool, NoneType] = Noneuse_auth_token: dataclasses.InitVar[typing.Union[str, bool, NoneType]] = 'deprecated'ignore_url_params: bool = Falsestorage_options: typing.Dict[str, typing.Any] = <factory>download_desc: typing.Optional[str] = None )
Parameters
cache_dir (str
or Path
, optional) — Specify a cache directory to save the file to (overwrite the default cache dir).
force_download (bool
, defaults to False
) — If True
, re-dowload the file even if it’s already cached in the cache dir.
resume_download (bool
, defaults to False
) — If True
, resume the download if an incompletely received file is found.
proxies (dict
, optional) —
user_agent (str
, optional) — Optional string or dict that will be appended to the user-agent on remote requests.
extract_compressed_file (bool
, defaults to False
) — If True
and the path point to a zip or tar file, extract the compressed file in a folder along the archive.
force_extract (bool
, defaults to False
) — If True
when extract_compressed_file
is True
and the archive was already extracted, re-extract the archive and override the folder where it was extracted.
delete_extracted (bool
, defaults to False
) — Whether to delete (or keep) the extracted files.
use_etag (bool
, defaults to True
) — Whether to use the ETag HTTP response header to validate the cached files.
num_proc (int
, optional) — The number of processes to launch to download the files in parallel.
max_retries (int
, default to 1
) — The number of times to retry an HTTP request if it fails.
token (str
or bool
, optional) — Optional string or boolean to use as Bearer token for remote files on the Datasets Hub. If True
, or not specified, will get token from ~/.huggingface
.
use_auth_token (str
or bool
, optional) — Optional string or boolean to use as Bearer token for remote files on the Datasets Hub. If True
, or not specified, will get token from ~/.huggingface
.
Deprecated in 2.14.0
use_auth_token
was deprecated in favor of token
in version 2.14.0 and will be removed in 3.0.0.
ignore_url_params (bool
, defaults to False
) — Whether to strip all query parameters and fragments from the download URL before using it for caching the file.
storage_options (dict
, optional) — Key/value pairs to be passed on to the dataset file-system backend, if any.
download_desc (str
, optional) — A description to be displayed alongside with the progress bar while downloading the files.
Configuration for our cached path manager.
( valuenames = Nonemodule = Nonequalname = Nonetype = Nonestart = 1 )
Enum
for how to treat pre-existing downloads and data.
The default mode is REUSE_DATASET_IF_EXISTS
, which will reuse both raw downloads and the prepared dataset if they exist.
The generations modes:
REUSE_DATASET_IF_EXISTS
(default)
Reuse
Reuse
REUSE_CACHE_IF_EXISTS
Reuse
Fresh
FORCE_REDOWNLOAD
Fresh
Fresh
( valuenames = Nonemodule = Nonequalname = Nonetype = Nonestart = 1 )
Enum
that specifies which verification checks to run.
The default mode is BASIC_CHECKS
, which will perform only rudimentary checks to avoid slowdowns when generating/downloading a dataset for the first time.
The verification modes:
ALL_CHECKS
Split checks, uniqueness of the keys yielded in case of the GeneratorBuilder
and the validity (number of files, checksums, etc.) of downloaded files
BASIC_CHECKS
(default)
Same as ALL_CHECKS
but without checking downloaded files
NO_CHECKS
None
( name: strgen_kwargs: typing.Dict = <factory> )
Parameters
name (str
) — Name of the Split
for which the generator will create the examples.
**gen_kwargs (additional keyword arguments) — Keyword arguments to forward to the DatasetBuilder._generate_examples
method of the builder.
Defines the split information for the generator.
This should be used as returned value of GeneratorBasedBuilder._split_generators
. See GeneratorBasedBuilder._split_generators
for more info and example of usage.
Example:
Copied
( name )
Enum
for dataset splits.
Datasets are typically split into different subsets to be used at various stages of training and evaluation.
TRAIN
: the training data.
VALIDATION
: the validation data. If present, this is typically used as evaluation data while iterating on a model (e.g. changing hyperparameters, model architecture, etc.).
TEST
: the testing data. This is the data to report metrics on. Typically you do not want to use this during model iteration as you may overfit to it.
ALL
: the union of all defined dataset splits.
All splits, including compositions inherit from datasets.SplitBase
.
Example:
Copied
( name )
Descriptor corresponding to a named split (train, test, …).
Example:
Each descriptor can be composed with other using addition or slice:
Copied
The resulting split will correspond to 25% of the train split merged with 100% of the test split.
A split cannot be added twice, so the following will fail:
Copied
The slices can be applied only one time. So the following are valid:
Copied
But this is not valid:
Copied
( )
Split corresponding to the union of all defined dataset splits.
( split_namerounding = Nonefrom_ = Noneto = Noneunit = None )
Reading instruction for a dataset.
Examples:
Copied
from_spec
( spec )
Parameters
spec (str
) — Split(s) + optional slice(s) to read + optional rounding if percents are used as the slicing unit. A slice can be specified, using absolute numbers (int
) or percentages (int
).
Creates a ReadInstruction
instance out of a string spec.
Examples:
Copied
to_absolute
( name2len )
Parameters
name2len (dict
) — Associating split names to number of examples.
Translate instruction into a list of absolute instructions.
Those absolute instructions are then to be added together.
( version_str: strdescription: typing.Optional[str] = Nonemajor: typing.Union[str, int, NoneType] = Noneminor: typing.Union[str, int, NoneType] = Nonepatch: typing.Union[str, int, NoneType] = None )
Parameters
version_str (str
) — The dataset version.
description (str
) — A description of what is new in this version.
major (str
) —
minor (str
) —
patch (str
) —
Dataset version MAJOR.MINOR.PATCH
.
Example:
Copied
: Downloads the source data and writes it to disk.
: Generates a .
Some DatasetBuilder
s expose multiple variants of the dataset by defining a subclass and accepting a config object (or name) on construction. Configurable datasets expose a pre-defined set of configurations in DatasetBuilder.builder_configs()
.
verification_mode ( or str
, defaults to BASIC_CHECKS
) — Verification mode determining the checks to run on the downloaded/processed dataset information (checksums/size/splits/…).
download_mode ( or str
, optional) — Select the download/generate mode, default to REUSE_DATASET_IF_EXISTS
.
verification_mode ( or str
, defaults to BASIC_CHECKS
) — Verification mode determining the checks to run on the downloaded/processed dataset information (checksums/size/splits/…).
See the on splits for more information.