import re from pathlib import Path from ..runner import _integer, _number from . import ( ANALYZE, INSPECT, LATENTSYNC_PREVIEW, SILENCE_POLICY, SOURCE_REST_POLICY, SPEECH, SPEECH_INSPECT, profile, ) def identifier(value): if not isinstance(value, str) or not re.fullmatch("[0-9a-f]{32}", value): raise ValueError("Invalid speaker or track ID") def text(value, limit, required=True): if ( not isinstance(value, str) or len(value) > limit or (required and not value.strip()) ): raise ValueError("Invalid dialogue text") def validate(task, inputs, params, workspace): fields = {"video_path"} controls = {"profile", "fps", "num_frames"} if params.get("profile") == LATENTSYNC_PREVIEW and task == "video.lipsync": if ( params.get("speech_silence_policy") != SOURCE_REST_POLICY or params.get("audio_context_shift_frames", 0) != 0 or not isinstance(inputs.get("speech_tracks"), list) or len(inputs["speech_tracks"]) != 1 ): raise ValueError( "LatentSync preview requires a single unshifted source-rest stem" ) _integer(params.get("num_frames"), "num_frames", 1, 144) elif params.get("profile") != profile(task): raise ValueError("Unrecognized dialogue profile") if task == SPEECH_INSPECT: fields = {"audio_path", "expected_text", "speaker_id"} controls = { "profile", "sample_rate", "source_start_sample", "source_end_sample", "native_av_policy", } if 'native_av_policy' in params and params['native_av_policy'] != 'native_av_v1': raise ValueError('Unknown native AV alignment policy') start = _integer( params.get("source_start_sample", 0), "source_start_sample", 0, 2**40 ) if params.get("source_end_sample") is not None: _integer(params["source_end_sample"], "source_end_sample", start + 1, 2**40) text(inputs.get("expected_text"), 2000, params.get('native_av_policy') != 'native_av_v1') identifier(inputs.get("speaker_id")) _integer(params.get("sample_rate"), "sample_rate", 48000, 48000) if "reference_path" in inputs: fields.add("reference_path") if inputs["reference_path"] == inputs.get("audio_path"): raise ValueError("Voice reference must be independent") elif task == SPEECH: fields = {"prompt"} controls = {"profile", "voice", "style", "speed", "seed", "reference_text"} text(inputs.get("prompt"), 2000) text(params.get("voice"), 200) text(params.get("style", ""), 500, False) text(params.get("reference_text", ""), 2000, False) if "reference_path" in inputs: fields.add("reference_path") elif params.get("reference_text"): raise ValueError("Reference audio is required for its transcript") _number(params.get("speed", 1), "speed", 0.5, 2) _integer(params.get("seed"), "seed", 0, 2**32 - 1) else: _integer(params.get("fps"), "fps", 24, 24) _integer(params.get("num_frames"), "num_frames", 1, 345) if task == ANALYZE: fields.add("reference_paths") controls.add("speaker_ids") ids, paths = params.get("speaker_ids"), inputs.get("reference_paths") if ( not isinstance(ids, list) or not isinstance(paths, list) or not 1 <= len(ids) <= 2 or len(paths) != len(ids) ): raise ValueError("One reference per visible speaker is required") for speaker in ids: identifier(speaker) if len(set(ids)) != len(ids) or len(set(paths)) != len(paths): raise ValueError("Duplicate speaker or reference") else: fields.add("face_tracks_path") controls.update(("seed", "speech_silence_policy")) if params.get("speech_silence_policy", SILENCE_POLICY) not in ( SILENCE_POLICY, SOURCE_REST_POLICY, ): raise ValueError("Unknown speech silence policy") if task == "video.lipsync": controls.add("audio_context_shift_frames") _integer( params.get("audio_context_shift_frames", 0), "audio_context_shift_frames", -6, 6, ) if "speech_tracks" in inputs: fields.add("speech_tracks") rows = inputs["speech_tracks"] if not isinstance(rows, list) or not 1 <= len(rows) <= 2: raise ValueError("Invalid number of speech stems") for row in rows: if not isinstance(row, dict) or set(row) != { "speaker_id", "target_track_id", "audio_path", }: raise ValueError("Invalid stem binding") identifier(row["speaker_id"]) identifier(row["target_track_id"]) for key in ("speaker_id", "target_track_id", "audio_path"): if len({row[key] for row in rows}) != len(rows): raise ValueError("Duplicate stem binding") else: fields.add("audio_path") controls.update(("target_track_id", "speaker_id")) identifier(params.get("target_track_id")) identifier(params.get("speaker_id")) _integer(params.get("seed", 0), "seed", 0, 2**32 - 1) if task == INSPECT: fields.add("base_video_path") if set(inputs) != fields or set(params) - controls: raise ValueError("Unknown or missing dialogue fields") # 服务端请求只接受素材桥接目录,防止把任意内部配置当作媒体传入。 root = Path(workspace).resolve() roots = [root / "inputs", root / "cmvr-visual" / "assets"] for key, value in inputs.items(): if key in {"prompt", "speaker_id", "expected_text"}: continue paths = ( [row["audio_path"] for row in value] if key == "speech_tracks" else value if key == "reference_paths" else [value] ) for name in paths: path = Path(name).resolve(strict=True) if not path.is_file() or not any(path.is_relative_to(r) for r in roots): raise ValueError("Dialogue source outside asset storage") return dict(inputs), dict(params)