import os import json import gzip import gc import numpy as np from datasets import load_from_disk, Dataset, Features, Sequence, Value from tqdm import tqdm RAW_DATASET_PATH = "/home/ubuntu/Complete-HF-Dataset-7/test" TMP_JSONL = "/home/ubuntu/tmp_batch.jsonl.gz" CHUNK_SIZE = 200 start_chunk = 100 num_chunks = 100 # split_name = "train" # test FEATURES = Features( { "input_features": Sequence(Sequence(Value("float32"))), "labels": Sequence(Value("int64")), } ) raw_ds = load_from_disk(RAW_DATASET_PATH) dataset_split = raw_ds total_chunks = (len(dataset_split) + CHUNK_SIZE - 1) // CHUNK_SIZE print(f"Total Chunks : {total_chunks}") end_chunk = min(start_chunk + num_chunks, total_chunks) print(f"Processing chunks {start_chunk} → {end_chunk}") kept = 0 with gzip.open(TMP_JSONL, "wt") as f: for c in range(start_chunk, end_chunk): i = c * CHUNK_SIZE chunk = dataset_split.select(range(i, min(i + CHUNK_SIZE, len(dataset_split)))) for _, sample in enumerate(tqdm(chunk)): feats = np.asarray(sample["input_features"], dtype=np.float32) labels = np.asarray(sample["labels"], dtype=np.int64) if ( feats.ndim == 2 and feats.size > 0 and np.isfinite(feats).all() and feats.shape[-1] >= 10 and labels.size > 1 ): f.write( json.dumps( {"input_features": feats.tolist(), "labels": labels.tolist()} ) + "\n" ) kept += 1 del chunk gc.collect() print(f"✅ Kept {kept} samples") print("JSON file has saved successfully") # shard_ds = Dataset.from_json(TMP_JSONL, features=FEATURES) # # Save shard # os.makedirs(SHARD_DIR, exist_ok=True) # shard_path = os.path.join(SHARD_DIR, f"{split_name}_part_{start_chunk:03d}") # shard_ds.save_to_disk(shard_path) # del shard_ds # gc.collect() # After confirming save, you can manually delete JSONL # os.remove(TMP_JSONL)