141 lines
4.9 KiB
Python
141 lines
4.9 KiB
Python
"""静默镜头使用独立 VAD 证据,不把 Whisper 幻觉伪装成空转录。"""
|
|
|
|
from copy import deepcopy
|
|
|
|
import pytest
|
|
|
|
from app.services.orchestration import native_dialogue_engine as engine
|
|
|
|
|
|
def silence_report(*, audio_sha="a" * 64, count=48000):
|
|
samples = (count + 2) // 3
|
|
windows = (samples + 511) // 512
|
|
probabilities = [0.01] * windows
|
|
return dict(
|
|
decision="pass",
|
|
reasons=[],
|
|
expected_text="",
|
|
transcript="错误的静默幻觉",
|
|
normalized_expected_text="",
|
|
normalized_transcript="错误的静默幻觉",
|
|
content_error_rate=1.0,
|
|
words=None,
|
|
primary_reasons=["spoken_content_mismatch", "invalid_word_alignment"],
|
|
content_decision_source="verified_silence",
|
|
content_verification_policy="silero-sensevoice-verified-silence-v1",
|
|
secondary_status="completed",
|
|
secondary_transcript="",
|
|
normalized_secondary_transcript="",
|
|
secondary_content_error_rate=0.0,
|
|
secondary_asr_revision="90c1c61912018b70ada0fcc024ea24aca62f2e63",
|
|
secondary_runtime_revision="6991744856587fa44379e8b5dcc432debffeb1be",
|
|
audio_sha256=audio_sha,
|
|
source_start_sample=0,
|
|
source_end_sample=count,
|
|
sample_rate=48000,
|
|
clipped_fraction=0.0,
|
|
silence_verification=dict(
|
|
policy="silero-sensevoice-verified-silence-v1",
|
|
source="silero-vad-onnx-cpu",
|
|
revision="be95df9152c0d7618fa1edfeb296fc3dae32376f",
|
|
model_sha256="1a153a22f4509e292a94e67d6f9b85e8deb25b4988682b7e174c65279d8788e3",
|
|
runtime="onnxruntime-cpu",
|
|
runtime_version="1.31.1",
|
|
channel_policy="all_channels_no_speech",
|
|
channels=2,
|
|
channel_probabilities=[probabilities.copy(), probabilities.copy()],
|
|
status="completed",
|
|
decision="no_speech",
|
|
source_audio_sha256=audio_sha,
|
|
source_start_sample=0,
|
|
source_end_sample=count,
|
|
sample_rate=16000,
|
|
sample_count=samples,
|
|
window_size_samples=512,
|
|
window_count=windows,
|
|
threshold=0.5,
|
|
negative_threshold=0.35,
|
|
max_speech_probability=0.01,
|
|
probabilities=probabilities,
|
|
),
|
|
)
|
|
|
|
|
|
def test_verified_silence_preserves_primary_hallucination_and_has_no_word_alignment():
|
|
report = silence_report()
|
|
before = deepcopy(report)
|
|
assert engine._content_consensus(report, "")
|
|
assert report == before
|
|
assert not engine._content_consensus(report, "请说话")
|
|
assert not engine._content_consensus(report, "。")
|
|
|
|
|
|
def test_silence_context_invalidates_old_asr_only_report_reuse():
|
|
from app.services.workflows import digest
|
|
|
|
old = digest(
|
|
dict(
|
|
audio_sha="a" * 64,
|
|
expected_text="",
|
|
speaker_id="s",
|
|
policy="native_av_v1",
|
|
content_verification_policy="whisper-sensevoice-consensus-v1",
|
|
)
|
|
)
|
|
assert engine._audio_review_context("a" * 64, "", "s") != old
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"field,value",
|
|
[
|
|
("status", "unavailable"),
|
|
("decision", "indeterminate"),
|
|
("source_audio_sha256", "b" * 64),
|
|
("source_end_sample", 47999),
|
|
("sample_rate", 48000),
|
|
("sample_count", 15999),
|
|
("window_count", 31),
|
|
("window_size_samples", 1024),
|
|
("revision", "unverified"),
|
|
("model_sha256", "f" * 64),
|
|
("runtime", "energy-threshold"),
|
|
("runtime_version", ""),
|
|
("threshold", 0.9),
|
|
("negative_threshold", 0.6),
|
|
("max_speech_probability", 0.0),
|
|
("probabilities", [0.01] * 31),
|
|
("probabilities", [0.01] * 31 + [0.35]),
|
|
("probabilities", [0.01] * 31 + [float("nan")]),
|
|
("probabilities", [0.01] * 31 + [-0.1]),
|
|
("channel_policy", "mono_mean"),
|
|
("channels", 1),
|
|
("channel_probabilities", [[0.01] * 32]),
|
|
("channel_probabilities", [[0.01] * 32, [0.01] * 31 + [0.9]]),
|
|
("channel_probabilities", [[0.0] * 32, [0.0] * 32]),
|
|
],
|
|
)
|
|
def test_unbound_or_ambiguous_vad_cannot_accept_silence(field, value):
|
|
report = silence_report()
|
|
report["silence_verification"][field] = value
|
|
assert not engine._content_consensus(report, "")
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"field,value",
|
|
[
|
|
("secondary_status", "unavailable"),
|
|
("secondary_transcript", "有人说话"),
|
|
("secondary_transcript", "。"),
|
|
("secondary_content_error_rate", 1),
|
|
("expected_text", "。"),
|
|
("normalized_expected_text", "伪造"),
|
|
("content_decision_source", "two_asr_agreement"),
|
|
("clipped_fraction", 0.1),
|
|
("clipped_fraction", float("nan")),
|
|
],
|
|
)
|
|
def test_silence_does_not_hide_content_failure_or_input_mismatch(field, value):
|
|
report = silence_report()
|
|
report[field] = value
|
|
assert not engine._content_consensus(report, "")
|