Dataset_STT / hientran /clean_data /split_dataset.py
Hien141625's picture
Add files using upload-large-folder tool
4f375b7 verified
Raw
History Blame Contribute Delete
3.17 kB
#!/usr/bin/env python3
"""
Split dataset for training with WaveformCache.
Outputs audio paths that WaveformCache will use to compute cache paths.
"""
import csv
import json
import random
from pathlib import Path
METADATA = Path("/home/dev-52/hientran/clean_data/metadata.csv")
CACHE_DIR = Path("/home/dev-52/hientran/clean_data/waveform_cache")
OUTPUT_DIR = Path("/home/dev-52/hientran/Qwen3-ASR_edit/finetuning")
TRAIN_RATIO = 0.7
random.seed(42)
def build_cache_index():
"""Build index of all cache files for fast lookup."""
print("[1/4] Building cache index...")
cache_index = {} # filename -> full_path
for subdir in CACHE_DIR.iterdir():
if subdir.is_dir():
for f in subdir.glob("*.npy"):
cache_index[f.name] = str(f)
print(f" Found {len(cache_index)} cache files")
return cache_index
def main():
rows = []
missing_cache = 0
print("=" * 60)
print("SPLIT DATASET (for cache training)")
print("=" * 60)
print(f"Cache dir: {CACHE_DIR}")
print(f"Meta CSV: {METADATA}")
print(f"Output: {OUTPUT_DIR}")
print("=" * 60)
print()
# Build cache index
cache_index = build_cache_index()
print()
print("[2/4] Processing metadata...")
with open(METADATA, "r", encoding="utf-8-sig") as f:
reader = csv.DictReader(f)
for i, row in enumerate(reader):
audio_path = row["audio"].strip()
text = row["text"].strip().strip('"')
# Look up cache file exists
cache_filename = Path(audio_path).name + ".npy"
if cache_filename in cache_index:
# Save audio path - WaveformCache will compute cache path
rows.append({"audio": audio_path, "text": text})
else:
missing_cache += 1
if missing_cache <= 5:
print(f"[MISSING] {cache_filename}")
# Progress
if (i + 1) % 50000 == 0:
pct = len(rows) / (i + 1) * 100
print(f"[PROGRESS] {i+1} rows - Found: {len(rows)} ({pct:.1f}%)")
print()
print(f"[3/4] Processing complete")
print(f" Total: {len(rows) + missing_cache}")
print(f" Found: {len(rows)}")
print(f" Missing: {missing_cache}")
if len(rows) == 0:
print("[ERROR] No cache files found!")
return
# Shuffle and split
print()
print("[4/4] Writing train/valid files...")
random.shuffle(rows)
split_idx = int(len(rows) * TRAIN_RATIO)
train_rows = rows[:split_idx]
valid_rows = rows[split_idx:]
print(f" Train: {len(train_rows)}")
print(f" Valid: {len(valid_rows)}")
for name, data in [("train", train_rows), ("valid", valid_rows)]:
out_path = OUTPUT_DIR / f"{name}.jsonl"
with open(out_path, "w", encoding="utf-8") as f:
for item in data:
f.write(json.dumps(item, ensure_ascii=False) + "\n")
print(f"[DONE] {name}.jsonl: {len(data)} samples -> {out_path}")
print()
print("=" * 60)
print("COMPLETE!")
print("=" * 60)
if __name__ == "__main__":
main()