"""Generate 4 permitted spoken audio test fixtures (2 EN, 2 PCM) and evaluate Whisper ASR + pipeline.""" import argparse import asyncio import json from pathlib import Path from typing import Dict, List import edge_tts import soundfile as sf from src.config import GOLD_ANSWERS_PATH, VOICE_RECORDS_DIR, VOICE_RECORDS_JSON_PATH from src.pipeline.orchestrator import answer_question from src.speech.transcription import SpeechTranscriber, get_transcriber VOICE_SCENARIOS = [ ("V01", "P06", "English", "en-NG-EzinneNeural", "+0%"), ("V02", "P06", "Nigerian Pidgin", "en-NG-AbeoNeural", "+5%"), ("V03", "P07", "English", "en-NG-AbeoNeural", "+0%"), ("V04", "P09", "Nigerian Pidgin", "en-NG-EzinneNeural", "+5%"), ] async def synth_mp3(text: str, voice: str, rate: str, mp3_path: Path) -> None: comm = edge_tts.Communicate(text=text, voice=voice, rate=rate) await comm.save(str(mp3_path)) def convert_to_wav_16k(mp3_path: Path, wav_path: Path) -> None: arr, sr = SpeechTranscriber.load_audio_16k(mp3_path) sf.write(str(wav_path), arr, sr, subtype="PCM_16") def main() -> None: parser = argparse.ArgumentParser() parser.add_argument("--asr-only", action="store_true") parser.add_argument("--asr-model", default=None) args = parser.parse_args() VOICE_RECORDS_DIR.mkdir(parents=True, exist_ok=True) pairs = {p["pair_id"]: p for p in json.loads(GOLD_ANSWERS_PATH.read_text(encoding="utf-8"))} transcriber = get_transcriber(args.asr_model) if args.asr_model else get_transcriber() records: List[Dict] = [] for rec_id, pair_id, lang, voice_name, rate in VOICE_SCENARIOS: pair = pairs[pair_id] prompt_key = "english_prompt" if lang == "English" else "pidgin_prompt" intended = pair[prompt_key] slug = lang.replace(" ", "_").lower() mp3_tmp = VOICE_RECORDS_DIR / f"{rec_id}_{pair_id}_{slug}.mp3" wav_path = VOICE_RECORDS_DIR / f"{rec_id}_{pair_id}_{slug}.wav" asyncio.run(synth_mp3(intended, voice_name, rate, mp3_tmp)) convert_to_wav_16k(mp3_tmp, wav_path) mp3_tmp.unlink(missing_ok=True) raw_transcript, status_msg, _ = transcriber.transcribe(str(wav_path), delete_after=False) print(f"[{rec_id} | {lang}] intended='{intended}'\n raw_asr='{raw_transcript}'") rec = { "record_id": rec_id, "pair_id": pair_id, "language": lang, "audio_file": str(wav_path.relative_to(VOICE_RECORDS_DIR.parent.parent).as_posix()), "voice_fixture_note": f"Permitted synthetic Nigerian-accented speech fixture ({voice_name}); zero student PII.", "intended_wording": intended, "raw_transcript": raw_transcript, "asr_status": status_msg, "key_facts_preserved": None, "user_correction": "Pending review", "submitted_query": raw_transcript or intended, } if not args.asr_only: resp = answer_question(query=rec["submitted_query"], reply_language=lang) rec.update( { "predicted_intent": resp.intent.label, "top3_passage_ids": [rp.passage.passage_id for rp in resp.top_passages], "final_answer": resp.grounded.answer, "cited_passage_ids": resp.grounded.cited_passage_ids, "missing_information": resp.grounded.missing_information, } ) records.append(rec) VOICE_RECORDS_JSON_PATH.write_text( json.dumps(records, indent=2, ensure_ascii=False), encoding="utf-8" ) print(f"Saved {len(records)} voice test records to {VOICE_RECORDS_JSON_PATH}") if __name__ == "__main__": main()