cmvr-visual/backend/app/domain/video_generation.py

121 lines
4.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""普通视频模型的不可变配置;缺 profile 永远保留历史 Wan 语义。"""
from typing import Literal
H3_PROFILE = "minimax-h3-fl2va-bf16-v1"
H3_V2_PROFILE = "minimax-h3-fl2va-bf16-v2"
H3_TP2_PROFILE = "minimax-h3-fl2va-bf16-tp2-v1"
H3_TP2_MAX_REQUESTED_FRAMES = 145
H3_TP2_MAX_SOURCE_SECONDS = 6
H3_PROFILES = (H3_PROFILE, H3_V2_PROFILE, H3_TP2_PROFILE)
LEGACY_PROFILE = "wan2.2-ti2v-5b-v1"
VideoProfile = Literal[
"minimax-h3-fl2va-bf16-v2",
"minimax-h3-fl2va-bf16-v1",
"minimax-h3-fl2va-bf16-tp2-v1",
"wan2.2-ti2v-5b-v1",
]
def profile_name(parameters):
profile = parameters.get("profile", LEGACY_PROFILE)
if profile not in {*H3_PROFILES, LEGACY_PROFILE}:
raise ValueError("不支持此视频模型配置")
return profile
def defaults(profile, *, explicit=True):
h3 = profile in H3_PROFILES
if profile not in {*H3_PROFILES, LEGACY_PROFILE}:
raise ValueError("不支持此视频模型配置")
result = dict(
width=1344 if h3 else 1280,
height=768 if h3 else 704,
num_inference_steps=50,
seed=42,
num_frames=121,
fps=24,
upscale=not h3,
upscale_width=1920,
upscale_height=1080,
tile_size=128,
)
if profile == H3_V2_PROFILE:
result.update(num_frames=124, conditioning_mode="first_frame")
if not h3:
result["guidance_scale"] = 5.0
if explicit:
result["profile"] = profile
return result
def constraints(profile):
h3 = profile in H3_PROFILES
return {
"width": {"values": [768, 1344]} if h3 else {"min": 256, "max": 1280, "step": 32},
"height": {"values": [768, 1344]} if h3 else {"min": 256, "max": 1280, "step": 32},
"num_inference_steps": {"min": 2 if h3 else 1, "max": 100},
"seed": {"min": 0, "max": 2**32 - 1},
"count": {"min": 1, "max": 1},
"max_pixels": 1344 * 768 if h3 else 1280 * 704,
"num_frames": {"min": 124, "max": 345, "step": 17}
if profile == H3_V2_PROFILE
else {"min": 25, "max": H3_TP2_MAX_REQUESTED_FRAMES, "step": 4}
if profile == H3_TP2_PROFILE
else {"min": 25 if h3 else 5, "max": 337 if h3 else 361, "step": 4},
**(
{"source_seconds": {"min": 1, "max": H3_TP2_MAX_SOURCE_SECONDS}}
if profile == H3_TP2_PROFILE
else {}
),
"fps": {"values": [24]},
"upscale": {"values": [False] if profile == H3_TP2_PROFILE else [True, False]},
"upscale_width": {"min": 256, "max": 3840, "step": 2},
"upscale_height": {"min": 256, "max": 2160, "step": 2},
"tile_size": {"min": 64, "max": 256, "step": 16},
**({} if h3 else {"guidance_scale": {"min": 1, "max": 20}}),
}
def recipe_parameters(parameters, profile, *, portrait=False, validate_duration=True):
"""仅编译新配方;不重算历史 Job、上游原包或已冻结配方。"""
result = dict(parameters)
if profile is not None:
result["profile"] = profile
if profile in H3_PROFILES:
result.update(width=768 if portrait else 1344, height=1344 if portrait else 768)
result.pop("guidance_scale", None)
if profile == H3_TP2_PROFILE:
result["upscale"] = False
if validate_duration and (
type(result.get("num_frames")) is not int
or not 25 <= result["num_frames"] <= H3_TP2_MAX_REQUESTED_FRAMES
or result["num_frames"] % 4 != 1
):
raise ValueError(
"H3双卡单镜头支持1–6秒、25–145请求帧;原时长已保留,请缩短本次生成源长度"
)
if profile == H3_V2_PROFILE:
frames = result.get("source_frames", result.get("num_frames", 124))
if type(frames) is not int or frames not in range(124, 346, 17):
raise ValueError("Invalid H3 v2 source frames")
result.pop("source_frames", None)
result["num_frames"] = frames
elif validate_duration and not 25 <= result["num_frames"] <= 337:
raise ValueError("H3单镜头支持1–14秒;旧15秒镜头请保留Wan或缩短选用区间")
return result
def reuse_parameters(parameters):
result = dict(parameters)
if result.get("profile") == LEGACY_PROFILE:
result.pop("profile")
return result
def required_output_frames(parameters):
frames = parameters.get(
"num_frames", 124 if parameters.get("profile") == H3_V2_PROFILE else 121
)
return frames if parameters.get("profile") in H3_PROFILES else frames - 1