wan-studio-pro / app.py
vimalprasad37
Issue fix2
25bca48
Raw History Blame Contribute Delete
228 kB
import os try: import spaces except ImportError: os.system('pip install --upgrade --no-deps spaces') import spaces import shutil import subprocess import sys import copy import random import tempfile import warnings import time import threading import gc import uuid from tqdm import tqdm import cv2 import numpy as np import torch import torch._dynamo from torch.nn import functional as F from PIL import Image, ImageDraw, ImageFilter import gradio as gr import lora_library from diffusers import ( FlowMatchEulerDiscreteScheduler, SASolverScheduler, DEISMultistepScheduler, DPMSolverMultistepInverseScheduler, UniPCMultistepScheduler, DPMSolverMultistepScheduler, DPMSolverSinglestepScheduler, ) from diffusers.pipelines.wan.pipeline_wan_i2v import WanImageToVideoPipeline from diffusers.utils.export_utils import export_to_video from torchao.quantization import quantize_, Float8DynamicActivationFloat8WeightConfig, Int8WeightOnlyConfig import aoti import lora_loader import audio_runtime from studio_preferences import (VIDEO_QUALITY, DEFAULT_REDUCE_DURATION, validate_reduction, prompt_history, LOAD_BROWSER_JS, SAVE_KEY_JS, FORGET_KEY_JS, SAVE_HISTORY_JS) from gif_export import export_gif from trim_tools import trim_video, video_duration as trim_video_duration os.environ["TOKENIZERS_PARALLELISM"] = "true" warnings.filterwarnings("ignore") IS_ZERO_GPU = bool(os.getenv("SPACES_ZERO_GPU")) # --- FRAME EXTRACTION JS & LOGIC --- get_timestamp_js = """ function() { const video = document.querySelector('#generated-video video'); if (video) { console.log("Video found! Time: " + video.currentTime); return video.currentTime; } else { console.log("No video element found."); return 0; } } """ get_timestamp_js_last = get_timestamp_js def extract_frame(video_path, timestamp): if not video_path: return None print(f"Extracting frame at timestamp: {timestamp}") cap = cv2.VideoCapture(video_path) if not cap.isOpened(): return None fps = cap.get(cv2.CAP_PROP_FPS) target_frame_num = int(float(timestamp) * fps) total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) if target_frame_num >= total_frames: target_frame_num = total_frames - 1 cap.set(cv2.CAP_PROP_POS_FRAMES, target_frame_num) ret, frame = cap.read() cap.release() if ret: return cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) return None # --- END FRAME EXTRACTION LOGIC --- def clear_vram(): gc.collect() torch.cuda.empty_cache() # RIFE (Lazy-loaded on first interpolation request) device = torch.device("cuda" if torch.cuda.is_available() else "cpu") _rife_model = None def get_rife_model(): global _rife_model if _rife_model is None: if not os.path.exists("RIFEv4.26_0921.zip"): print("Downloading RIFE Model...") _rife_cmd = ["wget", "-q", "https://huggingface.co/vml12/RIFE/resolve/main/RIFEv4.26_0921.zip", "-O", "RIFEv4.26_0921.zip"] if os.environ.get("HF_TOKEN"): _rife_cmd[2:2] = ["--header", f"Authorization: Bearer {os.environ['HF_TOKEN']}"] subprocess.run(_rife_cmd, check=True) subprocess.run(["unzip", "-o", "RIFEv4.26_0921.zip"], check=True) from train_log.RIFE_HDv3 import Model m = Model() m.load_model("train_log", -1) m.eval() _rife_model = m return _rife_model @torch.no_grad() def interpolate_bits(frames_np, multiplier=2, scale=1.0): rife_model = get_rife_model() if isinstance(frames_np, list): T = len(frames_np) H, W, C = frames_np[0].shape else: T, H, W, C = frames_np.shape if multiplier < 2: if isinstance(frames_np, np.ndarray): return list(frames_np) return frames_np n_interp = multiplier - 1 tmp = max(128, int(128 / scale)) ph = ((H - 1) // tmp + 1) * tmp pw = ((W - 1) // tmp + 1) * tmp padding = (0, pw - W, 0, ph - H) def to_tensor(frame_np): t = torch.from_numpy(frame_np).to(device) t = t.permute(2, 0, 1).unsqueeze(0) return F.pad(t, padding).half() def from_tensor(tensor): t = tensor[0, :, :H, :W] t = t.permute(1, 2, 0) return t.float().cpu().numpy() def make_inference(I0, I1, n): if rife_model.version >= 3.9: res = [] for i in range(n): res.append(rife_model.inference(I0, I1, (i+1) * 1. / (n+1), scale)) return res else: middle = rife_model.inference(I0, I1, scale) if n == 1: return [middle] first_half = make_inference(I0, middle, n=n//2) second_half = make_inference(middle, I1, n=n//2) if n % 2: return [*first_half, middle, *second_half] else: return [*first_half, *second_half] output_frames = [] I1 = to_tensor(frames_np[0]) total_steps = T - 1 with tqdm(total=total_steps, desc="Interpolating", unit="frame") as pbar: for i in range(total_steps): I0 = I1 output_frames.append(from_tensor(I0)) I1 = to_tensor(frames_np[i+1]) mid_tensors = make_inference(I0, I1, n_interp) for mid in mid_tensors: output_frames.append(from_tensor(mid)) if (i + 1) % 50 == 0: pbar.update(50) pbar.update(total_steps % 50) output_frames.append(from_tensor(I1)) del I0, I1, mid_tensors torch.cuda.empty_cache() return output_frames # WAN MODEL_ID = "vml12/WAMU_v3_WAN2.2_I2V_LIGHTNING" LORA_MODELS = [] MAX_DIM = 832 MIN_DIM = 480 SQUARE_DIM = 640 MULTIPLE_OF = 16 MAX_SEED = np.iinfo(np.int32).max FIXED_FPS = 16 MIN_FRAMES_MODEL = 8 MAX_FRAMES_MODEL = 321 MIN_DURATION = round(MIN_FRAMES_MODEL / FIXED_FPS, 1) MAX_DURATION = round(MAX_FRAMES_MODEL / FIXED_FPS, 1) SCHEDULER_MAP = { "FlowMatchEulerDiscrete": FlowMatchEulerDiscreteScheduler, "SASolver": SASolverScheduler, "DEISMultistep": DEISMultistepScheduler, "DPMSolverMultistepInverse": DPMSolverMultistepInverseScheduler, "UniPCMultistep": UniPCMultistepScheduler, "DPMSolverMultistep": DPMSolverMultistepScheduler, "DPMSolverSinglestep": DPMSolverSinglestepScheduler, } pipe = WanImageToVideoPipeline.from_pretrained( MODEL_ID, torch_dtype=torch.bfloat16, ).to('cuda') original_scheduler = copy.deepcopy(pipe.scheduler) for i, lora in enumerate(LORA_MODELS): name_high_tr = lora["high_tr"].split(".")[0].split("/")[-1] + "Hh" name_low_tr = lora["low_tr"].split(".")[0].split("/")[-1] + "Ll" try: pipe.load_lora_weights( lora["repo_id"], weight_name=lora["high_tr"], adapter_name=name_high_tr ) kwargs_lora = {"load_into_transformer_2": True} pipe.load_lora_weights( lora["repo_id"], weight_name=lora["low_tr"], adapter_name=name_low_tr, **kwargs_lora ) pipe.set_adapters([name_high_tr, name_low_tr], adapter_weights=[1.0, 1.0]) pipe.fuse_lora(adapter_names=[name_high_tr], lora_scale=lora["high_scale"], components=["transformer"]) pipe.fuse_lora(adapter_names=[name_low_tr], lora_scale=lora["low_scale"], components=["transformer_2"]) pipe.unload_lora_weights() print(f"Applied: {lora['high_tr']}, hs={lora['high_scale']}/ls={lora['low_scale']}, {i+1}/{len(LORA_MODELS)}") except Exception as e: print("Error:", str(e)) print("Failed lora:", name_high_tr) pipe.unload_lora_weights() quantize_(pipe.text_encoder, Int8WeightOnlyConfig()) torch._dynamo.reset() quantize_(pipe.transformer, Float8DynamicActivationFloat8WeightConfig()) torch._dynamo.reset() quantize_(pipe.transformer_2, Float8DynamicActivationFloat8WeightConfig()) torch._dynamo.reset() spaces.aoti_load( module=pipe.transformer, repo_id='vml12/WanTransformer3DModel-sm120-cu130-raa', ) spaces.aoti_load( module=pipe.transformer_2, repo_id='vml12/WanTransformer3DModel-sm120-cu130-raa', ) default_prompt_i2v = "make this image come alive, cinematic motion, smooth animation" default_negative_prompt = "色调艳丽, 过曝, 静态, 细节模糊不清, 字幕, 风格, 作品, 画作, 画面, 静止, 整体发灰, 最差质量, 低质量, JPEG压缩残留, 丑陋的, 残缺的, 多余的手指, 画得不好的手部, 画得不好的脸部, 畸形的, 毁容的, 形态畸形的肢体, 手指融合, 静止不动的画面, 杂乱的背景, 三条腿, 背景人很多, 倒着走" LORA_CHOICES = lora_loader.get_lora_choices() HIGH_LORA_CHOICES = lora_loader.get_high_lora_choices() LOW_LORA_CHOICES = lora_loader.get_low_lora_choices() # ---------- Speed / step presets ---------- SPEED_LORA_SOURCES = { "seko_v1": { "repo": "lightx2v/Wan2.2-Lightning", "file_high": "Wan2.2-I2V-A14B-4steps-lora-rank64-Seko-V1/high_noise_model.safetensors", "file_low": "Wan2.2-I2V-A14B-4steps-lora-rank64-Seko-V1/low_noise_model.safetensors", }, "distill": { "repo": "lightx2v/Wan2.2-Distill-Loras", "match_high": ["i2v", "high", "4step"], "match_low": ["i2v", "low", "4step"], }, } SPEED_NONE = "Built-in Lightning · 6 steps (balanced)" SPEED_PRESETS = { "Built-in Lightning · 4 steps (fastest)": {"steps": 4, "flow_shift": 3.0, "scheduler": "UniPCMultistep", "lora": None}, SPEED_NONE: {"steps": 6, "flow_shift": 3.0, "scheduler": "UniPCMultistep", "lora": None}, "Built-in Lightning · 8 steps (best motion)": {"steps": 8, "flow_shift": 5.0, "scheduler": "UniPCMultistep", "lora": None}, "Lightning lora (lightx2v Seko V1) · 4 steps": {"steps": 4, "flow_shift": 5.0, "scheduler": "UniPCMultistep", "lora": "seko_v1"}, "Lightning lora (lightx2v Seko V1) · 6 steps": {"steps": 6, "flow_shift": 5.0, "scheduler": "UniPCMultistep", "lora": "seko_v1"}, "Lightning lora (lightx2v Seko V1) · 8 steps": {"steps": 8, "flow_shift": 5.0, "scheduler": "UniPCMultistep", "lora": "seko_v1"}, "Distill lora (lightx2v 4step) · 4 steps": {"steps": 4, "flow_shift": 5.0, "scheduler": "UniPCMultistep", "lora": "distill"}, "Distill lora (lightx2v 4step) · 8 steps": {"steps": 8, "flow_shift": 5.0, "scheduler": "UniPCMultistep", "lora": "distill"}, } _SPEED_CACHE = {} def _match_all(name, keys): n = str(name).lower() return all(k in n for k in keys) def resolve_speed_lora(key): if not key: return None if key in _SPEED_CACHE: return _SPEED_CACHE[key] src = SPEED_LORA_SOURCES.get(key) if not src: return None if "file_high" in src: out = {"repo": src["repo"], "file_high": src["file_high"], "file_low": src["file_low"]} _SPEED_CACHE[key] = out return out try: files = list(lora_loader.list_loras_from_repo(src["repo"])) except Exception as e: print("Speed lora listing failed:", e) return None highs = [f for f in files if f and f != "(None)" and _match_all(f, src["match_high"])] lows = [f for f in files if f and f != "(None)" and _match_all(f, src["match_low"])] if not highs or not lows: print("Speed lora files not found in", src["repo"]) return None out = {"repo": src["repo"], "file_high": sorted(highs)[-1], "file_low": sorted(lows)[-1]} _SPEED_CACHE[key] = out return out PROMPT_CHIPS = [ "cinematic motion", "slow camera zoom in", "camera orbit around subject", "slow dolly forward", "gentle handheld camera", "wind blowing, hair and cloth moving", "soft natural light, shallow depth of field", "subject slowly turns head and smiles", "rain falling, wet reflections", "smoke and dust particles in the air", ] def model_title(): return "## Wan 2.2 I2V 14B Custom Lora" def resize_image(image: Image.Image, aspect_mode: str = "crop") -> Image.Image: """Fit an image into the model's frame. The checkpoint accepts 480 to 832 on a side, so anything taller than 832:480 (1.73:1) cannot be represented as it is - a 487x1056 phone shot is 2.17:1. `crop` takes the middle out, `pad` keeps the whole picture and fills the rest with black.""" width, height = image.size if width == height: return image.resize((SQUARE_DIM, SQUARE_DIM), Image.LANCZOS) aspect_ratio = width / height MAX_ASPECT_RATIO = MAX_DIM / MIN_DIM MIN_ASPECT_RATIO = MIN_DIM / MAX_DIM image_to_resize = image if aspect_ratio > MAX_ASPECT_RATIO or aspect_ratio < MIN_ASPECT_RATIO: wide = aspect_ratio > MAX_ASPECT_RATIO target_w, target_h = (MAX_DIM, MIN_DIM) if wide else (MIN_DIM, MAX_DIM) if str(aspect_mode).startswith("pad"): # The whole picture, scaled to fit inside the frame, centred on black. Nothing is lost; # the bars are. final_w = max(MIN_DIM, min(MAX_DIM, round(target_w / MULTIPLE_OF) * MULTIPLE_OF)) final_h = max(MIN_DIM, min(MAX_DIM, round(target_h / MULTIPLE_OF) * MULTIPLE_OF)) scale = min(final_w / width, final_h / height) inner = image.resize((max(1, int(width * scale)), max(1, int(height * scale))), Image.LANCZOS) canvas = Image.new("RGB", (final_w, final_h), (0, 0, 0)) canvas.paste(inner, ((final_w - inner.width) // 2, (final_h - inner.height) // 2)) return canvas if wide: crop_width = int(round(height * MAX_ASPECT_RATIO)) left = (width - crop_width) // 2 image_to_resize = image.crop((left, 0, left + crop_width, height)) else: crop_height = int(round(width / MIN_ASPECT_RATIO)) top = (height - crop_height) // 2 image_to_resize = image.crop((0, top, width, top + crop_height)) else: if width > height: target_w = MAX_DIM target_h = int(round(target_w / aspect_ratio)) else: target_h = MAX_DIM target_w = int(round(target_h * aspect_ratio)) final_w = round(target_w / MULTIPLE_OF) * MULTIPLE_OF final_h = round(target_h / MULTIPLE_OF) * MULTIPLE_OF final_w = max(MIN_DIM, min(MAX_DIM, final_w)) final_h = max(MIN_DIM, min(MAX_DIM, final_h)) return image_to_resize.resize((final_w, final_h), Image.LANCZOS) def resize_and_crop_to_match(target_image, reference_image): ref_width, ref_height = reference_image.size target_width, target_height = target_image.size scale = max(ref_width / target_width, ref_height / target_height) new_width, new_height = int(target_width * scale), int(target_height * scale) resized = target_image.resize((new_width, new_height), Image.Resampling.LANCZOS) left, top = (new_width - ref_width) // 2, (new_height - ref_height) // 2 return resized.crop((left, top, left + ref_width, top + ref_height)) # --------------------------------------------------------------------------- # Identity lock # --------------------------------------------------------------------------- # Ported from the LTX build. There the locked face feeds the likeness guide / # anchor nodes while the i2v nodes keep the temporal frame; Wan's pipeline has # no separate identity input, so the lock works at the pixel level instead: # before a continuation runs, the locked face is blended back over the face # region of the start frame through a feathered mask. The scene and the motion # carry over untouched, while identity is nudged back toward the original. _FACE_CASCADES = None _FACE_DETECT_MAX_SIDE = 1600 def _to_pil_rgb(img): """Accept PIL / ndarray / file path and hand back an RGB PIL image, or None.""" if img is None: return None try: if isinstance(img, Image.Image): return img.convert("RGB") if isinstance(img, np.ndarray): arr = img if arr.ndim == 2: arr = np.stack([arr] * 3, axis=-1) if arr.ndim != 3: return None if arr.dtype != np.uint8: arr = arr.clip(0, 255).astype(np.uint8) return Image.fromarray(arr[..., :3]).convert("RGB") if isinstance(img, str) and os.path.isfile(img): return Image.open(img).convert("RGB") except Exception: return None return None def _load_face_cascades(): """Haar cascades, loaded once and reused. Broken/missing files are skipped.""" global _FACE_CASCADES if _FACE_CASCADES is not None: return _FACE_CASCADES cascades = [] try: base = getattr(getattr(cv2, "data", None), "haarcascades", "") or "" for fname in ( "haarcascade_frontalface_default.xml", "haarcascade_frontalface_alt2.xml", "haarcascade_profileface.xml", ): try: clf = cv2.CascadeClassifier(base + fname) cascades.append(None if clf.empty() else clf) except Exception: cascades.append(None) except Exception: cascades = [None, None, None] _FACE_CASCADES = cascades return _FACE_CASCADES def _detect_face_box(img_rgb): """Largest face bbox (x, y, w, h) in an RGB image (PIL or ndarray), or None. Frontal cascades first, then profile in both directions. Detection runs on a bounded copy: a 4000px phone photo costs seconds with no accuracy gain. """ try: cascades = _load_face_cascades() if not any(c is not None for c in cascades): return None det = img_rgb if isinstance(img_rgb, np.ndarray) else np.array(img_rgb) if det.ndim != 3 or det.shape[2] < 3: return None det = np.ascontiguousarray(det[..., :3]) if det.dtype != np.uint8: det = det.clip(0, 255).astype(np.uint8) h, w = det.shape[:2] scale = 1.0 if max(h, w) > _FACE_DETECT_MAX_SIDE: s = _FACE_DETECT_MAX_SIDE / float(max(h, w)) det = cv2.resize(det, (max(1, int(w * s)), max(1, int(h * s)))) scale = 1.0 / s gray = cv2.equalizeHist(cv2.cvtColor(det, cv2.COLOR_RGB2GRAY)) min_side = max(24, min(gray.shape[0], gray.shape[1]) // 20) min_size = (min_side, min_side) best = None # (area, x, y, w, h) def _consider(x, y, fw, fh): nonlocal best area = int(fw) * int(fh) if best is None or area > best[0]: best = (area, int(x), int(y), int(fw), int(fh)) def _run(clf, image): if clf is None: return try: found = clf.detectMultiScale(image, 1.1, 5, minSize=min_size) except Exception: return if found is None or len(found) == 0: return for (x, y, fw, fh) in found: _consider(x, y, fw, fh) for clf in cascades[:2]: _run(clf, gray) # A face covering more than 8% of the frame is trusted as-is; the # profile passes below can only cost time, not change the answer. frame_area = gray.shape[0] * gray.shape[1] confident = best is not None and best[0] > 0.08 * frame_area if not confident and len(cascades) > 2 and cascades[2] is not None: _run(cascades[2], gray) flipped = cv2.flip(gray, 1) try: found = cascades[2].detectMultiScale(flipped, 1.1, 5, minSize=min_size) except Exception: found = None if found is not None and len(found) > 0: for (x, y, fw, fh) in found: _consider(flipped.shape[1] - int(x) - int(fw), y, fw, fh) if best is None: return None _, x, y, fw, fh = best return (int(x * scale), int(y * scale), int(fw * scale), int(fh * scale)) except Exception: return None def _images_similar(a, b): """Cheap 'same picture?' check on a 16x16 grayscale signature.""" if a is None or b is None: return False try: if a.size != b.size: return False sa = np.asarray(a.convert("L").resize((16, 16)), dtype=np.float32) sb = np.asarray(b.convert("L").resize((16, 16)), dtype=np.float32) return float(np.abs(sa - sb).mean()) < 2.0 except Exception: return False def _blend_identity(start_img, identity_img, strength): """Pull the face in `start_img` back toward the face in `identity_img`. Returns (image, anchored). The locked face is colour-matched to the frame, scaled onto the frame's face box and blended through a feathered elliptical mask at `strength`. A no-op (start_img, False) when the lock is off, when a face cannot be found in either picture, or when the start frame already is the locked picture (the first generation of a chain). """ if start_img is None or identity_img is None or float(strength) <= 0.0: return start_img, False try: base = _to_pil_rgb(start_img) ref = _to_pil_rgb(identity_img) if base is None or ref is None: return start_img, False if _images_similar(base, ref): return start_img, False box_base = _detect_face_box(base) box_ref = _detect_face_box(ref) if box_base is None or box_ref is None: print("[identity] no face found in the frame or in the lock - not anchored") return start_img, False bx, by, bw, bh = box_base rx, ry, rw, rh = box_ref margin = 0.35 # room for hairline and jaw around the box itself def _expand(x, y, w, h, W, H): mx, my = int(w * margin), int(h * margin) return (max(0, x - mx), max(0, y - my), min(W, x + w + mx), min(H, y + h + my)) b_x0, b_y0, b_x1, b_y1 = _expand(bx, by, bw, bh, base.width, base.height) r_x0, r_y0, r_x1, r_y1 = _expand(rx, ry, rw, rh, ref.width, ref.height) patch_w, patch_h = b_x1 - b_x0, b_y1 - b_y0 if patch_w < 16 or patch_h < 16 or (r_x1 - r_x0) < 8 or (r_y1 - r_y0) < 8: return start_img, False face = ref.crop((r_x0, r_y0, r_x1, r_y1)).resize((patch_w, patch_h), Image.LANCZOS) # Colour-match to the frame's face so the blend carries the structure of # the original face without dragging the original lighting along. Stats # come from the central core (the face), not the margin around it. face_arr = np.asarray(face, dtype=np.float32) base_patch = np.asarray(base.crop((b_x0, b_y0, b_x1, b_y1)), dtype=np.float32) cy = max(1, int(patch_h * 0.2)) cx = max(1, int(patch_w * 0.2)) core_y = slice(cy, max(cy + 1, patch_h - cy)) core_x = slice(cx, max(cx + 1, patch_w - cx)) for c in range(3): f_core = face_arr[..., c][core_y, core_x] b_core = base_patch[..., c][core_y, core_x] f_mean, f_std = float(f_core.mean()), float(f_core.std()) + 1e-5 b_mean, b_std = float(b_core.mean()), float(b_core.std()) + 1e-5 face_arr[..., c] = (face_arr[..., c] - f_mean) * (b_std / f_std) + b_mean face = Image.fromarray(face_arr.clip(0, 255).astype(np.uint8)) # Feathered elliptical mask over the patch, scaled by the strength knob. mask = Image.new("L", (patch_w, patch_h), 0) ImageDraw.Draw(mask).ellipse( (int(patch_w * 0.08), int(patch_h * 0.08), patch_w - int(patch_w * 0.08), patch_h - int(patch_h * 0.08)), fill=255, ) blur = max(3, (min(patch_w, patch_h) // 8) | 1) mask = mask.filter(ImageFilter.GaussianBlur(blur)) mask = Image.fromarray( (np.asarray(mask, dtype=np.float32) * float(strength)).clip(0, 255).astype(np.uint8) ) out = base.copy() out.paste(face, (b_x0, b_y0), mask) print("[identity] anchored: face %dx%d at (%d,%d), strength %g" % (bw, bh, bx, by, float(strength))) return out, True except Exception as error: print("[identity] anchor failed, frame kept as-is: %s" % error) return start_img, False def get_num_frames(duration_seconds: float): raw = int(round(duration_seconds * FIXED_FPS)) raw = max(MIN_FRAMES_MODEL, min(MAX_FRAMES_MODEL, raw)) return ((raw - 1) // 4) * 4 + 1 def get_inference_duration( resized_image, processed_last_image, prompt, steps, negative_prompt, num_frames, guidance_scale, guidance_scale_2, current_seed, scheduler_name, flow_shift, frame_multiplier, quality, duration_seconds, safe_mode, lora_groups, progress, reduce_duration=DEFAULT_REDUCE_DURATION, ): # Recalibrated after a report of an 8 s clip at 128 fps fluidity being killed mid-interpolation. # Two numbers were optimistic: the per-step cost, and above all the 0.02 s per interpolated frame - # at 128 fps RIFE produces eight frames for every one the model made, so a 129-frame clip becomes # over a thousand, and the error compounds there rather than in the diffusion. BASE_FRAMES_HEIGHT_WIDTH = 81 * 832 * 624 BASE_STEP_DURATION = 7. width, height = resized_image.size factor = num_frames * width * height / BASE_FRAMES_HEIGHT_WIDTH step_duration = BASE_STEP_DURATION * factor ** 1.5 gen_time = int(steps) * step_duration if guidance_scale > 1: gen_time = gen_time * 2.4 frame_factor = frame_multiplier // FIXED_FPS if frame_factor > 1: total_out_frames = (num_frames * frame_factor) - num_frames gen_time += total_out_frames * 0.06 total_time = 20 + gen_time if safe_mode: total_time = total_time * 1.30 if duration_seconds >= 8: total_time = total_time - validate_reduction(reduce_duration) print("total_time-------", total_time) # ZeroGPU refuses a booking above its own ceiling outright, and `size="xlarge"` doubles what is # asked for, so a runaway estimate fails before it starts. return max(1, min(total_time, 600)) @spaces.GPU(duration=get_inference_duration, size='xlarge') def run_inference( resized_image, processed_last_image, prompt, steps, negative_prompt, num_frames, guidance_scale, guidance_scale_2, current_seed, scheduler_name, flow_shift, frame_multiplier, quality, duration_seconds, safe_mode=False, lora_groups=None, progress=gr.Progress(track_tqdm=True), reduce_duration=DEFAULT_REDUCE_DURATION, ): scheduler_class = SCHEDULER_MAP.get(scheduler_name) if scheduler_class.__name__ != pipe.scheduler.config._class_name or flow_shift != pipe.scheduler.config.get("flow_shift", pipe.scheduler.config.get("shift")): config = copy.deepcopy(original_scheduler.config) if scheduler_class == FlowMatchEulerDiscreteScheduler: config['shift'] = flow_shift else: config['flow_shift'] = flow_shift pipe.scheduler = scheduler_class.from_config(config) clear_vram() task_name = str(uuid.uuid4())[:8] # Only the shape of the request: no prompt text, and adapters by name rather than by what the # visitor typed. Enough to read a report or spot a bad adapter, without recording anybody's words. active = [] for group in lora_groups or []: if group.get("preset") and group["preset"] != "(None)": active.append(f"{group['preset']} @ {group.get('high_scale')}/{group.get('low_scale')}") continue for role in ("file_high", "file_low"): value = group.get(role) if value and value != "(None)": scale = group.get("high_scale" if role == "file_high" else "low_scale") active.append(f"{os.path.basename(str(value))[:60]} ({role[5:]}) @ {scale}") print(f"[request {task_name}] {num_frames} frames, {duration_seconds}s, {resized_image.size}, " f"{int(steps)} steps, seed {current_seed}") if active: print(f"[request {task_name}] lora: {', '.join(active)}") start = time.time() lora_loaded = False if lora_groups: try: lora_loaded = lora_loader.load_lora_slots(pipe, lora_groups) print(f"lora loaded: {lora_groups}") except Exception as e: print(f"lora warning: {e}") try: lora_loader.unload_lora(pipe) except Exception: pass result = pipe( image=resized_image, last_image=processed_last_image, prompt=prompt, negative_prompt=negative_prompt, height=resized_image.height, width=resized_image.width, num_frames=num_frames, guidance_scale=float(guidance_scale), guidance_scale_2=float(guidance_scale_2), num_inference_steps=int(steps), generator=torch.Generator(device="cuda").manual_seed(current_seed), output_type="np" ) if lora_loaded: lora_loader.unload_lora(pipe) print("gen time passed:", time.time() - start) raw_frames_np = result.frames[0] pipe.scheduler = original_scheduler frame_factor = frame_multiplier // FIXED_FPS if frame_factor > 1: start = time.time() print(f"Processing frames (RIFE Multiplier: {frame_factor}x)...") rife_model = get_rife_model() rife_model.device() rife_model.flownet = rife_model.flownet.half() final_frames = interpolate_bits(raw_frames_np, multiplier=int(frame_factor)) print("Interpolation time passed:", time.time() - start) else: final_frames = list(raw_frames_np) final_fps = FIXED_FPS * int(frame_factor) with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as tmpfile: video_path = tmpfile.name start = time.time() with tqdm(total=3, desc="Rendering Media", unit="clip") as pbar: pbar.update(2) export_to_video(final_frames, video_path, fps=final_fps, quality=quality) pbar.update(1) print(f"Export time passed, {final_fps} FPS:", time.time() - start) persisted_path = _persist(video_path) or video_path return persisted_path, task_name # --------------------------------------------------------------------------- # Access key # --------------------------------------------------------------------------- # Everything that books the GPU - generating, the scene chain, sound, the prompt # enhancer - is behind a key. Everything else (browsing, joining, looping, the # CivitAI search, profiles) stays open, so a visitor without a key can still look # around and set things up. # # Valid keys come from the ACCESS_KEYS environment variable: a comma- or # newline-separated list, set under Settings -> Variables and secrets. Keys are # never in the repo, and adding or revoking one is a restart rather than an edit. # # The unlocked state lives per browser session in a gr.State, so one visitor's # key never unlocks the page for anybody else. TELEGRAM_CONTACT = "@vmlhub" TELEGRAM_URL = "https://t.me/" + TELEGRAM_CONTACT.lstrip("@") # Where an anonymous visitor is sent to raise their ZeroGPU quota. Signed out, # spaces.zero.client tracks usage by IP address (see client.py:134, "Falling # back to IP-based quotas") rather than per person, so it is the smallest # allowance there is and it is shared with anyone on the same network. SIGNUP_URL = "https://huggingface.co/join" # The handle as a clickable link with the Telegram mark in front of it. Inline SVG # rather than an emoji or an <img>: it renders identically everywhere, needs no # network fetch, and picks up the surrounding text colour. TELEGRAM_ICON = ( "<svg viewBox='0 0 24 24' width='14' height='14' fill='currentColor' " "aria-hidden='true'>" "<path d='M9.78 18.65l.28-4.23 7.68-6.92c.34-.31-.07-.46-.52-.19L7.74 13.3 3.64 12c-.88-.25-.89-.86.2-1.3" "l15.97-6.16c.73-.33 1.43.18 1.15 1.3l-2.72 12.81c-.19.91-.74 1.13-1.5.71L12.6 16.3l-1.99 1.93" "c-.23.23-.42.42-.83.42z'/></svg>" ) TELEGRAM_LINK = ( f"<a href='{TELEGRAM_URL}' target='_blank' rel='noopener noreferrer' " f"class='tg-link'>{TELEGRAM_ICON}" f"<span class='tg-handle'>{TELEGRAM_CONTACT}</span></a>" ) # Support email. Overridable so it can change without a code edit, and used # everywhere Telegram is offered so a buyer always has a second way to reach us - # Telegram is blocked or simply unused in plenty of places. SUPPORT_EMAIL = os.environ.get("SUPPORT_EMAIL", "vmlhub.ai@gmail.com") # Inline SVG envelope for the same reasons as the Telegram mark: no network # fetch, renders identically everywhere, inherits the surrounding text colour. EMAIL_ICON = ( "<svg viewBox='0 0 24 24' width='14' height='14' fill='currentColor' " "aria-hidden='true'>" "<path d='M20 4H4a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h16a2 2 0 0 0 2-2V6a2 2 0 0 0-2-2zm0 " "4.24-8 4.75-8-4.75V6l8 4.75L20 6v2.24z'/></svg>" ) EMAIL_LINK = ( f"<a href='mailto:{SUPPORT_EMAIL}?subject=Wan%20Studio%20Pro%20support' " f"class='mail-link'>{EMAIL_ICON}" f"<span class='mail-addr'>{SUPPORT_EMAIL}</span></a>" ) def _current_odoo_url(url): """Follow the checkout Space rename for existing Pro settings.""" old_origin = "https://vml12-odoo19-test.hf.space" new_origin = "https://vml12-erp.hf.space" if url == old_origin or url.startswith(old_origin + "/"): return new_origin + url[len(old_origin):] return url # Product identity is fixed by this app, not a user-supplied key or backend response. ACCESS_PRODUCT = "wan-studio-pro" ACCESS_KEY_PREFIX = "WSP-" def _product_keys(values): """Keep only issued keys belonging to this app's product namespace.""" return {str(key).strip().upper() for key in values if str(key).strip().upper().startswith(ACCESS_KEY_PREFIX)} # Send buyers straight to Odoo's product details and PayPal form. BUY_URL = _current_odoo_url(os.environ.get("BUY_URL", "").strip()).rstrip("/") if (not BUY_URL or BUY_URL.startswith("https://vimalprasad37.github.io/wan-studio-pro") or BUY_URL.startswith("https://vmlhub.github.io/wan-studio-pro")): BUY_URL = "https://vml12-erp.hf.space/wan-studio-pro/checkout" if BUY_URL in ("https://vml12-erp.hf.space/wan-studio-pro/checkout", "https://vml12-erp.hf.space/wan-studio-pro-v2/checkout"): BUY_URL = "https://vml12-erp.hf.space/wan-studio-pro/checkout" BUY_LINK = ( f"<a href='{BUY_URL}' target='_blank' rel='noopener noreferrer' " f"class='buy-link'>Get Pro access <span aria-hidden='true'>↗</span></a>" ) # Wise and PayPal keep separate key stores during the PayPal rollout. This # preserves existing Wise keys and scopes the Odoo integration to this Space. KEY_BACKEND = (os.environ.get("KEY_BACKEND", "") or "").rstrip("/") KEY_BACKEND_TOKEN = os.environ.get("KEY_BACKEND_TOKEN", "") or "" ODOO_KEY_BACKEND = _current_odoo_url((os.environ.get("ODOO_KEY_BACKEND", "") or "").strip().rstrip("/")) if ODOO_KEY_BACKEND in ("https://vml12-erp.hf.space/wan-paypal", "https://vml12-erp.hf.space/wan-studio-pro-v2"): ODOO_KEY_BACKEND = "https://vml12-erp.hf.space/wan-paypal" ODOO_KEY_BACKEND_TOKEN = os.environ.get("ODOO_KEY_BACKEND_TOKEN", "") or "" _KEY_CACHE = {"wise": {"keys": None, "at": 0.0}, "odoo": {"keys": None, "at": 0.0}} _KEY_CACHE_TTL = 60.0 def _env_keys(): """Manual passwords and this product's issued keys, configured per Space.""" import re raw = os.environ.get("ACCESS_KEYS", "") or "" keys = set() for part in re.split(r"[,\n;]+", raw): part = part.strip().strip('"').strip("'").strip().upper() if part: # Plain operator passwords need no prefix. Issued keys remain # scoped to their product even when included in a fallback list. if part.startswith(("WSP-", "WSP2-", "HZN-", "MMP-")) and not part.startswith(ACCESS_KEY_PREFIX): continue keys.add(part) return keys def _fetch_key_source(name, base_url, token, bearer=False): """Fetch one source, retaining its last answer during a brief outage.""" if not base_url: return None cache = _KEY_CACHE[name] now = time.time() if cache["keys"] is not None and (now - cache["at"]) < _KEY_CACHE_TTL: return cache["keys"] try: import json as _json import urllib.parse import urllib.request url = base_url + "/keys" headers = {} if token: if bearer: headers["Authorization"] = "Bearer " + token else: url += "?" + urllib.parse.urlencode({"token": token}) req = urllib.request.Request(url, headers=headers) with urllib.request.urlopen(req, timeout=8) as resp: data = _json.loads(resp.read().decode("utf-8")) if bearer and data.get("product") != ACCESS_PRODUCT: raise ValueError("The key service returned another product's key list.") keys = _product_keys(data.get("keys", [])) cache["keys"] = keys cache["at"] = now return keys except Exception as exc: # noqa: BLE001 - an outage must not reject existing buyers print(f"[access] {name} key source unavailable ({type(exc).__name__})") return None if bearer else cache["keys"] def _backend_keys(): """Read existing Wise keys.""" return _fetch_key_source("wise", KEY_BACKEND, KEY_BACKEND_TOKEN) def _odoo_keys(): """Read PayPal keys from the protected Odoo Space.""" return _fetch_key_source("odoo", ODOO_KEY_BACKEND, ODOO_KEY_BACKEND_TOKEN, bearer=True) def _valid_keys(): keys = set(_env_keys()) for source in (_backend_keys(), _odoo_keys()): if source: keys.update(_product_keys(source)) return keys def _report_access_config(): env_count = len(_env_keys()) for name, base_url, source in (("Wise", KEY_BACKEND, _backend_keys()), ("Odoo", ODOO_KEY_BACKEND, _odoo_keys())): if base_url: status = "unavailable" if source is None else f"{len(source)} key(s)" print(f"[access] {name} key source {status}") print(f"[access] ACCESS_KEYS fallback: {env_count} key(s)") if not _valid_keys(): print("[access] no keys from any source; check KEY_BACKEND, " "ODOO_KEY_BACKEND, and ACCESS_KEYS") def check_access_key(key): """Validate a typed key. Returns the state flag, a status line, and the button updates that lock or unlock the page.""" typed = str(key or "").strip() # A configured manual password works independently of payment-service # availability and does not wait for either service's network timeout. keys = _env_keys() if not typed or typed.upper() not in keys: keys = _valid_keys() # Issued wan-studio-pro keys use WSP- and are upper-case. People paste them out of an # email with whatever case their client decided to use. Compare case-folded # so "wsp-..." is not a support ticket. folded = {k.upper() for k in keys} if not keys: # No keys reached the process. Almost always the secret was added while # the Space was already running: HF injects secrets at container start, # so an existing process never sees a new one. Say that, rather than # sending the owner back to a Settings page where the secret already is. return (False, "<div class='status-idle'><b>The key list has not reached the app.</b> " "The key services did not answer and no <code>ACCESS_KEYS</code> " "fallback is set. If you have already bought a key, message Telegram " f"{TELEGRAM_LINK} or email {EMAIL_LINK} and it will be sorted out. " f"Otherwise {BUY_LINK}.</div>", *_lock_updates(False)) if typed and typed.upper() in folded: return (True, "<div class='status-ok'><b>&#128275; Access granted</b>" "<span>generation is unlocked for this browser session</span></div>", *_lock_updates(True)) if not typed: return (False, "<div class='status-idle'>Paste your access key above and press " "<b>Unlock</b>.</div>", *_lock_updates(False)) return (False, "<div class='status-lock'><b>&#128274; That key is not valid</b>" f"<span>{BUY_LINK} &mdash; or contact {TELEGRAM_LINK} / {EMAIL_LINK}</span></div>", *_lock_updates(False)) def _lock_updates(unlocked): """Button states for the four things that cost GPU time.""" if unlocked: return ( gr.update(value="\U0001f680 Generate Video", interactive=True), gr.update(interactive=True), gr.update(interactive=True), gr.update(interactive=True), ) return ( gr.update(value="\U0001f512 Locked — enter an access key", interactive=False), gr.update(interactive=False), gr.update(interactive=False), gr.update(interactive=False), ) def _unlock_browser(key): result = check_access_key(key) return (*result, str(key or "").strip() if result[0] else "") def _history_choices(entries): items = prompt_history("", entries) choices = [(" ".join(item.split())[:90], item) for item in items] return gr.update(choices=choices, value=None) def _remember_prompt(prompt, entries): entries = prompt_history(prompt, entries) return entries, _history_choices(entries) def _restore_browser(key, entries): # Restore credentials, never a client-supplied unlocked flag. result = _unlock_browser(key) if key else (False, gr.update(), *_lock_updates(False), "") return (*result, _history_choices(entries)) def _forget_browser_key(): return ("", False, "<div class='status-idle'>Saved key forgotten on this browser.</div>", *_lock_updates(False), "") def require_access(unlocked): """Raised from inside every gated handler. The disabled buttons are the visible half of the lock; this is the half that matters. A disabled button is only a property of one browser's DOM, and the Gradio endpoint behind it stays callable by anything that can reach the Space - so the check lives in the handler, where it cannot be clicked past.""" if not unlocked: raise gr.Error( "This action needs a valid access key. Enter one at the top of the page - " f"get one at {BUY_URL}, " f"or contact Telegram {TELEGRAM_CONTACT} / {SUPPORT_EMAIL}." ) def generate_video( input_image, last_image, prompt, steps=4, negative_prompt=default_negative_prompt, duration_seconds=MAX_DURATION, guidance_scale=1, guidance_scale_2=1, seed=42, randomize_seed=False, quality=VIDEO_QUALITY, scheduler="UniPCMultistep", flow_shift=6.0, frame_multiplier=16, safe_mode=False, aspect_mode="crop", speed_preset=SPEED_NONE, speed_high_scale=1.0, speed_low_scale=1.0, # original 3 (separated high and low) lora1_high_preset="(None)", lora1_high_scale=1.0, lora1_low_preset="(None)", lora1_low_scale=1.0, lora2_high_preset="(None)", lora2_high_scale=1.0, lora2_low_preset="(None)", lora2_low_scale=1.0, lora3_high_preset="(None)", lora3_high_scale=1.0, lora3_low_preset="(None)", lora3_low_scale=1.0, # my 5 my1_repo="", my1_high="(None)", my1_hs=1.0, my1_low="(None)", my1_ls=1.0, my2_repo="", my2_high="(None)", my2_hs=1.0, my2_low="(None)", my2_ls=1.0, my3_repo="", my3_high="(None)", my3_hs=1.0, my3_low="(None)", my3_ls=1.0, my4_repo="", my4_high="(None)", my4_hs=1.0, my4_low="(None)", my4_ls=1.0, my5_repo="", my5_high="(None)", my5_hs=1.0, my5_low="(None)", my5_ls=1.0, identity_ref=None, identity_strength=0.25, video_component=True, unlocked=False, reduce_duration=DEFAULT_REDUCE_DURATION, progress=gr.Progress(track_tqdm=True), ): require_access(unlocked) reduce_duration = validate_reduction(reduce_duration) if input_image is None: raise gr.Error("Please upload an input image.") wall_start = time.time() num_frames = get_num_frames(duration_seconds) current_seed = random.randint(0, MAX_SEED) if randomize_seed else int(seed) # Identity lock: pull the start frame's face back toward the locked face # before the resize touches it. A no-op on the first generation of a chain # (the start frame already IS the locked picture). identity_anchored = False if identity_ref is not None: input_image, identity_anchored = _blend_identity( input_image, identity_ref, float(identity_strength) ) resized_image = resize_image(input_image, aspect_mode) processed_last_image = None if last_image: processed_last_image = resize_and_crop_to_match(last_image, resized_image) preset_cfg = SPEED_PRESETS.get(speed_preset) or {} speed_info = "" speed_group = None if preset_cfg.get("lora"): resolved = resolve_speed_lora(preset_cfg["lora"]) if resolved: speed_group = { "repo": resolved["repo"], "file_high": resolved["file_high"], "file_low": resolved["file_low"], "high_scale": speed_high_scale, "low_scale": speed_low_scale, } speed_info = f"<span>speed lora on</span>" else: speed_info = "<span>speed lora not found — skipped</span>" lora_groups = [ {"preset_high": lora1_high_preset, "preset_low": lora1_low_preset, "high_scale": lora1_high_scale, "low_scale": lora1_low_scale}, {"preset_high": lora2_high_preset, "preset_low": lora2_low_preset, "high_scale": lora2_high_scale, "low_scale": lora2_low_scale}, {"preset_high": lora3_high_preset, "preset_low": lora3_low_preset, "high_scale": lora3_high_scale, "low_scale": lora3_low_scale}, {"repo": my1_repo, "file_high": my1_high, "file_low": my1_low, "high_scale": my1_hs, "low_scale": my1_ls}, {"repo": my2_repo, "file_high": my2_high, "file_low": my2_low, "high_scale": my2_hs, "low_scale": my2_ls}, {"repo": my3_repo, "file_high": my3_high, "file_low": my3_low, "high_scale": my3_hs, "low_scale": my3_ls}, {"repo": my4_repo, "file_high": my4_high, "file_low": my4_low, "high_scale": my4_hs, "low_scale": my4_ls}, {"repo": my5_repo, "file_high": my5_high, "file_low": my5_low, "high_scale": my5_hs, "low_scale": my5_ls}, ] if speed_group: lora_groups.insert(0, speed_group) video_path, task_n = run_inference( resized_image, processed_last_image, prompt, steps, negative_prompt, num_frames, guidance_scale, guidance_scale_2, current_seed, scheduler, flow_shift, frame_multiplier, quality, duration_seconds, safe_mode, lora_groups, progress, reduce_duration=reduce_duration, ) persisted_path = _persist(video_path) or video_path print(f"GPU complete: {task_n}") elapsed = time.time() - wall_start w, h = resized_image.size identity_info = "<span>&#128274; identity anchored</span>" if identity_anchored else "" status = ( "<div class='status-ok'>" "<b>&#10003; Ready</b>" f"<span>{w}&times;{h}</span>" f"<span>{num_frames} frames &middot; {int(frame_multiplier)} fps</span>" f"<span>seed {current_seed}</span>" f"<span>{int(steps)} steps</span>" + speed_info + identity_info + f"<span>{elapsed:.1f} s</span>" "</div>" ) return (persisted_path if video_component else None), persisted_path, current_seed, status def _pick_file(files, *keys): for key in keys: for f in files: if f and f != "(None)" and key in str(f).lower(): return f return "(None)" def _load_repo_files(repo_link): if not repo_link or not str(repo_link).strip(): return ( gr.update(choices=["(None)"], value="(None)"), gr.update(choices=["(None)"], value="(None)"), ) # A CivitAI (or any other) download link is a single file, not a repo to list: put it in the # High box and leave Low empty, since a Wan adapter ships its two halves as two separate files. link = str(repo_link).strip() if getattr(lora_loader, "is_direct_link", lambda _: False)(link): return ( gr.update(choices=["(None)", link], value=link), gr.update(choices=["(None)", link], value="(None)"), ) try: files = lora_loader.list_loras_from_repo(repo_link) except Exception as e: raise gr.Error(f"Cannot read repo: {e}") files = list(files) if files else [] if "(None)" not in files: files = ["(None)"] + files auto_high = _pick_file(files, "high", "_h.", "-h.", "hn") auto_low = _pick_file(files, "low", "_l.", "-l.", "ln") return ( gr.update(choices=files, value=auto_high), gr.update(choices=files, value=auto_low), ) # --------------------------------------------------------------------------- # Named profiles # --------------------------------------------------------------------------- # Everything typed rather than uploaded, stored under a name inside the Space. # `/data` is used when the Space has persistent storage attached, so profiles # survive a restart; otherwise they live for as long as the Space runs. import json import re import tempfile import time if os.path.isdir("/data") and os.access("/data", os.W_OK): PROFILES_DIR = os.path.join("/data", "wan22-profiles") else: PROFILES_DIR = os.path.join(tempfile.gettempdir(), "wan22-profiles") os.makedirs(PROFILES_DIR, exist_ok=True) NO_PROFILE = "\u2014 no saved profile \u2014" SETTINGS_KEYS = [ "prompt", "negative_prompt", "duration", "steps", "quality", "seed", "randomize_seed", "scheduler", "flow_shift", "fps", "safe_mode", "speed_preset", "speed_high_scale", "speed_low_scale", "lora1_high_preset", "lora1_high", "lora1_low_preset", "lora1_low", "lora2_high_preset", "lora2_high", "lora2_low_preset", "lora2_low", "lora3_high_preset", "lora3_high", "lora3_low_preset", "lora3_low", ] for _slot in range(1, 6): SETTINGS_KEYS += [ f"my{_slot}_repo", f"my{_slot}_high_file", f"my{_slot}_high_scale", f"my{_slot}_low_file", f"my{_slot}_low_scale", ] SETTINGS_KEYS.append("identity_strength") SETTINGS_KEYS.append("reduce_duration") # The per-repo file dropdowns are populated only after a repo has been read, so a restored value # would not be among the choices. These keys get their choice list rebuilt around the stored value. _FILE_DROPDOWN_KEYS = {f"my{slot}_{end}_file" for slot in range(1, 6) for end in ("high", "low")} def _profile_path(name): safe = re.sub(r"[^A-Za-z0-9 ._-]", "_", str(name or "").strip())[:64].strip() or "profile" return os.path.join(PROFILES_DIR, f"{safe}.json") def list_profiles(): if not os.path.isdir(PROFILES_DIR): return [] return [entry[:-5] for entry in sorted(os.listdir(PROFILES_DIR)) if entry.endswith(".json")] def refresh_profiles(current=None): """Re-read the folder. The dropdown's choices are built once at start-up, so a profile saved in another tab (or after this page was opened) would otherwise stay invisible until a restart.""" names = list_profiles() value = current if current in names else NO_PROFILE return gr.update(choices=[NO_PROFILE, *names], value=value) def save_profile(name, *values): if not str(name or "").strip(): return gr.update(), "Give the profile a name first." payload = {"saved": time.strftime("%Y-%m-%d %H:%M:%S")} payload.update(dict(zip(SETTINGS_KEYS, values))) try: with open(_profile_path(name), "w", encoding="utf-8") as handle: json.dump(payload, handle, ensure_ascii=False, indent=2, default=str) except Exception as error: return gr.update(), f"Could not save: `{type(error).__name__}: {error}`" saved = os.path.basename(_profile_path(name))[:-5] return gr.update(choices=[NO_PROFILE, *list_profiles()], value=saved), f"Saved **{saved}**." def load_profile(name): blank = [gr.update() for _ in SETTINGS_KEYS] if not name or name == NO_PROFILE: return [*blank, gr.update(), ""] path = _profile_path(name) if not os.path.exists(path): return [*blank, gr.update(), f"No profile named **{name}**."] try: with open(path, encoding="utf-8") as handle: payload = json.load(handle) except Exception as error: return [*blank, gr.update(), f"Could not read it: `{type(error).__name__}: {error}`"] updates = [] for key in SETTINGS_KEYS: value = payload.get(key) if value is None: updates.append(gr.update()) elif key in _FILE_DROPDOWN_KEYS: choices = ["(None)"] if value == "(None)" else ["(None)", value] updates.append(gr.update(choices=choices, value=value)) else: updates.append(gr.update(value=value)) stamp = payload.get("saved", "") return [*updates, gr.update(value=name), f"Loaded **{name}**{f' (saved {stamp})' if stamp else ''}."] def delete_profile(name): if not name or name == NO_PROFILE: return gr.update(), "Pick a profile first." try: os.remove(_profile_path(name)) except FileNotFoundError: return gr.update(choices=[NO_PROFILE, *list_profiles()], value=NO_PROFILE), f"No profile named **{name}**." except Exception as error: return gr.update(), f"Could not delete: `{type(error).__name__}: {error}`" return gr.update(choices=[NO_PROFILE, *list_profiles()], value=NO_PROFILE), f"Deleted **{name}**." def export_settings(*values): payload = {"saved": time.strftime("%Y-%m-%d %H:%M:%S")} payload.update(dict(zip(SETTINGS_KEYS, values))) directory = os.path.join(tempfile.gettempdir(), "wan22-settings") os.makedirs(directory, exist_ok=True) path = os.path.join(directory, f"wan22-settings-{int(time.time())}.json") with open(path, "w", encoding="utf-8") as handle: json.dump(payload, handle, ensure_ascii=False, indent=2, default=str) return gr.update(value=path, visible=True) def import_settings(path): if not path: return [gr.update() for _ in SETTINGS_KEYS] try: with open(path, encoding="utf-8") as handle: payload = json.load(handle) except Exception as error: raise gr.Error(f"That settings file cannot be read: `{type(error).__name__}: {error}`") updates = [] for key in SETTINGS_KEYS: value = payload.get(key) if value is None: updates.append(gr.update()) elif key in _FILE_DROPDOWN_KEYS: choices = ["(None)"] if value == "(None)" else ["(None)", value] updates.append(gr.update(choices=choices, value=value)) else: updates.append(gr.update(value=value)) return updates # --------------------------------------------------------------------------- # Clip stitching, boomerang, and persistent saving - all CPU, zero GPU quota # --------------------------------------------------------------------------- # Every one of these runs after the video already exists, so none of them touch # the card or the ZeroGPU allowance. Chaining three 3.5 s clips into a 10 s one # costs nothing extra. def _ffmpeg_exe() -> str: """imageio-ffmpeg ships a binary, so this works even when the image has no system ffmpeg.""" try: import imageio_ffmpeg return imageio_ffmpeg.get_ffmpeg_exe() except Exception: return "ffmpeg" def _video_size(path): capture = cv2.VideoCapture(path) width = int(capture.get(cv2.CAP_PROP_FRAME_WIDTH) or 0) height = int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT) or 0) capture.release() return (width or 832), (height or 624) def _video_fps(path): capture = cv2.VideoCapture(path) fps = float(capture.get(cv2.CAP_PROP_FPS) or 0.0) capture.release() if not fps or fps != fps or fps <= 0.5 or fps > 1000: fps = float(FIXED_FPS) return fps def _first_frame_small(path): capture = cv2.VideoCapture(str(path)) if not capture.isOpened(): return None ok, frame = capture.read() capture.release() if not ok or frame is None: return None return cv2.resize(frame, (64, 64)).astype(np.float32) def _last_frame_bgr(path, tail=40): """The genuine final decoded frame. Seeking straight to total-1 is unreliable on some encodes, so it seeks a little short and decodes forward to the end.""" capture = cv2.VideoCapture(str(path)) if not capture.isOpened(): return None total = int(capture.get(cv2.CAP_PROP_FRAME_COUNT) or 0) if total > tail: capture.set(cv2.CAP_PROP_POS_FRAMES, max(0, total - tail)) frames = [] while True: ok, candidate = capture.read() if not ok or candidate is None: break frames.append(candidate) capture.release() if not frames: return None for candidate in reversed(frames): if float(candidate.mean()) > 2.0: return candidate return frames[-1] def _last_frame_small(path): frame = _last_frame_bgr(path) if frame is None: return None return cv2.resize(frame, (64, 64)).astype(np.float32) def _same_frame(a, b, tolerance=7.0): if a is None or b is None: return False if a.shape != b.shape: return False return float(np.abs(a - b).mean()) < tolerance def _persist(path): """Copy to /data/videos when persistent storage is available or configured.""" if not path or not os.path.exists(path): return path target_dir = os.getenv("VIDEO_STORAGE_DIR", "/data/videos") try: os.makedirs(target_dir, exist_ok=True) filename = os.path.basename(path) target_path = os.path.join(target_dir, filename) if os.path.abspath(path) != os.path.abspath(target_path): shutil.copy(path, target_path) print(f"[storage] saved to {target_path}") return target_path except Exception as error: print(f"[storage] could not save to {target_dir}: {error}") return path # --------------------------------------------------------------------------- # Housekeeping for /data/videos # --------------------------------------------------------------------------- # _persist() copies every finished clip to persistent storage and nothing ever # removes them, so the disk fills until writes start failing. Gradio's own # delete_cache= covers its temp files only, never this directory. # # A daemon thread rather than a scheduled job: Spaces have no cron, and a # daemon thread dies with the process instead of keeping a stopped Space alive. VIDEO_RETENTION_HOURS = float(os.environ.get("VIDEO_RETENTION_HOURS", "4")) VIDEO_SWEEP_MINUTES = float(os.environ.get("VIDEO_SWEEP_MINUTES", "30")) _SWEEP_SUFFIXES = (".mp4", ".wav", ".webm", ".mkv") def sweep_old_videos(directory=None, retention_hours=None): """Delete clips older than the retention window. Returns (files, bytes). Only the media extensions this app writes are touched, and only plain files directly in the directory - never a subdirectory, never anything else that happens to share the volume. """ directory = directory or os.getenv("VIDEO_STORAGE_DIR", "/data/videos") hours = VIDEO_RETENTION_HOURS if retention_hours is None else float(retention_hours) if hours <= 0 or not os.path.isdir(directory): return 0, 0 cutoff = time.time() - hours * 3600.0 removed = freed = 0 try: entries = os.listdir(directory) except OSError as error: print(f"[cleanup] cannot list {directory}: {error}", flush=True) return 0, 0 for name in entries: if not name.lower().endswith(_SWEEP_SUFFIXES): continue path = os.path.join(directory, name) try: stat = os.stat(path) # os.path.isfile() follows symlinks; stat.S_ISREG on the lstat-free # stat still needs the isfile guard for directories named *.mp4. if not os.path.isfile(path) or stat.st_mtime >= cutoff: continue size = stat.st_size os.remove(path) removed += 1 freed += size except FileNotFoundError: continue # already gone - another sweep, or the OS except OSError as error: print(f"[cleanup] could not remove {path}: {error}", flush=True) if removed: print(f"[cleanup] removed {removed} file(s), freed {freed / 2**20:.1f} MB " f"from {directory} (older than {hours:g}h)", flush=True) return removed, freed def start_video_sweeper(): """Sweep now, then every VIDEO_SWEEP_MINUTES, on a daemon thread.""" if VIDEO_RETENTION_HOURS <= 0: print("[cleanup] VIDEO_RETENTION_HOURS=0 - keeping every clip", flush=True) return None directory = os.getenv("VIDEO_STORAGE_DIR", "/data/videos") print(f"[cleanup] sweeping {directory} every {VIDEO_SWEEP_MINUTES:g} min, " f"keeping clips for {VIDEO_RETENTION_HOURS:g}h", flush=True) def loop(): while True: try: sweep_old_videos(directory) except Exception as error: # never let housekeeping kill the thread print(f"[cleanup] sweep failed: {type(error).__name__}: {error}", flush=True) time.sleep(max(60.0, VIDEO_SWEEP_MINUTES * 60.0)) thread = threading.Thread(target=loop, name="video-sweeper", daemon=True) thread.start() return thread def _has_audio(path): """Whether ffmpeg reports an audio stream on the file.""" try: result = subprocess.run([_ffmpeg_exe(), "-i", str(path)], capture_output=True) return "Audio:" in result.stderr.decode("utf-8", "ignore") except Exception: return False def _video_duration(path): capture = cv2.VideoCapture(str(path)) fps = float(capture.get(cv2.CAP_PROP_FPS) or 0.0) total = int(capture.get(cv2.CAP_PROP_FRAME_COUNT) or 0) capture.release() if fps <= 0.5 or total <= 0: return 4.0 return max(1.0, total / fps) def _normalise_clip(path, width, height, fps, out_path, trim_head=False): """One frame size, one frame rate, one audio format. A clip with no sound gets silence, so a dubbed clip and a silent one still concatenate. `trim_head` drops the duplicated first frame and the matching sliver of audio together.""" fps_str = f"{fps:.6f}".rstrip("0").rstrip(".") video_chain = (f"scale={width}:{height}:force_original_aspect_ratio=decrease," f"pad={width}:{height}:(ow-iw)/2:(oh-ih)/2,setsar=1,fps={fps_str}") audio_chain = "aresample=48000,aformat=sample_fmts=fltp:channel_layouts=stereo" if trim_head: video_chain += ",trim=start_frame=1,setpts=PTS-STARTPTS" audio_chain += f",atrim=start={1.0 / fps:.6f},asetpts=PTS-STARTPTS" tail = ["-c:v", "libx264", "-preset", "fast", "-crf", "20", "-pix_fmt", "yuv420p", "-c:a", "aac", "-ar", "48000", "-ac", "2", out_path] if _has_audio(path): command = [_ffmpeg_exe(), "-y", "-i", path, "-filter_complex", f"[0:v]{video_chain}[v];[0:a]{audio_chain}[a]", "-map", "[v]", "-map", "[a]", *tail] else: command = [_ffmpeg_exe(), "-y", "-i", path, "-f", "lavfi", "-i", "anullsrc=r=48000:cl=stereo", "-filter_complex", f"[0:v]{video_chain}[v];[1:a]{audio_chain}[a]", "-map", "[v]", "-map", "[a]", "-shortest", *tail] subprocess.run(command, check=True, capture_output=True) return out_path def concat_videos(paths, name_hint=""): """Stitch clips into one file. Everything is scaled and padded to the first clip's frame so a mixed-aspect queue still concatenates cleanly.""" valid = [p for p in (paths or []) if p and os.path.exists(p)] if not valid: return None if len(valid) == 1: return valid[0] width, height = _video_size(valid[0]) fps = max(_video_fps(p) for p in valid) fps_str = f"{fps:.6f}".rstrip("0").rstrip(".") prefix = uuid.uuid4().hex[:4] clean = (str(name_hint).strip().replace(" ", "_") or f"merged_{len(valid)}clips") if clean.lower().endswith(".mp4"): clean = clean[:-4] out_path = os.path.join(tempfile.gettempdir(), f"{prefix}_{clean}.mp4") # The seam. A continued clip starts on the still that was lifted off the end of the # clip before it, so that frame exists in both files and the join stutters by exactly # one frame. Compare the two and trim the repeat when it is really there. trims = [0] * len(valid) previous_tail = _last_frame_small(valid[0]) for index in range(1, len(valid)): head = _first_frame_small(valid[index]) if _same_frame(previous_tail, head): trims[index] = 1 previous_tail = _last_frame_small(valid[index]) # Sound. Once anything in the queue has been dubbed the whole join has to carry an audio # track, so every clip is rewritten to a common format first and the silent ones are given # silence. Nothing in the queue has sound - the usual case - and the fast one-pass route # below is used instead. if any(_has_audio(p) for p in valid): work_dir = tempfile.mkdtemp(prefix="stitch-") try: parts = [ _normalise_clip(path, width, height, fps, os.path.join(work_dir, f"part{index}.mp4"), trim_head=bool(trims[index])) for index, path in enumerate(valid) ] inputs = [] for part in parts: inputs += ["-i", part] chain = "".join(f"[{i}:v][{i}:a]" for i in range(len(parts))) subprocess.run( [_ffmpeg_exe(), "-y", *inputs, "-filter_complex", f"{chain}concat=n={len(parts)}:v=1:a=1[outv][outa]", "-map", "[outv]", "-map", "[outa]", "-r", fps_str, "-c:v", "libx264", "-preset", "fast", "-crf", "20", "-pix_fmt", "yuv420p", "-c:a", "aac", "-ar", "48000", "-ac", "2", "-movflags", "+faststart", out_path], check=True, capture_output=True, ) if os.path.exists(out_path): _persist(out_path) return out_path except Exception as error: print(f"[merge] audio-aware join failed ({error}); falling back to video only") inputs, chains = [], [] for index, path in enumerate(valid): inputs += ["-i", path] cut = (f"trim=start_frame={trims[index]},setpts=PTS-STARTPTS," if trims[index] else "") chains.append( f"[{index}:v]{cut}" f"scale={width}:{height}:force_original_aspect_ratio=decrease," f"pad={width}:{height}:(ow-iw)/2:(oh-ih)/2,setsar=1," f"fps={fps_str},settb=1/{int(round(fps * 1000))},setpts=PTS-STARTPTS[v{index}];" ) joined = "".join(f"[v{i}]" for i in range(len(valid))) filter_str = "".join(chains) + f"{joined}concat=n={len(valid)}:v=1:a=0[outv]" command = [_ffmpeg_exe(), "-y", *inputs, "-filter_complex", filter_str, "-map", "[outv]", "-r", fps_str, "-c:v", "libx264", "-preset", "fast", "-crf", "20", "-pix_fmt", "yuv420p", "-movflags", "+faststart", out_path] try: subprocess.run(command, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) except Exception as error: print(f"[merge] filter_complex failed ({error}); trying the concat demuxer") list_file = os.path.join(tempfile.gettempdir(), f"concat_{prefix}.txt") with open(list_file, "w", encoding="utf-8") as handle: for path in valid: handle.write(f"file '{path}'\n") subprocess.run([_ffmpeg_exe(), "-y", "-f", "concat", "-safe", "0", "-i", list_file, "-c", "copy", "-movflags", "+faststart", out_path], check=False) if os.path.exists(out_path): _persist(out_path) return out_path return valid[-1] def _queue_status(count, path=None): if not count: return "<div class='status-idle'>Merge queue: empty</div>" name = f" &rarr; {os.path.basename(path)}" if path else "" return f"<div class='status-ok'><b>{count} clip(s) in the queue</b><span>{name}</span></div>" def add_to_queue(video_path, queue, name_hint): queue = list(queue or []) # Not twice. "Continue the scene" parks the finished clip before it generates, and the previous # press already parked that same clip on its way out - so without this the queue grows # A, B, B, C, C rather than A, B, C. if video_path and os.path.exists(str(video_path)) and (not queue or queue[-1] != video_path): queue.append(video_path) if not queue: return None, None, [], _queue_status(0) merged = concat_videos(queue, name_hint) return merged, merged, queue, _queue_status(len(queue), merged) def clear_queue(): return None, None, [], _queue_status(0) def auto_queue(video_path, auto_on, queue, name_hint): """Runs after every generation; only appends when the box is ticked.""" if not auto_on: queue = list(queue or []) if not queue: return gr.update(), gr.update(), queue, _queue_status(0) return gr.update(), gr.update(), queue, _queue_status(len(queue)) return add_to_queue(video_path, queue, name_hint) def export_gif_download(video_path, fps, width): """Expose Horizon-style GIF export as a Gradio download, without GPU time.""" try: path = export_gif(video_path, fps, width) except (ValueError, RuntimeError) as error: raise gr.Error(str(error)) from error return gr.update(value=path, visible=True) def prepare_trim(current, joined, source): """Show the CPU editor only when the chosen video exists; reset stale exports.""" path = joined if source == "Joined video" else current try: duration = trim_video_duration(path) available = duration > 0 except Exception: duration, available = 0, False return (gr.update(visible=bool(current or joined)), 0, duration, None, None, "", gr.update(interactive=available)) def export_trim_download(current, joined, source, start, end): """Export a separate MP4 on the CPU, without changing either source video.""" path = joined if source == "Joined video" else current try: result = _persist(str(trim_video(path, start, end))) except (ValueError, RuntimeError) as error: raise gr.Error(str(error)) from error return result, result, f"Trim ready: {float(start):g}–{float(end):g} seconds. Original kept." def boomerang(video_path, mode, tail_seconds=2.0): """`full` plays forward then backward. `ending` keeps the clip as it is and boomerangs only its tail, which loops without the whole thing rewinding.""" if not video_path or not os.path.exists(video_path): raise gr.Error("Generate a video first.") prefix = uuid.uuid4().hex[:4] out_path = os.path.join(tempfile.gettempdir(), f"{prefix}_boomerang.mp4") with_audio = _has_audio(video_path) if mode == "ending": capture = cv2.VideoCapture(video_path) fps = capture.get(cv2.CAP_PROP_FPS) or 16 total = int(capture.get(cv2.CAP_PROP_FRAME_COUNT) or 0) capture.release() duration = total / fps if fps else 0 start = max(0.0, duration - float(tail_seconds)) filter_str = ( f"[0:v]split=2[base][tail];" f"[tail]trim=start={start:.3f},setpts=PTS-STARTPTS,reverse[rev];" f"[base][rev]concat=n=2:v=1[outv]" ) if with_audio: filter_str = ( f"[0:v]split=2[base][tail];" f"[tail]trim=start={start:.3f},setpts=PTS-STARTPTS,reverse[rev];" f"[0:a]asplit=2[abase][atail];" f"[atail]atrim=start={start:.3f},asetpts=PTS-STARTPTS,areverse[arev];" f"[base][abase][rev][arev]concat=n=2:v=1:a=1[outv][outa]" ) else: filter_str = "[0:v]split=2[a][b];[b]reverse[r];[a][r]concat=n=2:v=1[outv]" if with_audio: filter_str = ( "[0:v]split=2[v1][v2];[v2]reverse[vr];" "[0:a]asplit=2[a1][a2];[a2]areverse[ar];" "[v1][a1][vr][ar]concat=n=2:v=1:a=1[outv][outa]" ) command = [_ffmpeg_exe(), "-y", "-i", video_path, "-filter_complex", filter_str, "-map", "[outv]"] if with_audio: command += ["-map", "[outa]", "-c:a", "aac", "-ar", "48000", "-ac", "2"] command += ["-c:v", "libx264", "-preset", "fast", "-crf", "20", "-movflags", "+faststart", out_path] try: subprocess.run(command, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) except Exception as error: raise gr.Error(f"Loop failed: {error}") _persist(out_path) return out_path, out_path # --------------------------------------------------------------------------- # Sound. Video-to-audio (Foley) on a finished clip # --------------------------------------------------------------------------- # Wan 2.2 makes pictures only, so sound is a second pass over the clip that already exists. # Two open models, both free and both running inside this Space - nothing is uploaded anywhere: # # HunyuanVideo-Foley (Tencent) - the best open video-to-audio there is; 48 kHz, heavier. # MMAudio - a fraction of the size and seconds instead of a minute. # # MMAudio and its feature models are prepared on CPU before serving the UI. Video decoding # and final audio/video muxing run on CPU; sound inference books a separate GPU lease. # Hunyuan remains optional and is downloaded only when selected. FOLEY_ENGINES = { "HunyuanVideo-Foley — best quality, slower": "hunyuan", "MMAudio — fast and light": "mmaudio", } FOLEY_DEFAULT = "MMAudio — fast and light" FOLEY_INSTALL_HINT = ( "The sound model could not be installed. Check the Space logs, or add these lines to " "requirements.txt and restart: av, einops, colorlog, omegaconf, hydra-core, " "open_clip_torch, timm, tensordict, torchdiffeq, nitrous-ema, librosa, soundfile, torchaudio." ) _MMAUDIO_SPEC = "git+https://github.com/hkchengrex/MMAudio.git" _HUNYUAN_SPEC = "git+https://github.com/Tencent-Hunyuan/HunyuanVideo-Foley.git" _HUNYUAN_REPO = "tencent/HunyuanVideo-Foley" _FOLEY_PATHS = {} def _foley_lib_dir(): """A writable folder of our own for the sound packages, kept off the Space's main site-packages. `/data` when the Space has persistent storage, so the install survives a restart; a temp folder otherwise. Appended to the end of sys.path, never the front. Anything in front would shadow the Space's own libraries, and a sound package quietly standing in for the torch or transformers that Wan runs on is the worst kind of breakage - it looks like the video model went wrong.""" base = "/data" if os.path.isdir("/data") and os.access("/data", os.W_OK) else tempfile.gettempdir() path = os.path.join(base, "foley_pkgs") os.makedirs(path, exist_ok=True) if path not in sys.path: sys.path.append(path) return path def _probe_imports(modules, target): """Try the imports in a throwaway process; return the first missing module, or "". In a separate process on purpose. These packages pull in torch modules that touch CUDA as soon as they are imported, and on ZeroGPU that outside a booked slot takes the whole Space down with it - which is exactly what checking the imports in-process did. A subprocess with the card hidden can fail however it likes; the worst it costs is a second.""" code = ( "import sys, importlib\n" "for name in %r:\n" " try:\n" " importlib.import_module(name)\n" " except ModuleNotFoundError as error:\n" " print('MISSING:' + ((error.name or '').split('.')[0])); sys.exit(0)\n" " except BaseException:\n" " pass\n" "print('OK')\n" % (list(modules),) ) env = dict(os.environ) env["CUDA_VISIBLE_DEVICES"] = "" env["PYTHONPATH"] = target + os.pathsep + env.get("PYTHONPATH", "") try: result = subprocess.run([sys.executable, "-c", code], capture_output=True, env=env, timeout=300) except Exception: # noqa: BLE001 return "" for line in result.stdout.decode("utf-8", "ignore").splitlines(): if line.startswith("MISSING:"): return line.split(":", 1)[1].strip() return "" # Import name -> pip name, where they differ. `None` means never install it: those are the # libraries Wan itself runs on, and letting a sound package pull its own pinned copy over the # top is exactly how a working Space stops working. _PIP_NAMES = { "cv2": "opencv-python", "yaml": "pyyaml", "PIL": "pillow", "dac": "descript-audio-codec", "sklearn": "scikit-learn", "skimage": "scikit-image", # The `audiotools` module is Descript's: there is no `audiotools` on PyPI # (404), so without this mapping pip is asked for a package that cannot exist. "audiotools": "descript-audiotools", # descript-audiotools is installed --no-deps, so its imports surface one at a # time. These are the ones whose module name differs from their pip name. "soundfile": "soundfile", "pyloudnorm": "pyloudnorm", "julius": "julius", "ffmpy": "ffmpy", "IPython": "ipython", "rich": "rich", "importlib_resources": "importlib-resources", "flatten_dict": "flatten-dict", "markdown2": "markdown2", "randomname": "randomname", "argbind": "argbind", "pystoi": "pystoi", "torch_stoi": "torch-stoi", "librosa": "librosa", "tensorboard": "tensorboard", "matplotlib": "matplotlib", "open_clip": "open_clip_torch", "hydra": "hydra-core", "nitrous_ema": "nitrous-ema", "attr": "attrs", "pkg_resources": "setuptools", "google": "protobuf", "torch": None, "torchvision": None, "torchaudio": None, "numpy": None, "transformers": None, "diffusers": None, "gradio": None, "gradio_client": None, "huggingface_hub": None, "safetensors": None, "accelerate": None, "peft": None, } def _pip_no_deps(spec, target): """Install one thing, without letting pip touch anything else.""" print(f"[sound] installing {spec} into {target}", flush=True) result = subprocess.run( [sys.executable, "-m", "pip", "install", "--no-deps", "--quiet", "--target", target, "--upgrade", spec], capture_output=True, ) if result.returncode != 0: print(result.stderr.decode("utf-8", "ignore")[-2000:], flush=True) return result.returncode == 0 def _ensure_package(module_name, spec, extra_modules=()): """Get the package - and whatever it turns out to need - installed, without importing any of it here. `--no-deps` on purpose: both of these repos pin an old gradio, an old diffusers and an old transformers in their own requirements, and letting pip resolve that tree either fails the build or quietly downgrades the libraries Wan itself runs on. The cost is that nothing brings its own dependencies, so the chain is walked one piece at a time: probe the imports in a throwaway process, read the name off the failure, install just that one thing, probe again.""" target = _foley_lib_dir() wanted = [module_name, *extra_modules] installed_root = False tried = set() for _attempt in range(24): missing = _probe_imports(wanted, target) if not missing: return if missing == module_name.split(".")[0]: if installed_root or not spec: raise gr.Error(FOLEY_INSTALL_HINT) installed_root = True if not _pip_no_deps(spec, target): raise gr.Error(FOLEY_INSTALL_HINT) continue pip_name = _PIP_NAMES.get(missing, missing) if pip_name is None: raise gr.Error( f"The sound model wants its own build of `{missing}`, which Wan itself runs on. " f"Installing it would break video generation, so it is refused. Use the other " f"sound model instead." ) if missing in tried: raise gr.Error( f"The sound model needs `{missing}` and it could not be installed. Add " f"`{pip_name}` to the Space's requirements.txt and restart, or use the other " f"sound model." ) tried.add(missing) if not _pip_no_deps(pip_name, target): raise gr.Error( f"The sound model needs `{missing}` and pip could not install `{pip_name}`. " f"The Space logs carry the reason." ) raise gr.Error(FOLEY_INSTALL_HINT) def _mmaudio_config(): """Install, download, and load all MMAudio components once during startup.""" if _FOLEY_PATHS.get("mmaudio") is not None: return _FOLEY_PATHS["mmaudio"] _ensure_package("mmaudio", _MMAUDIO_SPEC, extra_modules=( "mmaudio.eval_utils", "mmaudio.model.flow_matching", "mmaudio.model.networks", "mmaudio.model.utils.features_utils", )) _foley_lib_dir() from mmaudio.eval_utils import all_model_cfg cfg = all_model_cfg["large_44k_v2"] cfg.download_if_needed() audio_runtime.prepare(cfg) _FOLEY_PATHS["mmaudio"] = cfg return cfg @spaces.GPU(duration=120) def _mmaudio_run(clip_frames, sync_frames, prompt, negative, steps, guidance, seconds, seed): from mmaudio.eval_utils import generate from mmaudio.model.flow_matching import FlowMatching cfg, net, feature_utils = audio_runtime.ready_models() device = torch.device("cuda" if torch.cuda.is_available() else "cpu") dtype = torch.bfloat16 if device.type == "cuda" else torch.float32 net.to(device=device, dtype=dtype) feature_utils.to(device=device, dtype=dtype) seq_cfg = copy.deepcopy(cfg.seq_cfg) seq_cfg.duration = float(seconds) net.update_seq_lengths(seq_cfg.latent_seq_len, seq_cfg.clip_seq_len, seq_cfg.sync_seq_len) flow = FlowMatching(min_sigma=0, inference_mode="euler", num_steps=int(steps)) rng = torch.Generator(device=device).manual_seed(int(seed)) with torch.inference_mode(): audios = generate( clip_frames.unsqueeze(0), sync_frames.unsqueeze(0), [prompt], negative_text=[negative], feature_utils=feature_utils, net=net, fm=flow, rng=rng, cfg_strength=float(guidance), ) return audios.float().cpu()[0], seq_cfg.sampling_rate def _hunyuan_paths(): """CPU work: the package, the weights, and the config that goes with them.""" if _FOLEY_PATHS.get("hunyuan"): return _FOLEY_PATHS["hunyuan"] _ensure_package("hunyuanvideo_foley", _HUNYUAN_SPEC, extra_modules=( "hunyuanvideo_foley.utils.model_utils", "hunyuanvideo_foley.utils.feature_utils", "hunyuanvideo_foley.utils.media_utils", )) _foley_lib_dir() from huggingface_hub import snapshot_download model_path = snapshot_download(repo_id=_HUNYUAN_REPO, token=os.environ.get("HF_TOKEN")) config_path = os.environ.get("HUNYUAN_FOLEY_CONFIG", "").strip() or None if config_path is None: for root, _dirs, files in os.walk(model_path): for name in files: if name.endswith((".yaml", ".yml")): config_path = os.path.join(root, name) break if config_path: break if config_path is None: import hunyuanvideo_foley roots = [ os.path.dirname(os.path.abspath(hunyuanvideo_foley.__file__)), os.path.dirname(os.path.dirname(os.path.abspath(hunyuanvideo_foley.__file__))), ] for base in roots: for candidate in ("configs/hunyuanvideo-foley-xxl.yaml", "configs/hunyuanvideo-foley-xl.yaml"): full = os.path.join(base, candidate) if os.path.exists(full): config_path = full break if config_path: break if config_path is None: raise gr.Error("HunyuanVideo-Foley is installed but its .yaml config could not be found. " "Use MMAudio instead, or set HUNYUAN_FOLEY_CONFIG to the config path.") _FOLEY_PATHS["hunyuan"] = (model_path, config_path) return _FOLEY_PATHS["hunyuan"] @spaces.GPU(duration=240) def _hunyuan_run(video_path, prompt, negative, steps, guidance, model_path, config_path, out_path): import torch import torchaudio _foley_lib_dir() from hunyuanvideo_foley.utils.model_utils import load_model, denoise_process from hunyuanvideo_foley.utils.feature_utils import feature_process from hunyuanvideo_foley.utils.media_utils import merge_audio_video device = torch.device("cuda" if torch.cuda.is_available() else "cpu") model_dict, cfg = load_model(model_path, config_path, device) visual_feats, text_feats, audio_len = feature_process(video_path, prompt, model_dict, cfg) audio, sample_rate = denoise_process( visual_feats, text_feats, audio_len, model_dict, cfg, guidance_scale=float(guidance), num_inference_steps=int(steps), batch_size=1, ) wav_path = os.path.join(tempfile.gettempdir(), f"foley_{uuid.uuid4().hex[:6]}.wav") torchaudio.save(wav_path, audio[0], sample_rate) merge_audio_video(wav_path, video_path, out_path) return out_path FOLEY_NEGATIVE = "music, speech, voice, singing, noise, static, hiss, distortion" def add_sound(video_path, engine_label, prompt, negative, steps, guidance, seed=0, unlocked=True): """Dub the clip and hand back a copy with the sound on it. `unlocked` defaults to True because the scene chain calls this itself, after its own gate check has already passed; the button passes the real flag.""" require_access(unlocked) if not video_path or not os.path.exists(str(video_path)): raise gr.Error("Generate a video first — there is nothing to add sound to.") engine = FOLEY_ENGINES.get(engine_label, "mmaudio") prompt = str(prompt or "").strip() negative = str(negative or "").strip() or FOLEY_NEGATIVE out_path = os.path.join(tempfile.gettempdir(), f"{uuid.uuid4().hex[:4]}_sound.mp4") started = time.time() try: if engine == "hunyuan": model_path, config_path = _hunyuan_paths() _hunyuan_run(str(video_path), prompt, negative, steps, guidance, model_path, config_path, out_path) else: from pathlib import Path import soundfile as sf from mmaudio.eval_utils import load_video audio_runtime.ready_models() # No model installation or downloads in this handler. info = load_video(Path(video_path), _video_duration(video_path), load_all_frames=False) audio, sample_rate = _mmaudio_run( info.clip_frames, info.sync_frames, prompt, negative, steps, guidance, info.duration_sec, int(seed) or random.randint(0, 10_000), ) waveform = audio.numpy() if waveform.ndim == 2: waveform = waveform.T wav_path = os.path.join(tempfile.gettempdir(), f"foley_{uuid.uuid4().hex}.wav") try: sf.write(wav_path, waveform, sample_rate) audio_runtime.mux_audio(video_path, wav_path, out_path) finally: if os.path.exists(wav_path): os.remove(wav_path) except (ImportError, ModuleNotFoundError): raise gr.Error(FOLEY_INSTALL_HINT) except gr.Error: raise except Exception as error: raise gr.Error(f"Sound failed: {error}") if not os.path.exists(out_path): raise gr.Error("The sound model produced nothing.") out_path = _persist(out_path) or out_path label = "HunyuanVideo-Foley" if engine == "hunyuan" else "MMAudio" status = (f"<div class='status-ok'><b>&#128266; sound added</b><span>{label}</span>" f"<span>{time.time() - started:.1f} s</span></div>") return out_path, out_path, status def maybe_sound(video_path, wanted, engine_label, prompt, negative, steps, guidance, unlocked=True): """Used after a generation when the automatic box is ticked; a no-op otherwise.""" if not wanted or not video_path: return gr.update(), gr.update(), gr.update() return add_sound(video_path, engine_label, prompt, negative, steps, guidance, unlocked=unlocked) # --------------------------------------------------------------------------- # Prompt building and enhancement # --------------------------------------------------------------------------- # Two paths. The builder is pure string work: it takes what you typed and wraps # it in the camera, framing, light and pacing language Wan responds to. It is # instant, costs nothing, and cannot fail or refuse. # # The optional rewrite runs on the same external Qwen3-VL conditioner Horizon # uses. This Space never loads a second model or books its GPU for prompt writing. CONDITIONER_SPACE = os.environ.get("PROMPT_CONDITIONER", "multimodalart/qwen3vl-conditioner") CAMERA_MOVES = { "(keep as written)": "", "static shot": "the camera holds still", "slow push in": "the camera pushes in slowly", "slow pull back": "the camera pulls back slowly", "orbit around the subject": "the camera orbits around the subject", "tracking alongside": "the camera tracks alongside the subject", "handheld, subtle shake": "handheld camera with a subtle shake", "tilt up": "the camera tilts up", "tilt down": "the camera tilts down", "crane up and back": "the camera cranes up and back", } FRAMINGS = { "(keep as written)": "", "extreme close-up": "extreme close-up", "close-up": "close-up", "medium shot": "medium shot", "medium wide": "medium wide shot", "wide shot": "wide shot", "low angle": "low angle shot", "high angle": "high angle shot", "over the shoulder": "over-the-shoulder shot", "point of view": "point-of-view shot", } LIGHTING = { "(keep as written)": "", "soft natural light": "soft natural light, gentle falloff", "golden hour": "warm golden-hour light with a soft rim", "hard key light": "a hard key light with deep shadows", "candle / firelight": "flickering warm candlelight", "neon night": "neon light, wet reflections, night", "overcast daylight": "flat overcast daylight", "studio softbox": "clean studio softbox lighting on a plain backdrop", "single lamp in a dark room": "one warm lamp in an otherwise dark room", "moonlight": "cool moonlight through a window", } PACING = { "(keep as written)": "", "slow and deliberate": "slow, deliberate movement", "steady": "steady, even movement", "building": "movement that builds through the shot", "fast and urgent": "fast, urgent movement", "loose and languid": "loose, languid movement", } QUALITY_TAIL = ( "photorealistic, natural skin texture, shallow depth of field, " "consistent anatomy, smooth motion, no morphing" ) def _strip_built(text): """Remove sentences a previous Build added, so pressing it again rewrites rather than stacks.""" known = {QUALITY_TAIL.lower().rstrip(".")} for table in (LIGHTING, PACING): for value in table.values(): if value: known.add(value.lower().rstrip(".")) for framing in FRAMINGS.values(): for camera in CAMERA_MOVES.values(): joined = ", ".join(part for part in (framing, camera) if part) if joined: known.add(joined.lower().rstrip(".")) kept = [] for sentence in re.split(r"(?<=\.)\s+", (text or "").strip()): if sentence.strip().lower().rstrip(".") not in known: kept.append(sentence.strip()) return " ".join(kept).strip() def build_prompt(prompt, camera, framing, lighting, pace, add_quality=True): """Wrap what the user wrote in the shot language Wan responds to. No model, no network, nothing to refuse - it only ever adds to your own words, and it replaces what it added last time.""" body = _strip_built(prompt).strip().rstrip(".,;") if not body: raise gr.Error("Write what should happen first - the builder adds the shot around it.") parts = [body + "."] shot = ", ".join(part for part in (FRAMINGS.get(framing, ""), CAMERA_MOVES.get(camera, "")) if part) if shot: parts.append(shot[0].upper() + shot[1:] + ".") if LIGHTING.get(lighting): parts.append(LIGHTING[lighting][0].upper() + LIGHTING[lighting][1:] + ".") if PACING.get(pace): parts.append(PACING[pace][0].upper() + PACING[pace][1:] + ".") if add_quality: parts.append(QUALITY_TAIL + ".") return " ".join(parts) def _rewrite_with_conditioner(text, ip_token=None): """Ask Horizon's conditioner for its rewritten prompt without booking this Space's GPU.""" from gradio_client import Client client = (Client(CONDITIONER_SPACE, headers={"x-ip-token": ip_token}) if ip_token else Client(CONDITIONER_SPACE)) _, plan = client.predict( prompt=text, image_path=None, last_image_path=None, canvas="960x544 · 16:9 fast", num_frames=124, rewrite_prompt=True, api_name="/encode", ) return (plan or {}).get("refined_prompt") or "" def enhance_prompt(prompt, use_model, camera, framing, lighting, pace, add_quality, unlocked=False, progress=gr.Progress(), request: gr.Request = None): """Build locally, then optionally rewrite on the external Qwen3-VL conditioner.""" built = build_prompt(prompt, camera, framing, lighting, pace, add_quality) if not use_model: return built, "<div class='status-ok'><b>Built</b><span>no GPU time used</span></div>" if not unlocked: return built, ( "<div class='status-lock'><b>&#128274; The Qwen3-VL rewrite needs an access key</b>" f"<span>the built prompt is above; {BUY_LINK} to unlock the model writer</span>" "</div>" ) try: progress(0.1, desc="Qwen3-VL conditioner is rewriting the prompt...") ip_token = request.headers.get("x-ip-token") if request is not None else None written = _rewrite_with_conditioner(built, ip_token=ip_token) except Exception as error: print(f"[prompt builder] conditioner rewrite failed: {error!r}", flush=True) return built, ( "<div class='status-idle'>Qwen3-VL is unavailable; the built prompt is shown instead.</div>" ) if not written.strip(): return built, "<div class='status-idle'>Qwen3-VL returned no rewrite; built prompt kept.</div>" return written, ( "<div class='status-ok'><b>Rewritten by Qwen3-VL</b>" f"<span>{len(written.split())} words</span></div>" ) # --------------------------------------------------------------------------- # Naming CivitAI links # --------------------------------------------------------------------------- # A CivitAI download URL is a bare number, which is useless once three slots are # filled. The number is the *model version* id, and CivitAI's public API turns it # into the model's title, the version, the file behind `fileId`, and the words the # adapter was trained on. Public, cached, and never fatal. from functools import lru_cache CIVITAI_DOWNLOAD_RE = re.compile(r"/api/download/models/(\d+)") @lru_cache(maxsize=128) def civitai_details(version_id: str, file_id: str = ""): import requests headers = {"User-Agent": "Mozilla/5.0"} token = os.environ.get("CIVITAI_TOKEN", "").strip() if token: headers["Authorization"] = f"Bearer {token}" try: response = requests.get( f"https://civitai.com/api/v1/model-versions/{version_id}", headers=headers, timeout=20 ) response.raise_for_status() data = response.json() except Exception as error: return f"CivitAI {version_id} (name unavailable: {type(error).__name__})", [] model_name = (data.get("model") or {}).get("name") or f"model {data.get('modelId', '?')}" version_name = data.get("name") or "" label = f"{model_name} · {version_name}".strip(" ·") if file_id: for entry in data.get("files") or []: if str(entry.get("id")) == str(file_id): label += f" · {entry.get('name', '')}" break return label, [word for word in (data.get("trainedWords") or []) if word] def describe_lora(reference: str): """A readable line for whatever is in a box: a CivitAI link becomes its real title.""" reference = (reference or "").strip() if not reference or reference == "(None)": return "", [] match = CIVITAI_DOWNLOAD_RE.search(reference) if match: file_id = "" if "fileId=" in reference: file_id = reference.split("fileId=", 1)[1].split("&")[0] return civitai_details(match.group(1), file_id) if reference.startswith(("http://", "https://")): return os.path.basename(reference.split("?")[0]) or reference, [] return reference, [] def identify_loras(*fields): """Name every filled box across the five slots. `fields` arrives as `repo, high, low` per slot, so each slot is reported on one line.""" lines = [] for slot in range(len(fields) // 3): repo, high, low = fields[slot * 3: slot * 3 + 3] parts = [] for role, value in (("repo", repo), ("high", high), ("low", low)): label, words = describe_lora(value) if not label: continue entry = f"{role}: {label}" if words: entry += f" — triggers: {', '.join(words[:6])}" parts.append(entry) if parts: lines.append(f"<b>Custom lora {slot + 1}</b> — " + " · ".join(parts)) if not lines: return "<div class='status-idle'>Nothing in the slots yet.</div>" return "<div class='status-ok'>" + "<br>".join(lines) + "</div>" def last_frame_of(video_path): """The final frame of a clip, as the array the image box takes. Used to chain a scene: the shot that just ended becomes the still the next one starts from.""" if not video_path or not os.path.exists(str(video_path)): raise gr.Error("Generate a video first - there is nothing to continue from.") capture = cv2.VideoCapture(str(video_path)) if not capture.isOpened(): raise gr.Error("That video cannot be read.") capture.release() # The true final frame, decoded forward rather than seeked to. Stepping back three # frames "for safety" is what put the join out by roughly one frame: the next clip # then started from a moment the previous clip had already played past. frame = _last_frame_bgr(video_path) if frame is None: capture = cv2.VideoCapture(str(video_path)) while True: ok, candidate = capture.read() if not ok or candidate is None: break frame = candidate capture.release() if frame is None: raise gr.Error("That video has no readable frames.") return cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) def _last_frame_pil(video_path): """The final frame of a clip as a PIL image, ready to be fed straight back in.""" return Image.fromarray(last_frame_of(video_path)) def stage_extension(video_path, queue, name_hint, current_image=None, identity_ref=None): """Before a continuation runs: park the finished clip in the merge queue and hand its last frame back as the new starting image. The clip is always parked, whatever the auto-merge box says - otherwise the first clip of a chain is lost and the stitched file starts from the second one. The end frame is wiped as well: an end frame left over from the previous take drags every continuation back to the same picture. The identity lock is only ever the face the user uploaded, so the new clip opens on exactly the frame the old one closed on and the seam trims cleanly.""" frame = _last_frame_pil(video_path) merged, merged_file, queue, status = add_to_queue(video_path, queue, name_hint) locked = identity_ref if identity_ref is not None else gr.update() return frame, None, None, merged, merged_file, queue, status, locked def _scene_prompt_lines(text, total, fallback): """One prompt per clip. A line the user left empty - or a line that is not there at all - means that clip runs on the main prompt, so the box can be left alone entirely.""" lines = str(text or "").splitlines() out = [] for index in range(total): line = lines[index].strip() if index < len(lines) else "" out.append(line or fallback) return out MAX_SCENE_CLIPS = 8 def _scene_trouble(error): """The quota messages read like a stack trace. Say what actually happened.""" text = str(error) if "runs limit" in text: return ("ZeroGPU has a limit on how many separate runs you may start, not just on " "seconds, and this run reached it. Wait for it to reset, or sign in to " "Hugging Face in this browser if you have not - the allowance is counted " "against whoever is watching the page, not against the Space.") if "quota" in text.lower(): return ("The ZeroGPU allowance ran out part-way through. " + text) return text def _scene_label(text, limit=90): """The prompt as it will read in a one-line status.""" line = " ".join(str(text or "").split()) return (line[:limit] + "\u2026") if len(line) > limit else (line or "(no prompt)") def mark_running(message="Generating\u2026"): """Clear the player and say the work has started. Gradio's own spinner sits over the output components, which is easy to miss on a video that still shows the previous clip - it looks as though nothing happened. Emptying the player and writing a line into the status is unmissable, and it costs nothing: this runs instantly, before the GPU is booked.""" return None, (f"<div class='status-run'><b>&#9203; {message}</b>" f"<span>the video appears here when it is done</span></div>") def scene_begin(clip_count, start_at, unlocked=False): """Arm the chain, from `start_at` to the end of the scene. `start_at` is what makes a stopped run resumable. The prompt sheet is numbered against the whole scene, so a second press has to know it is making clips five to eight and not one to four again - otherwise it silently repeats the first four prompts. Every stop leaves this box pointing at the clip that did not get made.""" # Refused here rather than one clip in: arming a chain that cannot make its # first clip would leave eight queued steps to fail one after another. require_access(unlocked) total = max(1, min(MAX_SCENE_CLIPS, int(clip_count or 1))) start = max(1, min(total, int(start_at or 1))) left = total - start + 1 span = f"clip {start}" if left == 1 else f"clips {start} to {total}" return ({"left": left, "total": total, "made": 0}, f"<div class='status-ok'><b>Making {span}</b>" f"<span>one after another</span></div>") def scene_step(state, scene_prompts, sound_on, sound_engine, sound_prompt, sound_negative, sound_steps, sound_guidance, queue, name_hint, *ui_values): """One clip of a chain, and nothing at all once the chain is finished. Deliberately one clip per call rather than a loop inside a single handler. A ZeroGPU token is minted for the browser request that started the work and it expires within minutes; a loop that runs for a quarter of an hour is still holding the first one by clip two. Chaining separate steps means every clip arrives on its own fresh request. The start image is handed back as the last frame of the clip just made, so the next step picks it up without anything having to be threaded through.""" state = dict(state or {}) left = int(state.get("left", 0)) idle = tuple(gr.update() for _ in range(10)) if left <= 0: yield (state, *idle) return total = int(state.get("total", left)) index = total - left values = list(ui_values) if values[0] is None: state["left"] = 0 raise gr.Error("Upload a start image first.") queue = list(queue or []) values[2] = _scene_prompt_lines(scene_prompts, total, values[2])[index] # An end frame left over from a previous take would drag every clip back to it, so it goes. values[1] = None # Say which clip is running and on which prompt before it starts, not after it finishes - # a clip takes minutes, and a chain of eight is a long time to sit in front of a screen # that will not say what it is doing. yield (state, gr.update(), gr.update(), gr.update(), f"<div class='status-run'><b>&#127916; clip {index + 1} of {total} running</b>" f"<span>{_scene_label(values[2])}</span></div>", *(gr.update() for _ in range(6))) try: video, file_path, seed, status = generate_video(*values) except Exception as error: # noqa: BLE001 state["left"] = 0 if not queue: raise merged = concat_videos(queue, name_hint) if len(queue) > 1 else queue[0] yield (state, gr.update(), queue[-1], gr.update(), f"<div class='status-ok'><b>&#9888;&#65039; Stopped after " f"{state.get('made', 0)} of {total} clips</b>" f"<span>{_scene_trouble(error)}</span><span>the clips above are joined and " f"safe; the start image is the last frame, so pressing &#127916; again carries " f"on from here</span></div>", merged, merged, queue, _queue_status(len(queue), merged), gr.update(), index + 1) return if not file_path: state["left"] = 0 yield (state, *idle) return # The next clip's start frame comes from the silent file, before any dubbing rewrites it. frame_source = file_path if sound_on: dubbed, file_path, status = add_sound(file_path, sound_engine, sound_prompt, sound_negative, sound_steps, sound_guidance) if video is not None: video = dubbed if not queue or queue[-1] != file_path: queue.append(file_path) merged = concat_videos(queue, name_hint) if len(queue) > 1 else queue[0] state["left"] = left - 1 state["made"] = int(state.get("made", 0)) + 1 done = state["made"] note = (status if state["left"] <= 0 else f"<div class='status-ok'><b>&#10003; clip {done} of {total} done</b>" f"<span>{_scene_label(values[2])}</span>" f"<span>{len(queue)} clip(s) joined</span></div>") yield (state, video, file_path, seed, note, merged, merged, queue, _queue_status(len(queue), merged), _last_frame_pil(frame_source), 1 if state["left"] <= 0 else index + 2) # Which host answers the search. `.red` is a mirror of the same API and carries entries the main # domain hides, so it is asked first and `.com` is the fallback. Set CIVITAI_API_HOST to pin one. # Note that adult entries are returned to an authenticated caller only, whichever host answers: # without CIVITAI_TOKEN the X / XXX levels are simply absent from the results. CIVITAI_API_HOSTS = [h for h in (os.environ.get("CIVITAI_API_HOST", "").strip(), "civitai.red", "civitai.com") if h] WAN_BASE_MODELS = [ "Wan Video 2.2 I2V-A14B", "Wan Video 2.2 T2V-A14B", "Wan Video 14B i2v 480p", "Wan Video 14B i2v 720p", "(any base model)", ] def civitai_search(query, base_model, want_nsfw, limit=20): """Search CivitAI for lora. Returns `(readable list, dropdown update, {label: url})`.""" import requests query = (query or "").strip() if not query: return "<div class='status-idle'>Type something to search for.</div>", gr.update(choices=[], value=None), {} params = {"query": query, "types": "LORA", "limit": int(limit), "sort": "Most Downloaded"} if base_model and base_model != "(any base model)": params["baseModels"] = base_model if want_nsfw: params["nsfw"] = "true" headers = {"User-Agent": "Mozilla/5.0"} token = os.environ.get("CIVITAI_TOKEN", "").strip() if token: # Both forms on purpose. The header is the documented one, but search honours the query # parameter more reliably - and without an authenticated call the adult browsing levels are # simply missing from the results, whichever host answers. headers["Authorization"] = f"Bearer {token}" params["token"] = token items, error, answered = [], None, False host_used = CIVITAI_API_HOSTS[0] for host in CIVITAI_API_HOSTS: try: response = requests.get(f"https://{host}/api/v1/models", params=params, headers=headers, timeout=30) response.raise_for_status() items = response.json().get("items") or [] host_used, answered = host, True if items: break except Exception as failure: # noqa: BLE001 error = failure if not items and error is not None and not answered: # Deliberately not quoting the exception: `requests` puts the full URL in its message, and the # URL carries the token as a query parameter, so echoing it would print the key on screen. status = getattr(getattr(error, "response", None), "status_code", None) reason = f"HTTP {status}" if status else type(error).__name__ tried = ", ".join(CIVITAI_API_HOSTS) return (f"<div class='status-idle'>Search failed ({reason}). Tried: {tried}. CivitAI answers 503 " f"when it is rate-limiting or briefly down - wait a moment and press Search again.</div>", gr.update(choices=[], value=None), {}) if not items: return ("<div class='status-idle'>Nothing found. Try fewer words, or set the base model to " "(any base model).</div>", gr.update(choices=[], value=None), {}) mapping, lines, choices = {}, [], [] for item in items: model_name = item.get("name") or "?" creator = (item.get("creator") or {}).get("username") or "?" downloads = ((item.get("stats") or {}).get("downloadCount") or 0) for version in (item.get("modelVersions") or [])[:3]: version_name = version.get("name") or "" # What the adapter was trained against. Worth showing on every row: with the filter set to # "(any base model)" the results mix families, and a lora for another one downloads happily # and then does nothing useful. base = version.get("baseModel") or "base model unknown" words = [w for w in (version.get("trainedWords") or []) if w] for entry in (version.get("files") or []): name = entry.get("name") or "" if not name.lower().endswith(".safetensors"): continue url = f"https://{host_used}/api/download/models/{version.get('id')}?fileId={entry.get('id')}" size = float(entry.get("sizeKB") or 0) / 1024 label = f"{len(choices) + 1}. [{base}] {model_name} · {version_name} · {name}"[:160] mapping[label] = url choices.append(label) lines.append( f"<b>{label.split('. ', 1)[0]}.</b> <b>{model_name}</b> · {version_name} · {name} " f"<span>[{base}] ({size:.0f} MB · {downloads} downloads · by {creator}" + (f" · triggers: {', '.join(words[:5])}" if words else "") + ")</span>" ) if not choices: return ("<div class='status-idle'>Found models, but none with a .safetensors file.</div>", gr.update(choices=[], value=None), {}) # A scrolling box: the picker sits above this, and a 40-line wall would push it off the screen. body = ("<div class='search-results'>" + "<br>".join(lines[:40]) + "</div>") return body, gr.update(choices=choices, value=choices[0]), mapping def put_in_slot(label, mapping, slot, role, *current): """Write the chosen result's download link into one High or Low box, leaving the rest alone.""" updates = [gr.update() for _ in range(10)] url = (mapping or {}).get(label) if not url: return [*updates, "<div class='status-idle'>Search and pick a file first.</div>"] index = (int(str(slot).split()[-1]) - 1) * 2 + (0 if role == "High" else 1) updates[index] = gr.update(choices=["(None)", url], value=url) return [*updates, f"<div class='status-ok'><b>Put into {slot}, {role}</b><span>{label}</span></div>"] def _target_size(image): """Same math as resize_image(), but returns only the final (w, h).""" if image is None: return (832, 624) width, height = image.size if width == height: return (SQUARE_DIM, SQUARE_DIM) aspect_ratio = width / height MAX_ASPECT_RATIO = MAX_DIM / MIN_DIM MIN_ASPECT_RATIO = MIN_DIM / MAX_DIM if aspect_ratio > MAX_ASPECT_RATIO: target_w, target_h = MAX_DIM, MIN_DIM elif aspect_ratio < MIN_ASPECT_RATIO: target_w, target_h = MIN_DIM, MAX_DIM else: if width > height: target_w = MAX_DIM target_h = int(round(target_w / aspect_ratio)) else: target_h = MAX_DIM target_w = int(round(target_h * aspect_ratio)) final_w = round(target_w / MULTIPLE_OF) * MULTIPLE_OF final_h = round(target_h / MULTIPLE_OF) * MULTIPLE_OF final_w = max(MIN_DIM, min(MAX_DIM, final_w)) final_h = max(MIN_DIM, min(MAX_DIM, final_h)) return (final_w, final_h) class _SizeOnly: def __init__(self, w, h): self.size = (w, h) self.width = w self.height = h def _gpu_estimate_text(input_image, duration_seconds, steps, guidance_scale, frame_multiplier, safe_mode, clip_count=1, sound_on=False, sound_engine=None, sound_steps=25, reduce_duration=DEFAULT_REDUCE_DURATION): """Live ZeroGPU cost readout, using the exact same formula the @spaces.GPU decorator asks for at generation time.""" try: num_frames = get_num_frames(float(duration_seconds)) w, h = _target_size(input_image) secs = get_inference_duration( _SizeOnly(w, h), None, "", steps, "", num_frames, float(guidance_scale), 1.0, 0, "UniPCMultistep", 3.0, int(frame_multiplier), VIDEO_QUALITY, float(duration_seconds), bool(safe_mode), None, None, reduce_duration=reduce_duration, ) except Exception as exc: # never break the UI over a readout return f"⏳ **ZeroGPU cost:** estimate unavailable ({exc})" note = "" if input_image is not None else " · upload an image for an exact number" out_fps = int(frame_multiplier) out_frames = num_frames * max(1, out_fps // FIXED_FPS) # Sound is a second booking on top of every clip: model load plus the denoise. sound_secs = 0.0 if sound_on: engine = FOLEY_ENGINES.get(sound_engine, "mmaudio") if engine == "hunyuan": sound_secs = 60.0 + float(sound_steps) * 1.5 else: sound_secs = 25.0 + float(sound_steps) * 0.5 per_clip = secs + sound_secs clips = max(1, int(clip_count or 1)) lines = [f"⏳ **ZeroGPU cost** "] sound_bit = f" + ~{int(round(sound_secs))} s sound" if sound_secs else "" lines.append(f"one clip **~{int(round(per_clip))} s**" f"{f' ({int(round(secs))} s video{sound_bit})' if sound_secs else ''} ") lines.append(f"🎬 whole scene, {clips} clips: **~{int(round(per_clip * clips))} s** ") lines.append( f"<sub>{num_frames} frames · {w}×{h} · {int(steps)} steps · " f"output {out_frames} frames @ {out_fps} fps{note} \n" f"joining and looping run on the processor and cost nothing. MMAudio is prepared " f"at startup; selecting Hunyuan may require an initial download.</sub>" ) return "\n".join(lines) def _apply_speed_preset(name): cfg = SPEED_PRESETS.get(name) or {} return ( gr.update(value=cfg.get("steps", 6)), gr.update(value=cfg.get("flow_shift", 3.0)), gr.update(value=cfg.get("scheduler", "UniPCMultistep")), ) def _append_chip(prompt, chip): text = (prompt or "").strip().rstrip(",") if chip.lower() in text.lower(): return text return f"{text}, {chip}" if text else chip def _clear_all(): return ( None, None, default_prompt_i2v, gr.update(value=None), gr.update(value=None), "<div class='status-idle'>Ready when you are.</div>", gr.update(value=None), ) HEAD = """ <link rel="preconnect" href="https://fonts.googleapis.com"> <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin> <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=DM+Sans:wght@400;500;600;700&family=Space+Grotesk:wght@400;500;600;700&display=swap"> """ CSS = """ /* One type scale for the page. Everything below is expressed against --fs-base rather than as its own pixel value, so the whole app moves together and a future change is one number instead of twenty. Purely declarative: no extra elements, no scripting, nothing evaluated at request time - the rendering cost is identical to the smaller text. */ :root { --fs-base: 15px; /* was an effective ~13.5px */ --fs-sm: 13.5px; --fs-xs: 12.5px; --fs-lg: 16.5px; --ink: var(--body-text-color); } .gradio-container { font-family: 'Inter', ui-sans-serif, system-ui, sans-serif !important; font-size: var(--fs-base) !important; line-height: 1.62 !important; /* Centred, with a readable cap. The original 1440px was not the problem - the missing `margin: auto` was, which left the page hard against the left edge of a wide monitor. Capped and centred, it fills a laptop screen and stays a comfortable reading width on a large one rather than stretching edge to edge. */ max-width: 1760px !important; width: 100% !important; /* Without this the side padding is ADDED to the 100% width, making the box wider than the screen; `margin:auto` then centres that oversized box and the content slides off to the right - which is what happened on the phone. border-box keeps the padding inside the 100%. */ box-sizing: border-box !important; margin-left: auto !important; margin-right: auto !important; padding-left: 24px !important; padding-right: 24px !important; overflow-x: hidden !important; padding-top: 20px !important; padding-bottom: 28px !important; background: radial-gradient(circle at 6% 0%, rgba(13, 148, 136, .07), transparent 30rem), radial-gradient(circle at 94% 10%, rgba(16, 185, 129, .05), transparent 26rem) !important; } /* Full-width page shell. Gradio's host/root can otherwise keep the app inside a narrower default column even when .gradio-container itself is 100% wide. */ html, body, #root, gradio-app { width: 100% !important; max-width: none !important; min-width: 0 !important; } .gradio-container { width: 100vw !important; max-width: 100vw !important; margin-left: 0 !important; margin-right: 0 !important; } .gradio-container > .main, .gradio-container > .main > .wrap, .gradio-container > .main > .wrap > .contain, .gradio-container .wrap, .gradio-container .contain, .gradio-container .grid-wrap { width: 100% !important; max-width: none !important; min-width: 0 !important; } #hero, #access-panel { width: 100% !important; max-width: none !important; box-sizing: border-box !important; } /* Gradio's own wrapper is the element that actually positions the container on the page; without this the cap above still sits left on a wide screen. */ .gradio-container > .main, .gradio-container .wrap > .contain, gradio-app { width: 100% !important; margin-left: auto !important; margin-right: auto !important; box-sizing: border-box !important; max-width: 100% !important; } /* Gradio may apply its own content cap to one of the intermediate wrappers. The page container must be allowed to occupy the full viewport before its children can expand to the same width. */ .gradio-container > .main, .gradio-container > .main > .wrap, .gradio-container .wrap, .gradio-container .contain, .gradio-container .grid-wrap { width: 100% !important; max-width: none !important; box-sizing: border-box !important; } /* Nothing may be wider than the screen. A single overflowing child - a long unbroken URL in a lora box, a wide row - is enough to shift the whole page sideways on a phone, which reads as "the layout is right-aligned". */ gradio-app, .gradio-container { overflow-x: clip; } #hidden-timestamp { opacity: 0; height: 0px; width: 0px; margin: 0px; padding: 0px; overflow: hidden; position: absolute; pointer-events: none; } #hero { position: relative; overflow: hidden; border-radius: 18px; padding: 24px 28px; margin-bottom: 18px; background: linear-gradient(125deg, #e0f2fe 0%, #bae6fd 54%, #dbeafe 100%); color: #0f2f4a; border: 1px solid #7dd3fc; box-shadow: 0 14px 34px rgba(14, 116, 144, .16); } #hero::after { content: ''; position: absolute; right: -84px; bottom: -120px; width: 280px; height: 280px; border: 1px solid rgba(3, 105, 161, .18); border-radius: 50%; box-shadow: 0 0 0 28px rgba(3, 105, 161, .04), 0 0 0 56px rgba(3, 105, 161, .03); pointer-events: none; } #hero .eyebrow { margin-bottom: 8px; font-size: 12px; font-weight: 700; letter-spacing: .14em; text-transform: uppercase; opacity: .72; } #hero h1 { margin: 0; font-size: clamp(27px, 3.1vw, 34px); font-weight: 700; letter-spacing: -.02em; color: #0c2942; } #hero p { margin: 8px 0 0 0; font-size: var(--fs-lg); line-height: 1.6; opacity: .96; color: #0f2f4a; } #hero p b { color: #0f2f4a !important; } /* Pro-only hero furniture. Purely presentational - no new elements are created at request time and nothing here is measured or scripted, so the rendering cost is identical to the plain paragraph it replaces. */ #hero .hero-mark { background: rgba(255, 255, 255, .65); border: 1px solid rgba(3, 105, 161, .25); border-radius: 10px; padding: 1px 12px 3px; margin-left: 6px; font-weight: 700; color: #0c2942 !important; } #hero .lede { margin: 10px 0 0; font-size: clamp(15px, 1.5vw, 17.5px); opacity: .95; max-width: 62ch; } #hero .hero-steps { display: flex; flex-wrap: wrap; gap: 8px; margin: 14px 0 12px; } #hero .hero-steps span { display: inline-flex; align-items: baseline; gap: 7px; background: rgba(255, 255, 255, .55); border: 1px solid rgba(3, 105, 161, .19); border-radius: 10px; padding: 6px 13px; font-size: var(--fs-sm); font-weight: 500; color: #0f2f4a !important; } #hero .hero-steps b { font-size: 12px; opacity: .75; font-weight: 700; color: #0f2f4a !important; } #hero .pills { margin-top: 12px; display: flex; flex-wrap: wrap; gap: 8px; } #hero .policy { margin-top: 12px; font-size: var(--fs-base); opacity: .95; } #hero .pills span { background: rgba(255,255,255,.62); border: 1px solid rgba(3,105,161,.22); padding: 5px 13px; border-radius: 999px; font-size: var(--fs-sm); font-weight: 500; color: #0f2f4a !important; } .panel { border-radius: 16px !important; border: 1px solid var(--border-color-primary) !important; padding: 18px !important; background: var(--background-fill-secondary) !important; box-shadow: 0 4px 16px rgba(15, 23, 42, .045); transition: border-color .18s ease, box-shadow .18s ease; } .panel:hover { border-color: rgba(13, 148, 136, .34) !important; box-shadow: 0 8px 22px rgba(15, 23, 42, .07); } #generate-btn { font-size: 18px !important; font-weight: 700 !important; min-height: 64px !important; border-radius: 14px !important; letter-spacing: .01em; box-shadow: 0 8px 22px rgba(13, 148, 136, .30); } #out-col { align-self: stretch !important; } #run-dock { position: sticky !important; top: 8px; z-index: 60; flex: 0 0 auto !important; flex-grow: 0 !important; height: auto !important; min-height: 0 !important; align-self: flex-start !important; width: 100% !important; gap: 8px !important; padding: 8px !important; margin-bottom: 10px !important; border-radius: 16px !important; border: 1px solid var(--border-color-primary) !important; background: var(--background-fill-primary) !important; box-shadow: 0 10px 24px rgba(15, 23, 42, .12); backdrop-filter: blur(10px); } #run-dock > * { flex: 0 0 auto !important; } #run-dock .gpu-estimate { width: 100% !important; } #estimate-btn { flex: 1 1 auto !important; } #estimate-btn, #enhance-btn { min-height: 42px !important; border-radius: 12px !important; } #search-btn, #search-put, #extend-btn, #lora-identify, #profile-load, #profile-save, #profile-delete, #profile-refresh { min-height: 42px !important; border-radius: 12px !important; font-weight: 600 !important; } #stop-btn, #clear-btn { min-height: 46px !important; border-radius: 12px !important; } #grab-btn { min-height: 48px !important; border-radius: 12px !important; font-weight: 600 !important; } .chip-row { gap: 6px !important; } .chip-row button { border-radius: 999px !important; font-size: var(--fs-sm) !important; padding: 7px 14px !important; min-height: 36px !important; font-weight: 500 !important; transition: transform .15s ease, border-color .15s ease, background .15s ease; } .chip-row button:hover { transform: translateY(-1px); } .search-results { max-height: 300px; overflow-y: auto; border-radius: 12px; padding: 10px 14px; background: rgba(16, 185, 129, .10); border: 1px solid rgba(16, 185, 129, .30); font-size: var(--fs-base); line-height: 1.6; } .search-results span { opacity: .85; font-size: var(--fs-sm); } .consent-note { border-radius: 12px; padding: 8px 12px; margin-bottom: 10px; background: rgba(239, 68, 68, .08); border: 1px solid rgba(239, 68, 68, .30); font-size: var(--fs-sm); opacity: 1; } .lora-age-warning { border-radius: 12px; padding: 10px 13px; margin-bottom: 6px; background: rgba(245, 158, 11, .12); border: 1px solid rgba(245, 158, 11, .45); font-size: var(--fs-sm); line-height: 1.55; } .lora-age-warning b { display: block; margin-bottom: 2px; } .gpu-estimate { border-radius: 12px; padding: 12px 16px; width: 100%; box-sizing: border-box; background: rgba(13, 148, 136, .09); border: 1px solid rgba(13, 148, 136, .32); font-size: var(--fs-base); } .gpu-estimate sub { opacity: .85; font-size: var(--fs-xs); } .status-ok, .status-idle { display: flex; flex-wrap: wrap; align-items: center; gap: 14px; border-radius: 12px; padding: 12px 16px; font-size: var(--fs-base); } .status-ok { background: rgba(16, 185, 129, .12); border: 1px solid rgba(16, 185, 129, .35); } .status-idle { background: var(--background-fill-secondary); border: 1px solid var(--border-color-primary); opacity: .8; } .status-run { background: rgba(13, 148, 136, .13); border: 1px solid rgba(13, 148, 136, .38); } .status-ok span, .status-run span { opacity: .92; } button.primary, .primary > button { background: linear-gradient(125deg, #0f766e 0%, #0d9488 55%, #059669 100%) !important; border: none !important; color: #fff !important; } button.primary:hover, .primary > button:hover { filter: brightness(1.08); } .block, .form { border-radius: 14px !important; } button { border-radius: 12px !important; } .tab-nav { gap: 6px !important; } .tab-nav button { font-weight: 600 !important; font-size: var(--fs-base) !important; border-radius: 10px !important; transition: background .15s ease, color .15s ease; } label > span { font-weight: 600 !important; font-size: var(--fs-sm) !important; } /* Headings. Gradio renders section titles as plain markdown, which at the new body size no longer stands apart from the text under it. */ .prose h1, .prose h2, .prose h3, .gradio-container h3 { font-weight: 700 !important; letter-spacing: -.01em; color: var(--ink); } .prose h3, .gradio-container h3 { font-size: 19px !important; margin-bottom: 6px !important; } .prose h2 { font-size: 22px !important; } /* Body copy. The defaults lean on low opacity for hierarchy, which at small sizes reads as washed out rather than secondary. */ .prose p, .prose li { font-size: var(--fs-base) !important; line-height: 1.65 !important; } .gradio-container .prose { color: var(--ink) !important; } label > span, .gradio-container .info, .gradio-container small { opacity: .95 !important; } /* The small print under a control (Gradio's `info=`). It carries most of the guidance on this page, so it is the worst place to be faint and tiny. */ .gradio-container .info, .gradio-container div[data-testid="block-info"], .gradio-container .block-info { font-size: var(--fs-sm) !important; line-height: 1.55 !important; opacity: .95 !important; } /* Accordion titles carry most of the page's navigation, so they get a step up. */ .gradio-container .label-wrap > span, .gradio-container button.label-wrap > span { font-size: var(--fs-lg) !important; font-weight: 600 !important; } @media (max-width: 720px) { /* A step down on a phone: the same scale at full size would push the panels far taller than the screen. */ :root { --fs-base: 14.5px; --fs-lg: 15.5px; } /* Measured on a 412px viewport: the panel started at x=42 with the same gap on the right. 10px of that was this container; the other 32px was Gradio's own `.main.fillable`, which carries 32px of side padding that none of the earlier rules touched. Trimming it is what actually widens the page - and it leaves .block / .form alone, so controls stay aligned with their labels. */ .gradio-container { padding-left: 6px !important; padding-right: 6px !important; padding-top: 12px !important; padding-bottom: 20px !important; } .gradio-container > .main, .gradio-container .main.fillable, main.fillable { padding-left: 8px !important; padding-right: 8px !important; } .panel { padding: 12px !important; } #access-panel { padding: 12px !important; } /* The key field measured 86px against the button's 140px: `scale=4` cannot win because the button refuses to shrink below its own label. Stack them instead - full-width field, full-width button underneath. Gradio lays this row out with `display: grid`, not flex, so the stacking has to be done with grid-template-columns; flex-direction alone is inert here and was silently doing nothing. */ #access-panel .key-row.key-row.key-row { display: grid !important; grid-template-columns: 1fr !important; align-items: stretch !important; gap: 8px !important; } #access-panel .key-row.key-row > * { width: 100% !important; min-width: 0 !important; max-width: none !important; } #access-panel #unlock-btn { width: 100% !important; } .gradio-container .grid-wrap, .gradio-container > .main .wrap { min-width: 0 !important; } #run-dock { padding: 8px !important; } /* A long unbroken string - a CivitAI download URL in a lora box, a status line - is wider than the screen on its own and drags the page sideways with it. Let them wrap mid-word rather than overflow. */ .status-ok, .status-idle, .status-lock, .status-run, .search-results, #access-panel .access-blurb, .gpu-estimate { overflow-wrap: anywhere; word-break: break-word; } #hero { padding: 20px; border-radius: 15px; } #hero p { font-size: var(--fs-base); line-height: 1.6; } #generate-btn { min-height: 56px !important; } /* Keep both image inputs visible instead of moving the end-frame tab into Gradio's overflow menu on narrow screens. */ #frame-tabs .tab-nav { display: flex !important; flex-wrap: nowrap !important; gap: 4px !important; overflow: visible !important; } #frame-tabs .tab-nav > button { display: block !important; flex: 1 1 0 !important; min-width: 0 !important; padding-left: 6px !important; padding-right: 6px !important; white-space: normal !important; overflow: visible !important; } #frame-tabs .tab-nav-more { display: none !important; } } #access-panel { width: 100% !important; max-width: none !important; box-sizing: border-box !important; border-radius: 16px !important; padding: 14px 16px !important; margin-bottom: 16px !important; border: 1px solid rgba(13, 148, 136, .40) !important; background: linear-gradient(125deg, rgba(13, 148, 136, .10), rgba(5, 150, 105, .06)) !important; box-shadow: 0 6px 18px rgba(13, 148, 136, .12); } /* Gradio can leave HTML children inside a Group at their component width. Keep the access panel's contents aligned to the panel instead of narrowing them inside the available mobile column. */ #access-panel > .block, #access-panel > .wrap, #access-panel .wrap, #access-panel .form, #access-panel .prose { width: 100% !important; max-width: none !important; min-width: 0 !important; box-sizing: border-box !important; } #access-panel .access-blurb { font-size: var(--fs-lg); line-height: 1.65; margin-bottom: 6px; } #access-panel .access-blurb b { font-weight: 700; } /* The key row. The textbox carries a label above it and the button does not, so aligning the two to the top of the row leaves the button floating high. Bottom alignment lines the button up with the input itself, which is what the eye actually compares, and a matching height stops it looking stubby beside it. */ #access-panel .key-row { align-items: flex-end !important; gap: 10px !important; } #access-panel .key-row > * { margin-bottom: 0 !important; } #unlock-btn { height: 46px !important; min-height: 46px !important; border-radius: 12px !important; font-weight: 700 !important; align-self: flex-end !important; } #access-panel .key-row input[type="password"], #access-panel .key-row input[type="text"] { height: 46px !important; border-radius: 12px !important; } .status-lock { display: flex; flex-wrap: wrap; align-items: center; gap: 14px; border-radius: 12px; padding: 10px 14px; font-size: var(--fs-base); background: rgba(239, 68, 68, .12); border: 1px solid rgba(239, 68, 68, .38); } .status-lock span { opacity: .92; } /* One unpickable unit. As plain inline content the mark and the handle are two separate items and the browser is free to break between them - which is what put the icon alone on its own line. inline-flex makes the pair a single inline box that wraps as a whole or not at all. The underline is drawn on the inner span, not the <a>: on the anchor it also ran under the icon and under the trailing space, which is the stray dash that appeared after "Telegram". */ .buy-link { display: inline-flex; align-items: center; gap: 4px; padding: 2px 10px; border-radius: 999px; background: #2563eb; color: #fff !important; font-weight: 700; text-decoration: none; white-space: nowrap; vertical-align: baseline; line-height: 1.55; } .buy-link:hover { background: #1d4ed8; text-decoration: none; } #hero .buy-link { background: #1d4ed8; color: #fff !important; } #hero .buy-link:hover { background: #1e40af; } .tg-link { display: inline-flex; align-items: center; gap: 4px; color: #1d4ed8 !important; font-weight: 700; text-decoration: none; white-space: nowrap; vertical-align: baseline; } .tg-link svg { flex: none; color: inherit !important; } .tg-link .tg-handle { color: inherit !important; border-bottom: 1px solid currentColor; } .tg-link:hover { color: #1e40af !important; } .tg-link:hover .tg-handle { border-bottom-color: currentColor; } /* The support address, styled to match the Telegram handle so the two read as a pair of equal options rather than a primary and an afterthought. */ .mail-link { display: inline-flex; align-items: center; gap: 4px; color: #1d4ed8 !important; font-weight: 700; text-decoration: none; white-space: nowrap; vertical-align: baseline; } .mail-link svg { flex: none; color: inherit !important; } .mail-link .mail-addr { color: inherit !important; border-bottom: 1px solid currentColor; } .mail-link:hover { color: #1e40af !important; } .mail-link:hover .mail-addr { border-bottom-color: currentColor; } .mail-link:focus-visible, .tg-link:focus-visible { outline: 2px solid #1d4ed8; outline-offset: 3px; border-radius: 3px; } /* A standing support strip: green, so it reads as help being offered rather than a warning or an error. */ .support-note { margin: 10px 0 0; border-radius: 12px; padding: 11px 14px; font-size: var(--fs-sm); line-height: 1.6; background: rgba(16, 185, 129, .10); border: 1px solid rgba(16, 185, 129, .35); } /* Amber, so it reads as a standing notice rather than a result line. */ .gpu-notice { margin-top: 10px; border-radius: 12px; padding: 11px 14px; font-size: var(--fs-sm); line-height: 1.6; background: rgba(245, 158, 11, .12); border: 1px solid rgba(245, 158, 11, .45); } /* Blue, distinct from the amber quota notice below it - this one is an action to take, not a limitation to accept. */ .signin-note { margin: 10px 0 0; border-radius: 12px; padding: 11px 14px; font-size: var(--fs-sm); line-height: 1.6; background: rgba(37, 99, 235, .10); border: 1px solid rgba(37, 99, 235, .35); } .signin-link { display: inline-flex; align-items: center; padding: 2px 10px; border-radius: 999px; background: #2563eb; color: #fff !important; font-weight: 700; text-decoration: none; white-space: nowrap; } .signin-link:hover { background: #1d4ed8; text-decoration: none; } @media (max-width: 720px) { /* Gradio's scale values make the password field collapse beside the unlock button on a phone. Use the available row width explicitly instead. */ #access-panel .key-row { display: grid !important; grid-template-columns: minmax(0, 1fr) 140px !important; width: 100% !important; align-items: end !important; } #access-panel .key-row > * { width: 100% !important; min-width: 0 !important; flex: none !important; } #access-panel .key-row > *:last-child, #unlock-btn { width: 100% !important; min-width: 0 !important; } #access-panel .key-row label > span { white-space: nowrap !important; } } footer { display: none !important; } /* Studio Pro refresh: a dark editorial masthead on a warm, quiet workspace. */ :root { --fs-base: 15px; --fs-sm: 13px; --fs-xs: 12px; --fs-lg: 17px; --studio-ink: #173239; --studio-muted: #5e7477; --studio-edge: #dce8e3; --studio-coral: #eb7257; --studio-teal: #137e78; --ink: #173239; } html, body, gradio-app, .gradio-container { background: #f4f7f3 !important; color: var(--studio-ink) !important; font-family: 'DM Sans', sans-serif !important; } .gradio-container { padding-top: 22px !important; background: radial-gradient(circle at 90% 0, rgba(219, 239, 227, .9), transparent 34rem), #f4f7f3 !important; --background-fill-primary: #f4f7f3; --background-fill-secondary: #fffefa; --border-color-primary: #dce8e3; --body-text-color: #173239; --primary-500: #137e78; --primary-600: #0d6865; } .gradio-container h1, .gradio-container h2, .gradio-container h3, .gradio-container .label-wrap, .gradio-container button { font-family: 'Space Grotesk', sans-serif !important; } #hero { border-radius: 28px !important; padding: clamp(24px, 4vw, 52px) !important; margin-bottom: 20px !important; background: radial-gradient(circle at 85% 10%, rgba(63, 191, 161, .24), transparent 26%), linear-gradient(125deg, #102c32 0%, #173d42 55%, #294052 100%) !important; color: #f7fbf8 !important; border: 1px solid #31565a !important; box-shadow: 0 26px 54px rgba(18, 50, 53, .19) !important; } #hero::after { right: -95px; bottom: -170px; width: 430px; height: 430px; border-color: rgba(200, 248, 223, .18); box-shadow: 0 0 0 36px rgba(212, 249, 228, .025), 0 0 0 82px rgba(212, 249, 228, .02); } #hero .hero-content { position: relative; z-index: 1; } #hero .hero-topline { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 20px; } #hero .eyebrow { color: #9fe3cd !important; opacity: 1 !important; letter-spacing: .22em; font-size: 11px; margin: 0; } #hero .hero-edition { color: #d5ebe3; border: 1px solid rgba(211, 243, 229, .24); background: rgba(255,255,255,.065); border-radius: 999px; padding: 6px 12px; font-size: 11px; letter-spacing: .08em; } #hero h1 { color: #fffdf8 !important; font-family: 'Space Grotesk', sans-serif !important; font-size: clamp(38px, 6vw, 72px) !important; line-height: 1.04; font-weight: 700; letter-spacing: -.065em; max-width: 840px; } #hero .hero-mark { display: inline-block; margin-left: 2px; padding: 0 14px 3px; border: 1px solid rgba(255, 201, 169, .45); border-radius: 14px; background: rgba(235, 114, 87, .14); color: #ffb69f !important; font-size: .73em; letter-spacing: -.045em; transform: translateY(-3px); } #hero p, #hero p b { color: #e2f1eb !important; opacity: 1 !important; } #hero .lede { max-width: 650px; font-size: clamp(16px, 1.8vw, 21px) !important; line-height: 1.5; margin: 18px 0 0; } #hero .hero-action-row { display: flex; align-items: center; flex-wrap: wrap; gap: 16px; margin-top: 24px; } #hero .hero-action-note { color: #bed7d0; font-size: 13px; } .buy-link, #hero .buy-link { display: inline-flex; align-items: center; justify-content: center; gap: 18px; padding: 10px 18px !important; border-radius: 11px !important; background: #ed785e !important; color: #173039 !important; border: 1px solid #ffad95; box-shadow: 0 8px 16px rgba(17, 35, 41, .12); font-family: 'Space Grotesk', sans-serif; font-size: 14px; font-weight: 700; line-height: 1.3 !important; text-decoration: none !important; white-space: nowrap; transition: transform .18s ease, background .18s ease, box-shadow .18s ease; } .buy-link:hover, #hero .buy-link:hover { transform: translateY(-2px); background: #ffa187 !important; box-shadow: 0 12px 20px rgba(17, 35, 41, .17); } .buy-link:focus-visible { outline: 3px solid #ffcfa8; outline-offset: 3px; } #hero .hero-features { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 10px; margin-top: 36px; } #hero .feature { display: flex; align-items: flex-start; gap: 12px; min-width: 0; padding: 15px 16px; border-radius: 16px; border: 1px solid rgba(210, 244, 226, .17); background: rgba(255, 255, 255, .065); backdrop-filter: blur(5px); } #hero .feature-icon { display: grid; place-items: center; flex: none; width: 34px; height: 34px; border-radius: 10px; background: rgba(142, 226, 195, .15); color: #a8f0d5; font-size: 18px; } #hero .feature b { display: block; color: #fffdf8 !important; font-size: 14px; } #hero .feature small { display: block; margin-top: 3px; color: #c1d8d2; font-size: 12px; line-height: 1.42; } #hero .hero-footer { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: center; gap: 8px 20px; margin-top: 22px; color: #b7d3ca; font-size: 12px; } #hero .hero-footer a { color: #b8f4d7 !important; } #access-panel { border-radius: 22px !important; padding: clamp(18px, 3vw, 30px) !important; background: #fffdf9 !important; border: 1px solid #eadfd1 !important; box-shadow: 0 14px 34px rgba(36, 64, 58, .07) !important; } #access-panel .access-intro { display: flex; align-items: center; justify-content: space-between; gap: 20px; flex-wrap: wrap; margin-bottom: 14px; } #access-panel .access-kicker { display: block; color: #ba654c; font-size: 11px; font-weight: 700; letter-spacing: .18em; text-transform: uppercase; } #access-panel .access-intro h2 { margin: 4px 0 3px; color: #173239 !important; font-size: clamp(22px, 2.5vw, 29px); letter-spacing: -.04em; } #access-panel .access-intro p { margin: 0; color: #596d6c !important; max-width: 650px; font-size: 14px; line-height: 1.55; } #access-panel .access-cta { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; } #access-panel .access-cta small { color: #738581; font-size: 11px; max-width: 14ch; } #access-panel .key-row { gap: 12px !important; align-items: flex-end !important; } #access-panel .key-row input[type="password"], #access-panel .key-row input[type="text"] { min-height: 52px !important; border-radius: 12px !important; background: #f7faf7 !important; border: 1px solid #c9d9d3 !important; color: #173239 !important; } #unlock-btn { min-height: 52px !important; height: 52px !important; border-radius: 12px !important; background: #137e78 !important; border: 1px solid #137e78 !important; color: white !important; box-shadow: 0 8px 15px rgba(19, 126, 120, .17); } #unlock-btn:hover { background: #0d6865 !important; transform: translateY(-1px); } #access-panel .status-lock, #access-panel .status-ok, #access-panel .status-idle { margin-top: 10px; padding: 11px 14px; border-radius: 11px; } #access-panel .gpu-notice { margin-top: 12px; background: #f8f3e8 !important; border: 1px solid #e9ddc5 !important; color: #5c594f; } #access-panel .support-note { background: #f1f8f3 !important; border: 1px solid #dceadd !important; color: #506a61; } .panel { border-radius: 18px !important; background: #fffefa !important; border: 1px solid var(--studio-edge) !important; box-shadow: 0 7px 22px rgba(28, 57, 55, .055) !important; } .panel:hover { border-color: #bdd7cc !important; box-shadow: 0 12px 26px rgba(28, 57, 55, .08) !important; } #run-dock { border-radius: 18px !important; background: #fffdf9 !important; border-color: #dce8e3 !important; } .gradio-container .prose { color: #173239 !important; } .gradio-container .info, .gradio-container .block-info { color: #607773 !important; } .gradio-container button.secondary { background: #f2f7f3 !important; border: 1px solid #d6e5dd !important; color: #21464a !important; } .gradio-container button.secondary:hover { background: #e4f1e9 !important; border-color: #a4cdbd !important; } .gradio-container button.primary, .gradio-container .primary > button { background: linear-gradient(110deg, #137e78, #2b9d80) !important; border: 1px solid #137e78 !important; color: #fff !important; } .gradio-container input, .gradio-container textarea, .gradio-container select { border-radius: 10px !important; border-color: #d4e1d9 !important; color: var(--body-text-color) !important; } .gradio-container .tab-nav button[aria-selected="true"] { color: #106f69 !important; border-bottom-color: #137e78 !important; } .gpu-estimate { background: #eef7f0 !important; border-color: #cce6d4 !important; color: #315c50; } #enhance-btn { border-color: #e8c7b8 !important; background: #fff0e9 !important; color: #8d4837 !important; } #enhance-btn:hover { background: #ffe4d8 !important; } #generate-btn { background: linear-gradient(100deg, #137e78, #249a82) !important; border: 1px solid #0e756c !important; color: white !important; box-shadow: 0 10px 22px rgba(19, 126, 120, .18) !important; } #generate-btn[disabled] { background: #dbe7e0 !important; color: #60746d !important; box-shadow: none !important; } #gif-export-btn { background: #e6f4ee !important; border: 1px solid #a9d8c4 !important; color: #175b54 !important; font-weight: 700 !important; } #gif-export-btn:hover { background: #d1eddf !important; } .frame-actions.frame-actions { display: grid !important; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; } .frame-actions.frame-actions > * { min-width: 0 !important; } .frame-actions button { min-height: 48px; white-space: normal !important; line-height: 1.25; } .gif-options { margin-top: 2px; } .status-lock { background: #fff0ed !important; border-color: #edc6ba !important; color: #714536; } .status-ok { background: #e9f7ee !important; border-color: #addbc0 !important; color: #184e3b; } .status-idle { background: #f5f8f3 !important; border-color: #deeadf !important; color: #49615a; } .tg-link, .mail-link { color: #137e78 !important; } .tg-link:hover, .mail-link:hover { color: #0e5f5b !important; } .gradio-container button:focus-visible, .gradio-container input:focus-visible, .gradio-container textarea:focus-visible { outline: 2px solid #ed785e !important; outline-offset: 2px; } /* Gradio toggles .dark independently of the browser's preferred scheme. */ :is(.dark .gradio-container, .gradio-container.dark) { --studio-ink: #edf6f2; --studio-muted: #b2c7bf; --studio-edge: #3c5550; --ink: #edf6f2; --background-fill-primary: #142321; --background-fill-secondary: #20332f; --border-color-primary: #3c5550; --border-color-secondary: #3c5550; --body-text-color: #edf6f2; --body-text-color-subdued: #b2c7bf; --block-background-fill: #20332f; --block-label-background-fill: #20332f; --block-label-text-color: #edf6f2; --block-title-text-color: #edf6f2; --input-background-fill: #182a26; --input-background-fill-focus: #182a26; --input-text-color: #edf6f2; --input-placeholder-color: #b2c7bf; --button-secondary-background-fill: #29443c; --button-secondary-text-color: #edf6f2; background: radial-gradient(circle at 90% 0, rgba(43, 92, 75, .3), transparent 34rem), #142321 !important; color: #edf6f2 !important; color-scheme: dark; } html.dark, body.dark, .dark gradio-app { background: #142321 !important; color: #edf6f2 !important; } :is(.dark .gradio-container, .gradio-container.dark) :is(.panel, #run-dock, #access-panel) { background: #20332f !important; border-color: #3c5550 !important; } :is(.dark .gradio-container, .gradio-container.dark) :is(.prose, .label-wrap, label, .tab-nav button) { color: #edf6f2 !important; } :is(.dark .gradio-container, .gradio-container.dark) :is(.info, .block-info) { color: #b2c7bf !important; } :is(.dark .gradio-container, .gradio-container.dark) :is(input:not([type="checkbox"]):not([type="radio"]):not([type="range"]), textarea, select), :is(.dark .gradio-container, .gradio-container.dark) #access-panel .key-row input { background: #182a26 !important; border-color: #56756a !important; color: #edf6f2 !important; caret-color: #a8f0d5; } :is(.dark .gradio-container, .gradio-container.dark) :is(input, textarea)::placeholder { color: #b2c7bf !important; opacity: 1 !important; } :is(.dark .gradio-container, .gradio-container.dark) :is(input, textarea)::selection { background: #437d6b; color: #ffffff; } :is(.dark .gradio-container, .gradio-container.dark) #access-panel .access-intro h2 { color: #edf6f2 !important; } :is(.dark .gradio-container, .gradio-container.dark) #access-panel :is(.access-intro p, .access-cta small) { color: #b2c7bf !important; } :is(.dark .gradio-container, .gradio-container.dark) #access-panel .access-kicker { color: #ffb69f; } :is(.dark .gradio-container, .gradio-container.dark) :is(button.secondary, #gif-export-btn) { background: #29443c !important; border-color: #56756a !important; color: #edf6f2 !important; } :is(.dark .gradio-container, .gradio-container.dark) :is(button.secondary, #gif-export-btn):hover { background: #35594d !important; } :is(.dark .gradio-container, .gradio-container.dark) #enhance-btn { background: #503a32 !important; border-color: #916756 !important; color: #ffd3bf !important; } :is(.dark .gradio-container, .gradio-container.dark) #enhance-btn:hover { background: #64483c !important; } :is(.dark .gradio-container, .gradio-container.dark) #generate-btn[disabled] { background: #34463f !important; color: #bdcec6 !important; } :is(.dark .gradio-container, .gradio-container.dark) .tab-nav button[aria-selected="true"] { color: #a8f0d5 !important; border-bottom-color: #a8f0d5 !important; } :is(.dark .gradio-container, .gradio-container.dark) :is(.gpu-estimate, .status-ok, .status-idle), :is(.dark .gradio-container, .gradio-container.dark) #access-panel .support-note { background: #29443c !important; border-color: #56756a !important; color: #d5eee0 !important; } :is(.dark .gradio-container, .gradio-container.dark) .status-lock, :is(.dark .gradio-container, .gradio-container.dark) #access-panel .gpu-notice, :is(.dark .gradio-container, .gradio-container.dark) .lora-age-warning { background: #463b2f !important; border-color: #826d51 !important; color: #ffdfba !important; } :is(.dark .gradio-container, .gradio-container.dark) :is(.tg-link, .mail-link) { color: #a8f0d5 !important; } @media (max-width: 850px) { #hero .hero-features { grid-template-columns: repeat(2, minmax(0, 1fr)); } } @media (max-width: 580px) { #hero { padding: 22px !important; border-radius: 20px !important; } #hero .hero-features { grid-template-columns: 1fr; margin-top: 24px; } #hero .hero-edition { display: none; } #access-panel .access-cta { width: 100%; } #access-panel .access-cta .buy-link { width: 100%; } .frame-actions.frame-actions { grid-template-columns: 1fr; } } """ HERO = """ <div id="hero"> <div class="hero-content"> <div class="hero-topline"> <span class="eyebrow">Make every frame count</span> <span class="hero-edition">WAN 2.2 &nbsp;/&nbsp; CREATOR EDITION</span> </div> <h1>Wan Studio <span class="hero-mark">Pro</span></h1> <p class="lede">From a single image to a whole cinematic scene. Direct the motion, shape the sound, and finish a video worth sharing.</p> <div class="hero-action-row"> """ + BUY_LINK + """ <span class="hero-action-note">Explore the tools freely. Unlock when you're ready to render.</span> </div> <div class="hero-features" aria-label="Studio tools"> <div class="feature"><span class="feature-icon">◫</span><span><b>Image to video</b><small>Start and end frames, face consistency, full shot control</small></span></div> <div class="feature"><span class="feature-icon">✎</span><span><b>Prompt direction</b><small>Shot builder, motion tags, optional Qwen3-VL rewrite</small></span></div> <div class="feature"><span class="feature-icon">✧</span><span><b>LoRA library</b><small>Motion effects, CivitAI discovery, custom adapters</small></span></div> <div class="feature"><span class="feature-icon">♫</span><span><b>Sound studio</b><small>Soundtrack, Foley and voice tools</small></span></div> <div class="feature"><span class="feature-icon">▣</span><span><b>Scene workshop</b><small>Chain clips, stitch, loop and boomerang</small></span></div> <div class="feature"><span class="feature-icon">↗</span><span><b>Finishing tools</b><small>Trim clips, frame grabs, GIF export, presets and profiles</small></span></div> </div> <div class="hero-footer"> <span>Access keys unlock rendering features. GPU use follows your Hugging Face ZeroGPU allowance.</span> <span>Questions? """ + EMAIL_LINK + """ &nbsp;·&nbsp; """ + TELEGRAM_LINK + """</span> </div> </div> </div> """ # (sweep every hour, keep files for a day). The old (3600, 10800) deleted a grabbed frame after # three hours while the page still showed it, and the next Generate died in gradio's own # preprocessing with FileNotFoundError before this code ran. with gr.Blocks(delete_cache=(3600, 86400), css=CSS, head=HEAD) as demo: gr.HTML(HERO) # Per browser session, never module-level: a flag shared across the process would # mean one paying visitor's key unlocked the Space for everybody on it. access_state = gr.State(False) validated_browser_key = gr.Textbox(value="", visible=False) history_data = gr.JSON(value=[], visible=False) with gr.Group(elem_id="access-panel"): gr.HTML( "<div class='access-intro'><div>" "<span class='access-kicker'>Your studio pass</span>" "<h2>Ready to make something?</h2>" "<p>Browse, plan shots and explore the LoRA library for free. " "A Pro key unlocks video generation, scenes, sound and Qwen3-VL rewriting. " "Pro and Pro V2 are separate purchases. Paste your Pro key below.</p></div>" f"<div class='access-cta'>{BUY_LINK}<small>Delivered by email after payment verification</small>" "</div></div>" ) # `min_width` rather than `scale` alone. Gradio resolves `scale` in # JavaScript against each child's intrinsic width, and the button will not # go below its own label - so on a phone the field was measured at 86px # against the button's 140px however the scales were set. A min_width is # the value Gradio actually honours when it computes the columns, and # Gradio drops the row to one column per child when they no longer fit, # which gives the stacked full-width layout on a narrow screen. with gr.Row(elem_classes="key-row"): access_key_box = gr.Textbox( label="Pro access key", placeholder="WSP-... (Pro only)", type="password", max_lines=1, scale=5, min_width=240, container=True, ) access_unlock_btn = gr.Button("Unlock studio →", variant="primary", scale=1, min_width=130, elem_id="unlock-btn") gr.Markdown("Your key is remembered on this browser after a successful unlock.") forget_key_btn = gr.Button("Forget saved key", size="sm") access_status = gr.HTML( "<div class='status-lock'><b>&#128274; Studio locked</b>" "<span>Enter a key to enable rendering and sound.</span></div>" ) # Directly under the key box on purpose. GPU quota is charged to the # viewer, and an anonymous visitor is metered by IP address - so one # shared network or a CGNAT range is one allowance between everybody on # it. A buyer who never signs in gets the smallest quota there is and # reads it as the key being faulty, which arrives as a support message. # gr.HTML( # "<div class='signin-note'>&#128100; <b>Sign in to Hugging Face before you " # "generate.</b> It is free, takes a moment, and is the single biggest thing you " # "can do to get more out of your key. Signed out, your GPU allowance is counted " # "against your <b>IP address</b> &mdash; shared with anyone else on the same " # "network &mdash; and it is the smallest allowance there is. " # f"<a href='{SIGNUP_URL}' target='_blank' rel='noopener noreferrer' " # "class='signin-link'>Create a free account</a> or sign in, then reload this " # "page.</div>" # ) gr.HTML( "<div class='gpu-notice'><b>&#9888;&#65039; Important: the key unlocks features, " "not extra GPU time.</b> Generation runs on Hugging Face ZeroGPU, and the GPU " "allowance is counted by Hugging Face against whoever is viewing this page - not " "against this Space and not against the key. An access key does <b>not</b> raise " "that limit, add priority, or come with GPU time of its own. You still get the " "standard ZeroGPU allowance, you may still have to wait for a free slot at busy " "times, and signing in to a Hugging Face account (or its PRO tier) is what raises " "the allowance.</div>" ) # Last thing in the access panel, and deliberately not styled as a # warning: this is where a paying customer looks when a key will not # unlock, a payment has not arrived, or a generation failed. Both # channels are offered because Telegram is blocked or simply unused in # plenty of places, and an unanswered buyer is a refund. gr.HTML( "<div class='support-note'>&#128172; <b>Need help? We answer.</b> " "Key not working, payment not arrived, or something broken &mdash; " f"email {EMAIL_LINK} or message {TELEGRAM_LINK}. " "Include your access key and, if you have it, the payment reference, " "and it will be sorted out.</div>" ) with gr.Row(equal_height=False): # ---------------- LEFT: controls ---------------- with gr.Column(scale=5): with gr.Accordion("💡 Tips", open=False): gr.Markdown( "**Workflow**\n" "- Pause the video on any frame, then hit **Use current frame** to continue the shot from there.\n" "- Keep **Randomize seed** on while exploring, then lock a seed you like.\n" "- **Loop the clip** and **Stitch clips** both run on the CPU and cost nothing " "from your ZeroGPU allowance — use them freely.\n\n" "**Steps & speed**\n" "- 6 steps + UniPC is the sweet spot; more steps rarely helps this model.\n" "- A **Speed preset** sets steps, flow shift and scheduler together. Pick one " "first, then adjust individual sliders only if something looks off.\n" "- **Guidance** above 1 turns on classifier-free guidance: it uses roughly " "twice the GPU per step and takes proportionally longer. Leave both at 1 " "unless the prompt is being ignored.\n" "- The **Negative prompt** is only read when a Guidance slider is above 1. " "At guidance 1 it does nothing.\n\n" "**Duration & fluidity**\n" "- Duration is clamped to the model's frame range, so the slider may not land " "exactly where you put it. 3–3.5 s is the comfortable range.\n" "- **Fluidity** interpolates extra frames after generation — it adds smoothness, " "not new content. Motion that stutters at the base rate will still stutter, " "just more smoothly. It also makes the file bigger.\n" "- For anything longer than one clip, generate short clips and stitch them " "rather than pushing duration up.\n\n" "**Stitching**\n" "- Clips are concatenated end to end, everything scaled to the first clip's " "frame. There is no crossfade, so keep the same aspect and fluidity across a " "chain or the joins will be visible.\n" "- **🎬 Make the whole scene** and **➕ Just one more clip** always add their " "clips to the joined video, whatever the automatic box says — the very first " "clip included.\n" "- Leave **🔒 Keep the same face** empty unless you need it: with it empty each " "clip starts on the exact last frame of the one before, the duplicated frame is " "detected and trimmed, and the join is invisible.\n\n" "**Images & aspect**\n" "- The checkpoint accepts 480–832 px per side, so anything past about 1.73:1 " "has to give: **Crop to fit** cuts the middle out, **Keep it all** adds black " "bars. Crop looks better unless something important sits near an edge.\n" "- Cropping happens after upload, so pre-cropping your image to roughly the " "render aspect is the only way to control exactly what survives.\n\n" "**Loras**\n" "- Built-in LoRAs come from iamgroot1212/wan2.2-loras. Selected weights download " "on first use and are cached; the first run may take longer.\n" "- **High** and **Low** strength apply to the two noise stages. If a lora is " "overpowering the composition, drop High first and leave Low alone." ) with gr.Group(elem_classes="panel"): gr.Markdown( "Upload only photographs of yourself, or of people who have agreed to this. " "Sexual imagery of a real person made without their consent breaks Hugging " "Face's content policy and is not welcome here.", elem_classes="consent-note", ) with gr.Tabs(elem_id="frame-tabs"): with gr.Tab("🖼️ Start frame"): input_image_component = gr.Image( type="pil", label=None, show_label=False, sources=["upload", "clipboard"], height=300, ) with gr.Tab("🏁 End frame"): last_image_component = gr.Image( type="pil", label=None, show_label=False, sources=["upload", "clipboard"], height=300, ) with gr.Accordion("🔒 Keep the same face (optional)", open=False): gr.Markdown( "Only for long scenes where the face slowly stops looking like the person. " "Upload the face here and every clip is pulled back toward it. Leave it " "empty and nothing is touched — which is what you want most of the time, " "because it also makes the joins between clips perfectly seamless." ) identity_ref_component = gr.Image( type="pil", label="Face to keep", height=160, sources=["upload", "clipboard"], ) identity_strength_slider = gr.Slider( 0.0, 1.0, value=0.25, step=0.05, label="How hard to pull it back", info="0.2–0.35 is the safe band. Higher starts to look like a paste-over.", ) prompt_input = gr.Textbox( label="Prompt", value=default_prompt_i2v, lines=3, placeholder="Describe the motion you want to see…", ) with gr.Row(): history_picker = gr.Dropdown(choices=[], value=None, label="Prompt history", filterable=True, scale=4) clear_history_btn = gr.Button("Clear history", size="sm", scale=0) with gr.Accordion("🖋️ Prompt builder", open=False): gr.Markdown( "Write what should happen in the box above, pick the shot below, and press " "**Build**. The builder only ever adds to your own words — it is instant, " "costs no GPU time and refuses nothing.\n\n" "Tick **rewrite with Qwen3-VL** to send the built prompt to a separate " "conditioner Space. It may queue and uses that Space's GPU quota; this " "studio downloads no extra model and books no GPU time for the rewrite. " "An access key is required. If the service is unavailable, your built prompt is kept." ) with gr.Row(): pb_framing = gr.Dropdown(list(FRAMINGS), value="(keep as written)", label="Framing") pb_camera = gr.Dropdown(list(CAMERA_MOVES), value="(keep as written)", label="Camera move") with gr.Row(): pb_lighting = gr.Dropdown(list(LIGHTING), value="(keep as written)", label="Lighting") pb_pace = gr.Dropdown(list(PACING), value="(keep as written)", label="Pacing") pb_quality = gr.Checkbox( value=True, label="Add the quality tail", info="photorealistic, natural skin texture, consistent anatomy, smooth motion", ) pb_use_model = gr.Checkbox( value=False, label="🧠 Rewrite with Qwen3-VL (separate conditioner Space)", ) pb_button = gr.Button("🖋️ Build / rewrite the prompt", variant="secondary", elem_id="enhance-btn") pb_status = gr.HTML("") with gr.Accordion("✨ Quick motion tags — click to add", open=False): with gr.Row(elem_classes="chip-row"): for _chip in PROMPT_CHIPS[:5]: _b = gr.Button(_chip, size="sm", variant="secondary") _b.click( fn=lambda p, c=_chip: _append_chip(p, c), inputs=[prompt_input], outputs=[prompt_input], ) with gr.Row(elem_classes="chip-row"): for _chip in PROMPT_CHIPS[5:]: _b = gr.Button(_chip, size="sm", variant="secondary") _b.click( fn=lambda p, c=_chip: _append_chip(p, c), inputs=[prompt_input], outputs=[prompt_input], ) with gr.Group(elem_classes="panel"): duration_seconds_input = gr.Slider( minimum=MIN_DURATION, maximum=MAX_DURATION, step=0.1, value=3.5, label="⏱️ Duration (seconds)", info=f"Clamped to the model's {MIN_FRAMES_MODEL}–{MAX_FRAMES_MODEL} frames at {FIXED_FPS} fps.", ) frame_multi = gr.Dropdown( choices=[FIXED_FPS, FIXED_FPS*2, FIXED_FPS*4, FIXED_FPS*8], value=FIXED_FPS, label="🎞️ Fluidity (frames per second)", info="Extra frames are created with flow estimation for smoother motion.", ) safe_mode_checkbox = gr.Checkbox( label="🛠️ Safe Mode", value=True, info="Requests 30% extra processing time so busy servers don't cut the task short.", ) with gr.Row(): aspect_mode = gr.Radio( [("Crop to fit (default)", "crop"), ("Keep it all, add black bars", "pad")], value="crop", label="🖼️ Images taller or wider than the model's frame", info="The checkpoint accepts 480–832 px per side, so anything past 1.73:1 has to give: " "crop cuts the middle out, pad keeps the whole picture between bars.", ) with gr.Accordion("🎛️ Effects (lora)", open=False): gr.HTML( "<div class='lora-age-warning'>" "<b>&#9888;&#65039; 18+ Fair Warning</b>" "Only users aged 18+ can access LoRAs (presets include mature/NSFW content). " "Confirm below to enable the LoRAs dropdowns." "</div>" ) lora_age_confirm = gr.Checkbox( value=False, label="I confirm I am 18+ to access LoRAs", elem_id="lora-age-confirm", ) with gr.Group(elem_classes="panel"): gr.Markdown("### 🔺 LoRA (High)") simple_lora_high = gr.Dropdown( choices=[c for c in HIGH_LORA_CHOICES if c != "(None)"], value=[], multiselect=True, max_choices=3, filterable=True, interactive=False, label="Pick up to three (High)", info="Confirm you are 18+ above to enable LoRA selection. " "Leave it empty for none.", ) simple_lora_high_strength = gr.Slider( 0.0, 1.5, value=0.7, step=0.05, label="High strength", info="0.7 is the safe setting. Drop to 0.4–0.5 if the face stops looking like " "the person; raise it only if the effect is not showing at all.", ) with gr.Group(elem_classes="panel"): gr.Markdown("### 🔻 LoRA (Low)") simple_lora_low = gr.Dropdown( choices=[c for c in LOW_LORA_CHOICES if c != "(None)"], value=[], multiselect=True, max_choices=3, filterable=True, interactive=False, label="Pick up to three (Low)", info="Confirm you are 18+ above to enable LoRA selection. " "Leave it empty for none.", ) simple_lora_low_strength = gr.Slider( 0.0, 1.5, value=0.7, step=0.05, label="Low strength", info="0.7 is the safe setting. Drop to 0.4–0.5 if fine details drift.", ) simple_lora = simple_lora_high simple_lora_strength = simple_lora_high_strength with gr.Row(): quick_lora_link = gr.Textbox( label="…or paste your own link", max_lines=1, scale=4, placeholder="CivitAI download link, or a Hugging Face repo like user/my-loras", ) quick_lora_btn = gr.Button("📥 Add it", variant="secondary", scale=1, min_width=100) simple_lora_status = gr.HTML("<div class='status-idle'>Confirm you are 18+ above to enable LoRA selection.</div>") with gr.Accordion("⚡ Speed preset (optional)", open=False): speed_preset_dd = gr.Dropdown( choices=list(SPEED_PRESETS.keys()), value=SPEED_NONE, label="⚡ Speed preset (steps)", info="Sets steps, flow shift and scheduler in one click. lora options download on first use.", ) with gr.Row(): speed_high_scale = gr.Slider(0.0, 2.0, value=1.0, step=0.05, label="Speed lora — high strength") speed_low_scale = gr.Slider(0.0, 2.0, value=1.0, step=0.05, label="Speed lora — low strength") with gr.Accordion("⚙️ Advanced settings & Custom lora", open=False): with gr.Tabs(): with gr.Tab("Sampling"): reduce_duration_input = gr.Number( value=DEFAULT_REDUCE_DURATION, minimum=0, maximum=120, precision=0, label="Reduce duration (seconds)", info="Reduces the GPU reservation only for clips of 8 seconds or longer. It does not shorten your video.", ) with gr.Row(): steps_slider = gr.Slider(minimum=1, maximum=30, step=1, value=6, label="Inference Steps") quality_slider = gr.Slider( minimum=1, maximum=10, step=1, value=VIDEO_QUALITY, label="Video Quality", info="At 10 the file may be too large to preview in the browser.", ) with gr.Row(): seed_input = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=42, interactive=True) with gr.Row(): randomize_seed_checkbox = gr.Checkbox(label="🎲 Randomize seed", value=True, interactive=True) play_result_video = gr.Checkbox(label="▶️ Display result", value=True, interactive=True) with gr.Row(): guidance_scale_input = gr.Slider( minimum=0.0, maximum=10.0, step=0.5, value=1, label="Guidance — high noise", info="Above 1 uses more GPU and takes longer.", ) guidance_scale_2_input = gr.Slider( minimum=0.0, maximum=10.0, step=0.5, value=1, label="Guidance — low noise", ) with gr.Row(): scheduler_dropdown = gr.Dropdown( label="Scheduler", choices=list(SCHEDULER_MAP.keys()), value="UniPCMultistep", ) flow_shift_slider = gr.Slider(minimum=0.5, maximum=15.0, step=0.1, value=3.0, label="Flow Shift") negative_prompt_input = gr.Textbox( label="Negative Prompt", value=default_negative_prompt, info="Used if any Guidance Scale > 1.", lines=3, ) with gr.Tab("🎛️ Built-in lora"): lora_adv_age_notice = gr.HTML( "<div class='lora-age-warning'>" "<b>&#9888;&#65039; 18+ Fair Warning</b>" "Only users aged 18+ can access LoRAs. Confirm the 18+ checkbox under " "<b>🎛️ Effects (lora)</b> to enable these dropdowns." "</div>" ) gr.Markdown( "These three slots are what the **🎛️ Effects (lora)** panel on the " "main page fills in. Change them here only if you want the High and " "Low halves at different strengths.\n\n" "Built-in lora list — pick up to three and set their strengths. These are " "trained on particular faces, bodies and lighting, and that carries into " "everything they touch: at strength `1.0` a face and hair colour can change " "outright. They start at `0.7` for that reason; drop to `0.4`–`0.5` when the " "person has to stay recognisable." ) _preset_rows = [] for _i in range(1, 4): with gr.Group(elem_classes="panel"): with gr.Row(): _ph = gr.Dropdown(choices=HIGH_LORA_CHOICES, value="(None)", label=f"Built-in lora {_i} (High)", filterable=True, interactive=False, scale=3) _hs = gr.Slider(0.0, 2.0, value=0.7, step=0.05, label="High strength", info="Lower it if faces or hair drift", scale=2) with gr.Row(): _pl = gr.Dropdown(choices=LOW_LORA_CHOICES, value="(None)", label=f"Built-in lora {_i} (Low)", filterable=True, interactive=False, scale=3) _ls = gr.Slider(0.0, 2.0, value=0.7, step=0.05, label="Low strength", scale=2) _preset_rows.append((_ph, _hs, _pl, _ls)) (lora1_high_preset, lora1_high_scale, lora1_low_preset, lora1_low_scale) = _preset_rows[0] (lora2_high_preset, lora2_high_scale, lora2_low_preset, lora2_low_scale) = _preset_rows[1] (lora3_high_preset, lora3_high_scale, lora3_low_preset, lora3_low_scale) = _preset_rows[2] # Compatibility aliases lora1_preset = lora1_high_preset lora2_preset = lora2_high_preset lora3_preset = lora3_high_preset _p = _ph with gr.Tab("⭐ Custom lora"): gr.Markdown( "**Your own lora from Hugging Face or CivitAI.** Paste the repo " "(e.g. `username/my-loras`) and press **Enter** — the files load " "automatically and High/Low are pre-selected for you. 5 slots available.\n\n" "**CivitAI:** paste a download link " "(`https://civitai.com/api/download/models/...`) into the repo box and press " "Enter — it lands in the High box. A Wan adapter usually ships its two halves as " "two separate files, so paste the low-noise link straight into the Low box " "(both boxes accept a pasted link). The Space downloads it itself, so being " "signed in to CivitAI in your browser does not help a gated model: add " "`CIVITAI_TOKEN` under Settings → Variables and secrets. Downloads are cached.\n\n" "**Easier:** the **📚 lora library** tab keeps a shared list of adapters. " "Paste one model page link there, both halves are found for you, and a tick " "fills a whole slot — High, Low, both strengths and the trigger words.\n\n" "A CivitAI link is just a number, so press **🔎 name the CivitAI links** and " "each box is replaced by its real title, version, file name and trigger words, " "read from CivitAI's public API." ) with gr.Accordion("🔍 Search CivitAI", open=False): gr.Markdown( "Search CivitAI without leaving the Space, then drop a result straight " "into a slot. Each hit shows its size, downloads and trigger words." ) with gr.Row(): search_query = gr.Textbox(label="Search", placeholder="e.g. rain, dance, camera move", scale=3) search_base = gr.Dropdown(WAN_BASE_MODELS, value=WAN_BASE_MODELS[0], label="Base model", allow_custom_value=True, scale=2) with gr.Row(): search_nsfw = gr.Checkbox(value=True, label="Include NSFW results") search_btn = gr.Button("🔍 Search", variant="secondary", elem_id="search-btn") search_pick = gr.Dropdown(choices=[], label="Pick a file", allow_custom_value=False) with gr.Row(): search_slot = gr.Dropdown( [f"Custom lora {i}" for i in range(1, 6)], value="Custom lora 1", label="Into slot", scale=2, ) search_role = gr.Radio(["High", "Low"], value="High", label="Box", scale=2) search_put_btn = gr.Button("⬇️ Put it in", variant="primary", scale=1, elem_id="search-put") with gr.Accordion("📋 the results", open=False): search_results = gr.HTML("<div class='status-idle'>No search yet.</div>") search_state = gr.State({}) with gr.Row(): lora_identify_btn = gr.Button("🔎 name the CivitAI links", variant="secondary", elem_id="lora-identify") with gr.Accordion("📋 the named links", open=False): lora_names = gr.HTML("<div class='status-idle'>Nothing in the slots yet.</div>") _my_rows = [] with gr.Accordion("📁 the five slots", open=False): for _i in range(1, 6): with gr.Accordion(f"Custom lora {_i}", open=False): with gr.Row(): _repo = gr.Textbox(label="Repo", placeholder="username/repo", lines=1, scale=4) _load = gr.Button("📥 Load files", scale=1, variant="secondary") with gr.Row(): _high = gr.Dropdown(choices=["(None)"], value="(None)", label="High file", filterable=True, allow_custom_value=True, scale=3) _hs = gr.Slider(0.0, 2.0, value=1.0, step=0.05, label="High strength", scale=2) with gr.Row(): _low = gr.Dropdown(choices=["(None)"], value="(None)", label="Low file", filterable=True, allow_custom_value=True, scale=3) _ls = gr.Slider(0.0, 2.0, value=1.0, step=0.05, label="Low strength", scale=2) _load.click(fn=_load_repo_files, inputs=[_repo], outputs=[_high, _low]) _repo.submit(fn=_load_repo_files, inputs=[_repo], outputs=[_high, _low]) _my_rows.append((_repo, _high, _hs, _low, _ls)) (my1_repo, my1_high, my1_hs, my1_low, my1_ls) = _my_rows[0] (my2_repo, my2_high, my2_hs, my2_low, my2_ls) = _my_rows[1] (my3_repo, my3_high, my3_hs, my3_low, my3_ls) = _my_rows[2] (my4_repo, my4_high, my4_hs, my4_low, my4_ls) = _my_rows[3] (my5_repo, my5_high, my5_hs, my5_low, my5_ls) = _my_rows[4] lora_library.library_tab( pair_slots=[(repo, high, high_scale, low, low_scale) for (repo, high, high_scale, low, low_scale) in _my_rows], prompt_box=prompt_input, ) with gr.Tab("💾 Profiles"): gr.Markdown( "Every current setting, kept under a name inside this Space. Images are " "not stored." ) with gr.Row(): profile_picker = gr.Dropdown( label="Saved profiles", choices=[NO_PROFILE, *list_profiles()], value=NO_PROFILE, scale=3, ) profile_load_btn = gr.Button("📂 load", variant="secondary", scale=1, elem_id="profile-load") profile_refresh = gr.Button("🔄", variant="secondary", scale=1, min_width=60, elem_id="profile-refresh") with gr.Row(): profile_name = gr.Textbox( label="Save as", placeholder="e.g. portrait, turbo 4 steps", max_lines=1, scale=3, ) profile_save_btn = gr.Button("💾 save", variant="primary", scale=1, elem_id="profile-save") profile_delete_btn = gr.Button("🗑️ delete the selected profile", variant="secondary", elem_id="profile-delete") profile_status = gr.Markdown("") gr.Markdown("---") gr.Markdown("**Take it with you (.json file)**") settings_export_btn = gr.Button("⬇️ export the current settings to .json", size="sm") settings_download = gr.File(label="Your settings", visible=False, interactive=False) settings_upload = gr.File(label="📤 import a settings .json", file_types=[".json"], type="filepath") # ---------------- RIGHT: result ---------------- with gr.Column(scale=6, elem_id="out-col"): with gr.Column(elem_id="run-dock", scale=0, min_width=0): # Starts locked. check_access_key() swaps the label and re-enables it. generate_button = gr.Button("🔒 Locked — enter an access key", variant="primary", elem_id="generate-btn", interactive=False) with gr.Row(): stop_button = gr.Button("⏹ Stop", variant="stop", elem_id="stop-btn") clear_button = gr.Button("🧹 Clear", variant="secondary", elem_id="clear-btn") # The refresh sits up here with the other controls so the cost # readout below can have the full width to itself. Beside it the # text was squeezed into a narrow column and wrapped to a dozen # lines on a phone. gpu_estimate_btn = gr.Button("🔄 Recalculate", variant="secondary", elem_id="estimate-btn") gpu_estimate = gr.Markdown( _gpu_estimate_text(None, 3.5, 6, 1, FIXED_FPS, True, 3, True, FOLEY_DEFAULT, 25), elem_classes="gpu-estimate", ) with gr.Group(elem_classes="panel"): video_output = gr.Video( label="Generated Video", autoplay=True, sources=["upload"], buttons=["download", "share"], interactive=True, elem_id="generated-video", height=520, ) status_box = gr.HTML("<div class='status-idle'>Ready when you are.</div>") with gr.Row(elem_classes="frame-actions"): grab_frame_btn = gr.Button( "📸 Current frame → start image", variant="secondary", elem_id="grab-btn", ) gif_export_btn = gr.Button( "✦ Export GIF", variant="secondary", elem_id="gif-export-btn", ) grab_frame_last_btn = gr.Button( "🏁 Current frame → end image", variant="secondary", elem_id="grab-last-btn", ) timestamp_box = gr.Number(value=0, label="Timestamp", visible=True, elem_id="hidden-timestamp") timestamp_box_last = gr.Number(value=0, label="Timestamp", visible=True, elem_id="hidden-timestamp") with gr.Row(elem_classes="gif-options"): gif_width = gr.Dropdown([320, 480, 720], value=480, label="GIF width (px)") gif_fps = gr.Dropdown([8, 10, 15], value=10, label="GIF frames / sec") gif_output = gr.File(label="Download GIF · loops silently · first 20 seconds", visible=False) file_output = gr.File(label="⬇️ Download video") with gr.Group(elem_classes="panel"): gr.Markdown("### 🎬 Make it longer") with gr.Row(): chain_count = gr.Slider( 2, 8, value=3, step=1, label="How many clips in a row", info="3 clips of 3.5 s ≈ a 10 second video.", ) with gr.Accordion("✍️ A prompt per clip (optional)", open=False): scene_prompts = gr.Textbox( label="One line per clip", lines=8, max_lines=8, placeholder=("line 1 = clip 1, line 2 = clip 2, and so on\n" "she turns toward the window\n" "she smiles and looks down\n" "…"), info="Leave a line empty — or the whole box — and that clip uses the " "main prompt above, unchanged.", ) with gr.Row(): chain_btn = gr.Button("🎬 Make the whole scene and join it", variant="primary", elem_id="extend-btn", interactive=False) chain_stop_btn = gr.Button("⏹ Stop", variant="stop") chain_from = gr.Number( value=1, precision=0, minimum=1, maximum=8, label="Start at clip", info="Leave it at 1. After a stop it points at the clip that did not get " "made, so pressing 🎬 again carries on with the right prompt " "line instead of starting the sheet over.", ) gr.Markdown( "Press it once and leave it. Each clip starts on the last frame of the one " "before it and the joined video appears below, growing as each clip lands. " "Every clip costs one normal generation.", elem_classes="turbo-blurb", ) extend_btn = gr.Button("➕ Just one more clip", variant="secondary", interactive=False) gr.Markdown( "The same thing, one clip at a time — useful when you want to change the " "prompt between clips. The finished clip is always added to the joined video.", elem_classes="turbo-blurb", ) with gr.Accordion("🔊 Add sound", open=True): gr.Markdown( "Wan makes pictures only, so sound is a second pass over the clip you already " "have. It listens to the video and invents the noises that fit it. Everything " "runs inside this Space — nothing is uploaded anywhere." ) sound_prompt = gr.Textbox( label="What should it sound like (optional)", max_lines=2, placeholder="e.g. rain on a window, distant traffic — leave it empty and it " "works it out from the picture", ) with gr.Row(): sound_btn = gr.Button("🔊 Add sound to this clip", variant="primary", interactive=False) sound_auto = gr.Checkbox( value=True, label="🔊 Sound on every clip", info="Covers Generate, ➕ and 🎬. Each clip costs a little extra GPU time.", ) sound_status = gr.HTML("<div class='status-idle'>No sound added yet.</div>") with gr.Accordion("Sound settings", open=False): sound_engine = gr.Dropdown( choices=list(FOLEY_ENGINES.keys()), value=FOLEY_DEFAULT, label="Model", info="MMAudio takes seconds and is the safe choice here. Hunyuan sounds " "better but takes about a minute and is much heavier. MMAudio is prepared " "during startup; Hunyuan is installed only if selected.", ) with gr.Row(): sound_steps = gr.Slider(10, 60, value=25, step=1, label="Steps") sound_guidance = gr.Slider(1.0, 8.0, value=4.5, step=0.5, label="How hard to follow the text") sound_negative = gr.Textbox( label="Keep out of the sound", value=FOLEY_NEGATIVE, max_lines=2, ) with gr.Accordion("🔁 Loop the clip (no GPU used)", open=False): gr.Markdown( "Runs on the result you already have, on the CPU — it costs nothing from your " "ZeroGPU allowance." ) boomerang_tail = gr.Slider( 0.5, 4.0, value=2.0, step=0.5, label="Tail length for the ending loop (seconds)", ) with gr.Row(): boomerang_full_btn = gr.Button("🔁 Full boomerang (forward + reverse)", variant="secondary") boomerang_end_btn = gr.Button("🔂 Ending only", variant="secondary") with gr.Accordion("🎞️ The joined video (no GPU used)", open=True): gr.Markdown( "Clips are joined into one file with ffmpeg, on the processor — it costs " "nothing from your GPU allowance." ) merge_name = gr.Textbox(label="File name (optional)", placeholder="my_scene", max_lines=1) auto_merge = gr.Checkbox( value=True, label="⚡ Add every new clip automatically", info="Leave this on. Off = nothing is joined unless you press " "'Add the current video' yourself.", ) with gr.Row(): merge_add_btn = gr.Button("➕ Add the current video", variant="secondary") merge_clear_btn = gr.Button("🧹 Clear the queue", variant="secondary") merge_status = gr.HTML("<div class='status-idle'>Merge queue: empty</div>") merged_video = gr.Video(label="🎬 Stitched result", height=360) merged_file = gr.File(label="⬇️ Download the stitched video") merge_queue = gr.State([]) with gr.Accordion("✂️ Trim video (no GPU used)", open=True, visible=False, elem_id="trim-panel") as trim_panel: gr.Markdown("Keep a section of your clip, with matching audio. " "Export a separate MP4 on the CPU; your original stays unchanged.") trim_source = gr.Dropdown(["Current clip", "Joined video"], value="Current clip", label="Video to trim") with gr.Row(): trim_start = gr.Number(value=0, minimum=0, label="Start (seconds)") trim_end = gr.Number(value=0, minimum=0, label="End (seconds)") trim_btn = gr.Button("✂️ Export trimmed MP4", variant="secondary", interactive=False) trim_status = gr.Markdown("") trim_preview = gr.Video(label="Trimmed preview", interactive=False) trim_download = gr.File(label="Download trimmed MP4", interactive=False) ui_inputs = [ input_image_component, last_image_component, prompt_input, steps_slider, negative_prompt_input, duration_seconds_input, guidance_scale_input, guidance_scale_2_input, seed_input, randomize_seed_checkbox, quality_slider, scheduler_dropdown, flow_shift_slider, frame_multi, safe_mode_checkbox, aspect_mode, speed_preset_dd, speed_high_scale, speed_low_scale, # original 3 (separated high and low) lora1_high_preset, lora1_high_scale, lora1_low_preset, lora1_low_scale, lora2_high_preset, lora2_high_scale, lora2_low_preset, lora2_low_scale, lora3_high_preset, lora3_high_scale, lora3_low_preset, lora3_low_scale, # my 5 my1_repo, my1_high, my1_hs, my1_low, my1_ls, my2_repo, my2_high, my2_hs, my2_low, my2_ls, my3_repo, my3_high, my3_hs, my3_low, my3_ls, my4_repo, my4_high, my4_hs, my4_low, my4_ls, my5_repo, my5_high, my5_hs, my5_low, my5_ls, identity_ref_component, identity_strength_slider, play_result_video, access_state, reduce_duration_input, ] # The simple effects panel drives the three built-in slots: # High section drives high slots/strength; Low section drives low slots/strength. def _apply_simple_loras(picked_high=None, strength_high=0.7, picked_low=None, strength_low=0.7, age_confirmed=True): if not age_confirmed: val_h = float(strength_high) if strength_high is not None else 0.7 val_l = float(strength_low) if strength_low is not None else 0.7 updates = [] for _ in range(3): updates += [gr.update(value="(None)"), gr.update(value=val_h), gr.update(value="(None)"), gr.update(value=val_l)] updates.append("<div class='status-idle'>Confirm you are 18+ above to enable LoRA selection.</div>") return updates picked_h = [p for p in (picked_high or []) if p and p != "(None)"][:3] picked_l = [p for p in (picked_low or []) if p and p != "(None)"][:3] val_h = float(strength_high) if strength_high is not None else 0.7 val_l = float(strength_low) if strength_low is not None else 0.7 updates = [] for index in range(3): h_name = picked_h[index] if index < len(picked_h) else "(None)" l_name = picked_l[index] if index < len(picked_l) else "(None)" updates += [gr.update(value=h_name), gr.update(value=val_h), gr.update(value=l_name), gr.update(value=val_l)] total = len(picked_h) + len(picked_l) if total: body = "".join(f"<span>▲ High: {p}</span>" for p in picked_h) + "".join(f"<span>▼ Low: {p}</span>" for p in picked_l) updates.append( f"<div class='status-ok'><b>{total} effect(s) on</b>{body}" f"<span>high {val_h:g} / low {val_l:g}</span></div>" ) else: updates.append("<div class='status-idle'>No effects selected.</div>") return updates def _on_simple_high_change(picked_high, strength_high, current_low, strength_low, age_confirmed=True): if not age_confirmed: val_h = float(strength_high) if strength_high is not None else 0.7 val_l = float(strength_low) if strength_low is not None else 0.7 updates = [] for _ in range(3): updates += [gr.update(value="(None)"), gr.update(value=val_h), gr.update(value="(None)"), gr.update(value=val_l)] updates.append("<div class='status-idle'>Confirm you are 18+ above to enable LoRA selection.</div>") return [gr.update(value=[]), *updates] picked_h = [p for p in (picked_high or []) if p and p != "(None)"][:3] current_l = [p for p in (current_low or []) if p and p != "(None)"] # Counterparts for currently selected high loras active_cps = [lora_loader.get_low_counterpart(h) for h in picked_h] active_cps = [cp for cp in active_cps if cp] # Counterparts of high loras that are not currently selected unselected_cps = {cp for h, cp in lora_loader.HIGH_TO_LOW_MAP.items() if h not in picked_h and cp not in active_cps} # Build merged low list: active counterparts first, preserving other manual low loras new_low = [] for cp in active_cps: if cp not in new_low: new_low.append(cp) for l in current_l: if l not in new_low and l not in unselected_cps and len(new_low) < 3: new_low.append(l) val_h = float(strength_high) if strength_high is not None else 0.7 val_l = float(strength_low) if strength_low is not None else 0.7 slot_updates = [] for index in range(3): h_name = picked_h[index] if index < len(picked_h) else "(None)" l_name = new_low[index] if index < len(new_low) else "(None)" slot_updates += [gr.update(value=h_name), gr.update(value=val_h), gr.update(value=l_name), gr.update(value=val_l)] total = len(picked_h) + len(new_low) if total: body = "".join(f"<span>▲ High: {p}</span>" for p in picked_h) + "".join(f"<span>▼ Low: {p}</span>" for p in new_low) status = ( f"<div class='status-ok'><b>{total} effect(s) on</b>{body}" f"<span>high {val_h:g} / low {val_l:g}</span></div>" ) else: status = "<div class='status-idle'>No effects selected.</div>" return [gr.update(value=new_low), *slot_updates, status] def _on_slot_high_change(h_name, current_l_name): if not h_name or h_name == "(None)": return gr.update() cp = lora_loader.get_low_counterpart(h_name) if cp: return gr.update(value=cp) return gr.update() def _toggle_lora_age(age_confirmed): confirmed = bool(age_confirmed) if confirmed: return ( gr.update( interactive=True, info="Ready-made High LoRAs. Type to search. Leave empty for none.", ), gr.update( interactive=True, info="Ready-made Low LoRAs (and any low/Low LoRAs). Type to search. Leave empty for none.", ), gr.update(interactive=True), gr.update(interactive=True), gr.update(interactive=True), gr.update(interactive=True), gr.update(interactive=True), gr.update(interactive=True), gr.update(visible=False), "<div class='status-idle'>No effects selected.</div>", ) return ( gr.update( value=[], interactive=False, info="Confirm you are 18+ above to enable LoRA selection.", ), gr.update( value=[], interactive=False, info="Confirm you are 18+ above to enable LoRA selection.", ), gr.update(value="(None)", interactive=False), gr.update(value="(None)", interactive=False), gr.update(value="(None)", interactive=False), gr.update(value="(None)", interactive=False), gr.update(value="(None)", interactive=False), gr.update(value="(None)", interactive=False), gr.update(visible=True), "<div class='status-idle'>Confirm you are 18+ above to enable LoRA selection.</div>", ) def _enforce_lora_age(age_confirmed): if age_confirmed: return (gr.update(),) * 8 return ( gr.update(value=[], interactive=False), gr.update(value=[], interactive=False), gr.update(value="(None)", interactive=False), gr.update(value="(None)", interactive=False), gr.update(value="(None)", interactive=False), gr.update(value="(None)", interactive=False), gr.update(value="(None)", interactive=False), gr.update(value="(None)", interactive=False), ) _SIMPLE_LORA_OUT = [ lora1_high_preset, lora1_high_scale, lora1_low_preset, lora1_low_scale, lora2_high_preset, lora2_high_scale, lora2_low_preset, lora2_low_scale, lora3_high_preset, lora3_high_scale, lora3_low_preset, lora3_low_scale, simple_lora_status, ] _SIMPLE_LORA_IN = [ simple_lora_high, simple_lora_high_strength, simple_lora_low, simple_lora_low_strength, lora_age_confirm, ] lora_age_confirm.change( _toggle_lora_age, [lora_age_confirm], [ simple_lora_high, simple_lora_low, lora1_high_preset, lora1_low_preset, lora2_high_preset, lora2_low_preset, lora3_high_preset, lora3_low_preset, lora_adv_age_notice, simple_lora_status, ], ) simple_lora_high.change( _on_simple_high_change, _SIMPLE_LORA_IN, [simple_lora_low, *_SIMPLE_LORA_OUT], ) simple_lora_high_strength.release(_apply_simple_loras, _SIMPLE_LORA_IN, _SIMPLE_LORA_OUT) simple_lora_low.change(_apply_simple_loras, _SIMPLE_LORA_IN, _SIMPLE_LORA_OUT) simple_lora_low_strength.release(_apply_simple_loras, _SIMPLE_LORA_IN, _SIMPLE_LORA_OUT) lora1_high_preset.change(_on_slot_high_change, [lora1_high_preset, lora1_low_preset], [lora1_low_preset]) lora2_high_preset.change(_on_slot_high_change, [lora2_high_preset, lora2_low_preset], [lora2_low_preset]) lora3_high_preset.change(_on_slot_high_change, [lora3_high_preset, lora3_low_preset], [lora3_low_preset]) def _quick_link(link): text = str(link or "").strip() if not text: raise gr.Error("Paste a CivitAI download link or a Hugging Face repo first.") high, low = _load_repo_files(text) note = ("<div class='status-ok'><b>&#10003; added to Custom lora 1</b>" "<span>open &#9881;&#65039; Advanced &rarr; Custom lora to change it</span></div>") return gr.update(value=text), high, low, note quick_lora_btn.click(_quick_link, [quick_lora_link], [my1_repo, my1_high, my1_low, simple_lora_status]) quick_lora_link.submit(_quick_link, [quick_lora_link], [my1_repo, my1_high, my1_low, simple_lora_status]) speed_preset_dd.change( fn=_apply_speed_preset, inputs=[speed_preset_dd], outputs=[steps_slider, flow_shift_slider, scheduler_dropdown], ) # The key. Unlocking flips the session flag and re-enables the four buttons that # book the GPU; everything else on the page was never disabled. _ACCESS_OUT = [access_state, access_status, generate_button, chain_btn, extend_btn, sound_btn] _ACCESS_BROWSER_OUT = [*_ACCESS_OUT, validated_browser_key] for event in (access_unlock_btn.click, access_key_box.submit): event(_unlock_browser, [access_key_box], _ACCESS_BROWSER_OUT).then( fn=None, inputs=[validated_browser_key], outputs=None, js=SAVE_KEY_JS, ) forget_key_btn.click( _forget_browser_key, inputs=None, outputs=[access_key_box, *_ACCESS_BROWSER_OUT], queue=False, ).then(fn=None, inputs=None, outputs=None, js=FORGET_KEY_JS) demo.load(fn=None, inputs=None, outputs=[access_key_box, history_data], js=LOAD_BROWSER_JS).then( _restore_browser, [access_key_box, history_data], [*_ACCESS_BROWSER_OUT, history_picker], ) for button in (generate_button, extend_btn, chain_btn): button.click(_remember_prompt, [prompt_input, history_data], [history_data, history_picker], queue=False).then( fn=None, inputs=[history_data], outputs=None, js=SAVE_HISTORY_JS, ) history_picker.input(lambda text: text or "", [history_picker], [prompt_input], queue=False) clear_history_btn.click(lambda: ([], gr.update(choices=[], value=None)), None, [history_data, history_picker], queue=False).then( fn=None, inputs=[history_data], outputs=None, js=SAVE_HISTORY_JS, ) _SOUND_INPUTS = [sound_engine, sound_prompt, sound_negative, sound_steps, sound_guidance] sound_btn.click( # Wrapped rather than bound directly: add_sound() takes `seed` before # `unlocked`, so the gate flag cannot simply be appended to the list. fn=lambda path, eng, pr, neg, st, gd, ok: add_sound(path, eng, pr, neg, st, gd, unlocked=ok), inputs=[file_output, *_SOUND_INPUTS, access_state], outputs=[video_output, file_output, sound_status], concurrency_id="wan-gpu", concurrency_limit=1, ) gen_event = generate_button.click( fn=mark_running, inputs=None, outputs=[video_output, status_box], show_progress="hidden", ).success( fn=generate_video, concurrency_id="wan-gpu", concurrency_limit=1, inputs=ui_inputs, outputs=[video_output, file_output, seed_input, status_box], ).success( # Keeps the clip when the Space has persistent storage, then grows the # stitched file if auto-merge is on. Both are CPU-only. fn=lambda path: (_persist(path), gr.update())[1], inputs=[file_output], outputs=[status_box], show_progress="hidden", ).success( fn=maybe_sound, concurrency_id="wan-gpu", concurrency_limit=1, inputs=[file_output, sound_auto, *_SOUND_INPUTS, access_state], outputs=[video_output, file_output, sound_status], ).success( fn=auto_queue, inputs=[file_output, auto_merge, merge_queue, merge_name], outputs=[merged_video, merged_file, merge_queue, merge_status], show_progress="hidden", ) stop_button.click(fn=None, inputs=None, outputs=None, cancels=[gen_event]) clear_button.click( fn=_clear_all, inputs=None, outputs=[input_image_component, last_image_component, prompt_input, video_output, file_output, status_box, identity_ref_component], ) grab_frame_btn.click( fn=None, inputs=None, outputs=[timestamp_box], js=get_timestamp_js ) timestamp_box.change( fn=extract_frame, inputs=[video_output, timestamp_box], outputs=[input_image_component] ) grab_frame_last_btn.click( fn=None, inputs=None, outputs=[timestamp_box_last], js=get_timestamp_js_last, ) timestamp_box_last.change( fn=extract_frame, inputs=[video_output, timestamp_box_last], outputs=[last_image_component], ) gif_export_btn.click( fn=export_gif_download, inputs=[file_output, gif_fps, gif_width], outputs=[gif_output], ) trim_inputs = [video_output, merged_video, trim_source] for component in (video_output, merged_video, trim_source): component.change( fn=prepare_trim, inputs=trim_inputs, outputs=[trim_panel, trim_start, trim_end, trim_preview, trim_download, trim_status, trim_btn], show_progress="hidden", ) trim_btn.click( fn=export_trim_download, inputs=[*trim_inputs, trim_start, trim_end], outputs=[trim_preview, trim_download, trim_status], concurrency_id="wan-cpu-trim", concurrency_limit=1, ) file_output.change( fn=lambda: gr.update(value=None, visible=False), inputs=None, outputs=[gif_output], show_progress="hidden", ) boomerang_full_btn.click( fn=lambda path: boomerang(path, "full"), inputs=[file_output], outputs=[video_output, file_output], ) boomerang_end_btn.click( fn=lambda path, tail: boomerang(path, "ending", tail), inputs=[file_output, boomerang_tail], outputs=[video_output, file_output], ) merge_add_btn.click( fn=add_to_queue, inputs=[file_output, merge_queue, merge_name], outputs=[merged_video, merged_file, merge_queue, merge_status], ) merge_clear_btn.click( fn=clear_queue, inputs=None, outputs=[merged_video, merged_file, merge_queue, merge_status], ) # Continue the scene. Three steps, chained so each one sees what the one before it wrote: park the # finished clip in the queue and hand back its last frame, generate from that frame with the settings # untouched, then join the new clip on. `ui_inputs` starts with the image box, so the second step reads # the still the first step just put there. extend_btn.click( fn=lambda: mark_running("Continuing the scene\u2026")[1], inputs=None, outputs=[status_box], show_progress="hidden", ).success( fn=stage_extension, inputs=[file_output, merge_queue, merge_name, input_image_component, identity_ref_component], outputs=[input_image_component, last_image_component, video_output, merged_video, merged_file, merge_queue, merge_status, identity_ref_component], ).success( fn=generate_video, concurrency_id="wan-gpu", concurrency_limit=1, inputs=ui_inputs, outputs=[video_output, file_output, seed_input, status_box], ).success( fn=maybe_sound, concurrency_id="wan-gpu", concurrency_limit=1, inputs=[file_output, sound_auto, *_SOUND_INPUTS, access_state], outputs=[video_output, file_output, sound_status], ).success( fn=add_to_queue, inputs=[file_output, merge_queue, merge_name], outputs=[merged_video, merged_file, merge_queue, merge_status], show_progress="hidden", ) # One button, a whole scene: N clips in a row, each starting where the last one ended, # stitched as they arrive. scene_state = gr.State({"left": 0, "total": 0}) _scene_inputs = [scene_state, scene_prompts, sound_auto, *_SOUND_INPUTS, merge_queue, merge_name, *ui_inputs] _scene_outputs = [scene_state, video_output, file_output, seed_input, status_box, merged_video, merged_file, merge_queue, merge_status, input_image_component, chain_from] _chain_events = [ chain_btn.click(lambda: mark_running("Starting the scene\u2026")[0], None, [video_output], show_progress="hidden") .then(scene_begin, [chain_count, chain_from, access_state], [scene_state, status_box]) ] for _ in range(MAX_SCENE_CLIPS): _chain_events.append( _chain_events[-1].then(scene_step, _scene_inputs, _scene_outputs, concurrency_id="wan-gpu", concurrency_limit=1) ) chain_stop_btn.click( fn=lambda s: ({**(s or {}), "left": 0}, "<div class='status-ok'><b>Stopped</b>" "<span>what is joined below is kept</span></div>"), inputs=[scene_state], outputs=[scene_state, status_box], cancels=_chain_events, ) # Searching CivitAI, and dropping a result into a slot. `put_in_slot` writes to one of the ten # High / Low boxes and leaves the other nine untouched, so a half-filled set-up survives a search. _SLOT_BOXES = [ my1_high, my1_low, my2_high, my2_low, my3_high, my3_low, my4_high, my4_low, my5_high, my5_low, ] search_btn.click( fn=civitai_search, inputs=[search_query, search_base, search_nsfw], outputs=[search_results, search_pick, search_state], ) search_query.submit( fn=civitai_search, inputs=[search_query, search_base, search_nsfw], outputs=[search_results, search_pick, search_state], ) search_put_btn.click( fn=put_in_slot, inputs=[search_pick, search_state, search_slot, search_role], outputs=[*_SLOT_BOXES, search_results], ) # Naming the links. Registered here with the rest of the additions, and reading every box of every slot. _IDENTIFY_FIELDS = [ my1_repo, my1_high, my1_low, my2_repo, my2_high, my2_low, my3_repo, my3_high, my3_low, my4_repo, my4_high, my4_low, my5_repo, my5_high, my5_low, ] lora_identify_btn.click(identify_loras, _IDENTIFY_FIELDS, lora_names) _SETTINGS_FIELDS = [ prompt_input, negative_prompt_input, duration_seconds_input, steps_slider, quality_slider, seed_input, randomize_seed_checkbox, scheduler_dropdown, flow_shift_slider, frame_multi, safe_mode_checkbox, speed_preset_dd, speed_high_scale, speed_low_scale, lora1_high_preset, lora1_high_scale, lora1_low_preset, lora1_low_scale, lora2_high_preset, lora2_high_scale, lora2_low_preset, lora2_low_scale, lora3_high_preset, lora3_high_scale, lora3_low_preset, lora3_low_scale, my1_repo, my1_high, my1_hs, my1_low, my1_ls, my2_repo, my2_high, my2_hs, my2_low, my2_ls, my3_repo, my3_high, my3_hs, my3_low, my3_ls, my4_repo, my4_high, my4_hs, my4_low, my4_ls, my5_repo, my5_high, my5_hs, my5_low, my5_ls, identity_strength_slider, reduce_duration_input, ] profile_save_btn.click( save_profile, [profile_name, *_SETTINGS_FIELDS], [profile_picker, profile_status] ) profile_load_btn.click( load_profile, profile_picker, [*_SETTINGS_FIELDS, profile_name, profile_status] ).then( _enforce_lora_age, [lora_age_confirm], [ simple_lora_high, simple_lora_low, lora1_high_preset, lora1_low_preset, lora2_high_preset, lora2_low_preset, lora3_high_preset, lora3_low_preset, ], show_progress="hidden", ) profile_delete_btn.click(delete_profile, profile_picker, [profile_picker, profile_status]) profile_refresh.click(refresh_profiles, profile_picker, profile_picker) settings_export_btn.click(export_settings, _SETTINGS_FIELDS, settings_download) settings_upload.upload(import_settings, settings_upload, _SETTINGS_FIELDS).then( _enforce_lora_age, [lora_age_confirm], [ simple_lora_high, simple_lora_low, lora1_high_preset, lora1_low_preset, lora2_high_preset, lora2_low_preset, lora3_high_preset, lora3_low_preset, ], show_progress="hidden", ) # Repopulate on every page open, so profiles saved elsewhere show up without a restart. demo.load(refresh_profiles, profile_picker, profile_picker) _ESTIMATE_INPUTS = [ input_image_component, duration_seconds_input, steps_slider, guidance_scale_input, frame_multi, safe_mode_checkbox, chain_count, sound_auto, sound_engine, sound_steps, reduce_duration_input, ] # Recalculated on demand only. An automatic readout means an event on every slider # move, and a browser that never gets to rest. pb_button.click( fn=enhance_prompt, inputs=[prompt_input, pb_use_model, pb_camera, pb_framing, pb_lighting, pb_pace, pb_quality, access_state], outputs=[prompt_input, pb_status], ) gpu_estimate_btn.click( fn=_gpu_estimate_text, inputs=_ESTIMATE_INPUTS, outputs=[gpu_estimate], show_progress="hidden", ) if __name__ == "__main__": print("[startup] preparing MMAudio and its feature models on CPU", flush=True) _mmaudio_config() print("[startup] MMAudio ready", flush=True) _report_access_config() start_video_sweeper() storage_dir = os.getenv("VIDEO_STORAGE_DIR", "/data/videos") demo.queue().launch( mcp_server=True, show_error=True, allowed_paths=[storage_dir, "/data", tempfile.gettempdir()], )
import os
try:
import spaces
except ImportError:
os.system('pip install --upgrade --no-deps spaces')
import spaces
import shutil
import subprocess
import sys
import copy
import random
import tempfile
import warnings
import time
import threading
import gc
import uuid
from tqdm import tqdm
import cv2
import numpy as np
import torch
import torch._dynamo
from torch.nn import functional as F
from PIL import Image, ImageDraw, ImageFilter
import gradio as gr
import lora_library
from diffusers import (
FlowMatchEulerDiscreteScheduler,
SASolverScheduler,
DEISMultistepScheduler,
DPMSolverMultistepInverseScheduler,
UniPCMultistepScheduler,
DPMSolverMultistepScheduler,
DPMSolverSinglestepScheduler,
)
from diffusers.pipelines.wan.pipeline_wan_i2v import WanImageToVideoPipeline
from diffusers.utils.export_utils import export_to_video
from torchao.quantization import quantize_, Float8DynamicActivationFloat8WeightConfig, Int8WeightOnlyConfig
import aoti
import lora_loader
import audio_runtime
from studio_preferences import (VIDEO_QUALITY, DEFAULT_REDUCE_DURATION, validate_reduction,
prompt_history, LOAD_BROWSER_JS, SAVE_KEY_JS,
FORGET_KEY_JS, SAVE_HISTORY_JS)
from gif_export import export_gif
from trim_tools import trim_video, video_duration as trim_video_duration
os.environ["TOKENIZERS_PARALLELISM"] = "true"
warnings.filterwarnings("ignore")
IS_ZERO_GPU = bool(os.getenv("SPACES_ZERO_GPU"))
# --- FRAME EXTRACTION JS & LOGIC ---
get_timestamp_js = """
function() {
const video = document.querySelector('#generated-video video');
if (video) {
console.log("Video found! Time: " + video.currentTime);
return video.currentTime;
} else {
console.log("No video element found.");
return 0;
}
}
"""
get_timestamp_js_last = get_timestamp_js
def extract_frame(video_path, timestamp):
if not video_path:
return None
print(f"Extracting frame at timestamp: {timestamp}")
cap = cv2.VideoCapture(video_path)
if not cap.isOpened():
return None
fps = cap.get(cv2.CAP_PROP_FPS)
target_frame_num = int(float(timestamp) * fps)
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
if target_frame_num >= total_frames:
target_frame_num = total_frames - 1
cap.set(cv2.CAP_PROP_POS_FRAMES, target_frame_num)
ret, frame = cap.read()
cap.release()
if ret:
return cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
return None