foley.agent.verify#
verify_match — the retrieve→verify ladder (clap → listen → judge) + Judge impls.
Retrieval gives a ranked shortlist; verification confirms the intent before a clip is accepted (report 05 §3). Three rungs, cheapest first:
clap—ClapJudge, the zero-config gate on the retrieval cosine (no ML).listen— an audio-LM “does this contain {event}?” check; #7 ships the deterministic :class:`StringOverlapJudge` here (caption/tag overlap); the real Qwen2-AudioAudioLMJudge(foley[fit]) is aJudgebehind this sameVerifyLevelseam.judge—AnthropicJudge, an LLM arbiter (ties + scene consistency),foley[agent];anthropicimported lazily.
The ladder is AND-confirming: the clap gate must pass before a higher rung is asked
to confirm. **This module is the extension point #10b (the Tier-2 fit-judge) plugs
into** — a new Judge bound at level=judge via the judge= keyword, with its
own fit-precision metric; it does not touch retrieval ranking (the nDCG@10 gate).
Module Attributes
(yes-words, no-words). |
|
the checkpoint's own dtype (bf16 for Qwen2-Audio, ~15 GB) placed by |
|
|
Functions
|
The one-turn chat the audio-LM sees: the clip as an audio part, then |
|
The |
|
Verify |
Classes
|
LLM arbiter for the |
|
The audio-LM |
The zero-config |
|
|
Deterministic stand-in for the |
- foley.agent.verify.AQA_ANSWER_WORDS = (('Yes', 'yes'), ('No', 'no'))#
(yes-words, no-words). Each must be a single token; P(yes) is the softmax mass on the yes-group over both groups.
- Type:
The answer spellings AQAScore reads P(yes) from
- foley.agent.verify.AUDIOLM_LOAD_KWARGS = {'device_map': 'auto', 'dtype': 'auto'}#
the checkpoint’s own dtype (bf16 for Qwen2-Audio, ~15 GB) placed by
accelerateacross what devices there are.- Type:
from_pretrainedoptions for the audio-LM
- class foley.agent.verify.AnthropicJudge(*, client=None, model='claude-opus-4-8', max_tokens=500)[source]#
Bases:
objectLLM arbiter for the
judgerung (foley[agent]): Claude → aVerdict.anthropicimported lazily insidejudge(). Stashes theMessageonself.last_responsefor the GenAI span.
- class foley.agent.verify.AudioLMJudge(*, pipeline=None, model='Qwen/Qwen2-Audio-7B-Instruct', max_tokens=300, tau=0.5, load_kwargs=None)[source]#
Bases:
objectThe audio-LM
listenrung (foley[fit]): Qwen2-Audio “does this contain {event}?”.With no
pipeline, the firstjudge()loadsmodel(transformers,torch, imported lazily; the 7B weights download on first use) and reads P(yes) from one forward pass (qwen2_audio_pipeline()); later calls reuse it. An injectedpipeline(the test seam, mirroring the Stable-Audio fake-pipeline seam) drives the whole byte-decode → prompt → P(yes) →Verdictpath with no torch. Stashesself.model+self.last_responsesoverify_match’s GenAI span stays informative (same getattr contractAnthropicJudgeuses).- Parameters:
pipeline –
(prompt, wav, sr) -> {'p_yes': float}or yes/no text.model (
str) – The Hugging Face checkpoint to load when no pipeline is given.max_tokens (
int) – Kept for the GenAI span contract (the read-out generates none).tau (
float) – The P(yes) threshold for a match.load_kwargs (
Optional[dict]) –from_pretrainedoptions (defaultAUDIOLM_LOAD_KWARGS).
- class foley.agent.verify.ClapJudge[source]#
Bases:
objectThe zero-config
claprung: gate on the retrieval cosine (no ML, no network).matchiff the candidate’sclap_scoreclearstau_clap;confidenceis the score clamped to0..1so it is comparable to the higher rungs’ 0..1 scores.
- foley.agent.verify.MODEL_ONLY_LOAD_KWARGS = frozenset({'attn_implementation', 'device_map', 'dtype', 'low_cpu_mem_usage', 'max_memory', 'quantization_config', 'torch_dtype'})#
load_kwargsthat only the model takes; the rest (revision,cache_dir,token, …) also go to the processor so both come from the same snapshot.
- class foley.agent.verify.StringOverlapJudge(*, threshold=0.3)[source]#
Bases:
objectDeterministic stand-in for the
listen/judgerungs (the hermetic CI fake).Jaccard token overlap of the event query vs the candidate’s caption + tags. Lets the full ladder +
decide()branch run with zero ML/network. The returnedVerdictechoes back the requestedlevel.
- foley.agent.verify.aqa_conversation(prompt)[source]#
The one-turn chat the audio-LM sees: the clip as an audio part, then
prompt.
- foley.agent.verify.qwen2_audio_pipeline(processor, model, *, answers=(('Yes', 'yes'), ('No', 'no')))[source]#
The
(prompt, wav, sr) -> {'p_yes', 'p_no'}pipeline over a loaded Qwen2-Audio.One forward pass, no generation: the clip (mono, resampled to the processor’s rate) and the question go through the chat template with the assistant turn opened, and P(yes) is the next-token softmax mass on the yes-spellings, over the yes- and no-spellings together (the AQAScore read-out, report 08 §2.1). A clip longer than the feature extractor’s window (Whisper’s 30 s) is truncated by it, so that is warned about.
- foley.agent.verify.verify_match(event, candidate, *, level=VerifyLevel.clap, judge=None, tau_clap=0.35, _span=None)[source]#
Verify
candidateagainsteventup to runglevel(AND-confirming ladder).Runs the
clapgate always; iflevelis higher and the clap gate passed, escalates to the injected/default judge for that rung and returns its verdict (Verdict.level== the producing rung).- Parameters:
event (
SoundEvent) – The wantedSoundEvent.candidate (
Candidate) – A license-cleanCandidate— this MUST run after thegate_candidates()gate (asserted).level (
str|VerifyLevel) – The max rung to climb (clap|listen|judge).judge (
Optional[Judge]) – An injectedJudgefor the higher rungs (the DI seam; defaults per_default_judge()).tau_clap (
float) – The clap-gate threshold._span – Internal — the obs span handle for GenAI recording on the LLM rung.
- Raises:
AssertionError – If
candidate.license_okis notTrue(verify-before-gate is a bug — the license gate is the fail-closed first pass).- Return type: