import csv import os import datasets logger = datasets.logging.get_logger(__name__) _DESCRIPTION = "Custom dataset for extracting audio files and matching sentences." _DATA_URL = "https://huggingface.co/datasets/ugshanyu/dataset/resolve/main" # Replace with the URL of your data class CustomDataset(datasets.GeneratorBasedBuilder): VERSION = datasets.Version("1.0.0") def _info(self): features = datasets.Features( { "audio": datasets.Audio(sampling_rate=48_000), "sentence": datasets.Value("string"), } ) return datasets.DatasetInfo( description=_DESCRIPTION, features=features, supervised_keys=("audio", "sentence"), homepage=None, citation=None, ) def _split_generators(self, dl_manager): audio_path = dl_manager.download_and_extract(_DATA_URL+"/audo.zip") csv_path = dl_manager.download_and_extract(_DATA_URL+"/comma.csv") return [ datasets.SplitGenerator( name=datasets.Split.TRAIN, gen_kwargs={"audio_path": audio_path, "csv_path": csv_path}, ) ] def _generate_examples(self, audio_path, csv_path): print(audio_path) print(csv_path) key = 0 print(os.listdir(audio_path)) with open(csv_path, encoding="utf-8") as csv_file: csv_reader = csv.DictReader(csv_file) for row in csv_reader: original_sentence_id, sentence, locale = row.values() audio_file = f"{original_sentence_id}.mp3" audio_file_path = os.path.join(audio_path, audio_file) yield key, { "audio": audio_file_path, "sentence": sentence, } key += 1