from collections import defaultdict import os import json import csv import datasets _NAME="prueba" _VERSION="1.0.0" _DESCRIPTION = """ An extremely small corpus of 40 audio files taken from Common Voice (es) with the objective of testing how to share datasets in Hugging Face. """ _CITATION = """ @misc{toy_corpus_asr_es, title={Toy Corpus for ASR in Spanish.}, author={Hernandez Mena, Carlos Daniel}, year={2022}, url={https://huggingface.co/datasets/carlosdanielhernandezmena/toy_corpus_asr_es}, } """ _HOMEPAGE = "https://huggingface.co/datasets/carlosdanielhernandezmena/toy_corpus_asr_es" _LICENSE = "CC-BY-4.0, See https://creativecommons.org/licenses/by/4.0/" _BASE_DATA_DIR = "corpus/" _METADATA_TRAIN = os.path.join(_BASE_DATA_DIR,"files","metadata_train.tsv") _METADATA_TEST = os.path.join(_BASE_DATA_DIR,"files", "metadata_test.tsv") _METADATA_DEV = os.path.join(_BASE_DATA_DIR,"files", "metadata_dev.tsv") _TARS_TRAIN = os.path.join(_BASE_DATA_DIR,"files","tars_train.paths") _TARS_TEST = os.path.join(_BASE_DATA_DIR,"files", "tars_test.paths") _TARS_DEV = os.path.join(_BASE_DATA_DIR,"files", "tars_dev.paths") class ToyCorpusAsrEsConfig(datasets.BuilderConfig): """BuilderConfig for Toy Corpus ASR ES.""" def __init__(self, name, **kwargs): name=_NAME super().__init__(name=name, **kwargs) class ToyCorpusAsrEs(datasets.GeneratorBasedBuilder): """The Toy Corpus ASR ES dataset.""" VERSION = datasets.Version(_VERSION) BUILDER_CONFIGS = [ ToyCorpusAsrEsConfig( name=_NAME, version=datasets.Version(_VERSION), ) ] def _info(self): features = datasets.Features( { "audio_id": datasets.Value("string"), "audio": datasets.Audio(sampling_rate=16000), "split": datasets.Value("string"), "gender": datasets.Value("string"), "normalized_text": datasets.Value("string"), "relative_path": datasets.Value("string"), } ) return datasets.DatasetInfo( description=_DESCRIPTION, features=features, homepage=_HOMEPAGE, license=_LICENSE, citation=_CITATION, ) def _split_generators(self, dl_manager): metadata_train=dl_manager.download_and_extract(_METADATA_TRAIN) metadata_test=dl_manager.download_and_extract(_METADATA_TEST) metadata_dev=dl_manager.download_and_extract(_METADATA_DEV) tars_train=dl_manager.download_and_extract(_TARS_TRAIN) tars_test=dl_manager.download_and_extract(_TARS_TEST) tars_dev=dl_manager.download_and_extract(_TARS_DEV) hash_tar_files=defaultdict(dict) with open(tars_train,'r') as f: hash_tar_files['train']=[path.replace('\n','') for path in f] with open(tars_test,'r') as f: hash_tar_files['test']=[path.replace('\n','') for path in f] with open(tars_dev,'r') as f: hash_tar_files['dev']=[path.replace('\n','') for path in f] hash_meta_paths={"train":metadata_train,"test":metadata_test,"dev":metadata_dev} audio_paths = dl_manager.download(hash_tar_files) splits=["train","dev","test"] local_extracted_audio_paths = ( dl_manager.extract(audio_paths) if not dl_manager.is_streaming else { split:[None] * len(audio_paths[split]) for split in splits } ) return [ datasets.SplitGenerator( name=datasets.Split.TRAIN, gen_kwargs={ "audio_archives":[dl_manager.iter_archive(archive) for archive in audio_paths["train"]], "local_extracted_archives_paths": local_extracted_audio_paths["train"], "metadata_paths": hash_meta_paths["train"], } ), datasets.SplitGenerator( name=datasets.Split.VALIDATION, gen_kwargs={ "audio_archives": [dl_manager.iter_archive(archive) for archive in audio_paths["dev"]], "local_extracted_archives_paths": local_extracted_audio_paths["dev"], "metadata_paths": hash_meta_paths["dev"], } ), datasets.SplitGenerator( name=datasets.Split.TEST, gen_kwargs={ "audio_archives": [dl_manager.iter_archive(archive) for archive in audio_paths["test"]], "local_extracted_archives_paths": local_extracted_audio_paths["test"], "metadata_paths": hash_meta_paths["test"], } ), ] def _generate_examples(self, audio_archives, local_extracted_archives_paths, metadata_paths): features = ["normalized_text","gender","split","relative_path"] with open(metadata_paths) as f: metadata = {x["audio_id"]: x for x in csv.DictReader(f, delimiter="\t")} for audio_archive, local_extracted_archive_path in zip(audio_archives, local_extracted_archives_paths): for audio_filename, audio_file in audio_archive: audio_id =os.path.splitext(os.path.basename(audio_filename))[0] path = os.path.join(local_extracted_archive_path, audio_filename) if local_extracted_archive_path else audio_filename yield audio_id, { "audio_id": audio_id, **{feature: metadata[audio_id][feature] for feature in features}, "audio": {"path": path, "bytes": audio_file.read()}, }