Which spoken word decided the answer?

Goal: ask an audio language model yes-or-no questions out loud, then show which spoken words pushed its answer toward “Yes” or “No”.

Why it matters: a voice assistant that answers wrongly gives no reason. Removing each spoken word from the recording and re-scoring the answer shows what the model relied on, including when it latched onto the wrong word.

Prerequisites: uv sync --extra audio --extra transformers --extra viz. The first run downloads Qwen/Qwen2-Audio-7B-Instruct (about 16 GB) and facebook/wav2vec2-base-960h (about 360 MB); a GPU with 24 GB keeps the run to a few minutes.

Data: three questions of BoolQ-Audio (fixie-ai/boolq-audio, validation rows 2874, 2238 and 304; synthetic speech; CC BY-SA 3.0), stored as boolq_audio_*.wav.

Output: the model’s answer to each recording and one value per spoken word, drawn over the waveform.

from pathlib import Path

import matplotlib.pyplot as plt
from huggingface_hub.utils import disable_progress_bars
from transformers.utils.logging import disable_progress_bar

import mllm_shap
from mllm_shap import (
    AudioPart,
    ConversationSnapshot,
    ExplainConfig,
    ExplainRunner,
    Message,
    Role,
    TextPart,
    TransformersTextBackend,
    Turn,
)
from mllm_shap.alignment import SpectrogramGuidedAligner, Wav2Vec2Ctc
from mllm_shap.plugins import PluginContext
from mllm_shap.viz import plot_audio_segments

disable_progress_bars()
disable_progress_bar()
print("mllm-shap", mllm_shap.__version__)
mllm-shap 0.11.1

Set up the model and the game

Each spoken word is a player (players: sgpa_segments): the transcript is aligned to the recording, and an absent word is silenced (pad). With transcript: align_only the transcript only places the words; the model hears the audio alone. label_margin scores how much more likely the model makes “Yes” than “No” (teacher-forced log-odds), so a positive value pushes toward “Yes” and nothing is generated per coalition.

MODEL_ID = "Qwen/Qwen2-Audio-7B-Instruct"
REVISION = "0a095220c30b7b31434169c3086508ef3ea5bf0a"
ALIGNER = ("facebook/wav2vec2-base-960h", "22aad52d435eb6dbaf354bdad9b0da84ce7d6156")
SYSTEM = "Answer the spoken question with yes or no only."


def config(words: int) -> ExplainConfig:
    exact = words <= 10
    return ExplainConfig.model_validate({
        "backend": {
            "kind": "transformers",
            "model_id": MODEL_ID,
            "options": {"revision": REVISION, "dtype": "bfloat16", "batch_size": 8},
        },
        "generation": {"max_new_tokens": 4},
        "explanation": {
            "archive": {"path": ""},
            "players": "sgpa_segments",
            "absence_policy": "pad",
            "estimator": "exact" if exact else "leverage",
            **({} if exact else {"budget": {"evaluations": 768}}),
            "value_fn": "label_margin",
            "plugin_options": {
                "sgpa_segments": {
                    "model_id": ALIGNER[0],
                    "revision": ALIGNER[1],
                    "transcript": "align_only",
                },
                "label_margin": {"target": "Yes", "labels": ["Yes", "No"]},
            },
        },
    })
backend = TransformersTextBackend(PluginContext(config=config(1)))
aligner = SpectrogramGuidedAligner(Wav2Vec2Ctc(ALIGNER[0], revision=ALIGNER[1]))


def ask(*content) -> ConversationSnapshot:
    system = Message(role=Role.SYSTEM, text=SYSTEM)
    user = Message(role=Role.USER, content=content)
    return ConversationSnapshot.from_turns((Turn(messages=(system, user)),))


async def explain(clip_name: str, transcript: str):
    clip = AudioPart(Path(clip_name), sample_rate=16000)
    answer = await backend.generate(ask(clip), config(1).generation)
    runner = ExplainRunner(config(len(transcript.split())), backend=backend)
    output = await runner.explain(ask(TextPart(transcript), clip))
    result = output.result
    print("model answer:", answer.text.strip())
    print(f"log-odds of Yes: {result.grand_value:+.2f} with every word,", end=" ")
    print(f"{result.base_value:+.2f} with none")
    for span, value in zip(output.player_set.spans, result.values, strict=True):
        print(f"  {span.text:22} {value:+.2f}")
    return clip, transcript, result


def draw(clip, transcript, result):
    segments = aligner.align(transcript, clip)
    plot_audio_segments(clip, segments, values=list(result.values))
    plt.show()

A correct answer: the relation word carries it

“Is Aldi’s affiliated with Trader Joe’s?” (reference: yes). The model says yes, and the stretch of audio that says “affiliated” moves the answer most; the store names matter less.

aldi = await explain("boolq_audio_0013.wav", "Is aldi's affiliated with trader joe's?")
model answer: Yes.
log-odds of Yes: +2.75 with every word, +0.62 with none
  Is                     -1.36
  aldi's                 +0.58
  affiliated             +2.50
  with                   -0.97
  trader                 +0.27
  joe's?                 +1.10
draw(*aldi)
../_images/f74064d386a0e954643eabd5fa83667eaa829d9793aebb8519a460907a29c9a3.png

An answer against the reference: what drove it

“Does the speed of sound vary with changes in frequency at constant temperature?” (reference answer: yes). The model answers no. The explanation shows why: the spoken word “constant” alone pulls the answer toward “No” by more than the whole recording does, so the model treats “at constant temperature” as “nothing changes”. The disagreement traces back to one phrase, not to the audio quality, which tells a team where to look.

sound = await explain(
    "boolq_audio_0022.wav",
    "Does the speed of sound vary with changes in frequency at constant temperature?",
)
model answer: No, the speed
log-odds of Yes: -1.25 with every word, +0.62 with none
  Does                   -0.69
  the                    +0.41
  speed                  -0.02
  of                     +0.17
  sound                  +0.55
  vary                   -0.08
  with                   +0.72
  changes                +0.75
  in                     +0.48
  frequency              +1.01
  at                     -0.05
  constant               -4.34
  temperature?           -0.79
draw(*sound)
../_images/df84f765ff1706346f7e678f46a14a9547873d5a929d50d5e29c2b659c285c42.png

An absolute word decides

“Is language the only criteria of classifying state in india?” (reference: no). The word “only” turns a plausible claim into an absolute one, and it pushes the answer toward “No”.

india = await explain(
    "boolq_audio_0029.wav",
    "Is language the only criteria of classifying state in india?",
)
draw(*india)
model answer: No, language is
log-odds of Yes: -2.50 with every word, +0.62 with none
  Is                     -0.56
  language               -0.19
  the                    -1.02
  only                   -1.34
  criteria               -0.41
  of                     -0.17
  classifying            +0.46
  state                  -0.40
  in                     -0.04
  india?                 +0.55
../_images/62ffe749ea9b2954bfec3545d1566f7354451da797f6098c88f0eb5b23a93f6e.png

Reading the values

  • Values are in log-odds of “Yes” against “No”; they add up to the difference between the full recording and the silenced one (efficiency).

  • The question word “Is” or “Does” often carries a sizeable value: without it the recording stops sounding like a question.

  • One recording is one case. To claim a model relies on a word, explain many recordings and check the values with a removal test such as deletion_curve.