#!/usr/bin/env python3 """Run the locally installed three-category model on text or timestamped cues.""" from __future__ import annotations import argparse import json from dataclasses import asdict from pathlib import Path from scripts.package_curated_production import ( RANK4_FP32, RANK4_INT8, _tree_sha256, validate_production_bundle, validate_rank4_production_interface, ) from sponsor_detection.inference.stitching import WindowSponsorSpan, stitch_window_spans from sponsor_detection.inference.windowing import TranscriptCue, assemble_transcript, build_transcript_windows from sponsor_detection.model.smart_segment_decoder import ( decode_text_probabilities, filter_min_span_char_spans, softmax_probabilities, validate_min_span_chars, ) def resolve_current(production_root: Path) -> Path: root = production_root.resolve() pointer = json.loads((root / "current.json").read_text()) relative = Path(pointer["bundle_directory"]) bundle = (root / relative).resolve() if relative.is_absolute() or not bundle.is_relative_to(root / "versions"): raise ValueError("current model pointer escapes production versions") if _tree_sha256(bundle) != pointer["bundle_tree_sha256"]: raise ValueError("installed model files differ from the promoted bundle") return bundle def read_cues(path: Path) -> list[TranscriptCue]: payload = json.loads(path.read_text()) rows = payload.get("cues") if isinstance(payload, dict) else payload if not isinstance(rows, list): raise ValueError("transcript JSON must contain a cues list") cues = [] for index, row in enumerate(rows): start, end = row["start_ms"], row["end_ms"] if (isinstance(start, bool) or isinstance(end, bool) or not isinstance(start, int) or not isinstance(end, int) or start < 0 or end <= start or not isinstance(row["text"], str)): raise ValueError("each cue requires text and integer 0 <= start_ms < end_ms") cues.append(TranscriptCue(index, start, end, row["text"])) return cues def predict(bundle: Path, cues: list[TranscriptCue], *, timestamps: bool, precision: str = "int8", max_length: int = 1024, overlap: int = 128, min_span_chars: int | None = None) -> dict: import numpy as np import onnxruntime as ort from transformers import AutoTokenizer validation = validate_production_bundle(bundle) manifest, decoder = validation["manifest"], validation["decoder"] # The authoritative value is the persisted bundle's decoder contract; a # caller may only override it explicitly, and missing means legacy 0. bundle_min_span = validate_min_span_chars(decoder.get("min_span_chars", 0)) effective_min_span = ( bundle_min_span if min_span_chars is None else validate_min_span_chars(min_span_chars) ) artifact = RANK4_INT8 if precision == "int8" else RANK4_FP32 if artifact not in manifest["artifacts"]: raise ValueError(f"bundle does not contain a {precision} rank-4 runtime") options = ort.SessionOptions() options.intra_op_num_threads = 4 session = ort.InferenceSession(str(bundle / manifest["artifacts"][artifact]["file"]), sess_options=options, providers=["CPUExecutionProvider"]) categories = validation["categories"] validate_rank4_production_interface(session, categories) tokenizer = AutoTokenizer.from_pretrained(str(bundle), local_files_only=True) transcript = assemble_transcript(cues) windows = build_transcript_windows(transcript, tokenizer, max_length=max_length, overlap_tokens=overlap) by_category = {category: [] for category in categories} for window in windows: logits = session.run(["segment_logits"], { "input_ids": np.asarray([window.input_ids], dtype=np.int64), "attention_mask": np.asarray([window.attention_mask], dtype=np.int64), })[0][0] probabilities = softmax_probabilities(logits) spans = decode_text_probabilities(probabilities, window.offset_mapping, categories=categories, thresholds=decoder["thresholds"], default_threshold=decoder["default_threshold"], enabled_categories=validation["enabled_categories"]) for span in spans: start = window.offset_mapping[span.start_index][0] end = window.offset_mapping[span.end_index - 1][1] if end > start: by_category[span.category].append(WindowSponsorSpan(window.index, start, end, span.confidence)) results = [] for category, spans in by_category.items(): # Without real timestamps, prevent arbitrary synthetic time from joining distant text. merged = stitch_window_spans(transcript, spans, confidence_threshold=decoder["thresholds"][category], merge_gap_characters=decoder.get("merge_gap_characters", 24), preserve_window_boundaries=decoder.get("preserve_window_boundaries", False), merge_gap_ms=decoder["merge_gap_ms"] if timestamps else 0) # The min-span floor is applied AFTER the final per-category stitch so a # short window fragment can still join a valid whole event. The internal # pre-stitch list above is deliberately left unfiltered. merged = filter_min_span_char_spans( merged, effective_min_span, start_char=lambda value: value.start_char, end_char=lambda value: value.end_char, ) for span in merged: record = {"category": category, **asdict(span), "quote": transcript.text[span.start_char:span.end_char]} if not timestamps: record.pop("start_ms"); record.pop("end_ms") results.append(record) return {"model_version": manifest["model_version"], "precision": precision, "automatic_actions_enabled": False, "normalized_text": transcript.text, "timestamp_method": "interpolated cue times" if timestamps else None, "windows_processed": len(windows), "min_span_chars": effective_min_span, "bundle_min_span_chars": bundle_min_span, "spans": sorted(results, key=lambda value: (value["start_char"], value["category"]))} def main(argv=None): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--production-root", type=Path, default=Path("production")) parser.add_argument("--bundle", type=Path) source = parser.add_mutually_exclusive_group(required=True) source.add_argument("--text-file", type=Path) source.add_argument("--transcript-json", type=Path) parser.add_argument("--precision", choices=["fp32", "int8"], default="int8") parser.add_argument( "--min-span-chars", type=int, default=None, help="override the persisted bundle minimum decoded half-open character span length", ) parser.add_argument("--output", type=Path) args = parser.parse_args(argv) if args.min_span_chars is not None and args.min_span_chars < 0: parser.error("--min-span-chars must be a non-negative integer") bundle = args.bundle or resolve_current(args.production_root) cues = read_cues(args.transcript_json) if args.transcript_json else [ TranscriptCue(0, 0, 1, args.text_file.read_text())] report = predict(bundle, cues, timestamps=args.transcript_json is not None, precision=args.precision, min_span_chars=args.min_span_chars) payload = json.dumps(report, indent=2) + "\n" if args.output: args.output.parent.mkdir(parents=True, exist_ok=True) args.output.write_text(payload) else: print(payload, end="") return 0 if __name__ == "__main__": raise SystemExit(main())