duplex-v0 / eval /prep_bba.py
Jr23xd23's picture
RightNow Duplex v0
f730521 verified
Raw History Blame Contribute Delete
2.46 kB
"""Fetch a fixed 100-question subset of Big Bench Audio (ArtificialAnalysis/big_bench_audio,
MIT): 25 questions per category, chosen with a fixed seed, decoded to 16 kHz wav.
The dataset ships mp3. It is decoded with the soundfile package (libsndfile), or with the
ffmpeg program when soundfile is not installed.
python -m eval.prep_bba --out eval/data/bba
"""
import argparse
import json
import random
import subprocess
from collections import defaultdict
from pathlib import Path
from huggingface_hub import hf_hub_download
from duplex.spec import SR, write_json, write_wav
REPO = "ArtificialAnalysis/big_bench_audio"
def decode(mp3, wav):
"""mp3 -> 16 kHz mono wav. Uses the soundfile package (its bundled libsndfile reads mp3) when
present, so nothing has to be installed on the host; otherwise the ffmpeg program."""
try:
import soundfile as sf
except ImportError:
subprocess.run(["ffmpeg", "-loglevel", "error", "-y", "-i", mp3, "-ac", "1", "-ar", "16000", "-c:a", "pcm_s16le", str(wav)], check=True)
return
from scipy.signal import resample_poly
x, sr = sf.read(mp3, dtype="float32", always_2d=True)
x = x.mean(axis=1)
write_wav(wav, x if sr == SR else resample_poly(x, SR, sr), SR)
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--out", default="eval/data/bba")
ap.add_argument("--per-category", type=int, default=25)
ap.add_argument("--seed", type=int, default=0)
args = ap.parse_args()
out = Path(args.out)
out.mkdir(parents=True, exist_ok=True)
meta = [json.loads(line) for line in open(hf_hub_download(REPO, "metadata.jsonl", repo_type="dataset"), encoding="utf-8")]
by_cat = defaultdict(list)
for row in meta:
by_cat[row["category"]].append(row)
rng = random.Random(args.seed)
subset = [q for cat in sorted(by_cat) for q in rng.sample(sorted(by_cat[cat], key=lambda r: r["id"]), args.per_category)]
for q in subset:
wav = out / f"q_{q['id']}.wav"
if not wav.exists():
mp3 = hf_hub_download(REPO, q["file_name"].replace("\\", "/"), repo_type="dataset")
decode(mp3, wav)
write_json(out / "subset.json", [{"id": q["id"], "category": q["category"], "official_answer": q["official_answer"]} for q in subset])
print(f"{len(subset)} questions in {out}: " + ", ".join(f"{c}={args.per_category}" for c in sorted(by_cat)))
if __name__ == "__main__":
main()