| import io |
| import tarfile |
| import json |
| from collections import defaultdict |
| import glob |
| import os |
|
|
| from datasets import GeneratorBasedBuilder, DatasetInfo, SplitGenerator, Features, Value |
|
|
|
|
| TEXT_EXTS = {".txt", ".json", ".rttm"} |
|
|
|
|
| class Test500(GeneratorBasedBuilder): |
| VERSION = "1.0.0" |
|
|
| def _info(self): |
| |
| return DatasetInfo( |
| description="Sample-level tar dataset (grouped by channel + sample_id)", |
| features=Features( |
| { |
| "channel": Value("string"), |
| "id": Value("string"), |
| "transcribe_assembly": Value("string"), |
| "tokenize_vibevoice": Value("binary"), |
| "diarize_pyannote": Value("string"), |
| "transcribe_wav2vec2": Value("binary"), |
| } |
| ), |
| ) |
|
|
| def _split_generators(self, dl_manager): |
| |
| if self.config.data_dir is not None: |
| |
| pattern = os.path.join(self.config.data_dir, "batch_*.tar") |
| tar_files = sorted(glob.glob(pattern)) |
| else: |
| |
| pattern = "batch_*.tar" |
| hf_paths = sorted( |
| glob.glob(os.path.join(dl_manager._base_path, pattern)) |
| ) |
|
|
| if not hf_paths: |
| raise FileNotFoundError( |
| f"No tar files found in HF repo cache at {dl_manager._base_path}" |
| ) |
|
|
| |
| tar_files = dl_manager.download(hf_paths) |
|
|
| if not tar_files: |
| raise FileNotFoundError("No tar files resolved for dataset") |
|
|
| return [ |
| SplitGenerator( |
| name="train", |
| gen_kwargs={"tar_files": tar_files}, |
| ) |
| ] |
|
|
| def _generate_examples(self, tar_files): |
| idx = 0 |
|
|
| |
| current = None |
| current_key = None |
|
|
| for tar_path in tar_files: |
| with tarfile.open(tar_path, "r:*") as tar: |
| for member in tar: |
| if not member.isfile(): |
| continue |
|
|
| f = tar.extractfile(member) |
| if f is None: |
| continue |
|
|
| name = member.name |
| data = f.read() |
| |
| |
| |
| try: |
| channel, sample_id, rest = name.split("-", 2) |
| feature, ext = rest.rsplit(".", 1) |
| ext = "." + ext |
| except ValueError: |
| |
| continue |
| |
| key = (channel, sample_id) |
|
|
| |
| if current_key is not None and key != current_key: |
| yield idx, current |
| idx += 1 |
| current = None |
|
|
| |
| if current is None: |
| current_key = key |
| current = { |
| "channel": channel, |
| "id": sample_id, |
| } |
|
|
| |
| |
| if ext in TEXT_EXTS: |
| try: |
| current[feature] = data.decode("utf-8") |
| except Exception: |
| current[feature] = data |
| else: |
| current[feature] = data |
| |
|
|
| |
| if current is not None: |
| yield idx, current |