aurora-wake-model / code /pipeline_train /tts /scripts /index_vimd_references.py
thucdangvan020999's picture
Initial upload: releases, checkpoints, code, docs
a1fe2e2 verified
Raw History Blame Contribute Delete
3.38 kB
#!/usr/bin/env python3
"""Select and materialize the longest utterance for every ViMD speaker."""
from __future__ import annotations
import argparse
import io
import json
import re
from pathlib import Path
from datasets import Audio, get_dataset_split_names, load_dataset
import soundfile as sf
DATASET = "nguyendv02/ViMD_Dataset"
def duration_s(payload: bytes) -> float:
return float(sf.info(io.BytesIO(payload)).duration)
def safe_name(value: object) -> str:
text = re.sub(r"[^A-Za-z0-9_.-]+", "_", str(value)).strip("._")
return text or "unknown"
def rows(split: str):
ds = load_dataset(DATASET, split=split, streaming=True)
return ds.cast_column("audio", Audio(decode=False))
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument("output", type=Path)
parser.add_argument("--limit", type=int, help="debug limit per split")
args = parser.parse_args()
args.output.mkdir(parents=True, exist_ok=True)
splits = get_dataset_split_names(DATASET)
best: dict[str, dict] = {}
scanned = 0
rejected = 0
for split in splits:
for index, row in enumerate(rows(split)):
if args.limit is not None and index >= args.limit:
break
payload = row["audio"]["bytes"]
try:
seconds = duration_s(payload)
except Exception as exc:
rejected += 1
print(json.dumps({"event": "rejected_audio", "split": split, "filename": row.get("filename"), "error": repr(exc)}), flush=True)
continue
speaker = str(row["speakerID"])
candidate = {
"speaker_id": speaker,
"split": split,
"filename": row["filename"],
"duration_seconds": round(seconds, 6),
"text": row["text"],
"gender": row["gender"],
"region": row["region"],
"province_code": row["province_code"],
"province_name": row["province_name"],
}
if speaker not in best or seconds > best[speaker]["duration_seconds"]:
path = args.output / f"{safe_name(speaker)}.wav"
path.write_bytes(payload)
best[speaker] = {**candidate, "reference_path": str(path.resolve())}
scanned += 1
if scanned % 100 == 0:
print(json.dumps({"pass": 1, "split": split, "scanned": scanned, "speakers": len(best)}), flush=True)
print(json.dumps({"pass": 1, "split": split, "scanned": scanned, "speakers": len(best)}), flush=True)
manifest = args.output / "references.jsonl"
with manifest.open("w", encoding="utf-8") as out:
for speaker in sorted(best):
out.write(json.dumps(best[speaker], ensure_ascii=False) + "\n")
summary = {
"dataset": DATASET,
"license": "CC-BY-NC-ND-4.0",
"splits": splits,
"rows_scanned": scanned,
"rejected_audio": rejected,
"unique_speakers": len(best),
"references_written": len(best),
"manifest": str(manifest.resolve()),
}
(args.output / "summary.json").write_text(json.dumps(summary, ensure_ascii=False, indent=2), encoding="utf-8")
print(json.dumps(summary, ensure_ascii=False), flush=True)
if __name__ == "__main__":
main()