from collections import defaultdict import os import json import csv import datasets _DESCRIPTION = """ A large-scale speech corpus for representation learning, semi-supervised learning and interpretation. """ _CITATION = """ @inproceedings{} """ _HOMEPAGE = "" _LICENSE = "" _ASR_LANGUAGES = [ "hy" ] _ASR_ACCENTED_LANGUAGES = [ "" ] _LANGUAGES = _ASR_LANGUAGES + _ASR_ACCENTED_LANGUAGES _BASE_DATA_DIR = "data/" _N_SHARDS_FILE = _BASE_DATA_DIR + "n_files.json" _AUDIO_ARCHIVE_PATH = _BASE_DATA_DIR + "{split}/{split}_dataset.tar.gz" _METADATA_PATH = _BASE_DATA_DIR + "{split}.tsv" class HySpeech(datasets.GeneratorBasedBuilder): """The VoxPopuli dataset.""" VERSION = datasets.Version("1.1.0") # TODO: version DEFAULT_WRITER_BATCH_SIZE = 256 def _info(self): features = datasets.Features( { "audio_id": datasets.Value("string"), "language": datasets.ClassLabel(names=_LANGUAGES), "audio": datasets.Audio(sampling_rate=16_000), "raw_text": datasets.Value("string"), "normalized_text": datasets.Value("string"), "gender": datasets.Value("string"), # TODO: ClassVar? "speaker_id": datasets.Value("string"), "is_gold_transcript": datasets.Value("bool"), "accent": datasets.Value("string"), } ) return datasets.DatasetInfo( description=_DESCRIPTION, features=features, homepage=_HOMEPAGE, license=_LICENSE, citation=_CITATION, ) def _split_generators(self, dl_manager): # Define paths to your train, dev, and test data data_dir = "data/" train_data_dir = os.path.join(data_dir, "train") dev_data_dir = os.path.join(data_dir, "dev") test_data_dir = os.path.join(data_dir, "test") # Load metadata files for train, dev, and test train_metadata_path = os.path.join(data_dir, "train.tsv") dev_metadata_path = os.path.join(data_dir, "dev.tsv") test_metadata_path = os.path.join(data_dir, "test.tsv") # Yield split generators for train, dev, and test return [ datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"data_dir": train_data_dir, "metadata_path": train_metadata_path}), datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"data_dir": dev_data_dir, "metadata_path": dev_metadata_path}), datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"data_dir": test_data_dir, "metadata_path": test_metadata_path}), ] def _generate_examples(self, data_dir, metadata_path): # Load metadata from TSV file with open(metadata_path, "r") as f: metadata = csv.DictReader(f, delimiter="\t") # Iterate over metadata to yield examples for row in metadata: audio_id = row["audio_id"] audio_path = os.path.join(data_dir, row["audio_path"]) # Adjust column name accordingly # Load audio file and yield example with open(audio_path, "rb") as audio_file: yield audio_id, { "audio_id": audio_id, "language": row["language"], # Adjust column name accordingly "audio": {"path": audio_path, "bytes": audio_file.read()}, # Add other metadata fields as needed }