"""Fleet-2 teacher pass: label candidate clips with Whisper-large-v3-turbo (MLX). Usage: python teacher.py RUN_DIR Reads RUN_DIR/data/candidates.jsonl, appends one JSON line per clip to RUN_DIR/data/teacher.jsonl. Resumable: clips already present in teacher.jsonl are skipped. """ import json import sys import time from pathlib import Path import numpy as np import soundfile as sf def load_done(path): done, good = set(), [] if path.exists(): for line in path.read_text(encoding="utf-8").splitlines(): try: row = json.loads(line) except json.JSONDecodeError: continue # truncated last line from an interrupted run if row["id"] not in done: done.add(row["id"]) good.append(line) path.write_text("".join(l + "\n" for l in good), encoding="utf-8") return done def main(): run = Path(sys.argv[1]).resolve() data = run / "data" pin = json.loads((Path(__file__).resolve().parent / "teacher_pin.json").read_text()) from huggingface_hub import snapshot_download model_dir = snapshot_download(pin["repo"], revision=pin["revision"]) import mlx_whisper candidates = [json.loads(l) for l in (data / "candidates.jsonl").read_text(encoding="utf-8").splitlines() if l.strip()] out = data / "teacher.jsonl" done = load_done(out) todo = [c for c in candidates if c["id"] not in done] print(f"Teacher {pin['repo']}@{pin['revision'][:8]}: {len(done)} done, {len(todo)} to label", flush=True) start = time.monotonic() with out.open("a", encoding="utf-8") as handle: for n, row in enumerate(todo, 1): wav, rate = sf.read(data / row["audio"], dtype="float32") if rate != 16000 or wav.ndim != 1: raise ValueError(f"{row['id']}: expected 16 kHz mono audio") t0 = time.monotonic() # Raw decoding: one temperature, no internal skipping. Filtering happens in fleet2.py filter. result = mlx_whisper.transcribe( np.ascontiguousarray(wav), path_or_hf_repo=model_dir, language=row["language"], task="transcribe", temperature=0.0, condition_on_previous_text=False, compression_ratio_threshold=None, logprob_threshold=None, no_speech_threshold=None, word_timestamps=False, fp16=True, verbose=None) segments = [{"avg_logprob": float(s["avg_logprob"]), "compression_ratio": float(s["compression_ratio"]), "no_speech_prob": float(s["no_speech_prob"])} for s in result.get("segments", [])] handle.write(json.dumps({"id": row["id"], "language": row["language"], "text": result["text"].strip(), "segments": segments, "seconds": round(time.monotonic() - t0, 3)}, ensure_ascii=False) + "\n") handle.flush() if n % 25 == 0 or n == len(todo): rate_s = (time.monotonic() - start) / n print(f"teacher {n}/{len(todo)} {rate_s:.2f}s/clip ~{rate_s * (len(todo) - n) / 60:.0f} min left", flush=True) print("Teacher pass complete.", flush=True) if __name__ == "__main__": main()