Ship o1key MJ图片生成 with content/style refs and prompt flags, save-GIF with preview, Kling 4.0 helpers, plus GPT/unified image node polish and docs.
145 lines
5.6 KiB
Python
145 lines
5.6 KiB
Python
"""Kling 4.0 request helpers.
|
||
|
||
The released K video node still owns the HTTP task lifecycle. This module
|
||
keeps the Kling 4.0 capability matrix and request shape in one testable place
|
||
so the node only has to upload media and call the existing poll/download
|
||
pipeline.
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
|
||
KlingV4GenerationMode = str
|
||
|
||
GENERATION_MODES = {
|
||
"text": "文生视频",
|
||
"keyframes": "关键帧",
|
||
"omni": "全能参考",
|
||
}
|
||
|
||
ENDPOINTS = {
|
||
"text": ("/kling/v1/videos/text2video", "/kling/v1/videos/text2video/{task_id}"),
|
||
"keyframes": ("/kling/v1/videos/image2video", "/kling/v1/videos/image2video/{task_id}"),
|
||
"omni": ("/kling/v1/videos/omni-video", "/kling/v1/videos/omni-video/{task_id}"),
|
||
}
|
||
|
||
_RESOLUTION_MODE = {"720p": "std", "1080p": "pro"}
|
||
_ASPECT_RATIOS = {"智能", "16:9", "9:16", "1:1", "21:9"}
|
||
|
||
|
||
def canonical_generation_mode(value: str) -> str:
|
||
"""Return the wire value for a displayed Kling 4.0 generation mode."""
|
||
|
||
if value in GENERATION_MODES:
|
||
return value
|
||
for wire_value, display_value in GENERATION_MODES.items():
|
||
if value == display_value:
|
||
return wire_value
|
||
raise ValueError(f"不支持的 Kling 4.0 生成模式:{value}")
|
||
|
||
|
||
def build_kling_v4_body(
|
||
*,
|
||
generation_mode: str,
|
||
prompt: str,
|
||
negative_prompt: str = "",
|
||
duration: int = 5,
|
||
resolution: str = "720p",
|
||
aspect_ratio: str = "智能",
|
||
first_frame: str | None = None,
|
||
middle_frames: list[str] | None = None,
|
||
last_frame: str | None = None,
|
||
reference_images: list[str] | None = None,
|
||
reference_videos: list[str] | None = None,
|
||
keep_original_sound: bool = False,
|
||
) -> dict:
|
||
"""Build a Kling 4.0 proxy request without credentials or media bytes.
|
||
|
||
The proxy accepts the legacy Kling envelope (`model_name`, `mode`, and
|
||
`metadata`) and maps the metadata fields to the official Kling 4.0
|
||
contents/settings representation. Keyframes and Omni references are
|
||
mutually exclusive at the provider boundary.
|
||
"""
|
||
|
||
mode = canonical_generation_mode(generation_mode)
|
||
prompt = str(prompt or "").strip()
|
||
if not prompt:
|
||
raise ValueError("Kling 4.0 生成模式下提示词不能为空")
|
||
if resolution not in _RESOLUTION_MODE:
|
||
raise ValueError("Kling 4.0 仅支持 720p 或 1080p")
|
||
try:
|
||
duration = int(duration)
|
||
except (TypeError, ValueError):
|
||
raise ValueError("Kling 4.0 时长必须是整数秒") from None
|
||
if duration < 3 or duration > 30:
|
||
raise ValueError("Kling 4.0 时长必须在 3~30 秒之间")
|
||
if aspect_ratio not in _ASPECT_RATIOS:
|
||
raise ValueError(f"Kling 4.0 不支持宽高比:{aspect_ratio}")
|
||
|
||
middle_frames = list(middle_frames or [])
|
||
reference_images = list(reference_images or [])
|
||
reference_videos = list(reference_videos or [])
|
||
if len(middle_frames) > 8:
|
||
raise ValueError("Kling 4.0 最多支持 8 张中间关键帧")
|
||
if len(reference_images) > 10:
|
||
raise ValueError("Kling 4.0 最多支持 10 张参考图")
|
||
if len(reference_videos) > 5:
|
||
raise ValueError("Kling 4.0 最多支持 5 段参考视频")
|
||
|
||
body = {
|
||
"model_name": "kling-v4",
|
||
"prompt": prompt,
|
||
"duration": duration,
|
||
"mode": _RESOLUTION_MODE[resolution],
|
||
"watermark_info": {"enabled": False},
|
||
}
|
||
if negative_prompt and str(negative_prompt).strip():
|
||
body["negative_prompt"] = str(negative_prompt).strip()
|
||
|
||
if mode == "text":
|
||
if first_frame or middle_frames or last_frame or reference_images or reference_videos:
|
||
raise ValueError("文生视频模式不能连接图片或视频素材")
|
||
elif mode == "keyframes":
|
||
if not first_frame:
|
||
raise ValueError("关键帧模式必须提供首帧图片")
|
||
if reference_images or reference_videos:
|
||
raise ValueError("关键帧模式不能同时使用全能参考素材")
|
||
if last_frame and not first_frame:
|
||
raise ValueError("尾帧必须与首帧一起提供")
|
||
if len([first_frame, *middle_frames, last_frame] if last_frame else [first_frame, *middle_frames]) > 10:
|
||
raise ValueError("Kling 4.0 关键帧总数不能超过 10 张")
|
||
metadata = {"first_frame": first_frame}
|
||
if middle_frames:
|
||
metadata["middle_frames"] = middle_frames
|
||
if last_frame:
|
||
metadata["last_frame"] = last_frame
|
||
body["metadata"] = metadata
|
||
else:
|
||
if first_frame or middle_frames or last_frame:
|
||
raise ValueError("全能参考模式不能同时使用关键帧")
|
||
if not reference_images and not reference_videos:
|
||
raise ValueError("全能参考模式至少需要一张参考图或一段参考视频")
|
||
if len(reference_images) + len(reference_videos) > 15:
|
||
raise ValueError("Kling 4.0 全能参考素材总数不能超过 15 个")
|
||
metadata = {}
|
||
if reference_images:
|
||
metadata["refer_images"] = reference_images
|
||
if reference_videos:
|
||
metadata["videos"] = reference_videos
|
||
if keep_original_sound:
|
||
body["keep_original_sound"] = True
|
||
body["metadata"] = metadata
|
||
|
||
# The first frame determines the output shape. For text/Omni requests,
|
||
# "智能" is represented by the provider's adaptive ratio value.
|
||
if mode != "keyframes":
|
||
body["aspect_ratio"] = "adaptive" if aspect_ratio == "智能" else aspect_ratio
|
||
return body
|
||
|
||
|
||
def endpoint_for(generation_mode: str) -> tuple[str, str]:
|
||
"""Return create/status paths for a canonical or displayed mode."""
|
||
|
||
return ENDPOINTS[canonical_generation_mode(generation_mode)]
|
||
|