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"} class Test500(GeneratorBasedBuilder): VERSION = "1.0.0" def _info(self): # Flexible schema: features resolved at runtime return DatasetInfo( description="Sample-level tar dataset (grouped by channel + sample_id)", features=Features( { "channel": Value("string"), "id": Value("string"), "raw_audio": Value("binary"), "transcribe_assembly": Value("string"), "tokenize_vibevoice": Value("binary"), } ), ) # def _split_generators(self, dl_manager): # data_dir = self.config.data_dir # tar_files = sorted(glob.glob(os.path.join(data_dir, "batch_*.tar"))) # if not tar_files: # raise FileNotFoundError(f"No tar files found in {data_dir}") # return [ # SplitGenerator( # name="train", # gen_kwargs={"tar_files": tar_files}, # ) # ] # def _split_generators(self, dl_manager): # # 1. Prefer data_dir if provided (local testing) # if self.config.data_dir is not None: # base_dir = self.config.data_dir # else: # # 2. HF Hub case: files live in cache # base_dir = dl_manager._base_path # tar_files = sorted( # glob.glob(os.path.join(base_dir, "batch_*.tar")) # ) # if not tar_files: # raise FileNotFoundError( # f"No tar files found in {base_dir}. " # "Expected files like batch_000.tar" # ) # return [ # SplitGenerator( # name="train", # gen_kwargs={"tar_files": tar_files}, # ) # ] def _split_generators(self, dl_manager): # 1. Resolve where the files live if self.config.data_dir is not None: # Local testing pattern = os.path.join(self.config.data_dir, "batch_*.tar") tar_files = sorted(glob.glob(pattern)) else: # HF Hub: use virtual paths, then download them 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}" ) # 🔑 THIS IS THE CRITICAL STEP 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 # buffer for building one sample at a time 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() # -------- PARSE NAME -------- # format: channel-sample_id-feature.ext try: channel, sample_id, rest = name.split("-", 2) feature, ext = rest.rsplit(".", 1) ext = "." + ext except ValueError: # skip malformed files continue # print(channel, sample_id, feature, ext) key = (channel, sample_id) # -------- FLUSH PREVIOUS SAMPLE -------- if current_key is not None and key != current_key: yield idx, current idx += 1 current = None # -------- INIT SAMPLE -------- if current is None: current_key = key current = { "channel": channel, "id": sample_id, } # -------- LOAD FEATURE -------- if ext in TEXT_EXTS: try: current[feature] = data.decode("utf-8") except Exception: current[feature] = data else: current[feature] = data # -------- FINAL SAMPLE -------- if current is not None: yield idx, current