casting UX + batching
Browse files51 unmodified lines525354555657585960616263646566673 unmodified lines71727374757677784 unmodified lines838485868788899091929394959697989910010110210310410510610710810911011111211311411511656 unmodified lines17317417517617717817918018118218318418540 unmodified lines22622722822923023123223323417 unmodified lines25225325425525625725825926026126217 unmodified lines28028128228328428528628728828929029129241 unmodified lines33433533633733833934034151 unmodified linesfor n, c, t in ((n1, c1, t1), (n2, c2, t2), (n3, c3, t3))if (n or "").strip()]if cast_preset == "generated" and os.getenv("SPACE_ID"):# HF Spaces: on the Docker Space ComfyUI runs in-container and the# generated flow works normally; on ZeroGPU it can't exist. Probe# rather than assume — but ONLY on Spaces: local runs keep the# normal generated flow (with its own ComfyUI-down handling).from cast_pipeline import _comfy_healthif not _comfy_health():print("[cast] Spaces runtime without ComfyUI — generated cast ""falls back to curated")cast_preset = "curated"if cast_preset == "generated":from cast_pipeline import (set_fast_generation, build_custom_specs,3 unmodified linesset_fast_generation(fast)# Build specs: custom cards override presetsif cards:specs = build_custom_specs(cards)if not specs:specs = [(k, PRESET_CHARACTERS[k]) for k in ["yuki", "marie", "sakura"]]4 unmodified linesyield (*_empty(), None)return# VRAM: evict LLM so ComfyUI can load its modelschar_keys = [k for k, _ in specs]yield (None, None,_casting_screen_html(char_keys, {},"Waking the artists…"),{}, _cast_panel_html({}), "", "Preparing…", None, "", [], 0, None)release_vram()# Generate first base for reviewkey, preset = specs[0]yield (None, None,_loading_screen_html(f"Sketching {preset.get('name', key)}…","the artist is drawing the first portrait"),{}, _cast_panel_html({}), "",f"Generating {preset.get('name', key)}…", None, "", [], 0, None)char_dir = _char_dir(key)base_path = _generate_base_with_face_gate(key, preset, char_dir)new_state = {"specs": specs, "idx": 0, "phase": "review", "fast": fast}screen = _review_screen_html(key, preset, base_path or "", 1, len(specs))info = f"Review: {preset.get('name', key)} · 1/{len(specs)}"yield (None, None, screen, {}, _cast_panel_html({}), "", info, None, "",[], 0, new_state)return # generator ends; on_casting_confirm / on_casting_redo takes over# ── Curated mode (unchanged behaviour) ──────────────────────char_keys = ["yuki", "marie", "sakura"]56 unmodified linesf"{scene.chapter} · {scene.background} ({scene.mood}) · {status}",voice_path, prev_bg, beats, 0, None)def _loading_step(message: str, sub: str = "", info: str = ""):"""An intermediate 12-tuple for the casting generators: swap in the✦ loading screen + status line, gr.skip() everything else."""return (gr.skip(), gr.skip(), _loading_screen_html(message, sub),gr.skip(), gr.skip(), gr.skip(), info or message,gr.skip(), gr.skip(), gr.skip(), gr.skip(), gr.skip())def _apply_review_edits(preset: dict, key: str, casting_prompt: str,casting_desc: str, reseed: bool):"""Fold the review screen's edited prompt + personality back into the40 unmodified linesnext_key, next_preset = specs[next_idx]next_name = next_preset.get("name", next_key)yield _loading_step(f"Sketching {next_name}…","the artist is drawing the next portrait",f"Generating {next_name} · {next_idx + 1}/{len(specs)}")char_dir = _char_dir(next_key)base_path = _generate_base_with_face_gate(next_key, next_preset, char_dir)17 unmodified lines# Bake expressions for all characters (UNET still hot from base gen)members = []bake_warnings: list[str] = []for i, (skey, spreset) in enumerate(specs):sname = spreset.get("name", skey)yield _loading_step(f"Baking expressions — {sname}…",f"character {i + 1} of {len(specs)}",f"Baking expressions · {i + 1}/{len(specs)}")char_dir = _char_dir(skey)base = os.path.join(char_dir, f"{skey}_base.png")try:17 unmodified linesif bake_warnings:names_str = ", ".join(bake_warnings)narrator_sub = f"loading the story model (couldn't draw {names_str} — using a placeholder)"yield _loading_step("Waking the narrator…", narrator_sub, "Loading story model…")comfy_free_vram()ensure_llm(ModelConfig.from_env(), timeout=300)# Start the VNyield _loading_step("The story begins…","the narrator is setting the scene", "Opening scene…")scene = SceneState(cast=cast, present=[],background="classroom", mood="peaceful",41 unmodified linespreset["seed"] = preset.get("seed", 42) + 1name = preset.get("name", key)yield (_loading_screen_html(f"Re-sketching {name}…","the artist is trying again"),f"Regenerating {name} · {idx + 1}/{len(specs)}",gr.skip(), gr.skip())51 unmodified lines52535455565758596061626364656667686970717273747576773 unmodified lines8182838485868788899091924 unmodified lines97989910010110210310410510610710810911011111211311411511611711811956 unmodified lines17617717817918018118218318418518618718818919019119219319419519619719819920020120220320440 unmodified lines24524624724824925025125225325425517 unmodified lines27327427527627727827928028128228328428517 unmodified lines30330430530630730830931031131231331431531631741 unmodified lines35936036136236336436536636736851 unmodified linesfor n, c, t in ((n1, c1, t1), (n2, c2, t2), (n3, c3, t3))if (n or "").strip()]if cast_preset == "generated":# INSTANT feedback: paint the casting surface as the very FIRST# action — before any ComfyUI probe, LLM design, or VRAM work — so# pressing Generate never reads as "nothing happened / back to the# title". One waiting vocabulary (the cards) from here on; the ✦# start-symbol lives in its header and on the active card.preview = [c["name"] for c in cards] or ["?", "?", "?"]yield (None, None,_casting_screen_html(preview, {}, "Assembling your cast…",sub="warming up the studio", active=0),{}, _cast_panel_html({}), "", "Starting…", None, "", [], 0, None)# On Spaces, probe ComfyUI now (a cheap FS check in embed mode); if# it isn't there, fall through to the curated path below.if os.getenv("SPACE_ID"):from cast_pipeline import _comfy_healthif not _comfy_health():print("[cast] Spaces runtime without ComfyUI — ""falling back to curated")cast_preset = "curated"if cast_preset == "generated":from cast_pipeline import (set_fast_generation, build_custom_specs,3 unmodified linesset_fast_generation(fast)# Build specs: custom cards override presets. Designing custom cards# calls the LLM — keep the SAME cards surface up while it thinks.if cards:yield _casting_step([c["name"] for c in cards], {},"Designing your cast…","writing their personalities", active=0)specs = build_custom_specs(cards)if not specs:specs = [(k, PRESET_CHARACTERS[k]) for k in ["yuki", "marie", "sakura"]]4 unmodified linesyield (*_empty(), None)returnchar_keys = [k for k, _ in specs]release_vram() # evict LLM so ComfyUI can load its models# Generate the first base — same cards surface, character 0 active.key, preset = specs[0]yield _casting_step(char_keys, {},f"Sketching {preset.get('name', key)}…","drawing the first portrait", active=0)char_dir = _char_dir(key)base_path = _generate_base_with_face_gate(key, preset, char_dir)new_state = {"specs": specs, "idx": 0, "phase": "review", "fast": fast}screen = _review_screen_html(key, preset, base_path or "", 1, len(specs))info = f"Review: {preset.get('name', key)} · 1/{len(specs)}"yield (None, None, screen, {}, _cast_panel_html({}), "", info, None, "",[], 0, new_state)return # on_casting_confirm / on_casting_redo take over# ── Curated mode (unchanged behaviour) ──────────────────────char_keys = ["yuki", "marie", "sakura"]56 unmodified linesf"{scene.chapter} · {scene.background} ({scene.mood}) · {status}",voice_path, prev_bg, beats, 0, None)def _casting_step(char_keys, resolved, message, sub="", active=None,sprite_paths=None, info=""):"""An intermediate 12-tuple for the casting generators: swap in theUNIFIED casting screen (cards + ✦ header) + status line, gr.skip() thestate fields. This is the single waiting vocabulary for the generatedflow — no separate ✦-only loading screen."""return (gr.skip(), gr.skip(),_casting_screen_html(char_keys, resolved, message, sub=sub,sprite_paths=sprite_paths, active=active),gr.skip(), gr.skip(), gr.skip(), info or message,gr.skip(), gr.skip(), gr.skip(), gr.skip(), gr.skip())def _base_sprites(specs) -> dict:"""key -> base portrait path for any character whose base is already ondisk, so the casting cards show the sprites baked so far (not just …)."""from cast_pipeline import _char_dirout = {}for k, _ in specs:p = os.path.join(_char_dir(k), f"{k}_base.png")if os.path.exists(p):out[k] = preturn outdef _apply_review_edits(preset: dict, key: str, casting_prompt: str,casting_desc: str, reseed: bool):"""Fold the review screen's edited prompt + personality back into the40 unmodified linesnext_key, next_preset = specs[next_idx]next_name = next_preset.get("name", next_key)ckeys = [k for k, _ in specs]yield _casting_step(ckeys, {}, f"Sketching {next_name}…","drawing the next portrait", active=next_idx,sprite_paths=_base_sprites(specs),info=f"Generating {next_name} · {next_idx + 1}/{len(specs)}")char_dir = _char_dir(next_key)base_path = _generate_base_with_face_gate(next_key, next_preset, char_dir)17 unmodified lines# Bake expressions for all characters (UNET still hot from base gen)members = []bake_warnings: list[str] = []ckeys = [k for k, _ in specs]for i, (skey, spreset) in enumerate(specs):sname = spreset.get("name", skey)yield _casting_step(ckeys, {}, f"Baking expressions — {sname}…",f"character {i + 1} of {len(specs)}", active=i,sprite_paths=_base_sprites(specs),info=f"Baking expressions · {i + 1}/{len(specs)}")char_dir = _char_dir(skey)base = os.path.join(char_dir, f"{skey}_base.png")try:17 unmodified linesif bake_warnings:names_str = ", ".join(bake_warnings)narrator_sub = f"loading the story model (couldn't draw {names_str} — using a placeholder)"yield _casting_step(ckeys, cast, "Waking the narrator…", narrator_sub,info="Loading story model…")comfy_free_vram()ensure_llm(ModelConfig.from_env(), timeout=300)# Start the VNyield _casting_step(ckeys, cast, "The story begins…","the narrator is setting the scene",info="Opening scene…")scene = SceneState(cast=cast, present=[],background="classroom", mood="peaceful",41 unmodified linespreset["seed"] = preset.get("seed", 42) + 1name = preset.get("name", key)ckeys = [k for k, _ in specs]yield (_casting_screen_html(ckeys, {}, f"Re-sketching {name}…",sub="the artist is trying again",sprite_paths=_base_sprites(specs), active=idx),f"Regenerating {name} · {idx + 1}/{len(specs)}",gr.skip(), gr.skip())
ui/callbacks.py+69-42