Files
CyberGril/samples/companion/tools/generate_transition.py
T

252 lines
9.8 KiB
Python

"""Generate one v2 gaze idle and two forward first/last-frame transitions.
Owns and stops its exact server PID in finally. Does not modify the installation.
"""
import json
import os
import subprocess
import time
import urllib.error
import urllib.request
from pathlib import Path
ROOT = Path("/media/lucian/Downloads/ComfyUI")
TASK = Path("/opt/hel714-material")
URL = "http://127.0.0.1:18189"
def request(path, payload=None):
data = None if payload is None else json.dumps(payload).encode()
req = urllib.request.Request(
URL + path, data=data, headers={"Content-Type": "application/json"}
)
with urllib.request.urlopen(req, timeout=20) as response:
return json.load(response)
def graph(kind):
nodes = {}
def add(key, kind, **inputs):
nodes[str(key)] = dict(class_type=kind, inputs=inputs)
for key, mode in [(1, "high"), (2, "low")]:
add(
key,
"UNETLoader",
unet_name=f"wan2.2_i2v_{mode}_noise_14B_fp8_scaled.safetensors",
weight_dtype="default",
)
add(
key + 2,
"LoraLoaderModelOnly",
model=[str(key), 0],
lora_name=f"wan2.2_i2v_lightx2v_4steps_lora_v1_{mode}_noise.safetensors",
strength_model=1.0,
)
add(key + 4, "ModelSamplingSD3", model=[str(key + 2), 0], shift=5.0)
add(
7,
"CLIPLoader",
clip_name="umt5_xxl_fp8_e4m3fn_scaled.safetensors",
type="wan",
device="cpu",
)
add(8, "VAELoader", vae_name="wan_2.1_vae.safetensors")
add(9, "LoadImage", image="start.png")
add(17, "LoadImage", image="end.png")
add(
10,
"CLIPTextEncode",
clip=["7", 0],
text=(
"Locked off camera, photorealistic young adult woman with short dark hair, "
"cream cardigan and dark trousers seated on the same sofa in the same bedroom. "
"She gently raises her gaze from the book to meet the viewer, then holds a calm listening gaze. "
"Her hands keep holding the open book without moving it. Natural continuous breathing. "
"Fixed seat position, stationary room and furniture, identical face and outfit."
),
)
add(
11,
"CLIPTextEncode",
clip=["7", 0],
text="camera movement, zoom, changing face, changing clothes, standing up, "
"extra fingers, distorted hands, deformed face, moving furniture, subtitles",
)
add(
12,
"WanFirstLastFrameToVideo",
positive=["10", 0],
negative=["11", 0],
vae=["8", 0],
width=512,
height=512,
length=49,
batch_size=1,
start_image=["9", 0],
end_image=["17", 0],
)
add(
13,
"KSamplerAdvanced",
model=["5", 0],
add_noise="enable",
noise_seed=714,
steps=4,
cfg=1.0,
sampler_name="euler",
scheduler="simple",
positive=["12", 0],
negative=["12", 1],
latent_image=["12", 2],
start_at_step=0,
end_at_step=2,
return_with_leftover_noise="enable",
)
add(
14,
"KSamplerAdvanced",
model=["6", 0],
add_noise="disable",
noise_seed=714,
steps=4,
cfg=1.0,
sampler_name="euler",
scheduler="simple",
positive=["12", 0],
negative=["12", 1],
latent_image=["13", 0],
start_at_step=2,
end_at_step=4,
return_with_leftover_noise="disable",
)
add(15, "VAEDecode", samples=["14", 0], vae=["8", 0])
add(16, "SaveWEBM", images=["15", 0], codec="vp9", fps=16.0, filename_prefix="enter", crf=20.0)
nodes["9"]["inputs"]["image"] = "daily-cut.png" if kind == "enter" else "near.png"
nodes["17"]["inputs"]["image"] = "daily-cut.png" if kind == "return" else "near.png"
nodes["10"]["inputs"]["text"] = {
"enter": "Photorealistic adult woman, same short dark hair, cream cardigan, same bedroom. Starting from reading, she gently raises her head to meet the viewer. Smooth slow camera push toward her half body, ending at the exact reference framing. Hands keep holding the same book. Continuous physically plausible movement, stable face, stable clothing and light.",
"return": "Photorealistic adult woman, same short dark hair, cream cardigan, same bedroom. She gently lowers her gaze to the book and returns to reading, with a smooth slow camera pull back to the exact final reference. Same hands holding the same book, continuous physically plausible movement, stable face, stable clothing and light.",
"idle": "Locked camera, photorealistic adult woman in cream cardigan in the same bedroom. Calmly looks toward the viewer, lips gently closed, one subtle natural blink and breathing. Keeps holding the open book still. Stable face, clothing, seat, camera, light and room.",
}[kind]
nodes["11"]["inputs"]["text"] = (
"changing identity, changing clothes, extra fingers, distorted hands, deformed face, melting furniture, subtitles, speaking mouth"
)
nodes["12"]["inputs"]["length"] = 81 if kind == "idle" else 49
nodes["16"]["inputs"]["filename_prefix"] = kind
return nodes
def main():
(TASK / "assets").mkdir(parents=True, exist_ok=True)
# Explicit first/last frames are prepared from the approved v2 source.
assert (TASK / "assets/daily-cut.png").exists()
assert (TASK / "assets/near.png").exists()
if list(TASK.glob("*-history.json")):
raise RuntimeError("Use a new task directory; existing generation evidence is preserved")
for name in ["user", "temp"]:
(TASK / name).mkdir(exist_ok=True)
os.environ["HF_HUB_OFFLINE"] = "1"
os.environ["TRANSFORMERS_OFFLINE"] = "1"
command = [
str(ROOT / "venv/bin/python"),
str(ROOT / "main.py"),
"--listen",
"127.0.0.1",
"--port",
"18189",
"--disable-all-custom-nodes",
"--offline",
"--disable-manager-ui",
"--disable-assets",
"--reserve-vram",
"8",
"--disable-dynamic-vram",
"--input-directory",
str(TASK / "assets"),
"--output-directory",
str(TASK),
"--user-directory",
str(TASK / "user"),
"--temp-directory",
str(TASK / "temp"),
"--database-url",
"sqlite:///:memory:",
]
# Refuse to attach to an unrelated listener.
import socket
with socket.socket() as sock:
if sock.connect_ex(("127.0.0.1", 18189)) == 0:
raise RuntimeError("Task port already occupied; no process started")
with open(TASK / "server.log", "w") as log:
server = subprocess.Popen(command, cwd=ROOT, stdout=log, stderr=subprocess.STDOUT)
(TASK / "process.json").write_text(json.dumps(dict(pid=server.pid, command=command)))
try:
deadline = time.monotonic() + 90
while True:
if server.poll() is not None:
raise RuntimeError(f"ComfyUI exited: {server.returncode}; see server.log")
try:
info = request("/object_info")
break
except (OSError, urllib.error.URLError):
if time.monotonic() > deadline:
raise TimeoutError("Startup exceeded 90 seconds") from None
time.sleep(1)
for kind in ["idle", "enter", "return"]:
workflow = graph(kind)
(TASK / f"{kind}-workflow.json").write_text(json.dumps(workflow, indent=2))
required = {n["class_type"]: info.get(n["class_type"]) for n in workflow.values()}
(TASK / f"{kind}-nodes.json").write_text(json.dumps(required, indent=2))
if any(v is None for v in required.values()):
raise RuntimeError("Required native node unavailable; see nodes.json")
result = request("/prompt", {"prompt": workflow})
(TASK / f"{kind}-submission.json").write_text(json.dumps(result))
prompt_id = result["prompt_id"]
print(
f"Generating one clip: {prompt_id}; owned server PID {server.pid}", flush=True
)
deadline = time.monotonic() + 2400
last_report = 0
while time.monotonic() < deadline:
history = request("/history/" + prompt_id)
if prompt_id in history:
(TASK / f"{kind}-history.json").write_text(json.dumps(history, indent=2))
print(json.dumps(history[prompt_id]["status"]), flush=True)
if history[prompt_id]["status"]["status_str"] != "success":
raise RuntimeError("Generation failed; see history.json")
break
if time.monotonic() - last_report > 30:
print(
"Single generation still active; existing services untouched.",
flush=True,
)
last_report = time.monotonic()
time.sleep(3)
else:
request("/interrupt", {})
raise TimeoutError("Single generation exceeded 40 minutes")
except urllib.error.HTTPError as error:
(TASK / "api-error.json").write_bytes(error.read())
raise
finally:
server.terminate()
try:
server.wait(timeout=20)
except subprocess.TimeoutExpired:
server.kill()
server.wait()
print(f"Stopped owned server PID {server.pid}", flush=True)
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--task", type=Path, default=TASK)
TASK = parser.parse_args().task
main()