import datasets import csv import json import os import csv # Defining Access _DATA_URL = "data/BA_Datensatz_V2.tar.gz" _PROMPTS_URLS = { "train": "data/metadata-train.csv.gz", "test": "data/metadata-test.csv.gz", } class BA_Datensatz_V2(datasets.GeneratorBasedBuilder): def _info(self): return datasets.DatasetInfo( description="German Dataset that focuses on juristical data", features=datasets.Features({ "speaker_id": datasets.Value("string"), "path": datasets.Value("string"), "audio": datasets.Audio(sampling_rate=16_000), "sentence": datasets.Value("string"), }), supervised_keys=None, ) def _split_generators(self, dl_manager): audio_path = dl_manager.download(_DATA_URL) prompts_paths = dl_manager.download_and_extract(_PROMPTS_URLS) train_dir = os.path.join("BA_Datensatz_V2", "train", "wav/") test_dir = os.path.join("BA_Datensatz_V2", "test", "wav/") return [ datasets.SplitGenerator( name=datasets.Split.TRAIN, gen_kwargs={ "audio_files": dl_manager.iter_archive(audio_path), "metadata_path": prompts_paths["train"], "path_to_clips": train_dir, }, ), datasets.SplitGenerator( name=datasets.Split.TEST, gen_kwargs={ "audio_files": dl_manager.iter_archive(audio_path), "metadata_path": prompts_paths["test"], "path_to_clips": test_dir, }, ), ] def _generate_examples(self, audio_files, metadata_path, path_to_clips): metadata = {} with open(metadata_path, 'rt', encoding='utf-8') as f: reader = csv.reader(f) for row in reader: filename_with_extension, sentence = row speaker_id = filename_with_extension.split('_')[0] # Construct path relative to the dataset structure relative_path = os.path.join(path_to_clips, speaker_id, filename_with_extension) metadata[relative_path] = { "speaker_id": speaker_id, "path": relative_path, "sentence": sentence, } id_ = 0 for path, file_content in audio_files: if not path.endswith('/') and path.startswith(path_to_clips): # This checks if the file is relevant to the current split by checking its path if path in metadata: audio_data = { "path": metadata[path]["path"], "bytes": file_content.read(), } yield id_, {**metadata[path], "audio": audio_data} id_ += 1 else: print(f"No metadata entry for {path}") else: # Skip directories or files not relevant to the current split continue