""" Wan 2.2 Image-to-Video with Extend chaining. Each segment calls Casey's Runpod Wan 2.2 TI2V endpoint when RUNPOD_API_KEY is set (endpoint id defaults to op765mx2yf4kan). If that secret is missing, or WAN_BACKEND=zerogpu, segments fall back to the public ZeroGPU Space. Clips are stitched with ffmpeg for ~10–20s videos. """ from __future__ import annotations import os import shutil import subprocess import tempfile import time import uuid from pathlib import Path from typing import Any import cv2 import gradio as gr OWNER_USERNAME = "Simzy" def require_owner(profile) -> None: """Refuse before any RunPod/GPU call unless the signed-in HF user is the owner.""" if profile is None or getattr(profile, "username", None) != OWNER_USERNAME: raise gr.Error( f"Not allowed — sign in as `{OWNER_USERNAME}` with the link above, then retry." ) def auth_status_markdown(profile: gr.OAuthProfile | None = None) -> str: """Show whether HF OAuth session is present (owner-only Space).""" if profile is None: return ( "Not signed in. Use **Sign in with Hugging Face** below " f"(owner-only: `{OWNER_USERNAME}`)." ) name = getattr(profile, "username", None) or "?" if name == OWNER_USERNAME: return f"Signed in as **{name}** — Generate / Extend unlocked." return ( f"Signed in as **{name}**. This Space is owner-only " f"(`{OWNER_USERNAME}`). Logout, then sign in as the owner." ) # Gradio's client often swallows in-app /Button clicks (SPA router / # broken JS deps on Spaces). Use a capture-phase document listener in # Blocks(head=...) plus plain HTML buttons. Hidden LoginButton is still # required so Gradio mounts /login/huggingface (blocks.expects_oauth). # The huggingface.co Space iframe is sandboxed without allow-top-navigation, # so window.top.location throws. Sending /login inside that iframe also fails: # the Hugging Face login page refuses to render in a frame. Open a first-party # tab instead (allow-popups is set). On the direct *.hf.space page, stay in # this window. AUTH_HEAD_JS = """ """ AUTH_BUTTONS_HTML = """
""" import numpy as np import runpod_i2v from gradio_client import Client, handle_file from PIL import Image, ImageOps # Upstream Wan 2.2 I2V (ZeroGPU) — same Space Command Center uses UPSTREAM_SPACE = os.environ.get("WAN_UPSTREAM_SPACE", "kulkas2pintu/wan222") UPSTREAM_API = "generate_video" DEFAULT_PROMPT = "make this image come alive, cinematic motion, smooth animation" DEFAULT_NEGATIVE = ( "色调艳丽, 过曝, 静态, 细节模糊不清, 字幕, 风格, 作品, 画作, 画面, 静止, " "整体发灰, 最差质量, 低质量, JPEG压缩残留, 丑陋的, 残缺的, 多余的手指, " "画得不好的手部, 画得不好的脸部, 畸形的, 毁容的, 形态畸形的肢体, 手指融合, " "静止不动的画面, 杂乱的背景, 三条腿, 背景人很多, 倒着走" ) EXTEND_START_LAST_FRAME = "Last frame from video" EXTEND_START_UPLOAD = "Upload custom image" WORK = Path(tempfile.gettempdir()) / "wan22_extend" WORK.mkdir(parents=True, exist_ok=True) _client: Client | None = None def get_client() -> Client: global _client if _client is None: token = os.environ.get("HF_TOKEN") or os.environ.get("HUGGING_FACE_HUB_TOKEN") kwargs: dict[str, Any] = {} if token: kwargs["hf_token"] = token _client = Client(UPSTREAM_SPACE, **kwargs) return _client def _session_dir() -> Path: d = WORK / uuid.uuid4().hex[:12] d.mkdir(parents=True, exist_ok=True) return d def _as_image_source(img, _depth: int = 0): """Unwrap Gradio file-data / (image, caption) values to a PIL-loadable source.""" if img is None or _depth > 4: return img if isinstance(img, dict): inner = img.get("path") or img.get("name") or img.get("image") or img.get("url") return _as_image_source(inner, _depth + 1) if isinstance(img, (list, tuple)) and img: return _as_image_source(img[0], _depth + 1) return img def load_pil_image(img: Image.Image | np.ndarray | str | Path | dict | None) -> Image.Image | None: """Load an upload as RGB, honoring iPhone EXIF orientation.""" img = _as_image_source(img) if img is None: return None if isinstance(img, Image.Image): pil = img elif isinstance(img, np.ndarray): arr = img if arr.ndim == 2: arr = np.stack([arr, arr, arr], axis=-1) if arr.shape[-1] == 4: arr = arr[..., :3] pil = Image.fromarray(arr.astype("uint8")) elif isinstance(img, (str, Path)): src = Path(img) if not src.exists() or not src.is_file(): return None try: with Image.open(src) as opened: pil = opened.copy() except Exception: return None else: return None try: pil = ImageOps.exif_transpose(pil) except Exception: pass return pil.convert("RGB") def save_image(img: Image.Image | np.ndarray | str | Path | dict | None, dest: Path) -> Path | None: pil = load_pil_image(img) if pil is None: return None out = dest.with_suffix(".png") pil.save(out) return out def _even(n: int) -> int: n = int(n) if n < 2: return 2 return n - (n % 2) def probe_video_size(video_path: str | Path) -> tuple[int, int]: """Even pixel size of the first video stream (the size players lock onto).""" video_path = str(video_path) try: out = subprocess.check_output( [ "ffprobe", "-v", "error", "-select_streams", "v:0", "-show_entries", "stream=width,height", "-of", "csv=p=0:s=x", video_path, ], text=True, stderr=subprocess.DEVNULL, ).strip() w_s, h_s = out.split("x", 1) w, h = _even(int(w_s)), _even(int(h_s)) if w >= 2 and h >= 2: return w, h except Exception: pass cap = cv2.VideoCapture(video_path) w = _even(int(cap.get(cv2.CAP_PROP_FRAME_WIDTH) or 0)) h = _even(int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT) or 0)) cap.release() if w < 2 or h < 2: raise RuntimeError(f"Could not read video size: {video_path}") return w, h def cover_resize(image: Image.Image, width: int, height: int) -> Image.Image: """Scale-and-center-crop so `image` fills width×height without letterboxing.""" width, height = _even(width), _even(height) image = image.convert("RGB") src_w, src_h = image.size if src_w < 1 or src_h < 1: raise ValueError("Custom image has no pixels.") if src_w == width and src_h == height: return image scale = max(width / src_w, height / src_h) new_w = max(1, int(round(src_w * scale))) new_h = max(1, int(round(src_h * scale))) resized = image.resize((new_w, new_h), Image.Resampling.LANCZOS) left = max(0, (new_w - width) // 2) top = max(0, (new_h - height) // 2) cropped = resized.crop((left, top, left + width, top + height)) if cropped.size != (width, height): cropped = cropped.resize((width, height), Image.Resampling.LANCZOS) return cropped def reference_video_path(state: dict) -> Path | None: """Clip whose geometry later segments must match. Prefer the original segment.""" segs = state.get("segments") or [] if segs: first = Path(str(segs[0])) if first.exists(): return first video = state.get("video") if video: current = Path(str(video)) if current.exists(): return current return None def extract_last_frame(video_path: str | Path, out_path: Path) -> Path: video_path = str(video_path) cap = cv2.VideoCapture(video_path) if not cap.isOpened(): raise RuntimeError(f"Could not open video: {video_path}") total = int(cap.get(cv2.CAP_PROP_FRAME_COUNT) or 0) frame = None if total > 0: cap.set(cv2.CAP_PROP_POS_FRAMES, max(0, total - 1)) ok, frame = cap.read() if not ok or frame is None: cap.set(cv2.CAP_PROP_POS_FRAMES, 0) while True: ok, f = cap.read() if not ok: break frame = f else: while True: ok, f = cap.read() if not ok: break frame = f cap.release() if frame is None: raise RuntimeError("No frames found in video.") out_path = out_path.with_suffix(".png") cv2.imwrite(str(out_path), frame) return out_path def video_duration_seconds(video_path: str | Path) -> float: video_path = str(video_path) try: out = subprocess.check_output( [ "ffprobe", "-v", "error", "-show_entries", "format=duration", "-of", "default=noprint_wrappers=1:nokey=1", video_path, ], text=True, stderr=subprocess.DEVNULL, ).strip() return float(out) except Exception: cap = cv2.VideoCapture(video_path) fps = cap.get(cv2.CAP_PROP_FPS) or 16.0 frames = cap.get(cv2.CAP_PROP_FRAME_COUNT) or 0 cap.release() if fps > 0 and frames > 0: return frames / fps return 0.0 # Wan I2V on kulkas2pintu/wan222 exports at FIXED_FPS (16) unless fluidity # interpolation is requested. Concat has always retimed segments to 16fps. OUTPUT_FPS = 16 def concat_videos(paths: list[Path], out_path: Path) -> Path: if not paths: raise RuntimeError("No videos to concatenate.") if len(paths) == 1: shutil.copy2(paths[0], out_path) return out_path # iOS/Safari lock onto the first sample's resolution, pixel format, and # SAR. The old scale=trunc(iw/2)*2 kept each clip's own size, so a custom # extend still (different aspect → different Wan output size) was stream- # copied in as a mid-file geometry change. Players then hold the last # decodable frame — a freeze exactly at the custom-frame join. width, height = probe_video_size(paths[0]) fps = OUTPUT_FPS # Cover-crop into the first clip's canvas. `pad` rejects inputs that are # already larger on one side (portrait → landscape), which is the custom- # image case. Crop with a clamped window, then scale, so a 1px rounding # shortfall cannot fail the filter. crop_w = f"if(lt(iw\\,{width})\\,iw\\,{width})" crop_h = f"if(lt(ih\\,{height})\\,ih\\,{height})" vf = ( f"scale={width}:{height}:force_original_aspect_ratio=increase," f"crop=w={crop_w}:h={crop_h}," f"scale={width}:{height}," f"setsar=1,fps={fps},format=yuv420p" ) list_file = out_path.parent / "concat_list.txt" normalized: list[Path] = [] for i, p in enumerate(paths): norm = out_path.parent / f"norm_{i:03d}.mp4" subprocess.run( [ "ffmpeg", "-y", "-i", str(p), "-vf", vf, "-c:v", "libx264", "-pix_fmt", "yuv420p", "-an", "-vsync", "cfr", "-r", str(fps), str(norm), ], check=True, capture_output=True, ) normalized.append(norm) with list_file.open("w") as f: for p in normalized: escaped = str(p.resolve()).replace("'", r"'\''") f.write(f"file '{escaped}'\n") subprocess.run( [ "ffmpeg", "-y", "-f", "concat", "-safe", "0", "-i", str(list_file), "-c", "copy", "-movflags", "+faststart", str(out_path), ], check=True, capture_output=True, ) return out_path def _raise_segment_error(action: str, exc: Exception) -> None: """Surface Runpod failures directly. ``action`` is Generation or Extend.""" if isinstance(exc, gr.Error): raise exc if isinstance(exc, runpod_i2v.RunpodError): raise gr.Error(f"{action} failed: {exc}") from exc if runpod_i2v.active_backend() == "runpod": raise gr.Error(f"{action} failed (Runpod): {exc}") from exc if action == "Generation": raise gr.Error( f"Generation failed (ZeroGPU queue/quota or upstream error): {exc}" ) from exc raise gr.Error(f"Extend failed: {exc}") from exc def _runpod_progress(progress, phase: str, elapsed: float) -> None: if not progress: return if phase == "submit": progress(0.12, desc="Sending this segment to Runpod…") return if phase == "IN_QUEUE": progress( 0.25, desc=( "Waiting for a Runpod worker " f"(cold start + model download can take several minutes)… {int(elapsed)}s" ), ) return if phase == "IN_PROGRESS": progress(0.6, desc=f"Runpod is generating this segment… {int(elapsed)}s") return if phase == "save": progress(0.92, desc="Saving video…") return if phase: progress(0.4, desc=f"Runpod status: {phase}… {int(elapsed)}s") def call_runpod_i2v( image_path: Path, prompt: str, duration: float, steps: int, negative: str, seed: int, randomize: bool, progress: gr.Progress | None = None, ) -> Path: dest = image_path.parent / f"seg_{uuid.uuid4().hex[:8]}.mp4" return runpod_i2v.generate_i2v( image_path, dest, prompt=prompt or DEFAULT_PROMPT, negative_prompt=negative or DEFAULT_NEGATIVE, duration_seconds=float(duration), steps=int(steps), seed=runpod_i2v.resolve_seed(int(seed), bool(randomize)), on_progress=lambda phase, elapsed: _runpod_progress(progress, phase, elapsed), ) def call_zerogpu_i2v( image_path: Path, prompt: str, duration: float, steps: int, negative: str, seed: int, randomize: bool, quality: int, fps: int, safe_mode: bool, progress: gr.Progress | None = None, ) -> Path: client = get_client() if progress: progress(0.1, desc=f"Calling {UPSTREAM_SPACE} (ZeroGPU queue)…") # Match generate_video argument order from kulkas2pintu/wan222 /config result = client.predict( handle_file(str(image_path)), # input image None, # last image (optional end-frame; we chain via last-frame start instead) prompt or DEFAULT_PROMPT, int(steps), negative or DEFAULT_NEGATIVE, float(duration), 1.0, # guidance high 1.0, # guidance low int(seed), bool(randomize), int(quality), "UniPCMultistep", 3.0, # flow shift int(fps), True, # display result bool(safe_mode), api_name=f"/{UPSTREAM_API}", ) # Returns (video, download_file, seed) typically video_ref = None if isinstance(result, (list, tuple)): video_ref = result[0] else: video_ref = result if isinstance(video_ref, dict): video_path = video_ref.get("video") or video_ref.get("path") or video_ref.get("url") if isinstance(video_path, dict): video_path = video_path.get("path") or video_path.get("url") else: video_path = video_ref if not video_path or not Path(str(video_path)).exists(): raise RuntimeError( f"Upstream did not return a local video path. Got: {type(result)} {str(result)[:300]}" ) dest = image_path.parent / f"seg_{uuid.uuid4().hex[:8]}.mp4" shutil.copy2(str(video_path), dest) return dest def call_wan_i2v( image_path: Path, prompt: str, duration: float, steps: int, negative: str, seed: int, randomize: bool, quality: int, fps: int, safe_mode: bool, progress: gr.Progress | None = None, ) -> Path: """One I2V segment. Runpod when secrets are set, otherwise ZeroGPU.""" if runpod_i2v.active_backend() == "runpod": return call_runpod_i2v( image_path, prompt, duration, steps, negative, seed, randomize, progress, ) return call_zerogpu_i2v( image_path, prompt, duration, steps, negative, seed, randomize, quality, fps, safe_mode, progress, ) def status_line(segments: list[str], duration_est: float) -> str: n = len(segments) if runpod_i2v.active_backend() == "runpod": backend = f"Runpod `{runpod_i2v.endpoint_base_url().split('?', 1)[0]}`" else: backend = f"ZeroGPU `{UPSTREAM_SPACE}` (fallback — queued/quota-limited)" return ( f"**Segments:** {n} · **Duration ≈ {duration_est:.1f}s** · {backend}" ) def estimate_total(seg_duration: float, num_segments: int) -> float: return float(seg_duration) * int(num_segments) def resolve_extend_prompt(extend_prompt: str | None, generate_prompt: str | None, state: dict) -> str: """Prefer dedicated Extend prompt; else Generate prompt / saved state / default.""" if extend_prompt and str(extend_prompt).strip(): return str(extend_prompt).strip() if generate_prompt and str(generate_prompt).strip(): return str(generate_prompt).strip() return state.get("prompt") or DEFAULT_PROMPT def resolve_extend_start_image( extend_start: str, custom_image, state: dict, sess: Path, ) -> Path: """Pick I2V start image for an Extend segment.""" mode = (extend_start or EXTEND_START_LAST_FRAME).strip() if mode == EXTEND_START_UPLOAD: if custom_image is None: raise gr.Error("Upload a custom extend start image, or switch to “Last frame from video”.") pil = load_pil_image(custom_image) if pil is None: raise gr.Error("Could not read the custom extend start image.") # Match the clip Wan already produced. Upstream resize_image keeps # aspect ratio, so a differently shaped still (typical iPhone photo) # comes back at another resolution and the join freezes on playback. ref = reference_video_path(state) if ref is not None: try: w, h = probe_video_size(ref) pil = cover_resize(pil, w, h) except Exception as e: raise gr.Error( f"Could not match the custom image to the current video size: {e}" ) from e saved = save_image(pil, sess / f"extend_custom_{uuid.uuid4().hex[:8]}") if saved is None: raise gr.Error("Could not read the custom extend start image.") return saved # Default: last frame from current video last = Path(state["last_frame"]) if state.get("last_frame") else None if last is None or not last.exists(): if not state.get("video"): raise gr.Error("Generate a clip first, then use Extend.") last = extract_last_frame(state["video"], sess / "last_frame") state["last_frame"] = str(last) return last def do_generate( image, prompt, seg_duration, steps, negative, seed, randomize, quality, fps, safe_mode, state: dict, progress=gr.Progress(track_tqdm=False), profile: gr.OAuthProfile | None = None, ): require_owner(profile) if image is None: raise gr.Error("Upload a still image first.") sess = Path(state.get("dir") or str(_session_dir())) sess.mkdir(parents=True, exist_ok=True) state["dir"] = str(sess) img_path = save_image(image, sess / "input") if img_path is None: raise gr.Error("Could not read the uploaded image.") progress(0.05, desc="Starting generation…") try: clip = call_wan_i2v( img_path, prompt, float(seg_duration), int(steps), negative, int(seed), bool(randomize), int(quality), int(fps), bool(safe_mode), progress, ) except Exception as e: _raise_segment_error("Generation", e) out = sess / "current.mp4" shutil.copy2(clip, out) last = extract_last_frame(out, sess / "last_frame") dur = video_duration_seconds(out) state["segments"] = [str(clip)] state["video"] = str(out) state["last_frame"] = str(last) state["prompt"] = prompt progress(1.0, desc="Done") return ( str(out), str(out), status_line(state["segments"], dur), str(last), state, ) def do_extend( prompt, extend_prompt, extend_start, custom_extend_image, seg_duration, steps, negative, seed, randomize, quality, fps, safe_mode, state: dict, progress=gr.Progress(track_tqdm=False), profile: gr.OAuthProfile | None = None, ): require_owner(profile) if not state or not state.get("video"): raise gr.Error("Generate a clip first, then use Extend.") if len(state.get("segments", [])) >= 6: raise gr.Error("Segment cap reached (6). Reset and start a new chain.") sess = Path(state["dir"]) start_img = resolve_extend_start_image(extend_start, custom_extend_image, state, sess) use_prompt = resolve_extend_prompt(extend_prompt, prompt, state) mode_label = (extend_start or EXTEND_START_LAST_FRAME).strip() progress(0.05, desc=f"Extending ({mode_label})…") try: clip = call_wan_i2v( start_img, use_prompt, float(seg_duration), int(steps), negative, int(seed), bool(randomize), int(quality), int(fps), bool(safe_mode), progress, ) except Exception as e: _raise_segment_error("Extend", e) segs = [Path(p) for p in state["segments"]] + [clip] out = sess / f"current_{len(segs)}.mp4" try: concat_videos(segs, out) except Exception as e: raise gr.Error(f"Concat failed (ffmpeg): {e}") from e # Always refresh last_frame from the new concatenated video so later # “Last frame from video” extends stay correct. last = extract_last_frame(out, sess / "last_frame") dur = video_duration_seconds(out) state["segments"] = [str(p) for p in segs] state["video"] = str(out) state["last_frame"] = str(last) state["prompt"] = use_prompt progress(1.0, desc="Extended") return ( str(out), str(out), status_line(state["segments"], dur), str(last), state, ) def do_auto_extend( target_seconds, prompt, extend_prompt, extend_start, custom_extend_image, seg_duration, steps, negative, seed, randomize, quality, fps, safe_mode, state: dict, progress=gr.Progress(track_tqdm=False), profile: gr.OAuthProfile | None = None, ): require_owner(profile) if not state or not state.get("video"): raise gr.Error("Generate a first clip, then Auto-extend.") target = float(target_seconds) max_segs = 6 video, download, status, last_preview, state = ( state.get("video"), state.get("video"), status_line(state.get("segments", []), video_duration_seconds(state["video"])), state.get("last_frame"), state, ) first = True while len(state.get("segments", [])) < max_segs: dur = video_duration_seconds(state["video"]) if dur >= target: break progress( len(state["segments"]) / max_segs, desc=f"Auto-extend: {dur:.1f}s / {target:.0f}s…", ) # Custom image only applies to the first auto-extend segment; # later segments chain from the updated video last frame. this_start = extend_start if first else EXTEND_START_LAST_FRAME this_custom = custom_extend_image if first else None video, download, status, last_preview, state = do_extend( prompt, extend_prompt, this_start, this_custom, seg_duration, steps, negative, seed, randomize, quality, fps, safe_mode, state, progress, ) first = False time.sleep(0.3) return video, download, status, last_preview, state def do_reset(state: dict): if state and state.get("dir"): try: shutil.rmtree(state["dir"], ignore_errors=True) except Exception: pass return None, None, status_line([], 0), None, {} def update_estimate(seg_duration, num_planned): total = estimate_total(seg_duration, num_planned) return f"Planned length ≈ **{total:.1f}s** ({num_planned} × {seg_duration:.1f}s segments). Cap: 6 segments." def toggle_custom_extend_image(mode: str): visible = (mode or "").strip() == EXTEND_START_UPLOAD return gr.update(visible=visible) def default_steps() -> int: """Runpod's usual setting is 10. ZeroGPU stays on the faster default of 4.""" return 10 if runpod_i2v.active_backend() == "runpod" else 4 def intro_markdown() -> str: if runpod_i2v.active_backend() == "runpod": endpoint = runpod_i2v.endpoint_id() return f""" # Wan 2.2 Image → Video + Extend Upload a still, **Generate** a short Wan 2.2 clip, then **Extend** from the last frame (or a custom start image) to chain ~10–20s videos. Each segment runs on **Runpod** endpoint `{endpoint}` (`wan22-extend-i2v`). **The first Generate may take several minutes** — the worker scales from zero (cold start) and may download the model before it starts. Extend and Auto-extend call Runpod again for every segment. """ return f""" # Wan 2.2 Image → Video + Extend Upload a still, generate a short Wan 2.2 clip on **free ZeroGPU**, then **Extend** from the last frame (or a custom start image) to chain ~10–20s videos. ZeroGPU is the fallback while `RUNPOD_API_KEY` is unset. It is free but **queued and quota-limited** — each segment often takes 1–3 minutes when busy. Upstream: `{UPSTREAM_SPACE}`. """ def cold_start_note() -> str: if runpod_i2v.active_backend() != "runpod": return "" return ( "> **First Generate may take several minutes** (cold start + model download). " "The Runpod worker is scaled to zero when idle. Leave this tab open — " "the clip shows up here when the segment finishes. Later segments are faster " "while a worker stays warm." ) def limits_markdown() -> str: if runpod_i2v.active_backend() == "runpod": endpoint = runpod_i2v.endpoint_id() return f""" ### How Extend works 1. **Generate** — still image + Generate prompt → short Wan 2.2 I2V clip (~3.5s default). 2. **Extend** — start from the **last frame** (or a **custom uploaded image**), optional separate **Extend prompt**, run I2V again, **concatenate**. 3. Repeat until you hit ~10–20s (or use **Auto-extend**). Soft cap: **6 segments**. ### Limits - Every segment calls Runpod `{endpoint}`. The first call after idle can take several minutes (cold start + model download). - If Runpod fails, the error is shown here and earlier clips are left as they are. - Steps, seed, and the negative prompt are sent to Runpod. Clips are 16 fps. Segment length becomes Wan frames (4n+1, up to 81 / ~5s). - Fluidity FPS, video quality, and Safe Mode apply to the ZeroGPU fallback only. """ return f""" ### How Extend works 1. **Generate** — still image + Generate prompt → short Wan 2.2 I2V clip (~3.5s default). 2. **Extend** — start from the **last frame** (or a **custom uploaded image**), optional separate **Extend prompt**, run I2V again, **concatenate**. 3. Repeat until you hit ~10–20s (or use **Auto-extend**). Soft cap: **6 segments**. ### Limits - Fallback path: public ZeroGPU Space `{UPSTREAM_SPACE}` — free, but **queued / quota-limited**. - Set Space secrets `RUNPOD_API_KEY` and `RUNPOD_ENDPOINT_ID=op765mx2yf4kan` to use Runpod instead. - Single segments are typically a few seconds; longer clips = chained Extends. - Hugging Face Terms of Service apply to hosted Spaces. """ CSS = """ .big-btn button { font-size: 1.15rem !important; min-height: 3rem !important; } #status-md { font-size: 1rem; } #auth-status-md { font-size: 0.95rem; } .hf-auth-bar { display: flex; flex-wrap: wrap; gap: 0.75rem; align-items: center; margin: 0.4rem 0 0.75rem; } .hf-auth-btn { cursor: pointer; padding: 0.7rem 1.15rem; border-radius: 8px; font-size: 1rem; font-weight: 600; border: 1px solid #333; color: #fff; background: #0b0f19; } .hf-auth-btn:hover { filter: brightness(1.12); } .hf-auth-logout { background: #3f3f46; font-weight: 500; } footer { display: none !important; } """ def build_ui(): runpod = runpod_i2v.active_backend() == "runpod" with gr.Blocks( title="Wan 2.2 Extend — I2V", css=CSS, theme=gr.themes.Soft(), head=AUTH_HEAD_JS, ) as demo: gr.Markdown(intro_markdown()) auth_status = gr.Markdown( "Checking sign-in…", elem_id="auth-status-md", ) # Mounts OAuth routes (/login/huggingface). Must exist for expects_oauth. gr.LoginButton(visible=False) gr.HTML(AUTH_BUTTONS_HTML) gr.Markdown( f"Sign-in opens Hugging Face login in a **new tab** " f"(the Space frame is not allowed to navigate the page around it). " f"Finish login there, then use that tab. Owner-only: `{OWNER_USERNAME}`." ) demo.load(auth_status_markdown, inputs=None, outputs=[auth_status]) state = gr.State({}) with gr.Row(): with gr.Column(scale=1): image = gr.Image(label="Still image", type="pil", height=320) prompt = gr.Textbox( label="Generate prompt", value=DEFAULT_PROMPT, lines=3, placeholder="Describe the motion you want…", ) with gr.Accordion("Extend options", open=True): extend_prompt = gr.Textbox( label="Extend prompt", value="", lines=3, placeholder="Leave blank to reuse Generate prompt", info="Optional. Used only for Extend / Auto-extend.", ) extend_start = gr.Radio( choices=[EXTEND_START_LAST_FRAME, EXTEND_START_UPLOAD], value=EXTEND_START_LAST_FRAME, label="Extend start", info="Default keeps current behavior (auto last frame).", ) custom_extend_image = gr.Image( label="Custom extend start image (replaces auto last frame)", type="pil", height=220, visible=False, ) with gr.Row(): seg_duration = gr.Slider( 3.0, 5.0, value=3.5, step=0.5, label="Segment length (seconds)", info="Each Wan call; Extend adds another segment.", ) num_planned = gr.Slider( 1, 6, value=4, step=1, label="Target segments", info="For estimate only (3–5 ≈ 10–20s).", ) estimate = gr.Markdown(update_estimate(3.5, 4)) target_seconds = gr.Slider( 8, 22, value=14, step=1, label="Auto-extend target (seconds)", ) with gr.Accordion("Advanced", open=False): steps = gr.Slider( 1, 12, value=default_steps(), step=1, label="Inference steps", info=( "Sent to Runpod. 10 is the usual setting for this endpoint." if runpod else None ), ) quality = gr.Slider( 1, 10, value=6, step=1, label="Video quality", info="ZeroGPU fallback only." if runpod else None, ) fps = gr.Dropdown( choices=[16, 32, 64], value=16, label="Fluidity FPS", info="ZeroGPU fallback only. Runpod generates at 16 fps." if runpod else None, ) seed = gr.Number(value=42, label="Seed", precision=0) randomize = gr.Checkbox(value=True, label="Randomize seed") safe_mode = gr.Checkbox( value=True, label="Upstream Safe Mode", info=( "ZeroGPU fallback only." if runpod else "Extra ZeroGPU headroom on busy servers." ), ) negative = gr.Textbox( label="Negative prompt", value=DEFAULT_NEGATIVE, lines=2, ) cold_note = cold_start_note() if cold_note: gr.Markdown(cold_note) with gr.Row(elem_classes=["big-btn"]): btn_gen = gr.Button("Generate", variant="primary", size="lg") btn_ext = gr.Button("Extend", variant="secondary", size="lg") with gr.Row(elem_classes=["big-btn"]): btn_auto = gr.Button("Auto-extend to target", size="lg") btn_reset = gr.Button("Reset", size="lg") with gr.Column(scale=1): video = gr.Video(label="Preview", height=360, autoplay=True) status = gr.Markdown(status_line([], 0), elem_id="status-md") last_preview = gr.Image(label="Last frame (next Extend start)", height=180) download = gr.File(label="Download MP4") seg_duration.change(update_estimate, [seg_duration, num_planned], estimate) num_planned.change(update_estimate, [seg_duration, num_planned], estimate) extend_start.change(toggle_custom_extend_image, [extend_start], [custom_extend_image]) gen_inputs = [ image, prompt, seg_duration, steps, negative, seed, randomize, quality, fps, safe_mode, state, ] ext_inputs = [ prompt, extend_prompt, extend_start, custom_extend_image, seg_duration, steps, negative, seed, randomize, quality, fps, safe_mode, state, ] outs = [video, download, status, last_preview, state] btn_gen.click(do_generate, gen_inputs, outs, show_api=False) btn_ext.click(do_extend, ext_inputs, outs, show_api=False) btn_auto.click( do_auto_extend, [target_seconds] + ext_inputs, outs, show_api=False, ) btn_reset.click(do_reset, [state], outs, show_api=False) gr.Markdown(limits_markdown()) return demo if __name__ == "__main__": demo = build_ui() # SSR on Spaces embeds root http://0.0.0.0:7860 into HTML (broken asset/ # OAuth client paths). Force client-side render so relative /login works. demo.queue(default_concurrency_limit=1).launch( server_name="0.0.0.0", server_port=7860, show_api=False, ssr_mode=False, )