#!/usr/bin/env python3 import argparse import json import subprocess import sys from pathlib import Path from typing import Dict import orjson from tqdm import tqdm def run_cmd(template: str, audio_path: str) -> str: cmd = template.replace("{audio}", audio_path) proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) out, err = proc.communicate() if proc.returncode != 0: sys.stderr.write(f"[ASR ERROR] {audio_path}\n{err}\n") return "" # Take first line as transcript text = out.strip().splitlines()[0].strip() if out.strip() else "" return text def main() -> None: parser = argparse.ArgumentParser(description="Run external ASR command per audio from a JSONL and write hypothesis JSONL.") parser.add_argument("--jsonl", required=True, help="Input JSONL with fields id,audio,...") parser.add_argument("--cmd", required=True, help="Command template with {audio} placeholder") parser.add_argument("--out", required=True, help="Output hypothesis JSONL (id,text)") parser.add_argument("--limit", type=int, default=0, help="Limit number of items for quick tests") args = parser.parse_args() items = [] with open(args.jsonl, "r", encoding="utf-8") as f: for line in f: if not line.strip(): continue items.append(json.loads(line)) if args.limit: items = items[: args.limit] with open(args.out, "w", encoding="utf-8") as out_f: for it in tqdm(items, desc="Transcribing"): audio = it.get("audio") or it.get("audio_filepath") text = run_cmd(args.cmd, audio) rec = {"id": it["id"], "text": text} out_f.write(orjson.dumps(rec).decode("utf-8") + "\n") print(f"Wrote hypotheses: {args.out}") if __name__ == "__main__": main()