267 lines
10 KiB
Python
267 lines
10 KiB
Python
"""原生有声合同独立认证,旧静音配置与序列化保持兼容。"""
|
|
|
|
from copy import deepcopy
|
|
from types import SimpleNamespace
|
|
|
|
import pytest
|
|
from pydantic import ValidationError
|
|
|
|
from app.adapters.provider.video_generation import normalize
|
|
from app.domain.errors import MediaError
|
|
from app.domain.dialogue import DialogueSpec, DialogueSpeaker
|
|
from app.domain.video_generation import defaults, recipe_parameters, required_output_frames
|
|
|
|
NATIVE = "minimax-h3-fl2va-native-av-v1"
|
|
TP2 = "minimax-h3-fl2va-native-av-tp2-v1"
|
|
|
|
|
|
def certificate(profile=NATIVE):
|
|
tp2 = profile == TP2
|
|
return dict(
|
|
profile=profile,
|
|
available=True,
|
|
decision="pass",
|
|
audited=True,
|
|
revision="12253298adb81e36757eabebb81d2ba1c0cc70cc",
|
|
runtime_revision="423ab90580b2847998591669b8326f47b6d14d8b"
|
|
if tp2
|
|
else "f53d552036a0d1bd5570782a39cd40cfabf112bc",
|
|
adapter_contract_version="h3-fl2va-native-av-tp2-v1" if tp2 else "h3-fl2va-native-av-v1",
|
|
output_policy="full_native",
|
|
native_frame_counts=[124, 141, 158] if tp2 else list(range(124, 346, 17)),
|
|
input_modes=["first_frame"] if tp2 else ["first_frame", "first_last_frame"],
|
|
fps=24,
|
|
canvases=[[1344, 768], [768, 1344]],
|
|
upscale=False,
|
|
audio_output=True,
|
|
audio_sample_rate=32000,
|
|
audio_channels=2,
|
|
report_sha256="a" * 64,
|
|
)
|
|
|
|
|
|
def alignment_certificate():
|
|
return dict(
|
|
available=True,
|
|
profile="dialogue-audio-v1",
|
|
revision="f0c48c298fd835726c27956a5d617bad7115627e",
|
|
asr_revision="41f01f3fe87f28c78e2fbf8b568835947dd65ed9",
|
|
content_verification_policy="whisper-sensevoice-consensus-v1",
|
|
secondary_asr_revision="90c1c61912018b70ada0fcc024ea24aca62f2e63",
|
|
secondary_runtime_revision="6991744856587fa44379e8b5dcc432debffeb1be",
|
|
silence_verification_policy="silero-sensevoice-verified-silence-v1",
|
|
silence_vad_revision="be95df9152c0d7618fa1edfeb296fc3dae32376f",
|
|
silence_vad_sha256="1a153a22f4509e292a94e67d6f9b85e8deb25b4988682b7e174c65279d8788e3",
|
|
policy="native_av_v1",
|
|
timestamp_source="whisper-word-timestamps",
|
|
sample_rate=48000,
|
|
report_sha256="b" * 64,
|
|
decision="pass",
|
|
audited=True,
|
|
)
|
|
|
|
|
|
def test_native_fields_are_opt_in_and_legacy_dump_has_no_new_keys():
|
|
assert set(DialogueSpec().model_dump()) == {"enabled", "speakers", "utterances", "sequences"}
|
|
assert "native_voice" not in DialogueSpeaker(id="a" * 32, name="甲").model_dump()
|
|
d = DialogueSpec(generation="native_av", language="Chinese")
|
|
assert d.model_dump()["generation"] == "native_av"
|
|
speaker = DialogueSpeaker(id="a" * 32, name="甲", native_voice="低沉、温和的成年男声")
|
|
assert speaker.model_dump()["native_voice"].startswith("低沉")
|
|
with pytest.raises(ValidationError):
|
|
DialogueSpec(generation="musetalk")
|
|
|
|
|
|
@pytest.mark.parametrize("profile,frames", [(NATIVE, 345), (TP2, 158)])
|
|
def test_native_full_frames_do_not_inherit_legacy_crop(profile, frames):
|
|
p = defaults(profile)
|
|
assert p["num_frames"] == 124 and p["upscale"] is False
|
|
p.update(num_frames=frames)
|
|
out = normalize(p, {"prompt": "角色说话", "first_frame_asset_id": "a" * 32})
|
|
assert required_output_frames(out) == frames
|
|
assert recipe_parameters(p, profile)["num_frames"] == frames
|
|
for invalid in (121, 125, 159, True):
|
|
with pytest.raises(MediaError):
|
|
normalize(
|
|
{**p, "num_frames": invalid}, {"prompt": "说话", "first_frame_asset_id": "a" * 32}
|
|
)
|
|
with pytest.raises(MediaError):
|
|
normalize({**p, "upscale": True}, {"prompt": "说话", "first_frame_asset_id": "a" * 32})
|
|
|
|
|
|
def test_native_tp2_rejects_tail_conditioning():
|
|
with pytest.raises(MediaError):
|
|
normalize(
|
|
{**defaults(TP2), "conditioning_mode": "first_last_frame"},
|
|
{"prompt": "说话", "first_frame_asset_id": "a" * 32, "last_frame_asset_id": "b" * 32},
|
|
)
|
|
|
|
|
|
def test_v5_accepts_native_source_frames_without_changing_old_v4_parameters():
|
|
from app.domain.dialogue import StorySpecV5
|
|
from app.domain.continuity import StorySpecV4
|
|
|
|
for profile, frames in ((NATIVE, 345), (TP2, 158)):
|
|
raw = dict(
|
|
schema_version=5,
|
|
shots=[dict(id="a" * 32, video_parameters=dict(profile=profile, source_frames=frames))],
|
|
)
|
|
assert StorySpecV5.model_validate(raw).shots[0].video_parameters.source_frames == frames
|
|
with pytest.raises(ValidationError):
|
|
StorySpecV4.model_validate({**raw, "schema_version": 4})
|
|
with pytest.raises(ValidationError):
|
|
StorySpecV5.model_validate(
|
|
dict(
|
|
schema_version=5,
|
|
shots=[dict(id="a" * 32, video_parameters=dict(profile=TP2, source_frames=175))],
|
|
)
|
|
)
|
|
|
|
|
|
def test_native_certificate_cannot_inherit_silent_or_musetalk_certificates():
|
|
from app.adapters.provider.native_av import certified, select_evidence
|
|
|
|
cert = certificate()
|
|
assert certified(cert, NATIVE)
|
|
assert select_evidence({"video_generation": {"native_av": cert}}, NATIVE) == cert
|
|
assert not certified(certificate(TP2), NATIVE)
|
|
assert not certified({"available": True, "profile": "minimax-h3-fl2va-bf16-v2"}, NATIVE)
|
|
assert select_evidence({"dialogue": {"lipsync": cert}}, NATIVE) == {}
|
|
for field, value in (
|
|
("audio_output", 1),
|
|
("available", 1),
|
|
("fps", True),
|
|
("report_sha256", None),
|
|
("decision", "reject"),
|
|
("native_frame_counts", [124]),
|
|
("audio_channels", 1),
|
|
("upscale", True),
|
|
):
|
|
broken = deepcopy(cert)
|
|
broken[field] = value
|
|
assert not certified(broken, NATIVE), field
|
|
|
|
|
|
def test_native_alignment_certificate_requires_independent_content_consensus_policy():
|
|
from app.adapters.provider.native_av import alignment_certified
|
|
|
|
cert = alignment_certificate()
|
|
assert alignment_certified(cert)
|
|
for key in (
|
|
"content_verification_policy",
|
|
"secondary_asr_revision",
|
|
"secondary_runtime_revision",
|
|
"silence_verification_policy",
|
|
"silence_vad_revision",
|
|
"silence_vad_sha256",
|
|
):
|
|
old = deepcopy(cert)
|
|
old.pop(key)
|
|
assert not alignment_certified(old), key
|
|
old[key] = "unverified"
|
|
assert not alignment_certified(old), key
|
|
|
|
|
|
async def test_native_requires_own_local_flag_even_when_remote_is_certified():
|
|
from app.adapters.provider.native_av import available
|
|
|
|
settings = SimpleNamespace(
|
|
inference_base_url="http://example.test",
|
|
native_dialogue_enabled=False,
|
|
native_dialogue_tp2_enabled=False,
|
|
dialogue_lipsync_enabled=True,
|
|
)
|
|
|
|
async def snapshot():
|
|
return {
|
|
"video_generation": {"native_av": certificate(), "native_av_tp2": certificate(TP2)},
|
|
"dialogue": {"speech_inspect": {"native_alignment": alignment_certificate()}},
|
|
}
|
|
|
|
provider = SimpleNamespace(settings=settings, upstream_capabilities=snapshot)
|
|
assert not await available(provider, NATIVE)
|
|
settings.native_dialogue_enabled = True
|
|
assert await available(provider, NATIVE)
|
|
assert not await available(provider, TP2)
|
|
|
|
|
|
async def test_system_exposes_native_readiness_separately_from_legacy_dialogue(system):
|
|
system.settings.native_dialogue_enabled = True
|
|
system.settings.dialogue_quality_certified = True
|
|
system.settings.native_dialogue_tp2_enabled = False
|
|
|
|
async def snapshot():
|
|
return {
|
|
"video_generation": {"native_av": certificate(), "native_av_tp2": certificate(TP2)},
|
|
"dialogue": {"speech_inspect": {"native_alignment": alignment_certificate()}},
|
|
}
|
|
|
|
system.provider.upstream_capabilities = snapshot
|
|
data = (await system.client.get("/api/capabilities")).json()
|
|
assert data["dialogue"]["native_av"]["available"] is True
|
|
assert data["dialogue"]["native_av"]["supports_two_faces"] is False
|
|
options = next(r for r in data["items"] if r["task_type"] == "video.generate")["video_profiles"]
|
|
by_profile = {r["profile"]: r for r in options}
|
|
assert by_profile[NATIVE]["available"] is True
|
|
assert by_profile[TP2]["available"] is False
|
|
|
|
|
|
async def test_native_asr_is_independent_of_legacy_speech_and_musetalk_flags(tmp_path):
|
|
from app.adapters.provider.dialogue_gpu import prepare
|
|
|
|
settings = SimpleNamespace(
|
|
inference_base_url="http://example.test",
|
|
native_dialogue_enabled=True,
|
|
native_dialogue_tp2_enabled=False,
|
|
dialogue_speech_enabled=False,
|
|
dialogue_lipsync_enabled=False,
|
|
inference_remote_files=False,
|
|
)
|
|
|
|
async def snapshot():
|
|
return {
|
|
"dialogue": {
|
|
"speech_inspect": {"available": False, "native_alignment": alignment_certificate()}
|
|
}
|
|
}
|
|
|
|
source = tmp_path / "source.wav"
|
|
source.write_bytes(b"boundary")
|
|
provider = SimpleNamespace(
|
|
settings=settings, upstream_capabilities=snapshot, _asset=lambda aid, assets: source
|
|
)
|
|
params = dict(profile="dialogue-audio-v1", sample_rate=48000, native_av_policy="native_av_v1")
|
|
payload = await prepare(
|
|
provider,
|
|
"audio.dialogue.inspect",
|
|
dict(audio_asset_id="a" * 32, speaker_id="b" * 32, expected_text="你好"),
|
|
params,
|
|
{},
|
|
)
|
|
assert payload["parameters"]["native_av_policy"] == "native_av_v1"
|
|
settings.native_dialogue_enabled = False
|
|
with pytest.raises(MediaError):
|
|
await prepare(
|
|
provider,
|
|
"audio.dialogue.inspect",
|
|
dict(audio_asset_id="a" * 32, speaker_id="b" * 32, expected_text="你好"),
|
|
params,
|
|
{},
|
|
)
|
|
|
|
|
|
def test_only_explicit_native_policy_accepts_silent_expected_text():
|
|
from app.adapters.provider.contracts import validate_arguments
|
|
|
|
inputs = dict(audio_asset_id="a" * 32, speaker_id="b" * 32, expected_text="")
|
|
p = dict(profile="dialogue-audio-v1", sample_rate=48000)
|
|
validate_arguments("audio.dialogue.inspect", inputs, {**p, "native_av_policy": "native_av_v1"})
|
|
with pytest.raises(MediaError):
|
|
validate_arguments("audio.dialogue.inspect", inputs, p)
|
|
with pytest.raises(MediaError):
|
|
validate_arguments(
|
|
"audio.dialogue.inspect",
|
|
{**inputs, "expected_text": None},
|
|
{**p, "native_av_policy": "native_av_v1"},
|
|
)
|