""" DhVaani-0.5 local CPU benchmark. Loads ARTPARK-IISc/DhVaani-0.5 via transformers AutoModel, runs a few synthesis calls, and records wall-clock latency + real-time factor (RTF) for each. Writes .wav (native) and .mp3 (via ffmpeg/pydub) outputs, plus a latency.json summary. Usage: source venv/bin/activate python benchmark.py """ import json import os import time os.environ.setdefault("HF_TOKEN", os.environ.get("HF_TOKEN", "")) import torch import soundfile as sf from transformers import AutoModel HERE = os.path.dirname(os.path.abspath(__file__)) OUT_DIR = os.path.join(HERE, "outputs") os.makedirs(OUT_DIR, exist_ok=True) DEV = "cuda" if torch.cuda.is_available() else "cpu" REFERENCE_WAV = os.path.join(HERE, "reference.wav") # We don't have a ground-truth transcript for samples/malayalam.wav from the # model repo, so this is an approximate placeholder — good enough to prove # the pipeline runs and to measure latency, not tuned for max clone fidelity. REFERENCE_TEXT = "ഇത് ഒരു മാതൃകാ ശബ്ദമാണ്." TEST_CASES = [ {"name": "malayalam_greeting", "text": "നമസ്‌കാരം, സുഖമാണോ?"}, {"name": "malayalam_longer", "text": "ഇന്ന് കാലാവസ്ഥ വളരെ നല്ലതാണ്. നമുക്ക് പുറത്തു പോകാം."}, ] def main(): print(f"[info] device = {DEV}") print("[info] loading ARTPARK-IISc/DhVaani-0.5 (AutoModel, trust_remote_code=True) ...") t0 = time.perf_counter() model = AutoModel.from_pretrained( "ARTPARK-IISc/DhVaani-0.5", trust_remote_code=True ).to(DEV).eval() load_s = time.perf_counter() - t0 print(f"[info] model loaded in {load_s:.2f}s") sr = model.sampling_rate results = { "device": DEV, "model": "ARTPARK-IISc/DhVaani-0.5", "model_load_seconds": round(load_s, 3), "sampling_rate": sr, "runs": [], } for case in TEST_CASES: name, text = case["name"], case["text"] print(f"\n[run] {name!r}: {text!r}") t0 = time.perf_counter() audio = model.synthesize( text=text, prompt_wav=REFERENCE_WAV, prompt_text=REFERENCE_TEXT, ) gen_s = time.perf_counter() - t0 audio_duration_s = len(audio) / sr rtf = gen_s / audio_duration_s if audio_duration_s > 0 else float("nan") wav_path = os.path.join(OUT_DIR, f"{name}.wav") sf.write(wav_path, audio, sr) mp3_path = os.path.join(OUT_DIR, f"{name}.mp3") try: from pydub import AudioSegment AudioSegment.from_wav(wav_path).export(mp3_path, format="mp3", bitrate="192k") mp3_ok = True except Exception as e: print(f"[warn] mp3 export failed: {e}") mp3_ok = False print( f"[result] gen_time={gen_s:.3f}s audio_len={audio_duration_s:.3f}s " f"RTF={rtf:.3f} (RTF<1 means faster than real-time)" ) results["runs"].append( { "name": name, "text": text, "generation_seconds": round(gen_s, 3), "audio_duration_seconds": round(audio_duration_s, 3), "real_time_factor": round(rtf, 4), "wav_path": os.path.relpath(wav_path, HERE), "mp3_path": os.path.relpath(mp3_path, HERE) if mp3_ok else None, } ) summary_path = os.path.join(OUT_DIR, "latency.json") with open(summary_path, "w") as f: json.dump(results, f, indent=2, ensure_ascii=False) print(f"\n[done] summary written to {summary_path}") if __name__ == "__main__": main()