Spaces:
Runtime error
Runtime error
| import sys | |
| from pathlib import Path | |
| import uuid | |
| import tempfile | |
| import subprocess | |
| import torch | |
| import torch.nn.functional as F | |
| import torchaudio | |
| import os | |
| from typing import Any | |
| def _coerce_audio_path(audio_path: Any) -> str: | |
| # Common Gradio case: tuple where first item is the filepath | |
| if isinstance(audio_path, tuple) and len(audio_path) > 0: | |
| audio_path = audio_path[0] | |
| # Some gradio versions pass a dict-like object | |
| if isinstance(audio_path, dict): | |
| # common keys: "name", "path" | |
| audio_path = audio_path.get("name") or audio_path.get("path") | |
| # pathlib.Path etc. | |
| if not isinstance(audio_path, (str, bytes, os.PathLike)): | |
| raise TypeError(f"audio_path must be a path-like, got {type(audio_path)}: {audio_path}") | |
| return os.fspath(audio_path) | |
| def match_audio_to_duration( | |
| audio_path: str, | |
| target_seconds: float, | |
| target_sr: int = 48000, | |
| to_mono: bool = True, | |
| pad_mode: str = "silence", # "silence" | "repeat" | |
| device: str = "cuda", | |
| ): | |
| """ | |
| Load audio, resample, (optionally) mono, then trim/pad to exactly target_seconds. | |
| Returns: (waveform[T] or [1,T], sr) | |
| """ | |
| audio_path = _coerce_audio_path(audio_path) | |
| wav, sr = torchaudio.load(audio_path) # [C, T] float32 CPU | |
| # Resample to target_sr (recommended so duration math is stable) | |
| if sr != target_sr: | |
| wav = torchaudio.functional.resample(wav, sr, target_sr) | |
| sr = target_sr | |
| # Mono (common expectation; if your model supports stereo, set to_mono=False) | |
| if to_mono and wav.shape[0] > 1: | |
| wav = wav.mean(dim=0, keepdim=True) # [1, T] | |
| # Exact target length in samples | |
| target_len = int(round(target_seconds * sr)) | |
| cur_len = wav.shape[-1] | |
| if cur_len > target_len: | |
| wav = wav[..., :target_len] | |
| elif cur_len < target_len: | |
| pad_len = target_len - cur_len | |
| if pad_mode == "repeat" and cur_len > 0: | |
| # Repeat then cut to exact length | |
| reps = (target_len + cur_len - 1) // cur_len | |
| wav = wav.repeat(1, reps)[..., :target_len] | |
| else: | |
| # Silence pad | |
| wav = F.pad(wav, (0, pad_len)) | |
| # move to device | |
| wav = wav.to(device, non_blocking=True) | |
| return wav, sr | |
| def sh(cmd): subprocess.check_call(cmd, shell=True) | |
| sh("pip install --no-deps easy_dwpose") | |
| # Add packages to Python path | |
| current_dir = Path(__file__).parent | |
| sys.path.insert(0, str(current_dir / "packages" / "ltx-pipelines" / "src")) | |
| sys.path.insert(0, str(current_dir / "packages" / "ltx-core" / "src")) | |
| import spaces | |
| import flash_attn_interface | |
| import time | |
| import gradio as gr | |
| import numpy as np | |
| import random | |
| import torch | |
| from typing import Optional | |
| from pathlib import Path | |
| import torchaudio | |
| from huggingface_hub import hf_hub_download, snapshot_download | |
| from ltx_pipelines.distilled import DistilledPipeline | |
| from ltx_core.model.video_vae import TilingConfig | |
| from ltx_core.model.audio_vae.ops import AudioProcessor | |
| from ltx_core.loader.primitives import LoraPathStrengthAndSDOps | |
| from ltx_core.loader.sd_ops import LTXV_LORA_COMFY_RENAMING_MAP | |
| from ltx_pipelines.utils.constants import ( | |
| DEFAULT_SEED, | |
| DEFAULT_1_STAGE_HEIGHT, | |
| DEFAULT_1_STAGE_WIDTH , | |
| DEFAULT_NUM_FRAMES, | |
| DEFAULT_FRAME_RATE, | |
| DEFAULT_LORA_STRENGTH, | |
| ) | |
| from ltx_core.loader.single_gpu_model_builder import enable_only_lora | |
| from ltx_core.model.audio_vae import decode_audio | |
| from ltx_core.model.audio_vae import encode_audio | |
| from PIL import Image | |
| MAX_SEED = np.iinfo(np.int32).max | |
| # Import from public LTX-2 package | |
| # Install with: pip install git+https://github.com/Lightricks/LTX-2.git | |
| from ltx_pipelines.utils import ModelLedger | |
| from ltx_pipelines.utils.helpers import generate_enhanced_prompt | |
| import imageio | |
| import cv2 | |
| from controlnet_aux import CannyDetector | |
| from easy_dwpose import DWposeDetector | |
| # HuggingFace Hub defaults | |
| DEFAULT_REPO_ID = "Lightricks/LTX-2" | |
| DEFAULT_GEMMA_REPO_ID = "unsloth/gemma-3-12b-it-qat-bnb-4bit" | |
| DEFAULT_CHECKPOINT_FILENAME = "ltx-2-19b-dev.safetensors" | |
| def get_hub_or_local_checkpoint(repo_id: str, filename: str): | |
| """Download from HuggingFace Hub.""" | |
| print(f"Downloading {filename} from {repo_id}...") | |
| ckpt_path = hf_hub_download(repo_id=repo_id, filename=filename) | |
| print(f"Downloaded to {ckpt_path}") | |
| return ckpt_path | |
| def download_gemma_model(repo_id: str): | |
| """Download the full Gemma model directory.""" | |
| print(f"Downloading Gemma model from {repo_id}...") | |
| local_dir = snapshot_download(repo_id=repo_id) | |
| print(f"Gemma model downloaded to {local_dir}") | |
| return local_dir | |
| # Initialize model ledger and text encoder at startup (load once, keep in memory) | |
| print("=" * 80) | |
| print("Loading Gemma Text Encoder...") | |
| print("=" * 80) | |
| checkpoint_path = get_hub_or_local_checkpoint(DEFAULT_REPO_ID, DEFAULT_CHECKPOINT_FILENAME) | |
| gemma_local_path = download_gemma_model(DEFAULT_GEMMA_REPO_ID) | |
| device = "cuda" | |
| print(f"Initializing text encoder with:") | |
| print(f" checkpoint_path={checkpoint_path}") | |
| print(f" gemma_root={gemma_local_path}") | |
| print(f" device={device}") | |
| model_ledger = ModelLedger( | |
| dtype=torch.bfloat16, | |
| device=device, | |
| checkpoint_path=checkpoint_path, | |
| gemma_root_path=DEFAULT_GEMMA_REPO_ID, | |
| local_files_only=False | |
| ) | |
| canny_processor = CannyDetector() | |
| # Load text encoder once and keep it in memory | |
| text_encoder = model_ledger.text_encoder() | |
| print("=" * 80) | |
| print("Text encoder loaded and ready!") | |
| print("=" * 80) | |
| def on_lora_change(selected: str): | |
| needs_video = selected in {"Pose", "Canny", "Detailer"} | |
| return ( | |
| selected, | |
| gr.update(visible=not needs_video, value=None if needs_video else None), | |
| gr.update(visible=needs_video, value=None if not needs_video else None), | |
| ) | |
| def process_video_for_pose(frames, width: int, height: int): | |
| pose_processor = DWposeDetector("cuda") | |
| if not frames: | |
| return [] | |
| pose_frames = [] | |
| for frame in frames: | |
| # imageio frame -> PIL | |
| pil = Image.fromarray(frame.astype(np.uint8)).convert("RGB") | |
| # ✅ do NOT pass width/height here (easy_dwpose will handle drawing sizes internally) | |
| pose_img = pose_processor(pil) | |
| # Ensure it's PIL then resize to your conditioning size | |
| if not isinstance(pose_img, Image.Image): | |
| # some versions might return np array | |
| pose_img = Image.fromarray(pose_img.astype(np.uint8)) | |
| pose_img = pose_img.convert("RGB").resize((width, height), Image.BILINEAR) | |
| pose_np = np.array(pose_img).astype(np.float32) / 255.0 | |
| pose_frames.append(pose_np) | |
| return pose_frames | |
| def preprocess_video_to_pose_mp4(video_path: str, width: int, height: int, fps: float): | |
| frames = load_video_frames(video_path) | |
| pose_frames = process_video_for_pose(frames, width=width, height=height) | |
| tmp = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) | |
| tmp.close() | |
| return write_video_mp4(pose_frames, fps=fps, out_path=tmp.name) | |
| def load_video_frames(video_path: str): | |
| """Return list of frames as numpy arrays (H,W,3) uint8.""" | |
| frames = [] | |
| with imageio.get_reader(video_path) as reader: | |
| for frame in reader: | |
| frames.append(frame) | |
| return frames | |
| def process_video_for_canny(frames, width: int, height: int, | |
| low_threshold=50, high_threshold=200): | |
| """ | |
| Convert RGB frames -> canny edge frames. | |
| Returns list of np arrays (H,W,3) in float [0..1] (like controlnet_aux output). | |
| """ | |
| if not frames: | |
| return [] | |
| detect_resolution = max(frames[0].shape[0], frames[0].shape[1]) | |
| image_resolution = max(width, height) | |
| canny_frames = [] | |
| for frame in frames: | |
| # controlnet_aux CannyDetector returns float image in [0..1] if output_type="np" | |
| canny = canny_processor( | |
| frame, | |
| low_threshold=low_threshold, | |
| high_threshold=high_threshold, | |
| detect_resolution=detect_resolution, | |
| image_resolution=image_resolution, | |
| output_type="np", | |
| ) | |
| canny_frames.append(canny) | |
| return canny_frames | |
| def write_video_mp4(frames_float_01, fps: float, out_path: str): | |
| """Write frames in float [0..1] to mp4 as uint8.""" | |
| frames_uint8 = [(f * 255).astype(np.uint8) for f in frames_float_01] | |
| # PyAV backend doesn't support `quality=...` | |
| with imageio.get_writer(out_path, fps=fps, macro_block_size=1) as writer: | |
| for fr in frames_uint8: | |
| writer.append_data(fr) | |
| return out_path | |
| def preprocess_video_to_canny_mp4(video_path: str, width: int, height: int, fps: float): | |
| """End-to-end: read video -> canny -> write temp mp4 -> return path.""" | |
| frames = load_video_frames(video_path) | |
| canny_frames = process_video_for_canny(frames, width=width, height=height) | |
| tmp = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) | |
| tmp.close() | |
| return write_video_mp4(canny_frames, fps=fps, out_path=tmp.name) | |
| def encode_text_simple(text_encoder, prompt: str): | |
| """Simple text encoding without using pipeline_utils.""" | |
| v_context, a_context, _ = text_encoder(prompt) | |
| return v_context, a_context | |
| def encode_prompt( | |
| prompt: str, | |
| enhance_prompt: bool = True, | |
| input_image=None, # this is now filepath (string) or None | |
| seed: int = 42, | |
| negative_prompt: str = "" | |
| ): | |
| start_time = time.time() | |
| try: | |
| final_prompt = prompt | |
| if enhance_prompt: | |
| final_prompt = generate_enhanced_prompt( | |
| text_encoder=text_encoder, | |
| prompt=prompt, | |
| image_path=input_image if input_image is not None else None, | |
| seed=seed, | |
| ) | |
| with torch.inference_mode(): | |
| video_context, audio_context = encode_text_simple(text_encoder, final_prompt) | |
| video_context_negative = None | |
| audio_context_negative = None | |
| if negative_prompt: | |
| video_context_negative, audio_context_negative = encode_text_simple(text_encoder, negative_prompt) | |
| # IMPORTANT: return tensors directly (no torch.save) | |
| embedding_data = { | |
| "video_context": video_context.detach().cpu(), | |
| "audio_context": audio_context.detach().cpu(), | |
| "prompt": final_prompt, | |
| "original_prompt": prompt, | |
| } | |
| if video_context_negative is not None: | |
| embedding_data["video_context_negative"] = video_context_negative | |
| embedding_data["audio_context_negative"] = audio_context_negative | |
| embedding_data["negative_prompt"] = negative_prompt | |
| elapsed_time = time.time() - start_time | |
| if torch.cuda.is_available(): | |
| allocated = torch.cuda.memory_allocated() / 1024**3 | |
| peak = torch.cuda.max_memory_allocated() / 1024**3 | |
| status = f"✓ Encoded in {elapsed_time:.2f}s | VRAM: {allocated:.2f}GB allocated, {peak:.2f}GB peak" | |
| else: | |
| status = f"✓ Encoded in {elapsed_time:.2f}s (CPU mode)" | |
| return embedding_data, final_prompt, status | |
| except Exception as e: | |
| import traceback | |
| error_msg = f"Error: {str(e)}\n{traceback.format_exc()}" | |
| print(error_msg) | |
| return None, prompt, error_msg | |
| # Default prompt from docstring example | |
| DEFAULT_PROMPT = "An astronaut hatches from a fragile egg on the surface of the Moon, the shell cracking and peeling apart in gentle low-gravity motion. Fine lunar dust lifts and drifts outward with each movement, floating in slow arcs before settling back onto the ground. The astronaut pushes free in a deliberate, weightless motion, small fragments of the egg tumbling and spinning through the air. In the background, the deep darkness of space subtly shifts as stars glide with the camera's movement, emphasizing vast depth and scale. The camera performs a smooth, cinematic slow push-in, with natural parallax between the foreground dust, the astronaut, and the distant starfield. Ultra-realistic detail, physically accurate low-gravity motion, cinematic lighting, and a breath-taking, movie-like shot." | |
| # HuggingFace Hub defaults | |
| DEFAULT_REPO_ID = "Lightricks/LTX-2" | |
| DEFAULT_CHECKPOINT_FILENAME = "ltx-2-19b-dev.safetensors" | |
| DEFAULT_DISTILLED_LORA_FILENAME = "ltx-2-19b-distilled-lora-384.safetensors" | |
| DEFAULT_SPATIAL_UPSAMPLER_FILENAME = "ltx-2-spatial-upscaler-x2-1.0.safetensors" | |
| def get_hub_or_local_checkpoint(repo_id: Optional[str] = None, filename: Optional[str] = None): | |
| """Download from HuggingFace Hub or use local checkpoint.""" | |
| if repo_id is None and filename is None: | |
| raise ValueError("Please supply at least one of `repo_id` or `filename`") | |
| if repo_id is not None: | |
| if filename is None: | |
| raise ValueError("If repo_id is specified, filename must also be specified.") | |
| print(f"Downloading {filename} from {repo_id}...") | |
| ckpt_path = hf_hub_download(repo_id=repo_id, filename=filename) | |
| print(f"Downloaded to {ckpt_path}") | |
| else: | |
| ckpt_path = filename | |
| return ckpt_path | |
| # Initialize pipeline at startup | |
| print("=" * 80) | |
| print("Loading LTX-2 Distilled pipeline...") | |
| print("=" * 80) | |
| checkpoint_path = get_hub_or_local_checkpoint(DEFAULT_REPO_ID, DEFAULT_CHECKPOINT_FILENAME) | |
| spatial_upsampler_path = get_hub_or_local_checkpoint(DEFAULT_REPO_ID, DEFAULT_SPATIAL_UPSAMPLER_FILENAME) | |
| print(f"Initializing pipeline with:") | |
| print(f" checkpoint_path={checkpoint_path}") | |
| print(f" spatial_upsampler_path={spatial_upsampler_path}") | |
| distilled_lora_path = get_hub_or_local_checkpoint( | |
| DEFAULT_REPO_ID, | |
| DEFAULT_DISTILLED_LORA_FILENAME, | |
| ) | |
| distilled_lora_path = get_hub_or_local_checkpoint( | |
| DEFAULT_REPO_ID, | |
| DEFAULT_DISTILLED_LORA_FILENAME, | |
| ) | |
| static_lora_path = get_hub_or_local_checkpoint( | |
| "Lightricks/LTX-2-19b-LoRA-Camera-Control-Static", | |
| "ltx-2-19b-lora-camera-control-static.safetensors", | |
| ) | |
| dolly_in_lora_path = get_hub_or_local_checkpoint( | |
| "Lightricks/LTX-2-19b-LoRA-Camera-Control-Dolly-In", | |
| "ltx-2-19b-lora-camera-control-dolly-in.safetensors", | |
| ) | |
| dolly_out_lora_path = get_hub_or_local_checkpoint( | |
| "Lightricks/LTX-2-19b-LoRA-Camera-Control-Dolly-Out", | |
| "ltx-2-19b-lora-camera-control-dolly-out.safetensors", | |
| ) | |
| dolly_left_lora_path = get_hub_or_local_checkpoint( | |
| "Lightricks/LTX-2-19b-LoRA-Camera-Control-Dolly-Left", | |
| "ltx-2-19b-lora-camera-control-dolly-left.safetensors", | |
| ) | |
| dolly_right_lora_path = get_hub_or_local_checkpoint( | |
| "Lightricks/LTX-2-19b-LoRA-Camera-Control-Dolly-Right", | |
| "ltx-2-19b-lora-camera-control-dolly-right.safetensors", | |
| ) | |
| jib_down_lora_path = get_hub_or_local_checkpoint( | |
| "Lightricks/LTX-2-19b-LoRA-Camera-Control-Jib-Down", | |
| "ltx-2-19b-lora-camera-control-jib-down.safetensors", | |
| ) | |
| jib_up_lora_path = get_hub_or_local_checkpoint( | |
| "Lightricks/LTX-2-19b-LoRA-Camera-Control-Jib-Up", | |
| "ltx-2-19b-lora-camera-control-jib-up.safetensors", | |
| ) | |
| detailer_lora_path = get_hub_or_local_checkpoint( | |
| "Lightricks/LTX-2-19b-IC-LoRA-Detailer", | |
| "ltx-2-19b-ic-lora-detailer.safetensors", | |
| ) | |
| squish_lora_path = get_hub_or_local_checkpoint( | |
| "ovi054/LTX-2-19b-Squish-LoRA", | |
| "lora_weights_step_02000.safetensors", | |
| ) | |
| # Load distilled LoRA as a regular LoRA | |
| loras = [ | |
| # --- fused / base behavior --- | |
| LoraPathStrengthAndSDOps( | |
| path=distilled_lora_path, | |
| strength=0.6, | |
| sd_ops=LTXV_LORA_COMFY_RENAMING_MAP, | |
| ), | |
| LoraPathStrengthAndSDOps(static_lora_path, DEFAULT_LORA_STRENGTH, LTXV_LORA_COMFY_RENAMING_MAP), | |
| LoraPathStrengthAndSDOps(detailer_lora_path, DEFAULT_LORA_STRENGTH, LTXV_LORA_COMFY_RENAMING_MAP), | |
| LoraPathStrengthAndSDOps(dolly_in_lora_path, DEFAULT_LORA_STRENGTH, LTXV_LORA_COMFY_RENAMING_MAP), | |
| LoraPathStrengthAndSDOps(dolly_out_lora_path, DEFAULT_LORA_STRENGTH, LTXV_LORA_COMFY_RENAMING_MAP), | |
| LoraPathStrengthAndSDOps(dolly_left_lora_path, DEFAULT_LORA_STRENGTH, LTXV_LORA_COMFY_RENAMING_MAP), | |
| LoraPathStrengthAndSDOps(dolly_right_lora_path, DEFAULT_LORA_STRENGTH, LTXV_LORA_COMFY_RENAMING_MAP), | |
| LoraPathStrengthAndSDOps(jib_down_lora_path, DEFAULT_LORA_STRENGTH, LTXV_LORA_COMFY_RENAMING_MAP), | |
| LoraPathStrengthAndSDOps(jib_up_lora_path, DEFAULT_LORA_STRENGTH, LTXV_LORA_COMFY_RENAMING_MAP), | |
| LoraPathStrengthAndSDOps(squish_lora_path, DEFAULT_LORA_STRENGTH, LTXV_LORA_COMFY_RENAMING_MAP), | |
| ] | |
| # Runtime-toggle LoRAs (exclude fused distilled at index 0) | |
| RUNTIME_LORA_CHOICES = [ | |
| ("No LoRA", -1), | |
| ("Static", 0), | |
| ("Detailer", 1), | |
| ("Zoom In", 2), | |
| ("Zoom Out", 3), | |
| ("Slide Left", 4), | |
| ("Slide Right", 5), | |
| ("Slide Down", 6), | |
| ("Slide Up", 7), | |
| ("Squish", 8), | |
| ] | |
| # Initialize pipeline WITHOUT text encoder (gemma_root=None) | |
| # Text encoding will be done by external space | |
| pipeline = DistilledPipeline( | |
| device=torch.device("cuda"), | |
| checkpoint_path=checkpoint_path, | |
| spatial_upsampler_path=spatial_upsampler_path, | |
| gemma_root=None, # No text encoder in this space | |
| loras=loras, | |
| fp8transformer=False, | |
| local_files_only=False, | |
| ) | |
| pipeline._video_encoder = pipeline.model_ledger.video_encoder() | |
| pipeline._transformer = pipeline.model_ledger.transformer() | |
| print("=" * 80) | |
| print("Pipeline fully loaded and ready!") | |
| print("=" * 80) | |
| class RadioAnimated(gr.HTML): | |
| """ | |
| Animated segmented radio (like iOS pill selector). | |
| Outputs: selected option string, e.g. "768x512" | |
| """ | |
| def __init__(self, choices, value=None, **kwargs): | |
| if not choices or len(choices) < 2: | |
| raise ValueError("RadioAnimated requires at least 2 choices.") | |
| if value is None: | |
| value = choices[0] | |
| uid = uuid.uuid4().hex[:8] # unique per instance | |
| group_name = f"ra-{uid}" | |
| inputs_html = "\n".join( | |
| f""" | |
| <input class="ra-input" type="radio" name="{group_name}" id="{group_name}-{i}" value="{c}"> | |
| <label class="ra-label" for="{group_name}-{i}">{c}</label> | |
| """ | |
| for i, c in enumerate(choices) | |
| ) | |
| # NOTE: use classes instead of duplicate IDs | |
| html_template = f""" | |
| <div class="ra-wrap" data-ra="{uid}"> | |
| <div class="ra-inner"> | |
| <div class="ra-highlight"></div> | |
| {inputs_html} | |
| </div> | |
| </div> | |
| """ | |
| js_on_load = r""" | |
| (() => { | |
| const wrap = element.querySelector('.ra-wrap'); | |
| const inner = element.querySelector('.ra-inner'); | |
| const highlight = element.querySelector('.ra-highlight'); | |
| const inputs = Array.from(element.querySelectorAll('.ra-input')); | |
| const labels = Array.from(element.querySelectorAll('.ra-label')); | |
| if (!inputs.length || !labels.length) return; | |
| const choices = inputs.map(i => i.value); | |
| const PAD = 6; // must match .ra-inner padding and .ra-highlight top/left | |
| let currentIdx = 0; | |
| function setHighlightByIndex(idx) { | |
| currentIdx = idx; | |
| const lbl = labels[idx]; | |
| if (!lbl) return; | |
| const innerRect = inner.getBoundingClientRect(); | |
| const lblRect = lbl.getBoundingClientRect(); | |
| // width matches the label exactly | |
| highlight.style.width = `${lblRect.width}px`; | |
| // highlight has left: 6px, so subtract PAD to align | |
| const x = (lblRect.left - innerRect.left - PAD); | |
| highlight.style.transform = `translateX(${x}px)`; | |
| } | |
| function setCheckedByValue(val, shouldTrigger=false) { | |
| const idx = Math.max(0, choices.indexOf(val)); | |
| inputs.forEach((inp, i) => { inp.checked = (i === idx); }); | |
| // Wait a frame in case fonts/layout settle (prevents rare drift) | |
| requestAnimationFrame(() => setHighlightByIndex(idx)); | |
| props.value = choices[idx]; | |
| if (shouldTrigger) trigger('change', props.value); | |
| } | |
| // Init | |
| setCheckedByValue(props.value ?? choices[0], false); | |
| // Input handlers | |
| inputs.forEach((inp) => { | |
| inp.addEventListener('change', () => setCheckedByValue(inp.value, true)); | |
| }); | |
| // Recalc on resize (important in Gradio layouts) | |
| window.addEventListener('resize', () => setHighlightByIndex(currentIdx)); | |
| })(); | |
| """ | |
| super().__init__( | |
| value=value, | |
| html_template=html_template, | |
| js_on_load=js_on_load, | |
| **kwargs | |
| ) | |
| class PromptBox(gr.HTML): | |
| """ | |
| DeepSite-like prompt box (HTML textarea) that behaves like an input component. | |
| Outputs: the current text value (string) | |
| """ | |
| def __init__(self, value="", placeholder="Describe the video with audio you want to generate...", **kwargs): | |
| uid = uuid.uuid4().hex[:8] | |
| html_template = f""" | |
| <div style="text-align:center; font-weight:600; margin-bottom:6px;"> | |
| Prompt | |
| </div> | |
| <div class="ds-prompt" data-ds="{uid}"> | |
| <textarea class="ds-textarea" rows="3" | |
| placeholder="{placeholder}"></textarea> | |
| </div> | |
| """ | |
| js_on_load = r""" | |
| (() => { | |
| const textarea = element.querySelector(".ds-textarea"); | |
| if (!textarea) return; | |
| // Auto-resize (optional, but nice) | |
| const autosize = () => { | |
| textarea.style.height = "0px"; | |
| textarea.style.height = Math.min(textarea.scrollHeight, 240) + "px"; | |
| }; | |
| // Set initial value from props.value | |
| const setValue = (v, triggerChange=false) => { | |
| const val = (v ?? ""); | |
| if (textarea.value !== val) textarea.value = val; | |
| autosize(); | |
| props.value = textarea.value; | |
| if (triggerChange) trigger("change", props.value); | |
| }; | |
| setValue(props.value, false); | |
| // Update Gradio value on input | |
| textarea.addEventListener("input", () => { | |
| autosize(); | |
| props.value = textarea.value; | |
| trigger("change", props.value); | |
| }); | |
| let last = props.value; | |
| const syncFromProps = () => { | |
| if (props.value !== last) { | |
| last = props.value; | |
| setValue(last, false); // don't re-trigger change loop | |
| } | |
| requestAnimationFrame(syncFromProps); | |
| }; | |
| requestAnimationFrame(syncFromProps); | |
| })(); | |
| """ | |
| super().__init__( | |
| value=value, | |
| html_template=html_template, | |
| js_on_load=js_on_load, | |
| **kwargs | |
| ) | |
| class CameraDropdown(gr.HTML): | |
| """ | |
| Custom dropdown (More-style). | |
| Outputs: selected option string, e.g. "Slide Left" | |
| """ | |
| def __init__(self, choices, value="None", title="Camera LoRA", **kwargs): | |
| if not choices: | |
| raise ValueError("CameraDropdown requires choices.") | |
| uid = uuid.uuid4().hex[:8] | |
| safe_choices = [str(c) for c in choices] | |
| items_html = "\n".join( | |
| f"""<button type="button" class="cd-item" data-value="{c}">{c}</button>""" | |
| for c in safe_choices | |
| ) | |
| html_template = f""" | |
| <div class="cd-wrap" data-cd="{uid}"> | |
| <button type="button" class="cd-trigger" aria-haspopup="menu" aria-expanded="false"> | |
| <span class="cd-trigger-text">More</span> | |
| <span class="cd-caret">▾</span> | |
| </button> | |
| <div class="cd-menu" role="menu" aria-hidden="true"> | |
| <div class="cd-title">{title}</div> | |
| <div class="cd-items"> | |
| {items_html} | |
| </div> | |
| </div> | |
| </div> | |
| """ | |
| js_on_load = r""" | |
| (() => { | |
| const wrap = element.querySelector(".cd-wrap"); | |
| const trigger = element.querySelector(".cd-trigger"); | |
| const triggerText = element.querySelector(".cd-trigger-text"); | |
| const menu = element.querySelector(".cd-menu"); | |
| const items = Array.from(element.querySelectorAll(".cd-item")); | |
| if (!wrap || !trigger || !menu || !items.length) return; | |
| function closeMenu() { | |
| menu.classList.remove("open"); | |
| trigger.setAttribute("aria-expanded", "false"); | |
| menu.setAttribute("aria-hidden", "true"); | |
| } | |
| function openMenu() { | |
| menu.classList.add("open"); | |
| trigger.setAttribute("aria-expanded", "true"); | |
| menu.setAttribute("aria-hidden", "false"); | |
| } | |
| function setValue(val, shouldTrigger = false) { | |
| const v = (val ?? "None"); | |
| props.value = v; | |
| triggerText.textContent = v; | |
| // mark selected (for CSS tick) | |
| items.forEach(btn => { | |
| btn.dataset.selected = (btn.dataset.value === v) ? "true" : "false"; | |
| }); | |
| if (shouldTrigger) trigger("change", props.value); | |
| } | |
| // Toggle menu | |
| trigger.addEventListener("pointerdown", (e) => { | |
| e.preventDefault(); // prevents focus/blur weirdness | |
| e.stopPropagation(); | |
| if (menu.classList.contains("open")) closeMenu(); | |
| else openMenu(); | |
| }); | |
| // Close on outside interaction (use capture so it wins) | |
| document.addEventListener("pointerdown", (e) => { | |
| if (!wrap.contains(e.target)) closeMenu(); | |
| }, true); | |
| // Close on ESC | |
| document.addEventListener("keydown", (e) => { | |
| if (e.key === "Escape") closeMenu(); | |
| }); | |
| // Close when focus leaves the dropdown (keyboard users) | |
| wrap.addEventListener("focusout", (e) => { | |
| // if the newly-focused element isn't inside wrap, close | |
| if (!wrap.contains(e.relatedTarget)) closeMenu(); | |
| }); | |
| // Item selection: use pointerdown so it closes immediately | |
| items.forEach((btn) => { | |
| btn.addEventListener("pointerdown", (e) => { | |
| e.preventDefault(); | |
| e.stopPropagation(); | |
| // close first so it never "sticks" open | |
| closeMenu(); | |
| setValue(btn.dataset.value, true); | |
| }); | |
| }); | |
| // init | |
| setValue((props.value ?? "None"), false); | |
| // sync from Python updates | |
| let last = props.value; | |
| const syncFromProps = () => { | |
| if (props.value !== last) { | |
| last = props.value; | |
| setValue(last, false); | |
| } | |
| requestAnimationFrame(syncFromProps); | |
| }; | |
| requestAnimationFrame(syncFromProps); | |
| })(); | |
| """ | |
| super().__init__( | |
| value=value, | |
| html_template=html_template, | |
| js_on_load=js_on_load, | |
| **kwargs | |
| ) | |
| def generate_video_example(input_image, prompt, camera_lora, resolution, progress=gr.Progress(track_tqdm=True)): | |
| w, h = apply_resolution(resolution) | |
| output_video = generate_video( | |
| input_image, | |
| prompt, | |
| 10, # duration seconds | |
| True, # enhance_prompt | |
| 42, # seed | |
| True, # randomize_seed | |
| h, # height | |
| w, # width | |
| camera_lora, | |
| None, | |
| progress | |
| ) | |
| return output_video | |
| def generate_video_example_t2v(prompt, camera_lora, resolution, progress=gr.Progress(track_tqdm=True)): | |
| w, h = apply_resolution(resolution) | |
| output_video = generate_video( | |
| None, | |
| prompt, | |
| 15, # duration seconds | |
| True, # enhance_prompt | |
| 42, # seed | |
| True, # randomize_seed | |
| h, # height | |
| w, # width | |
| camera_lora, | |
| None, | |
| progress | |
| ) | |
| return output_video | |
| def generate_video_example_s2v(input_image, prompt, camera_lora, resolution, audio_path, progress=gr.Progress(track_tqdm=True)): | |
| w, h = apply_resolution(resolution) | |
| output_video = generate_video( | |
| input_image, | |
| prompt, | |
| 10, # duration seconds | |
| True, # enhance_prompt | |
| 42, # seed | |
| True, # randomize_seed | |
| h, # height | |
| w, # width | |
| camera_lora, | |
| audio_path, | |
| progress | |
| ) | |
| return output_video | |
| def get_duration( | |
| input_image, | |
| prompt, | |
| duration, | |
| enhance_prompt, | |
| seed, | |
| randomize_seed, | |
| height, | |
| width, | |
| camera_lora, | |
| audio_path, | |
| progress | |
| ): | |
| extra_time = 0 | |
| if audio_path is not None: | |
| extra_time += 10 | |
| if duration <= 5: | |
| return 80 + extra_time | |
| elif duration <= 10: | |
| return 120 + extra_time | |
| else: | |
| return 180 + extra_time | |
| def generate_video( | |
| input_image, | |
| prompt: str, | |
| duration: float, | |
| enhance_prompt: bool = True, | |
| seed: int = 42, | |
| randomize_seed: bool = True, | |
| height: int = DEFAULT_1_STAGE_HEIGHT, | |
| width: int = DEFAULT_1_STAGE_WIDTH, | |
| camera_lora: str = "No LoRA", | |
| audio_path = None, | |
| progress=gr.Progress(track_tqdm=True), | |
| ): | |
| """ | |
| Generate a short cinematic video from a text prompt and optional input image using the LTX-2 distilled pipeline. | |
| Args: | |
| input_image: Optional input image for image-to-video. If provided, it is injected at frame 0 to guide motion. | |
| prompt: Text description of the scene, motion, and cinematic style to generate. | |
| duration: Desired video length in seconds. Converted to frames using a fixed 24 FPS rate. | |
| enhance_prompt: Whether to enhance the prompt using the prompt enhancer before encoding. | |
| seed: Base random seed for reproducibility (ignored if randomize_seed is True). | |
| randomize_seed: If True, a random seed is generated for each run. | |
| height: Output video height in pixels. | |
| width: Output video width in pixels. | |
| camera_lora: Camera motion control LoRA to apply during generation (enables exactly one at runtime). | |
| audio_path: Optiona audio file for soundtrack. Could be a lipsync audio or a background music or a mixture of both guiding the image-to-vidoe motion process. | |
| progress: Gradio progress tracker. | |
| Returns: | |
| A tuple of: | |
| - output_path: Path to the generated MP4 video file. | |
| - seed: The seed used for generation. | |
| Notes: | |
| - Uses a fixed frame rate of 24 FPS. | |
| - Prompt embeddings are generated externally to avoid reloading the text encoder. | |
| - GPU cache is cleared after generation to reduce VRAM pressure. | |
| - If an input image is provided, it is temporarily saved to disk for processing. | |
| """ | |
| if camera_lora != "No LoRA" and duration == 15: | |
| gr.Info("15s not avaiable when a LoRA is activated, reducing to 10s for this generation") | |
| duration = 10 | |
| if audio_path is None: | |
| print(f'generating with duration:{duration} and LoRA:{camera_lora} in {width}x{height}') | |
| else: | |
| print(f'generating with duration:{duration} and audio in {width}x{height}') | |
| # Randomize seed if checkbox is enabled | |
| current_seed = random.randint(0, MAX_SEED) if randomize_seed else int(seed) | |
| # Calculate num_frames from duration (using fixed 24 fps) | |
| frame_rate = 24.0 | |
| num_frames = int(duration * frame_rate) + 1 # +1 to ensure we meet the duration | |
| with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as tmpfile: | |
| output_path = tmpfile.name | |
| images = [] | |
| if input_image is not None: | |
| images = [(input_image, 0, 1.0)] | |
| embeddings, final_prompt, status = encode_prompt( | |
| prompt=prompt, | |
| enhance_prompt=enhance_prompt, | |
| input_image=input_image, | |
| seed=current_seed, | |
| negative_prompt="", | |
| ) | |
| video_context = embeddings["video_context"].to("cuda", non_blocking=True) | |
| audio_context = embeddings["audio_context"].to("cuda", non_blocking=True) | |
| print("✓ Embeddings loaded successfully") | |
| # free prompt enhancer / encoder temps ASAP | |
| del embeddings, final_prompt, status | |
| torch.cuda.empty_cache() | |
| # ✅ if user provided audio, use a neutral audio_context | |
| n_audio_context = None | |
| if audio_path is not None: | |
| with torch.inference_mode(): | |
| _, n_audio_context = encode_text_simple(text_encoder, "") # returns tensors on GPU already | |
| del audio_context | |
| audio_context = n_audio_context | |
| camera_lora = "Static" | |
| torch.cuda.empty_cache() | |
| # Map dropdown name -> adapter index | |
| name_to_idx = {name: idx for name, idx in RUNTIME_LORA_CHOICES} | |
| selected_idx = name_to_idx.get(camera_lora, -1) | |
| enable_only_lora(pipeline._transformer, selected_idx) | |
| torch.cuda.empty_cache() | |
| # True video duration in seconds based on your rounding | |
| video_seconds = (num_frames - 1) / frame_rate | |
| if audio_path is not None: | |
| input_waveform, input_waveform_sample_rate = match_audio_to_duration( | |
| audio_path=audio_path, | |
| target_seconds=video_seconds, | |
| target_sr=48000, # pick what your model expects; 48k is common for AV models | |
| to_mono=True, # set False if your model wants stereo | |
| pad_mode="silence", # or "repeat" if you prefer looping over silence | |
| device="cuda", | |
| ) | |
| else: | |
| input_waveform = None | |
| input_waveform_sample_rate = None | |
| # Run inference - progress automatically tracks tqdm from pipeline | |
| with torch.inference_mode(): | |
| pipeline( | |
| prompt=prompt, | |
| output_path=str(output_path), | |
| seed=current_seed, | |
| height=height, | |
| width=width, | |
| num_frames=num_frames, | |
| frame_rate=frame_rate, | |
| images=images, | |
| tiling_config=TilingConfig.default(), | |
| video_context=video_context, | |
| audio_context=audio_context, | |
| input_waveform=input_waveform, | |
| input_waveform_sample_rate=input_waveform_sample_rate, | |
| ) | |
| del video_context, audio_context | |
| torch.cuda.empty_cache() | |
| print("successful generation") | |
| return str(output_path) | |
| def apply_resolution(resolution: str): | |
| if resolution == "16:9": | |
| w, h = 768, 512 | |
| elif resolution == "1:1": | |
| w, h = 512, 512 | |
| elif resolution == "9:16": | |
| w, h = 512, 768 | |
| return int(w), int(h) | |
| def apply_duration(duration: str): | |
| duration_s = int(duration[:-1]) | |
| return duration_s | |
| css = """ | |
| /* Make the row behave nicely */ | |
| #controls-row { | |
| display: flex; | |
| align-items: center; | |
| gap: 12px; | |
| flex-wrap: nowrap; /* or wrap if you prefer on small screens */ | |
| } | |
| /* Stop these components from stretching */ | |
| #controls-row > * { | |
| flex: 0 0 auto !important; | |
| width: auto !important; | |
| min-width: 0 !important; | |
| } | |
| /* Same idea for your radio HTML blocks (optional but helps) */ | |
| #radioanimated_duration, | |
| #radioanimated_duration > div, | |
| #radioanimated_resolution, | |
| #radioanimated_resolution > div { | |
| width: fit-content !important; | |
| } | |
| #col-container { | |
| margin: 0 auto; | |
| max-width: 1600px; | |
| } | |
| #modal-container { | |
| width: 100vw; /* Take full viewport width */ | |
| height: 100vh; /* Take full viewport height (optional) */ | |
| display: flex; | |
| justify-content: center; /* Center content horizontally */ | |
| align-items: center; /* Center content vertically if desired */ | |
| } | |
| #modal-content { | |
| width: 100%; | |
| max-width: 700px; /* Limit content width */ | |
| margin: 0 auto; | |
| border-radius: 8px; | |
| padding: 1.5rem; | |
| } | |
| #step-column { | |
| padding: 10px; | |
| border-radius: 8px; | |
| box-shadow: var(--card-shadow); | |
| margin: 10px; | |
| } | |
| #col-showcase { | |
| margin: 0 auto; | |
| max-width: 1100px; | |
| } | |
| .button-gradient { | |
| background: linear-gradient(45deg, rgb(255, 65, 108), rgb(255, 75, 43), rgb(255, 155, 0), rgb(255, 65, 108)) 0% 0% / 400% 400%; | |
| border: none; | |
| padding: 14px 28px; | |
| font-size: 16px; | |
| font-weight: bold; | |
| color: white; | |
| border-radius: 10px; | |
| cursor: pointer; | |
| transition: 0.3s ease-in-out; | |
| animation: 2s linear 0s infinite normal none running gradientAnimation; | |
| box-shadow: rgba(255, 65, 108, 0.6) 0px 4px 10px; | |
| } | |
| .toggle-container { | |
| display: inline-flex; | |
| background-color: #ffd6ff; /* light pink background */ | |
| border-radius: 9999px; | |
| padding: 4px; | |
| position: relative; | |
| width: fit-content; | |
| font-family: sans-serif; | |
| } | |
| .toggle-container input[type="radio"] { | |
| display: none; | |
| } | |
| .toggle-container label { | |
| position: relative; | |
| z-index: 2; | |
| flex: 1; | |
| text-align: center; | |
| font-weight: 700; | |
| color: #4b2ab5; /* dark purple text for unselected */ | |
| padding: 6px 22px; | |
| border-radius: 9999px; | |
| cursor: pointer; | |
| transition: color 0.25s ease; | |
| } | |
| /* Moving highlight */ | |
| .toggle-highlight { | |
| position: absolute; | |
| top: 4px; | |
| left: 4px; | |
| width: calc(50% - 4px); | |
| height: calc(100% - 8px); | |
| background-color: #4b2ab5; /* dark purple background */ | |
| border-radius: 9999px; | |
| transition: transform 0.25s ease; | |
| z-index: 1; | |
| } | |
| /* When "True" is checked */ | |
| #true:checked ~ label[for="true"] { | |
| color: #ffd6ff; /* light pink text */ | |
| } | |
| /* When "False" is checked */ | |
| #false:checked ~ label[for="false"] { | |
| color: #ffd6ff; /* light pink text */ | |
| } | |
| /* Move highlight to right side when False is checked */ | |
| #false:checked ~ .toggle-highlight { | |
| transform: translateX(100%); | |
| } | |
| """ | |
| css += """ | |
| /* ---- radioanimated ---- */ | |
| .ra-wrap{ | |
| width: fit-content; | |
| } | |
| .ra-inner{ | |
| position: relative; | |
| display: inline-flex; | |
| align-items: center; | |
| gap: 0; | |
| padding: 6px; | |
| background: #0b0b0b; | |
| border-radius: 9999px; | |
| overflow: hidden; | |
| user-select: none; | |
| } | |
| .ra-input{ | |
| display: none; | |
| } | |
| .ra-label{ | |
| position: relative; | |
| z-index: 2; | |
| padding: 10px 18px; | |
| font-family: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Arial; | |
| font-size: 14px; | |
| font-weight: 600; | |
| color: rgba(255,255,255,0.7); | |
| cursor: pointer; | |
| transition: color 180ms ease; | |
| white-space: nowrap; | |
| } | |
| .ra-highlight{ | |
| position: absolute; | |
| z-index: 1; | |
| top: 6px; | |
| left: 6px; | |
| height: calc(100% - 12px); | |
| border-radius: 9999px; | |
| background: #8bff97; /* green knob */ | |
| transition: transform 200ms ease, width 200ms ease; | |
| } | |
| /* selected label becomes darker like your screenshot */ | |
| .ra-input:checked + .ra-label{ | |
| color: rgba(0,0,0,0.75); | |
| } | |
| """ | |
| css += """ | |
| /* --- prompt box --- */ | |
| .ds-prompt{ | |
| width: 100%; | |
| max-width: 720px; | |
| margin-top: 3px; | |
| } | |
| .ds-textarea{ | |
| width: 100%; | |
| box-sizing: border-box; | |
| background: #2b2b2b; | |
| color: rgba(255,255,255,0.9); | |
| border: 1px solid rgba(255,255,255,0.12); | |
| border-radius: 14px; | |
| padding: 14px 16px; | |
| outline: none; | |
| font-family: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Arial; | |
| font-size: 15px; | |
| line-height: 1.35; | |
| resize: none; | |
| height: 94px; | |
| min-height: 94px; | |
| max-height: 94px; | |
| overflow-y: auto; | |
| } | |
| .ds-textarea::placeholder{ | |
| color: rgba(255,255,255,0.55); | |
| } | |
| .ds-textarea:focus{ | |
| border-color: rgba(255,255,255,0.22); | |
| box-shadow: 0 0 0 3px rgba(255,255,255,0.06); | |
| } | |
| """ | |
| css += """ | |
| /* ---- camera dropdown ---- */ | |
| /* 1) Fix overlap: make the Gradio HTML block shrink-to-fit when it contains a CameraDropdown. | |
| Gradio uses .gr-html for HTML components in most versions; older themes sometimes use .gradio-html. | |
| This keeps your big header HTML unaffected because it doesn't contain .cd-wrap. | |
| */ | |
| /* 2) Actual dropdown layout */ | |
| .cd-wrap{ | |
| position: relative; | |
| display: inline-block; | |
| } | |
| /* 3) Match RadioAnimated pill size/feel */ | |
| .cd-trigger{ | |
| margin-top: 2px; | |
| display: inline-flex; | |
| align-items: center; | |
| justify-content: center; | |
| gap: 10px; | |
| border: none; | |
| box-sizing: border-box; | |
| padding: 10px 18px; | |
| min-height: 52px; | |
| line-height: 1.2; | |
| border-radius: 9999px; | |
| background: #0b0b0b; | |
| font-family: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Arial; | |
| font-size: 14px; | |
| /* ✅ match .ra-label exactly */ | |
| color: rgba(255,255,255,0.7) !important; | |
| font-weight: 600 !important; | |
| cursor: pointer; | |
| user-select: none; | |
| white-space: nowrap; | |
| } | |
| /* Ensure inner spans match too */ | |
| .cd-trigger .cd-trigger-text, | |
| .cd-trigger .cd-caret{ | |
| color: rgba(255,255,255,0.7) !important; | |
| } | |
| /* keep caret styling */ | |
| .cd-caret{ | |
| opacity: 0.8; | |
| font-weight: 900; | |
| } | |
| /* 4) Ensure menu overlays neighbors and isn't clipped */ | |
| /* Move dropdown a tiny bit up (closer to the trigger) */ | |
| .cd-menu{ | |
| position: absolute; | |
| top: calc(100% + 4px); /* was +10px */ | |
| left: 0; | |
| min-width: 240px; | |
| background: #2b2b2b; | |
| border: 1px solid rgba(255,255,255,0.14); | |
| border-radius: 14px; | |
| box-shadow: 0 18px 40px rgba(0,0,0,0.35); | |
| padding: 10px; | |
| opacity: 0; | |
| transform: translateY(-6px); | |
| pointer-events: none; | |
| transition: opacity 160ms ease, transform 160ms ease; | |
| z-index: 9999; | |
| } | |
| .cd-title{ | |
| font-size: 12px; | |
| font-weight: 600; | |
| text-transform: uppercase; | |
| letter-spacing: 0.04em; | |
| color: rgba(255,255,255,0.45); /* 👈 muted grey */ | |
| margin-bottom: 6px; | |
| padding: 0 6px; | |
| pointer-events: none; /* title is non-interactive */ | |
| } | |
| .cd-menu.open{ | |
| opacity: 1; | |
| transform: translateY(0); | |
| pointer-events: auto; | |
| } | |
| .cd-items{ | |
| display: flex; | |
| flex-direction: column; | |
| gap: 0px; /* tighter, more like a native menu */ | |
| } | |
| /* Items: NO "boxed" buttons by default */ | |
| .cd-item{ | |
| width: 100%; | |
| text-align: left; | |
| border: none; | |
| background: transparent; /* ✅ removes box look */ | |
| color: rgba(255,255,255,0.92); | |
| padding: 8px 34px 8px 12px; /* right padding leaves room for tick */ | |
| border-radius: 10px; /* only matters on hover */ | |
| cursor: pointer; | |
| font-size: 14px; | |
| font-weight: 700; | |
| position: relative; | |
| transition: background 120ms ease; | |
| } | |
| /* “Box effect” only on hover (not always) */ | |
| .cd-item:hover{ | |
| background: rgba(255,255,255,0.08); | |
| } | |
| /* Tick on the right ONLY on hover */ | |
| .cd-item::after{ | |
| content: "✓"; | |
| position: absolute; | |
| right: 12px; | |
| top: 50%; | |
| transform: translateY(-50%); | |
| opacity: 0; /* hidden by default */ | |
| transition: opacity 120ms ease; | |
| color: rgba(255,255,255,0.9); | |
| font-weight: 900; | |
| } | |
| /* show tick ONLY for selected item */ | |
| .cd-item[data-selected="true"]::after{ | |
| opacity: 1; | |
| } | |
| /* keep hover box effect, but no tick change */ | |
| .cd-item:hover{ | |
| background: rgba(255,255,255,0.08); | |
| } | |
| /* Kill any old “selected” styling just in case */ | |
| .cd-item.selected{ | |
| background: transparent !important; | |
| border: none !important; | |
| } | |
| """ | |
| with gr.Blocks(title="LTX-2 Video Distilled 🎥🔈") as demo: | |
| gr.HTML( | |
| """ | |
| <div style="text-align: center;"> | |
| <p style="font-size:16px; display: inline; margin: 0;"> | |
| <strong>LTX-2 Distilled</strong> DiT-based audio-video foundation model | |
| </p> | |
| <a href="https://huggingface.co/Lightricks/LTX-2" | |
| target="_blank" | |
| rel="noopener noreferrer" | |
| style="display: inline-block; vertical-align: middle; margin-left: 0.5em;"> | |
| [model] | |
| </a> | |
| </div> | |
| <div style="text-align: center;"> | |
| <p style="font-size:16px; display: inline; margin: 0;"> | |
| Using FA3 and Gemma 3 12B 4bit Quantisation for Faster Inference | |
| </p> | |
| </div> | |
| """ | |
| ) | |
| with gr.Column(elem_id="col-container"): | |
| with gr.Row(): | |
| with gr.Column(elem_id="step-column"): | |
| input_image = gr.Image( | |
| label="Input Image (Optional)", | |
| type="filepath", # <-- was "pil" | |
| height=512 | |
| ) | |
| prompt_ui = PromptBox( | |
| value="Make this image come alive with cinematic motion, smooth animation", | |
| elem_id="prompt_ui", | |
| ) | |
| audio_input = gr.Audio(label="Audio (Optional)", type="filepath") | |
| prompt = gr.Textbox( | |
| label="Prompt", | |
| value="Make this image come alive with cinematic motion, smooth animation", | |
| lines=3, | |
| max_lines=3, | |
| placeholder="Describe the motion and animation you want...", | |
| visible=False | |
| ) | |
| enhance_prompt = gr.Checkbox( | |
| label="Enhance Prompt", | |
| value=True, | |
| visible=False | |
| ) | |
| with gr.Accordion("Advanced Settings", open=False, visible=False): | |
| seed = gr.Slider( | |
| label="Seed", | |
| minimum=0, | |
| maximum=MAX_SEED, | |
| value=DEFAULT_SEED, | |
| step=1 | |
| ) | |
| randomize_seed = gr.Checkbox(label="Randomize Seed", value=True) | |
| with gr.Column(elem_id="step-column"): | |
| output_video = gr.Video(label="Generated Video", autoplay=True, height=512) | |
| with gr.Row(elem_id="controls-row"): | |
| radioanimated_duration = CameraDropdown( | |
| choices=["3s", "5s", "10s", "15s"], | |
| value="5s", | |
| title="Clip Duration", | |
| elem_id="radioanimated_duration" | |
| ) | |
| duration = gr.Slider( | |
| label="Duration (seconds)", | |
| minimum=1.0, | |
| maximum=15.0, | |
| value=5.0, | |
| step=0.1, | |
| visible=False | |
| ) | |
| radioanimated_resolution = CameraDropdown( | |
| choices=["16:9", "1:1", "9:16"], | |
| value="16:9", | |
| title="Resolution", | |
| elem_id="radioanimated_resolution" | |
| ) | |
| width = gr.Number(label="Width", value=DEFAULT_1_STAGE_WIDTH, precision=0, visible=False) | |
| height = gr.Number(label="Height", value=DEFAULT_1_STAGE_HEIGHT, precision=0, visible=False) | |
| camera_lora_ui = CameraDropdown( | |
| choices=[name for name, _ in RUNTIME_LORA_CHOICES], | |
| value="No LoRA", | |
| title="Camera LoRA", | |
| elem_id="camera_lora_ui", | |
| ) | |
| # Hidden real dropdown (backend value) | |
| camera_lora = gr.Dropdown( | |
| label="Camera Control LoRA", | |
| choices=[name for name, _ in RUNTIME_LORA_CHOICES], | |
| value="No LoRA", | |
| visible=False | |
| ) | |
| generate_btn = gr.Button("🤩 Generate Video", variant="primary", elem_classes="button-gradient") | |
| # with gr.Sidebar(width=280): | |
| # gr.Examples( | |
| # examples=[ | |
| # [ | |
| # "The video opens on a cake. A knife, held by a hand, is coming into frame and hovering over the cake. The knife then begins cutting into the cake to c4k3 cakeify it. As the knife slices the cake open, the inside of the cake is revealed to be cake with chocolate layers. The knife cuts through and the contents of the cake are revealed. Greek music playing in the background.", | |
| # "Cakify", | |
| # ], | |
| # [ | |
| # "The video showcases an item. The camera zooms out. Then infl4t3 inflates it, the item expanding into giant, inflated balloon against the landscape.", | |
| # "Inflate", | |
| # ], | |
| # [ | |
| # "The video begins with an item. A hydraulic press positioned above slowly descends towards the item. Upon contact, the hydraulic press c5us4 crushes it, deforming and flattening the item, causing the item to collapse inward until the item is no longer recognizable.", | |
| # "Hydraulic", | |
| # ], | |
| # ], | |
| # inputs=[prompt_ui, camera_lora_ui], | |
| # label="Example", | |
| # cache_examples=False, | |
| # ) | |
| camera_lora_ui.change( | |
| fn=lambda x: x, | |
| inputs=camera_lora_ui, | |
| outputs=camera_lora, | |
| api_visibility="private" | |
| ) | |
| radioanimated_duration.change( | |
| fn=apply_duration, | |
| inputs=radioanimated_duration, | |
| outputs=[duration], | |
| api_visibility="private" | |
| ) | |
| radioanimated_resolution.change( | |
| fn=apply_resolution, | |
| inputs=radioanimated_resolution, | |
| outputs=[width, height], | |
| api_visibility="private" | |
| ) | |
| prompt_ui.change( | |
| fn=lambda x: x, | |
| inputs=prompt_ui, | |
| outputs=prompt, | |
| api_visibility="private" | |
| ) | |
| generate_btn.click( | |
| fn=generate_video, | |
| inputs=[ | |
| input_image, | |
| prompt, | |
| duration, | |
| enhance_prompt, | |
| seed, | |
| randomize_seed, | |
| height, | |
| width, | |
| camera_lora, | |
| audio_input | |
| ], | |
| outputs=[output_video] | |
| ) | |
| timestep_prompt = """Style: Realistic live-action, cinematic, shallow depth of field, 24 fps, natural and dramatic lighting | |
| Environment: Interior of a space station module or realistic mock-up, metal panels, blinking lights, Earth visible through a large window | |
| Character: | |
| Woman: mid-30s, athletic build, wearing authentic-style futuristic space suit (real materials, fabric details, visible patches); calm, confident, precise movements; hair tucked or braided; expressive eyes visible through visor, reflecting focus and awe | |
| Timestamps & action sequence: | |
| 0:00–0:05 — Close-up on her face through visor; soft reflections of Earth and panels in her eyes; she adjusts her gloves and glances around, calm and focused. | |
| 0:05–0:10 — Medium shot; she moves to the large window, gazes out as the sun begins to rise over Earth’s horizon; her expression shifts slightly, squinting at the bright sunlight. | |
| 0:10–0:15 — Close-up on her face; she squints, shading her eyes with a gloved hand, mutters with dry humor: “Damn thing is blinding.” Camera captures Earth glowing in the background with cinematic lens flare across the window edge. | |
| Audio: | |
| Woman: calm, soft-spoken, slight dry humor, low tone | |
| Environment: soft hum of space station, faint beeps from panels | |
| Music: subtle cinematic synth or ambient pad, futuristic and minimal, emphasizing awe and solitude""" | |
| gr.Examples( | |
| examples=[ | |
| [ | |
| "supergirl-2.png", | |
| "A fuzzy puppet superhero character resembling a female puppet with blonde hair and a blue superhero suit sleeping in bed and just waking up, she gradually gets up, rubbing her eyes and looking at her dog that just popped on the bed. the scene feels chaotic, comedic, and emotional with expressive puppet reactions, cinematic lighting, smooth camera motion, shallow depth of field, and high-quality puppet-style animation", | |
| "Static", | |
| "16:9", | |
| "supergirl.m4a" | |
| ], | |
| ], | |
| fn=generate_video_example_s2v, | |
| inputs=[input_image, prompt_ui, camera_lora_ui, radioanimated_resolution, audio_input], | |
| outputs = [output_video], | |
| label="S2V Example", | |
| cache_examples=True, | |
| ) | |
| gr.Examples( | |
| examples=[ | |
| [ | |
| timestep_prompt, | |
| "No LoRA", | |
| "16:9", | |
| ], | |
| ], | |
| fn=generate_video_example_t2v, | |
| inputs=[prompt_ui, camera_lora_ui, radioanimated_resolution], | |
| outputs = [output_video], | |
| label="T2V Example", | |
| cache_examples=True, | |
| ) | |
| # Add example | |
| gr.Examples( | |
| examples=[ | |
| [ | |
| "supergirl.png", | |
| "A fuzzy puppet superhero character resembling a female puppet with blonde hair and a blue superhero suit stands inside an icy cave made of frozen walls and icicles, she looks panicked and frantic, rapidly turning her head left and right and scanning the cave while waving her arms and shouting angrily and desperately, mouthing the words “where the hell is my dog,” her movements exaggerated and puppet-like with high energy and urgency, suddenly a second puppet dog bursts into frame from the side, jumping up excitedly and tackling her affectionately while licking her face repeatedly, she freezes in surprise and then breaks into relief and laughter as the dog continues licking her, the scene feels chaotic, comedic, and emotional with expressive puppet reactions, cinematic lighting, smooth camera motion, shallow depth of field, and high-quality puppet-style animation", | |
| "Detailer", | |
| "16:9", | |
| ], | |
| [ | |
| "highland.png", | |
| "Realistic POV selfie-style video in a snowy, foggy field. Two shaggy Highland cows with long curved horns stand ahead. The camera is handheld and slightly shaky. The woman filming talks nervously and excitedly in a vlog tone: \"Oh my god guys… look how big those horns are… I’m kinda scared.\" The cow on the left walks toward the camera in a cute, bouncy, hopping way, curious and gentle. Snow crunches under its hooves, breath visible in the cold air. The horns look massive from the POV. As the cow gets very close, its wet nose with slight dripping fills part of the frame. She laughs nervously but reaches out and pets the cow. The cow makes deep, soft, interesting mooing and snorting sounds, calm and friendly. Ultra-realistic, natural lighting, immersive audio, documentary-style realism.", | |
| "No LoRA", | |
| "16:9", | |
| ], | |
| [ | |
| "wednesday.png", | |
| "A cinematic dolly out of Wednesday Addams frozen mid-dance on a dark, blue-lit ballroom floor as students move indistinctly behind her, their footsteps and muffled music reduced to a distant, underwater thrum; the audio foregrounds her steady breathing and the faint rustle of fabric as she slowly raises one arm, never breaking eye contact with the camera, then after a deliberately long silence she speaks in a flat, dry, perfectly controlled voice, “I don’t dance… I vibe code,” each word crisp and unemotional, followed by an abrupt cutoff of her voice as the background sound swells slightly, reinforcing the deadpan humor, with precise lip sync, minimal facial movement, stark gothic lighting, and cinematic realism.", | |
| "Zoom Out", | |
| "16:9", | |
| ], | |
| [ | |
| "astronaut.png", | |
| "An astronaut hatches from a fragile egg on the surface of the Moon, the shell cracking and peeling apart in gentle low-gravity motion. Fine lunar dust lifts and drifts outward with each movement, floating in slow arcs before settling back onto the ground. The astronaut pushes free in a deliberate, weightless motion, small fragments of the egg tumbling and spinning through the air. In the background, the deep darkness of space subtly shifts as stars glide with the camera's movement, emphasizing vast depth and scale. The camera performs a smooth, cinematic slow push-in, with natural parallax between the foreground dust, the astronaut, and the distant starfield. Ultra-realistic detail, physically accurate low-gravity motion, cinematic lighting, and a breath-taking, movie-like shot.", | |
| "Static", | |
| "1:1", | |
| ], | |
| # [ | |
| # "astronaut.png", | |
| # "The video opens on object. A knife, held by a hand, is coming into frame and hovering over the object. The knife then begins cutting into the object to c4k3 cakeify them. As the knife slices the object open, the insides of the object are revealed to be cakes with chocolate layers. The knife cuts through and the contents of object are revealed.", | |
| # "Cakify", | |
| # ], | |
| # [ | |
| # "astronaut.png", | |
| # "The video showcases an item. The camera zooms out. Then infl4t3 inflates it, the item expanding into giant, inflated balloon against the landscape.", | |
| # "Inflate", | |
| # ], | |
| # [ | |
| # "astronaut.png", | |
| # "The video begins with an item. A hydraulic press positioned above slowly descends towards the item. Upon contact, the hydraulic press c5us4 crushes it, deforming and flattening the item, causing the item to collapse inward until the item is no longer recognizable.", | |
| # "Hydraulic", | |
| # ], | |
| ], | |
| fn=generate_video_example, | |
| inputs=[input_image, prompt_ui, camera_lora_ui, radioanimated_resolution], | |
| outputs = [output_video], | |
| label="I2V Examples", | |
| cache_examples=True, | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch(ssr_mode=False, mcp_server=True, css=css) |