File size: 2,102 Bytes
6a125ea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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)