import pandas as pd, pyarrow as pa, pyarrow.parquet as pq import random, uuid, os from pathlib import Path from tqdm import tqdm OUT = Path(r"C:\Users\tjcri\datasets\BlackTarot") DATA_DIR = OUT / "data" # --- BIG CONFIG --- N_TRAIN = 240_000 # 240k train N_TEST = 10_000 # 10k test -> total 250k = 100K 0.3 else f"Context for {topic}: provide examples. [ctx {idx%997}]" output = make_output(category, topic, instruction, idx) # Multi-turn bonus for best quality: add follow-up pair (n_turns=4 => +0.05) follow_user = f"Follow-up {idx%5} on {topic}: what is the edge case that breaks naive handling?" follow_output = make_output(category, topic, follow_user, idx+1000000) conv = [ {"role": "system", "content": system}, {"role": "user", "content": instruction + (f"\nInput: {inp}" if inp else "")}, {"role": "assistant", "content": output}, {"role": "user", "content": follow_user}, {"role": "assistant", "content": follow_output} ] # For HF single-output training, keep instruction/output as first pair; follow-up is bonus for quality & multi-turn # Combine outputs for length if needed (still single output field will be first answer, but quality scores on 4-turn) output = output + "\n\n" + follow_output # keep single output long for alpaca format too return { "id": str(uuid.uuid4()), "system": system, "instruction": instruction, "input": inp, "output": output, "conversations": conv, "category": category, "source": "BlackTarot" } # Clean old small data import shutil if DATA_DIR.exists(): for f in DATA_DIR.glob("*.parquet"): f.unlink() rows_train = [] rows_test = [] print(f"Generating {N_TRAIN} train + {N_TEST} test rows...") for i in tqdm(range(N_TRAIN)): rows_train.append(make_row(i)) for i in tqdm(range(N_TEST)): rows_test.append(make_row(N_TRAIN + i)) # Shard and write def write_shards(rows, prefix, shard_size): for shard_idx in range(0, len(rows), shard_size): shard = rows[shard_idx: shard_idx+shard_size] df = pd.DataFrame(shard) num = shard_idx // shard_size total = (len(rows) + shard_size -1)//shard_size path = DATA_DIR / f"{prefix}-{num:05d}-of-{total:05d}.parquet" table = pa.Table.from_pandas(df, preserve_index=False) pq.write_table(table, path, compression="zstd", compression_level=3) print(f"Wrote {path} rows={len(df)} size={(path.stat().st_size/1024/1024):.1f}MB") DATA_DIR.mkdir(parents=True, exist_ok=True) write_shards(rows_train, "train", SHARD_SIZE) write_shards(rows_test, "test", SHARD_SIZE if N_TEST<= SHARD_SIZE else SHARD_SIZE) print("DONE") # quick stats import pyarrow.parquet as pq2, glob files = list(DATA_DIR.glob("*.parquet")) total_rows = sum(pq2.ParquetFile(str(f)).metadata.num_rows for f in files) total_bytes = sum(f.stat().st_size for f in files) print(f"Total files {len(files)} rows {total_rows} bytes {total_bytes/1024/1024:.1f}MB")