The tool framework: sound-token, aud-cam and cross-check#
A WAYS tool answers one question about one clip. It has a name, a one-line summary, a JSON schema
and a run(wav, sr, **kwargs) that returns a ToolResult. Three tools ship with the package:
sound-tokenreports which audio each word used and whether that audio was clean or degraded.aud-camreports what the degraded audio does inside the encoder, layer by layer.cross-checkreports which tokens are on degraded audio under the erasure test and under the encoder’s gradient, and whether the two agree.
Everything else goes through the registry in ways.tools. ways tools lists what is registered,
every tool is a CLI subcommand, schemas() hands the tools to a language model, and
call(name, audio=...) runs one by name. A package of your own adds a tool by registering a Tool
under the ways.tools entry-point group (docs/tools.md,
CONTRIBUTING.md).
ways tools # sound-token any backend ... / aud-cam white-box ... / cross-check white-box ...
ways sound-token clip.wav --backend hf-whisper=openai/whisper-base --backend hf-moonshine=UsefulSensors/moonshine-base --aadd --out out/clip
ways aud-cam clip.wav --backend hf-whisper=openai/whisper-base --aadd --granular --out out/clip
ways cross-check clip.wav --backend hf-whisper=openai/whisper-base --window 0.6 2.6 --out out/clip
sound-token writes out/clip__sound-token.png and an SVG twin. aud-cam writes
out/clip__aud-cam.png, with --granular the Grad-CAM lane, the spill-over lane and the
per-layer summary as their own figures, and with --tokens a gradient saliency per token (one
backward pass each) with its own figure and per-token summary. cross-check writes
out/clip__cross-check.png. All three print the ToolResult as JSON.
from ways.tools import sound_token, aud_cam, call, schemas
res = sound_token(wav, sr, backends=[("hf-whisper", "openai/whisper-base")], aadd=True, out="out/clip",
text={"title": "my clip", "caption": "my caption"}) # any heading or sentence of the figure
res.summary["models"][0]["focus_ratio_mean"], res.figure, res.data["attributions"]
res = aud_cam(wav, sr, backend=("hf-whisper", "openai/whisper-base"), window=(1.5, 3.8), out="out/clip", granular=True, tokens=True)
res.summary["per_layer"][-1]["shift_out"], res.figures["spillover"], res.summary["per_token"], res.figures["tokens"]
tools = schemas() # OpenAI function shape; rename parameters to input_schema for Anthropic
call("aud-cam", audio="clip.wav", aadd=True, out="out/clip", granular=True).to_json()
aud-cam and cross-check need a white-box backend because they read the encoder. sound-token
also works through openai-compat with the black-box word-change map.
Tool reference#
A WAYS tool is one question about one clip. It takes a waveform and a sample rate, a backend and
the degradation context, and returns a ToolResult. Three ship with the package:
tool |
question |
backends |
main figure |
|---|---|---|---|
|
which audio did each word use, and was that audio clean or degraded |
any ( |
the per-word figure ( |
|
what does the degraded audio do inside the encoder, layer by layer |
white-box only ( |
the latent figure ( |
|
which tokens sit on degraded audio under the erasure test and under the encoder’s gradient map, and do the two agree |
white-box only |
the cross-check figure ( |
The contract#
from ways.tools import Tool, ToolResult, register
@dataclass(frozen=True)
class Tool:
name: str # the subcommand and the function-calling name
summary: str # one line, shown by `ways tools` and used as the schema description
run: Callable[..., ToolResult] # run(wav, sr, **kwargs)
parameters: dict # JSON schema of the keyword arguments: {"properties": {...}}
needs_white_box: bool = False
@dataclass
class ToolResult:
tool: str
summary: dict # JSON-serialisable numbers and strings
figure: str | None # the main figure's path, when `out` was given
figures: dict[str, str] # named extra figures
data: dict # arrays and objects that are not JSON (attributions, LatentResult)
Tool.schema() adds audio (a file path) as the one required argument and returns the OpenAI
function shape; for the Anthropic API rename parameters to input_schema.
Calling#
from ways.tools import sound_token, aud_cam, call, schemas, names
res = sound_token(wav, 16000, backends=[("hf-whisper", "openai/whisper-base")], aadd=True, out="out/clip")
res.summary["models"][0]["focus_ratio_mean"]; res.figure; res.data["attributions"][0]
res = aud_cam(wav, 16000, backend=("hf-moonshine", "UsefulSensors/moonshine-base"), window=(1.5, 3.8), out="out/clip", granular=True, tokens=True)
res.summary["per_layer"][-1]["shift_out"]; res.figures["spillover"]
call("sound-token", audio="clip.wav", aadd=True, out="out/clip").to_json() # by name, from a file
schemas() # hand these to a language model
From the command line every registered tool is a subcommand with the same flags:
ways tools # the registry, one line per tool; --schema prints the JSON schemas
ways sound-token clip.wav --backend hf-whisper=openai/whisper-base --backend hf-moonshine=UsefulSensors/moonshine-base --aadd --out out/clip
ways aud-cam clip.wav --backend hf-whisper=openai/whisper-base --window 1.5 3.8 --granular --tokens --out out/clip
ways cross-check clip.wav --backend hf-whisper=openai/whisper-base --window 1.5 3.8 --reference "what was said" --out out/clip
Writing a tool#
Write
run(wav, sr, **kwargs) -> ToolResult. Useways.tools.base.resample_16kandquality_framesfor the inputs every tool shares, andCOMMON_PARAMETERSfor their schema.Draw through
ways/figure.py, so every string is data, no text overlaps, and the same inputs give the same bytes. New figure kinds get their own templated function there.Register it. Inside this repository: a module under
ways/tools/, imported fromways/tools/__init__.py. From another package: an entry point in theways.toolsgroup that resolves to theToolinstance.Test it from synthetic arrays on the CPU (
tests/test_tools.pyshows the pattern), and run it once on a real clip before claiming a figure.
Backends are loaded once per process and shared between tools (ways.tools.sound_token.backend).