107 lines
3.9 KiB
Python
107 lines
3.9 KiB
Python
"""静默验收有独立证据合同,不能把 ASR 幻觉改写为空台词。"""
|
|
|
|
import math
|
|
import re
|
|
|
|
from .native_av import normalize_native_text
|
|
|
|
POLICY = "silero-sensevoice-verified-silence-v1"
|
|
REVISION = "be95df9152c0d7618fa1edfeb296fc3dae32376f"
|
|
MODEL_SHA256 = "1a153a22f4509e292a94e67d6f9b85e8deb25b4988682b7e174c65279d8788e3"
|
|
|
|
|
|
def verified_silence(report, expected):
|
|
"""只接纳同音轨全选段的逐窗低概率证据和实际空的第二路识别。"""
|
|
if not isinstance(expected, str) or expected.strip() or not isinstance(report, dict):
|
|
return False
|
|
fields = dict(
|
|
content_verification_policy=POLICY,
|
|
content_decision_source="verified_silence",
|
|
normalized_expected_text="",
|
|
secondary_status="completed",
|
|
secondary_asr_revision="90c1c61912018b70ada0fcc024ea24aca62f2e63",
|
|
secondary_runtime_revision="6991744856587fa44379e8b5dcc432debffeb1be",
|
|
normalized_secondary_transcript="",
|
|
sample_rate=48000,
|
|
)
|
|
if any(report.get(k) != v for k, v in fields.items()):
|
|
return False
|
|
if any(
|
|
not isinstance(report.get(k), str) or report[k].strip()
|
|
for k in ("expected_text", "secondary_transcript")
|
|
):
|
|
return False
|
|
transcript = report.get("transcript")
|
|
if not isinstance(transcript, str) or report.get(
|
|
"normalized_transcript"
|
|
) != normalize_native_text(transcript):
|
|
return False
|
|
for key, wanted in (
|
|
("secondary_content_error_rate", 0),
|
|
("clipped_fraction", 0),
|
|
("content_error_rate", 1 if normalize_native_text(transcript) else 0),
|
|
):
|
|
if type(report.get(key)) not in (int, float) or report[key] != wanted:
|
|
return False
|
|
start, end = report.get("source_start_sample"), report.get("source_end_sample")
|
|
if type(start) is not int or type(end) is not int or not 0 <= start < end:
|
|
return False
|
|
sha = report.get("audio_sha256")
|
|
if not isinstance(sha, str) or re.fullmatch(r"[0-9a-f]{64}", sha) is None:
|
|
return False
|
|
evidence = report.get("silence_verification")
|
|
if not isinstance(evidence, dict):
|
|
return False
|
|
count = (end - start + 2) // 3
|
|
windows = (count + 511) // 512
|
|
fixed = dict(
|
|
policy=POLICY,
|
|
source="silero-vad-onnx-cpu",
|
|
revision=REVISION,
|
|
model_sha256=MODEL_SHA256,
|
|
runtime="onnxruntime-cpu",
|
|
runtime_version="1.31.1",
|
|
channel_policy="all_channels_no_speech",
|
|
channels=2,
|
|
status="completed",
|
|
decision="no_speech",
|
|
source_audio_sha256=sha,
|
|
source_start_sample=start,
|
|
source_end_sample=end,
|
|
sample_rate=16000,
|
|
sample_count=count,
|
|
window_size_samples=512,
|
|
window_count=windows,
|
|
threshold=0.5,
|
|
negative_threshold=0.35,
|
|
)
|
|
if any(type(evidence.get(k)) is not type(v) or evidence[k] != v for k, v in fixed.items()):
|
|
return False
|
|
probabilities = evidence.get("probabilities")
|
|
if (
|
|
count < 512
|
|
or not 1 <= windows <= 10000
|
|
or not isinstance(probabilities, list)
|
|
or len(probabilities) != windows
|
|
or any(
|
|
type(p) not in (int, float) or not math.isfinite(p) or not 0 <= p < 0.35
|
|
for p in probabilities
|
|
)
|
|
):
|
|
return False
|
|
peak = evidence.get("max_speech_probability")
|
|
channels = evidence.get("channel_probabilities")
|
|
if (
|
|
not isinstance(channels, list)
|
|
or len(channels) != 2
|
|
or any(not isinstance(values, list) or len(values) != windows for values in channels)
|
|
or any(
|
|
type(p) not in (int, float) or not math.isfinite(p) or not 0 <= p < 0.35
|
|
for values in channels
|
|
for p in values
|
|
)
|
|
or probabilities != [max(values) for values in zip(*channels, strict=True)]
|
|
):
|
|
return False
|
|
return type(peak) in (int, float) and math.isfinite(peak) and peak == max(probabilities)
|