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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ####################################################################################################
2
+ ### PART 1: Imports & Basic Utilities
3
+ ####################################################################################################
4
+ import sys
5
+ from pathlib import Path
6
+ import uuid
7
+ import tempfile
8
+ import subprocess
9
+ import torch
10
+ import torch.nn.functional as F
11
+ import torchaudio
12
+ import os
13
+ from typing import Any
14
+ import time
15
+ from contextlib import contextmanager
16
+ from gradio.helpers import create_examples
17
+
18
+ @contextmanager
19
+ def timer(name: str):
20
+ start = time.time()
21
+ print(f"{name}...")
22
+ yield
23
+ print(f" -> {name} completed in {time.time() - start:.2f} sec")
24
+
25
+ def _coerce_audio_path(audio_path: Any) -> str:
26
+ # Common Gradio case: tuple where first item is the filepath
27
+ if isinstance(audio_path, tuple) and len(audio_path) > 0:
28
+ audio_path = audio_path[0]
29
+
30
+ # Some gradio versions pass a dict-like object
31
+ if isinstance(audio_path, dict):
32
+ # common keys: "name", "path"
33
+ audio_path = audio_path.get("name") or audio_path.get("path")
34
+
35
+ # pathlib.Path etc.
36
+ if not isinstance(audio_path, (str, bytes, os.PathLike)):
37
+ raise TypeError(f"audio_path must be a path-like, got {type(audio_path)}: {audio_path}")
38
+
39
+ return os.fspath(audio_path)
40
+
41
+
42
+ ####################################################################################################
43
+ ### PART 2: Audio Extraction & Matching Utilities
44
+ ####################################################################################################
45
+ def extract_audio_wav_ffmpeg(video_path: str, target_sr: int = 48000) -> str | None:
46
+ """
47
+ Extract audio from a video into a temp WAV (mono, target_sr).
48
+ Returns path, or None if the video has no audio stream.
49
+ """
50
+ out_path = tempfile.NamedTemporaryFile(suffix=".wav", delete=False).name
51
+
52
+ # Check if there's an audio stream
53
+ probe_cmd = [
54
+ "ffprobe", "-v", "error",
55
+ "-select_streams", "a:0",
56
+ "-show_entries", "stream=codec_type",
57
+ "-of", "default=nw=1:nk=1",
58
+ video_path,
59
+ ]
60
+ try:
61
+ out = subprocess.check_output(probe_cmd).decode("utf-8").strip()
62
+ if not out:
63
+ return None
64
+ except subprocess.CalledProcessError:
65
+ return None
66
+
67
+ # Extract + resample + mono
68
+ cmd = [
69
+ "ffmpeg", "-y", "-v", "error",
70
+ "-i", video_path,
71
+ "-vn",
72
+ "-ac", "1",
73
+ "-ar", str(int(target_sr)),
74
+ "-c:a", "pcm_s16le",
75
+ out_path
76
+ ]
77
+ subprocess.check_call(cmd)
78
+ return out_path
79
+
80
+ def match_audio_to_duration(
81
+ audio_path: str,
82
+ target_seconds: float,
83
+ target_sr: int = 48000,
84
+ to_mono: bool = True,
85
+ pad_mode: str = "silence", # "silence" | "repeat"
86
+ device: str = "cuda",
87
+ ):
88
+ """
89
+ Load audio, resample, (optionally) mono, then trim/pad to exactly target_seconds.
90
+ Returns: (waveform[T] or [1,T], sr)
91
+ """
92
+ audio_path = _coerce_audio_path(audio_path)
93
+
94
+ wav, sr = torchaudio.load(audio_path) # [C, T] float32 CPU
95
+
96
+ # Resample to target_sr (recommended so duration math is stable)
97
+ if sr != target_sr:
98
+ wav = torchaudio.functional.resample(wav, sr, target_sr)
99
+ sr = target_sr
100
+
101
+ # Mono (common expectation; if your model supports stereo, set to_mono=False)
102
+ if to_mono and wav.shape[0] > 1:
103
+ wav = wav.mean(dim=0, keepdim=True) # [1, T]
104
+
105
+ # Exact target length in samples
106
+ target_len = int(round(target_seconds * sr))
107
+ cur_len = wav.shape[-1]
108
+
109
+ if cur_len > target_len:
110
+ wav = wav[..., :target_len]
111
+ elif cur_len < target_len:
112
+ pad_len = target_len - cur_len
113
+ if pad_mode == "repeat" and cur_len > 0:
114
+ # Repeat then cut to exact length
115
+ reps = (target_len + cur_len - 1) // cur_len
116
+ wav = wav.repeat(1, reps)[..., :target_len]
117
+ else:
118
+ # Silence pad
119
+ wav = F.pad(wav, (0, pad_len))
120
+
121
+ # move to device
122
+ wav = wav.to(device, non_blocking=True)
123
+ return wav, sr
124
+
125
+ def sh(cmd): subprocess.check_call(cmd, shell=True)
126
+
127
+
128
+ ####################################################################################################
129
+ ### PART 3: LTX Pipeline Imports & Constants
130
+ ####################################################################################################
131
+ # Add packages to Python path
132
+ current_dir = Path(__file__).parent
133
+ sys.path.insert(0, str(current_dir / "packages" / "ltx-pipelines" / "src"))
134
+ sys.path.insert(0, str(current_dir / "packages" / "ltx-core" / "src"))
135
+
136
+ import spaces
137
+ import flash_attn_interface
138
+ import time
139
+ import gradio as gr
140
+ import numpy as np
141
+ import random
142
+ import torch
143
+ from typing import Optional
144
+ from pathlib import Path
145
+ import torchaudio
146
+ from huggingface_hub import hf_hub_download, snapshot_download
147
+ from ltx_pipelines.distilled import DistilledPipeline
148
+ from ltx_core.model.video_vae import TilingConfig
149
+ from ltx_core.model.audio_vae.ops import AudioProcessor
150
+ from ltx_core.loader.primitives import LoraPathStrengthAndSDOps
151
+ from ltx_core.loader.sd_ops import LTXV_LORA_COMFY_RENAMING_MAP
152
+ from ltx_pipelines.utils.constants import (
153
+ DEFAULT_SEED,
154
+ DEFAULT_1_STAGE_HEIGHT,
155
+ DEFAULT_1_STAGE_WIDTH ,
156
+ DEFAULT_NUM_FRAMES,
157
+ DEFAULT_FRAME_RATE,
158
+ DEFAULT_LORA_STRENGTH,
159
+ )
160
+ from ltx_core.loader.single_gpu_model_builder import enable_only_lora
161
+ from ltx_core.model.audio_vae import decode_audio
162
+ from ltx_core.model.audio_vae import encode_audio
163
+ from PIL import Image
164
+
165
+ MAX_SEED = np.iinfo(np.int32).max
166
+ # Import from public LTX-2 package
167
+ # Install with: pip install git+https://github.com/Lightricks/LTX-2.git
168
+ from ltx_pipelines.utils import ModelLedger
169
+ from ltx_pipelines.utils.helpers import generate_enhanced_prompt
170
+ import imageio
171
+ import cv2
172
+
173
+ # HuggingFace Hub defaults
174
+ DEFAULT_REPO_ID = "Lightricks/LTX-2"
175
+ DEFAULT_GEMMA_REPO_ID = "unsloth/gemma-3-12b-it-qat-bnb-4bit"
176
+ DEFAULT_CHECKPOINT_FILENAME = "ltx-2-19b-dev.safetensors"
177
+
178
+
179
+ ####################################################################################################
180
+ ### PART 4: Download Helpers
181
+ ####################################################################################################
182
+ def get_hub_or_local_checkpoint(repo_id: str, filename: str):
183
+ """Download from HuggingFace Hub."""
184
+ print(f"Downloading {filename} from {repo_id}...")
185
+ ckpt_path = hf_hub_download(repo_id=repo_id, filename=filename)
186
+ print(f"Downloaded to {ckpt_path}")
187
+ return ckpt_path
188
+
189
+ def download_gemma_model(repo_id: str):
190
+ """Download the full Gemma model directory."""
191
+ print(f"Downloading Gemma model from {repo_id}...")
192
+ local_dir = snapshot_download(repo_id=repo_id)
193
+ print(f"Gemma model downloaded to {local_dir}")
194
+ return local_dir
195
+
196
+
197
+ ####################################################################################################
198
+ ### PART 5: Text Encoder Initialization
199
+ ####################################################################################################
200
+ # Initialize model ledger and text encoder at startup (load once, keep in memory)
201
+ print("=" * 80)
202
+ print("Loading Gemma Text Encoder...")
203
+ print("=" * 80)
204
+
205
+ checkpoint_path = get_hub_or_local_checkpoint(DEFAULT_REPO_ID, DEFAULT_CHECKPOINT_FILENAME)
206
+ gemma_local_path = download_gemma_model(DEFAULT_GEMMA_REPO_ID)
207
+ device = "cuda"
208
+
209
+ print(f"Initializing text encoder with:")
210
+ print(f" checkpoint_path={checkpoint_path}")
211
+ print(f" gemma_root={gemma_local_path}")
212
+ print(f" device={device}")
213
+
214
+ model_ledger = ModelLedger(
215
+ dtype=torch.bfloat16,
216
+ device=device,
217
+ checkpoint_path=checkpoint_path,
218
+ gemma_root_path=DEFAULT_GEMMA_REPO_ID,
219
+ local_files_only=False
220
+ )
221
+
222
+ # Load text encoder once and keep it in memory
223
+ text_encoder = model_ledger.text_encoder()
224
+
225
+ print("=" * 80)
226
+ print("Text encoder loaded and ready!")
227
+ print("=" * 80)
228
+
229
+ def on_lora_change(selected: str):
230
+ # Only Detailer might need handling, but we simplified motion control out
231
+ needs_video = selected in {"Detailer"}
232
+ return (
233
+ selected,
234
+ gr.update(visible=not needs_video, value=None if needs_video else None),
235
+ gr.update(visible=needs_video, value=None if not needs_video else None),
236
+ )
237
+
238
+
239
+ ####################################################################################################
240
+ ### PART 6: Video/Frame Utilities
241
+ ####################################################################################################
242
+ def load_video_frames(video_path: str):
243
+ """Return list of frames as numpy arrays (H,W,3) uint8."""
244
+ frames = []
245
+ with imageio.get_reader(video_path) as reader:
246
+ for frame in reader:
247
+ frames.append(frame)
248
+ return frames
249
+
250
+ def write_video_mp4(frames_float_01, fps: float, out_path: str):
251
+ """Write frames in float [0..1] to mp4 as uint8."""
252
+ frames_uint8 = [(f * 255).astype(np.uint8) for f in frames_float_01]
253
+
254
+ # PyAV backend doesn't support `quality=...`
255
+ with imageio.get_writer(out_path, fps=fps, macro_block_size=1) as writer:
256
+ for fr in frames_uint8:
257
+ writer.append_data(fr)
258
+ return out_path
259
+
260
+ import json
261
+
262
+ def probe_video_duration_seconds(video_path: str) -> float:
263
+ """Return duration in seconds using ffprobe."""
264
+ cmd = [
265
+ "ffprobe", "-v", "error",
266
+ "-select_streams", "v:0",
267
+ "-show_entries", "format=duration",
268
+ "-of", "json",
269
+ video_path,
270
+ ]
271
+ out = subprocess.check_output(cmd).decode("utf-8")
272
+ data = json.loads(out)
273
+ dur = float(data["format"]["duration"])
274
+ return dur
275
+
276
+
277
+ ####################################################################################################
278
+ ### PART 7: FFmpeg Utils & Frame Prep
279
+ ####################################################################################################
280
+ def trim_video_to_seconds_ffmpeg(video_path: str, target_seconds: float, fps: float = None) -> str:
281
+ """
282
+ Trim video to [0, target_seconds]. Re-encode for accuracy & compatibility.
283
+ If fps is provided, also normalize fps.
284
+ Returns new temp mp4 path.
285
+ """
286
+ target_seconds = max(0.01, float(target_seconds))
287
+
288
+ out_path = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False).name
289
+
290
+ vf = []
291
+ if fps is not None:
292
+ vf.append(f"fps={float(fps)}")
293
+ vf_str = ",".join(vf) if vf else None
294
+
295
+ cmd = ["ffmpeg", "-y", "-v", "error"]
296
+
297
+ # Accurate trim: use -t and re-encode.
298
+ cmd += ["-i", video_path, "-t", f"{target_seconds:.6f}"]
299
+
300
+ if vf_str:
301
+ cmd += ["-vf", vf_str]
302
+
303
+ # Safe default encode
304
+ cmd += [
305
+ "-c:v", "libx264", "-pix_fmt", "yuv420p", "-preset", "veryfast", "-crf", "18",
306
+ "-an", # conditioning video doesn't need audio
307
+ out_path
308
+ ]
309
+
310
+ subprocess.check_call(cmd)
311
+ return out_path
312
+
313
+ def extract_first_frame_png(video_path: str) -> str:
314
+ """Extract first frame as png; returns png path."""
315
+ out_path = tempfile.NamedTemporaryFile(suffix=".png", delete=False).name
316
+ cmd = [
317
+ "ffmpeg", "-y", "-v", "error",
318
+ "-i", video_path,
319
+ "-frames:v", "1",
320
+ out_path
321
+ ]
322
+ subprocess.check_call(cmd)
323
+ return out_path
324
+
325
+ def _coerce_video_path(video_path: Any) -> str:
326
+ if isinstance(video_path, tuple) and len(video_path) > 0:
327
+ video_path = video_path[0]
328
+ if isinstance(video_path, dict):
329
+ video_path = video_path.get("name") or video_path.get("path")
330
+ if not isinstance(video_path, (str, bytes, os.PathLike)):
331
+ raise TypeError(f"video_path must be a path-like, got {type(video_path)}: {video_path}")
332
+ return os.fspath(video_path)
333
+
334
+
335
+ ####################################################################################################
336
+ ### PART 8: Prompt Encoding Logic
337
+ ####################################################################################################
338
+ def encode_text_simple(text_encoder, prompt: str):
339
+ """Simple text encoding without using pipeline_utils."""
340
+ v_context, a_context, _ = text_encoder(prompt)
341
+ return v_context, a_context
342
+
343
+ @spaces.GPU()
344
+ def encode_prompt(
345
+ prompt: str,
346
+ enhance_prompt: bool = True,
347
+ input_image=None, # this is now filepath (string) or None
348
+ seed: int = 42,
349
+ negative_prompt: str = ""
350
+ ):
351
+ start_time = time.time()
352
+ try:
353
+ final_prompt = prompt
354
+ if enhance_prompt:
355
+ final_prompt = generate_enhanced_prompt(
356
+ text_encoder=text_encoder,
357
+ prompt=prompt,
358
+ image_path=input_image if input_image is not None else None,
359
+ seed=seed,
360
+ )
361
+
362
+ with torch.inference_mode():
363
+ video_context, audio_context = encode_text_simple(text_encoder, final_prompt)
364
+
365
+ video_context_negative = None
366
+ audio_context_negative = None
367
+ if negative_prompt:
368
+ video_context_negative, audio_context_negative = encode_text_simple(text_encoder, negative_prompt)
369
+
370
+ # IMPORTANT: return tensors directly (no torch.save)
371
+ embedding_data = {
372
+ "video_context": video_context.detach().cpu(),
373
+ "audio_context": audio_context.detach().cpu(),
374
+ "prompt": final_prompt,
375
+ "original_prompt": prompt,
376
+ }
377
+ if video_context_negative is not None:
378
+ embedding_data["video_context_negative"] = video_context_negative
379
+ embedding_data["audio_context_negative"] = audio_context_negative
380
+ embedding_data["negative_prompt"] = negative_prompt
381
+
382
+ elapsed_time = time.time() - start_time
383
+ if torch.cuda.is_available():
384
+ allocated = torch.cuda.memory_allocated() / 1024**3
385
+ peak = torch.cuda.max_memory_allocated() / 1024**3
386
+ status = f"✓ Encoded in {elapsed_time:.2f}s | VRAM: {allocated:.2f}GB allocated, {peak:.2f}GB peak"
387
+ else:
388
+ status = f"✓ Encoded in {elapsed_time:.2f}s (CPU mode)"
389
+
390
+ return embedding_data, final_prompt, status
391
+
392
+ except Exception as e:
393
+ import traceback
394
+ error_msg = f"Error: {str(e)}\n{traceback.format_exc()}"
395
+ print(error_msg)
396
+ return None, prompt, error_msg
397
+
398
+ # Default prompt from docstring example
399
+ 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."
400
+
401
+
402
+ ####################################################################################################
403
+ ### PART 9: Pipeline Constants & Checkpoint Paths
404
+ ####################################################################################################
405
+ # HuggingFace Hub defaults
406
+ DEFAULT_REPO_ID = "Lightricks/LTX-2"
407
+ DEFAULT_CHECKPOINT_FILENAME = "ltx-2-19b-dev.safetensors"
408
+ DEFAULT_DISTILLED_LORA_FILENAME = "ltx-2-19b-distilled-lora-384.safetensors"
409
+ DEFAULT_SPATIAL_UPSAMPLER_FILENAME = "ltx-2-spatial-upscaler-x2-1.0.safetensors"
410
+
411
+ def get_hub_or_local_checkpoint_pipeline(repo_id: Optional[str] = None, filename: Optional[str] = None):
412
+ """Download from HuggingFace Hub or use local checkpoint."""
413
+ if repo_id is None and filename is None:
414
+ raise ValueError("Please supply at least one of `repo_id` or `filename`")
415
+
416
+ if repo_id is not None:
417
+ if filename is None:
418
+ raise ValueError("If repo_id is specified, filename must also be specified.")
419
+ print(f"Downloading {filename} from {repo_id}...")
420
+ ckpt_path = hf_hub_download(repo_id=repo_id, filename=filename)
421
+ print(f"Downloaded to {ckpt_path}")
422
+ else:
423
+ ckpt_path = filename
424
+
425
+ return ckpt_path
426
+
427
+
428
+ # Initialize pipeline at startup
429
+ print("=" * 80)
430
+ print("Loading LTX-2 Distilled pipeline...")
431
+ print("=" * 80)
432
+
433
+ checkpoint_path = get_hub_or_local_checkpoint_pipeline(DEFAULT_REPO_ID, DEFAULT_CHECKPOINT_FILENAME)
434
+ spatial_upsampler_path = get_hub_or_local_checkpoint_pipeline(DEFAULT_REPO_ID, DEFAULT_SPATIAL_UPSAMPLER_FILENAME)
435
+
436
+ print(f"Initializing pipeline with:")
437
+ print(f" checkpoint_path={checkpoint_path}")
438
+ print(f" spatial_upsampler_path={spatial_upsampler_path}")
439
+
440
+ distilled_lora_path = get_hub_or_local_checkpoint_pipeline(
441
+ DEFAULT_REPO_ID,
442
+ DEFAULT_DISTILLED_LORA_FILENAME,
443
+ )
444
+
445
+ static_lora_path = get_hub_or_local_checkpoint_pipeline(
446
+ "Lightricks/LTX-2-19b-LoRA-Camera-Control-Static",
447
+ "ltx-2-19b-lora-camera-control-static.safetensors",
448
+ )
449
+ dolly_in_lora_path = get_hub_or_local_checkpoint_pipeline(
450
+ "Lightricks/LTX-2-19b-LoRA-Camera-Control-Dolly-In",
451
+ "ltx-2-19b-lora-camera-control-dolly-in.safetensors",
452
+ )
453
+ dolly_out_lora_path = get_hub_or_local_checkpoint_pipeline(
454
+ "Lightricks/LTX-2-19b-LoRA-Camera-Control-Dolly-Out",
455
+ "ltx-2-19b-lora-camera-control-dolly-out.safetensors",
456
+ )
457
+ dolly_left_lora_path = get_hub_or_local_checkpoint_pipeline(
458
+ "Lightricks/LTX-2-19b-LoRA-Camera-Control-Dolly-Left",
459
+ "ltx-2-19b-lora-camera-control-dolly-left.safetensors",
460
+ )
461
+ dolly_right_lora_path = get_hub_or_local_checkpoint_pipeline(
462
+ "Lightricks/LTX-2-19b-LoRA-Camera-Control-Dolly-Right",
463
+ "ltx-2-19b-lora-camera-control-dolly-right.safetensors",
464
+ )
465
+ jib_down_lora_path = get_hub_or_local_checkpoint_pipeline(
466
+ "Lightricks/LTX-2-19b-LoRA-Camera-Control-Jib-Down",
467
+ "ltx-2-19b-lora-camera-control-jib-down.safetensors",
468
+ )
469
+ jib_up_lora_path = get_hub_or_local_checkpoint_pipeline(
470
+ "Lightricks/LTX-2-19b-LoRA-Camera-Control-Jib-Up",
471
+ "ltx-2-19b-lora-camera-control-jib-up.safetensors",
472
+ )
473
+
474
+ detailer_lora_path = get_hub_or_local_checkpoint_pipeline(
475
+ "Lightricks/LTX-2-19b-IC-LoRA-Detailer",
476
+ "ltx-2-19b-ic-lora-detailer.safetensors",
477
+ )
478
+
479
+
480
+ ####################################################################################################
481
+ ### PART 10: LoRA Setup & Configuration
482
+ ####################################################################################################
483
+ # Load distilled LoRA as a regular LoRA
484
+ loras = [
485
+ # --- fused / base behavior ---
486
+ LoraPathStrengthAndSDOps(
487
+ path=distilled_lora_path,
488
+ strength=0.6,
489
+ sd_ops=LTXV_LORA_COMFY_RENAMING_MAP,
490
+ ),
491
+ LoraPathStrengthAndSDOps(static_lora_path, DEFAULT_LORA_STRENGTH, LTXV_LORA_COMFY_RENAMING_MAP),
492
+ LoraPathStrengthAndSDOps(detailer_lora_path, DEFAULT_LORA_STRENGTH, LTXV_LORA_COMFY_RENAMING_MAP),
493
+ LoraPathStrengthAndSDOps(dolly_in_lora_path, DEFAULT_LORA_STRENGTH, LTXV_LORA_COMFY_RENAMING_MAP),
494
+ LoraPathStrengthAndSDOps(dolly_out_lora_path, DEFAULT_LORA_STRENGTH, LTXV_LORA_COMFY_RENAMING_MAP),
495
+ LoraPathStrengthAndSDOps(dolly_left_lora_path, DEFAULT_LORA_STRENGTH, LTXV_LORA_COMFY_RENAMING_MAP),
496
+ LoraPathStrengthAndSDOps(dolly_right_lora_path, DEFAULT_LORA_STRENGTH, LTXV_LORA_COMFY_RENAMING_MAP),
497
+ LoraPathStrengthAndSDOps(jib_down_lora_path, DEFAULT_LORA_STRENGTH, LTXV_LORA_COMFY_RENAMING_MAP),
498
+ LoraPathStrengthAndSDOps(jib_up_lora_path, DEFAULT_LORA_STRENGTH, LTXV_LORA_COMFY_RENAMING_MAP),
499
+ ]
500
+
501
+ # Runtime-toggle LoRAs (exclude fused distilled at index 0)
502
+ VISIBLE_RUNTIME_LORA_CHOICES = [
503
+ ("No LoRA", -1),
504
+ ("Static", 0),
505
+ ("Detailer", 1),
506
+ ("Zoom In", 2),
507
+ ("Zoom Out", 3),
508
+ ("Slide Left", 4),
509
+ ("Slide Right", 5),
510
+ ("Slide Down", 6),
511
+ ("Slide Up", 7),
512
+ ]
513
+
514
+ RUNTIME_LORA_CHOICES = [
515
+ ("No LoRA", -1),
516
+ ("Static", 0),
517
+ ("Detailer", 1),
518
+ ("Zoom In", 2),
519
+ ("Zoom Out", 3),
520
+ ("Slide Left", 4),
521
+ ("Slide Right", 5),
522
+ ("Slide Down", 6),
523
+ ("Slide Up", 7),
524
+ ]
525
+
526
+
527
+ ####################################################################################################
528
+ ### PART 11: Pipeline Initialization
529
+ ####################################################################################################
530
+ # Initialize pipeline WITHOUT text encoder (gemma_root=None)
531
+ # Text encoding will be done by external space
532
+ pipeline = DistilledPipeline(
533
+ device=torch.device("cuda"),
534
+ checkpoint_path=checkpoint_path,
535
+ spatial_upsampler_path=spatial_upsampler_path,
536
+ gemma_root=None, # No text encoder in this space
537
+ loras=loras,
538
+ fp8transformer=False,
539
+ local_files_only=False,
540
+ )
541
+
542
+ pipeline._video_encoder = pipeline.model_ledger.video_encoder()
543
+ pipeline._transformer = pipeline.model_ledger.transformer()
544
+
545
+ print("=" * 80)
546
+ print("Pipeline fully loaded and ready!")
547
+ print("=" * 80)
548
+
549
+
550
+ ####################################################################################################
551
+ ### PART 12: Custom Component - RadioAnimated
552
+ ####################################################################################################
553
+ class RadioAnimated(gr.HTML):
554
+ """
555
+ Animated segmented radio (like iOS pill selector).
556
+ Outputs: selected option string, e.g. "768x512"
557
+ """
558
+ def __init__(self, choices, value=None, **kwargs):
559
+ if not choices or len(choices) < 2:
560
+ raise ValueError("RadioAnimated requires at least 2 choices.")
561
+ if value is None:
562
+ value = choices[0]
563
+
564
+ uid = uuid.uuid4().hex[:8] # unique per instance
565
+ group_name = f"ra-{uid}"
566
+
567
+ inputs_html = "\n".join(
568
+ f"""
569
+ <input class="ra-input" type="radio" name="{group_name}" id="{group_name}-{i}" value="{c}">
570
+ <label class="ra-label" for="{group_name}-{i}">{c}</label>
571
+ """
572
+ for i, c in enumerate(choices)
573
+ )
574
+
575
+ # NOTE: use classes instead of duplicate IDs
576
+ html_template = f"""
577
+ <div class="ra-wrap" data-ra="{uid}">
578
+ <div class="ra-inner">
579
+ <div class="ra-highlight"></div>
580
+ {inputs_html}
581
+ </div>
582
+ </div>
583
+ """
584
+
585
+ js_on_load = r"""
586
+ (() => {
587
+ const wrap = element.querySelector('.ra-wrap');
588
+ const inner = element.querySelector('.ra-inner');
589
+ const highlight = element.querySelector('.ra-highlight');
590
+ const inputs = Array.from(element.querySelectorAll('.ra-input'));
591
+ const labels = Array.from(element.querySelectorAll('.ra-label'));
592
+
593
+ if (!inputs.length || !labels.length) return;
594
+
595
+ const choices = inputs.map(i => i.value);
596
+ const PAD = 6; // must match .ra-inner padding and .ra-highlight top/left
597
+
598
+ let currentIdx = 0;
599
+
600
+ function setHighlightByIndex(idx) {
601
+ currentIdx = idx;
602
+
603
+ const lbl = labels[idx];
604
+ if (!lbl) return;
605
+
606
+ const innerRect = inner.getBoundingClientRect();
607
+ const lblRect = lbl.getBoundingClientRect();
608
+
609
+ // width matches the label exactly
610
+ highlight.style.width = `${lblRect.width}px`;
611
+
612
+ // highlight has left: 6px, so subtract PAD to align
613
+ const x = (lblRect.left - innerRect.left - PAD);
614
+ highlight.style.transform = `translateX(${x}px)`;
615
+ }
616
+
617
+ function setCheckedByValue(val, shouldTrigger=false) {
618
+ const idx = Math.max(0, choices.indexOf(val));
619
+ inputs.forEach((inp, i) => { inp.checked = (i === idx); });
620
+
621
+ // Wait a frame in case fonts/layout settle (prevents rare drift)
622
+ requestAnimationFrame(() => setHighlightByIndex(idx));
623
+
624
+ props.value = choices[idx];
625
+ if (shouldTrigger) trigger('change', props.value);
626
+ }
627
+
628
+ // Init
629
+ setCheckedByValue(props.value ?? choices[0], false);
630
+
631
+ // Input handlers
632
+ inputs.forEach((inp) => {
633
+ inp.addEventListener('change', () => setCheckedByValue(inp.value, true));
634
+ });
635
+
636
+ // Recalc on resize (important in Gradio layouts)
637
+ window.addEventListener('resize', () => setHighlightByIndex(currentIdx));
638
+
639
+ // sync from Python (Examples / backend updates)
640
+ let last = props.value;
641
+ const syncFromProps = () => {
642
+ if (props.value !== last) {
643
+ last = props.value;
644
+ setCheckedByValue(last, false);
645
+ }
646
+ requestAnimationFrame(syncFromProps);
647
+ };
648
+ requestAnimationFrame(syncFromProps);
649
+
650
+ })();
651
+
652
+ """
653
+
654
+ super().__init__(
655
+ value=value,
656
+ html_template=html_template,
657
+ js_on_load=js_on_load,
658
+ **kwargs
659
+ )
660
+
661
+
662
+ ####################################################################################################
663
+ ### PART 13: Custom Component - PromptBox
664
+ ####################################################################################################
665
+ class PromptBox(gr.HTML):
666
+ """
667
+ Prompt textarea with an internal footer slot (.ds-footer) where we can inject dropdowns.
668
+ """
669
+ def __init__(self, value="", placeholder="Describe what you want...", **kwargs):
670
+ uid = uuid.uuid4().hex[:8]
671
+
672
+ html_template = f"""
673
+ <div class="ds-card" data-ds="{uid}">
674
+ <div class="ds-top">
675
+ <textarea class="ds-textarea" rows="3" placeholder="{placeholder}"></textarea>
676
+
677
+ <!-- footer slot -->
678
+ <div class="ds-footer" aria-label="prompt-footer"></div>
679
+ </div>
680
+ </div>
681
+ """
682
+
683
+ js_on_load = r"""
684
+ (() => {
685
+ const textarea = element.querySelector(".ds-textarea");
686
+ if (!textarea) return;
687
+
688
+ const autosize = () => {
689
+ textarea.style.height = "0px";
690
+ textarea.style.height = Math.min(textarea.scrollHeight, 240) + "px";
691
+ };
692
+
693
+ const setValue = (v, triggerChange=false) => {
694
+ const val = (v ?? "");
695
+ if (textarea.value !== val) textarea.value = val;
696
+ autosize();
697
+ props.value = textarea.value;
698
+ if (triggerChange) trigger("change", props.value);
699
+ };
700
+
701
+ setValue(props.value, false);
702
+
703
+ textarea.addEventListener("input", () => {
704
+ autosize();
705
+ props.value = textarea.value;
706
+ trigger("change", props.value);
707
+ });
708
+
709
+ // ✅ Focus-on-load (robust)
710
+ const shouldAutoFocus = () => {
711
+ // don’t steal focus if user already clicked/typed somewhere
712
+ const ae = document.activeElement;
713
+ if (ae && ae !== document.body && ae !== document.documentElement) return false;
714
+ // don’t auto-focus on small screens (optional; avoids mobile keyboard pop)
715
+ if (window.matchMedia && window.matchMedia("(max-width: 768px)").matches) return false;
716
+ return true;
717
+ };
718
+
719
+ const focusWithRetry = (tries = 30) => {
720
+ if (!shouldAutoFocus()) return;
721
+ // only focus if still not focused
722
+ if (document.activeElement !== textarea) textarea.focus({ preventScroll: true });
723
+ if (document.activeElement === textarea) return;
724
+ if (tries > 0) requestAnimationFrame(() => focusWithRetry(tries - 1));
725
+ };
726
+
727
+ // wait a tick so Gradio/layout settles
728
+ requestAnimationFrame(() => focusWithRetry());
729
+
730
+ // keep your sync loop
731
+ let last = props.value;
732
+ const syncFromProps = () => {
733
+ if (props.value !== last) {
734
+ last = props.value;
735
+ setValue(last, false);
736
+ }
737
+ requestAnimationFrame(syncFromProps);
738
+ };
739
+ requestAnimationFrame(syncFromProps);
740
+ })();
741
+
742
+ """
743
+
744
+ super().__init__(value=value, html_template=html_template, js_on_load=js_on_load, **kwargs)
745
+
746
+
747
+ ####################################################################################################
748
+ ### PART 14: Custom Component - CameraDropdown
749
+ ####################################################################################################
750
+ class CameraDropdown(gr.HTML):
751
+ """
752
+ Custom dropdown (More-style) with optional icons per item.
753
+ Outputs: selected option string, e.g. "16:9"
754
+ """
755
+ def __init__(self, choices, value="None", title="Dropdown", **kwargs):
756
+ if not choices:
757
+ raise ValueError("CameraDropdown requires choices.")
758
+
759
+ # Normalize choices -> list of dicts: {label, value, icon(optional)}
760
+ norm = []
761
+ for c in choices:
762
+ if isinstance(c, dict):
763
+ label = str(c.get("label", c.get("value", "")))
764
+ val = str(c.get("value", label))
765
+ icon = c.get("icon", None) # emoji or svg/html
766
+ norm.append({"label": label, "value": val, "icon": icon})
767
+ else:
768
+ s = str(c)
769
+ norm.append({"label": s, "value": s, "icon": None})
770
+
771
+ uid = uuid.uuid4().hex[:8]
772
+
773
+ def render_item(item):
774
+ icon_html = ""
775
+ if item["icon"]:
776
+ icon_html = f'<span class="cd-icn">{item["icon"]}</span>'
777
+ return (
778
+ f'<button type="button" class="cd-item" '
779
+ f'data-value="{item["value"]}">'
780
+ f'{icon_html}<span class="cd-label">{item["label"]}</span>'
781
+ f'</button>'
782
+ )
783
+
784
+ items_html = "\n".join(render_item(item) for item in norm)
785
+
786
+ html_template = f"""
787
+ <div class="cd-wrap" data-cd="{uid}">
788
+ <button type="button" class="cd-trigger" aria-haspopup="menu" aria-expanded="false">
789
+ <span class="cd-trigger-icon"></span>
790
+ <span class="cd-trigger-text"></span>
791
+ <span class="cd-caret">▾</span>
792
+ </button>
793
+
794
+ <div class="cd-menu" role="menu" aria-hidden="true">
795
+ <div class="cd-title">{title}</div>
796
+ <div class="cd-items">
797
+ {items_html}
798
+ </div>
799
+ </div>
800
+ </div>
801
+ """
802
+
803
+ # Pass a mapping value->label so the trigger can show label text
804
+ # (and still output value to Python)
805
+ value_to_label = {it["value"]: it["label"] for it in norm}
806
+ value_to_icon = {it["value"]: (it["icon"] or "") for it in norm}
807
+
808
+ js_on_load = r"""
809
+ (() => {
810
+ const wrap = element.querySelector(".cd-wrap");
811
+ const trigger = element.querySelector(".cd-trigger");
812
+ const triggerIcon = element.querySelector(".cd-trigger-icon");
813
+ const triggerText = element.querySelector(".cd-trigger-text");
814
+ const menu = element.querySelector(".cd-menu");
815
+ const items = Array.from(element.querySelectorAll(".cd-item"));
816
+ if (!wrap || !trigger || !menu || !items.length) return;
817
+
818
+ const valueToLabel = __VALUE_TO_LABEL__;
819
+ const valueToIcon = __VALUE_TO_ICON__;
820
+
821
+ const safeLabel = (v) => (valueToLabel && valueToLabel[v]) ? valueToLabel[v] : (v ?? "None");
822
+ const safeIcon = (v) => (valueToIcon && valueToIcon[v]) ? valueToIcon[v] : "";
823
+
824
+
825
+ function closeMenu() {
826
+ menu.classList.remove("open");
827
+ trigger.setAttribute("aria-expanded", "false");
828
+ menu.setAttribute("aria-hidden", "true");
829
+ }
830
+
831
+ function openMenu() {
832
+ menu.classList.add("open");
833
+ trigger.setAttribute("aria-expanded", "true");
834
+ menu.setAttribute("aria-hidden", "false");
835
+ }
836
+
837
+ function setValue(val, shouldTrigger = false) {
838
+ const v = (val ?? "None");
839
+ props.value = v;
840
+
841
+ // Trigger shows LABEL only (icons stay in menu)
842
+ triggerText.textContent = safeLabel(v);
843
+ if (triggerIcon) {
844
+ triggerIcon.innerHTML = safeIcon(v);
845
+ triggerIcon.style.display = safeIcon(v) ? "inline-flex" : "none";
846
+ }
847
+
848
+
849
+ items.forEach(btn => {
850
+ btn.dataset.selected = (btn.dataset.value === v) ? "true" : "false";
851
+ });
852
+
853
+ if (shouldTrigger) trigger("change", props.value);
854
+ }
855
+
856
+ trigger.addEventListener("pointerdown", (e) => {
857
+ e.preventDefault();
858
+ e.stopPropagation();
859
+ if (menu.classList.contains("open")) closeMenu();
860
+ else openMenu();
861
+ });
862
+
863
+ document.addEventListener("pointerdown", (e) => {
864
+ if (!wrap.contains(e.target)) closeMenu();
865
+ }, true);
866
+
867
+ document.addEventListener("keydown", (e) => {
868
+ if (e.key === "Escape") closeMenu();
869
+ });
870
+
871
+ wrap.addEventListener("focusout", (e) => {
872
+ if (!wrap.contains(e.relatedTarget)) closeMenu();
873
+ });
874
+
875
+ items.forEach((btn) => {
876
+ btn.addEventListener("pointerdown", (e) => {
877
+ e.preventDefault();
878
+ e.stopPropagation();
879
+ closeMenu();
880
+ setValue(btn.dataset.value, true);
881
+ });
882
+ });
883
+
884
+ // init
885
+ setValue((props.value ?? "None"), false);
886
+
887
+ // sync from Python
888
+ let last = props.value;
889
+ const syncFromProps = () => {
890
+ if (props.value !== last) {
891
+ last = props.value;
892
+ setValue(last, false);
893
+ }
894
+ requestAnimationFrame(syncFromProps);
895
+ };
896
+ requestAnimationFrame(syncFromProps);
897
+ })();
898
+ """
899
+
900
+ # Inject mapping into JS safely
901
+ import json
902
+ js_on_load = js_on_load.replace("__VALUE_TO_LABEL__", json.dumps(value_to_label))
903
+ js_on_load = js_on_load.replace("__VALUE_TO_ICON__", json.dumps(value_to_icon))
904
+
905
+ super().__init__(
906
+ value=value,
907
+ html_template=html_template,
908
+ js_on_load=js_on_load,
909
+ **kwargs
910
+ )
911
+
912
+
913
+ ####################################################################################################
914
+ ### PART 15: Custom Component - PresetGallery
915
+ ####################################################################################################
916
+ class PresetGallery(gr.HTML):
917
+ """
918
+ Clickable image presets -> outputs selected index (int as string, then cast in python)
919
+ """
920
+ def __init__(self, items, title="Click an Example", **kwargs):
921
+ """
922
+ items: list[dict] with keys:
923
+ - thumb: str (path to image file, e.g. "supergirl.png")
924
+ - label: str (optional)
925
+ """
926
+ uid = uuid.uuid4().hex[:8]
927
+
928
+ cards_html = []
929
+ for i, it in enumerate(items):
930
+ thumb = it["thumb"]
931
+ label = it.get("label", "")
932
+ cards_html.append(f"""
933
+ <button type="button" class="pg-card" data-idx="{i}" aria-label="{label}">
934
+ <img class="pg-img" src="gradio_api/file={thumb}" alt="{label}">
935
+ </button>
936
+ """)
937
+
938
+ html_template = f"""
939
+ <div class="pg-wrap" data-pg="{uid}">
940
+ <div class="pg-title">
941
+ <div class="pg-h1">{title}</div>
942
+ </div>
943
+ <div class="pg-grid">
944
+ {''.join(cards_html)}
945
+ </div>
946
+ </div>
947
+ """
948
+
949
+ # --- JAVASCRIPT LOGIC UPDATED ---
950
+ # Removed "pointerenter" and "pointerleave" events.
951
+ # All logic is now inside "pointerdown" (click event).
952
+ js_on_load = r"""
953
+ (() => {
954
+ const wrap = element.querySelector(".pg-wrap");
955
+ const cards = Array.from(element.querySelectorAll(".pg-card"));
956
+ if (!wrap || !cards.length) return;
957
+
958
+ function setDim(activeIdx) {
959
+ cards.forEach((c, i) => {
960
+ // Dim cards that are not the active one.
961
+ c.dataset.dim = (activeIdx !== null && i !== activeIdx) ? "true" : "false";
962
+ // Set the active state for the clicked card.
963
+ c.dataset.active = (i === activeIdx) ? "true" : "false";
964
+ });
965
+ }
966
+
967
+ let lastSent = null;
968
+ let lock = false;
969
+
970
+ cards.forEach((card) => {
971
+ // REMOVED: "pointerenter" event listener. No action on hover.
972
+ // REMOVED: "pointerleave" event listener. No action on hover.
973
+
974
+ // Use pointerdown (click/tap) for all actions.
975
+ card.addEventListener("pointerdown", (e) => {
976
+ e.preventDefault();
977
+ e.stopPropagation();
978
+
979
+ if (lock) return;
980
+ lock = true;
981
+ setTimeout(() => (lock = false), 250);
982
+
983
+ const idxNumber = Number(card.dataset.idx);
984
+ const idxString = String(idxNumber);
985
+
986
+ // 1. Update visual state immediately on click.
987
+ setDim(idxNumber);
988
+
989
+ // 2. Only update Python backend if the value has actually changed.
990
+ if (idxString === lastSent) return;
991
+ lastSent = idxString;
992
+
993
+ // ✅ Set props.value to send data to Python.
994
+ props.value = idxString;
995
+ }, { passive: false });
996
+ });
997
+
998
+ // Initially, no card is selected.
999
+ setDim(null);
1000
+ })();
1001
+ """
1002
+
1003
+
1004
+ super().__init__(value="", html_template=html_template, js_on_load=js_on_load, **kwargs)
1005
+
1006
+
1007
+ ####################################################################################################
1008
+ ### PART 16: Custom Component - AudioDropUpload
1009
+ ####################################################################################################
1010
+ class AudioDropUpload(gr.HTML):
1011
+ """
1012
+ Custom audio drop/click UI that proxies file into a hidden gr.Audio component.
1013
+ IMPORTANT:
1014
+ - Pass target_audio_elem_id = elem_id of your hidden gr.Audio.
1015
+ - The hidden gr.Audio must be type="filepath" (or whatever you need).
1016
+ """
1017
+ def __init__(self, target_audio_elem_id: str, value=None, **kwargs):
1018
+ uid = uuid.uuid4().hex[:8]
1019
+
1020
+ html_template = f"""
1021
+ <div class="aud-wrap" data-aud="{uid}">
1022
+ <div class="aud-drop" role="button" tabindex="0" aria-label="بارگذاری صدا">
1023
+ <div><strong>(اختیاری) فایل صوتی را اینجا بکشید و رها کنید</strong></div>
1024
+ <div class="aud-hint">…یا برای انتخاب کلیک کنید</div>
1025
+ </div>
1026
+ <div class="aud-row" aria-live="polite">
1027
+ <audio class="aud-player" controls></audio>
1028
+ <button class="aud-remove" type="button" aria-label="حذف صدا">
1029
+ <svg width="16" height="16" viewBox="0 0 24 24" aria-hidden="true" focusable="false">
1030
+ <path d="M18 6L6 18M6 6l12 12"
1031
+ stroke="currentColor"
1032
+ stroke-width="2.25"
1033
+ stroke-linecap="round"/>
1034
+ </svg>
1035
+ </button>
1036
+ </div>
1037
+ <div class="aud-filelabel"></div>
1038
+ </div>
1039
+ """
1040
+
1041
+ # JS:
1042
+ # - finds the hidden gr.Audio upload <input type=file> inside the component with elem_id=target_audio_elem_id
1043
+ # - sets the selected file onto it (DataTransfer) and dispatches change
1044
+ js_on_load = """
1045
+ (() => {{
1046
+ // Helper: access Gradio shadow DOM safely
1047
+ function grRoot() {{
1048
+ const ga = document.querySelector("gradio-app");
1049
+ return (ga && ga.shadowRoot) ? ga.shadowRoot : document;
1050
+ }}
1051
+ const root = grRoot();
1052
+ const wrap = element.querySelector(".aud-wrap");
1053
+ const drop = element.querySelector(".aud-drop");
1054
+ const row = element.querySelector(".aud-row");
1055
+ const player = element.querySelector(".aud-player");
1056
+ const removeBtn = element.querySelector(".aud-remove");
1057
+ const label = element.querySelector(".aud-filelabel");
1058
+ const TARGET_ID = "__TARGET_ID__";
1059
+ let currentUrl = null;
1060
+ function findHiddenAudioFileInput() {{
1061
+ const host = root.querySelector("#" + CSS.escape(TARGET_ID));
1062
+ if (!host) return null;
1063
+ // Gradio's Audio component contains an <input type=file> for upload.
1064
+ // This selector works in most Gradio 3/4 themes.
1065
+ const inp = host.querySelector('input[type="file"]');
1066
+ return inp;
1067
+ }}
1068
+ function showDrop() {{
1069
+ drop.style.display = "";
1070
+ row.style.display = "none";
1071
+ label.style.display = "none";
1072
+ label.textContent = "";
1073
+ }}
1074
+ function showPlayer(filename) {{
1075
+ drop.style.display = "none";
1076
+ row.style.display = "flex";
1077
+ if (filename) {{
1078
+ label.textContent = "فایل بارگذاری شده: " + filename;
1079
+ label.style.display = "block";
1080
+ }}
1081
+ }}
1082
+ function clearPreview() {{
1083
+ player.pause();
1084
+ player.removeAttribute("src");
1085
+ player.load();
1086
+ if (currentUrl) {{
1087
+ URL.revokeObjectURL(currentUrl);
1088
+ currentUrl = null;
1089
+ }}
1090
+ }}
1091
+ function clearHiddenGradioAudio() {{
1092
+ const fileInput = findHiddenAudioFileInput();
1093
+ if (!fileInput) return;
1094
+ // Clear file input (works by replacing its files with empty DataTransfer)
1095
+ fileInput.value = "";
1096
+ const dt = new DataTransfer();
1097
+ fileInput.files = dt.files;
1098
+ fileInput.dispatchEvent(new Event("input", {{ bubbles: true }}));
1099
+ fileInput.dispatchEvent(new Event("change", {{ bubbles: true }}));
1100
+ }}
1101
+ function clearAll() {
1102
+ clearPreview();
1103
+
1104
+ // Attempt DOM clear (still useful)
1105
+ clearHiddenGradioAudio();
1106
+
1107
+ // Tell Gradio/Python explicitly to clear backend state
1108
+ props.value = "__CLEAR__";
1109
+ trigger("change", props.value);
1110
+
1111
+ showDrop();
1112
+ }
1113
+
1114
+ function loadFileToPreview(file) {{
1115
+ if (!file) return;
1116
+ if (!file.type || !file.type.startsWith("audio/")) {{
1117
+ alert("لطفا یک فایل صوتی انتخاب کنید.");
1118
+ return;
1119
+ }}
1120
+ clearPreview();
1121
+ currentUrl = URL.createObjectURL(file);
1122
+ player.src = currentUrl;
1123
+ showPlayer(file.name);
1124
+
1125
+ }}
1126
+ function pushFileIntoHiddenGradioAudio(file) {
1127
+ const fileInput = findHiddenAudioFileInput();
1128
+ if (!fileInput) {
1129
+ console.warn("Could not find hidden gr.File input. Check elem_id:", TARGET_ID);
1130
+ return;
1131
+ }
1132
+
1133
+ // Hard reset (important for re-selecting same file)
1134
+ fileInput.value = "";
1135
+
1136
+ const dt = new DataTransfer();
1137
+ dt.items.add(file);
1138
+ fileInput.files = dt.files;
1139
+
1140
+ // Trigger Gradio listeners
1141
+ fileInput.dispatchEvent(new Event("input", {{ bubbles: true }}));
1142
+ fileInput.dispatchEvent(new Event("change", {{ bubbles: true }}));
1143
+ }
1144
+
1145
+ function handleFile(file) {{
1146
+ loadFileToPreview(file);
1147
+ pushFileIntoHiddenGradioAudio(file);
1148
+ }}
1149
+ // Click-to-browse uses a *local* ephemeral input (not Gradio’s),
1150
+ // then we forward to hidden gr.Audio.
1151
+ const localPicker = document.createElement("input");
1152
+ localPicker.type = "file";
1153
+ localPicker.accept = "audio/*";
1154
+ localPicker.style.display = "none";
1155
+ wrap.appendChild(localPicker);
1156
+ localPicker.addEventListener("change", () => {{
1157
+ const f = localPicker.files && localPicker.files[0];
1158
+ if (f) handleFile(f);
1159
+ localPicker.value = "";
1160
+ }});
1161
+ drop.addEventListener("click", () => localPicker.click());
1162
+ drop.addEventListener("keydown", (e) => {{
1163
+ if (e.key === "Enter" || e.key === " ") {{
1164
+ e.preventDefault();
1165
+ localPicker.click();
1166
+ }}
1167
+ }});
1168
+ removeBtn.addEventListener("click", clearAll);
1169
+ // Drag & drop
1170
+ ["dragenter","dragover","dragleave","drop"].forEach(evt => {{
1171
+ drop.addEventListener(evt, (e) => {{
1172
+ e.preventDefault();
1173
+ e.stopPropagation();
1174
+ }});
1175
+ }});
1176
+ drop.addEventListener("dragover", () => drop.classList.add("dragover"));
1177
+ drop.addEventListener("dragleave", () => drop.classList.remove("dragover"));
1178
+ drop.addEventListener("drop", (e) => {{
1179
+ drop.classList.remove("dragover");
1180
+ const f = e.dataTransfer.files && e.dataTransfer.files[0];
1181
+ if (f) handleFile(f);
1182
+ }});
1183
+ // init
1184
+ showDrop();
1185
+
1186
+ function setPreviewFromPath(path) {
1187
+ if (path === "__CLEAR__") path = null;
1188
+
1189
+ if (!path) {
1190
+ clearPreview();
1191
+ showDrop();
1192
+ return;
1193
+ }
1194
+
1195
+ // If path already looks like a URL, use it directly
1196
+ // otherwise serve it through Gradio's file route.
1197
+ let url = path;
1198
+ if (!/^https?:\/\//.test(path) && !path.startsWith("gradio_api/file=") && !path.startsWith("/file=")) {
1199
+ url = "gradio_api/file=" + path;
1200
+ }
1201
+
1202
+ clearPreview();
1203
+ player.src = url;
1204
+ showPlayer(path.split("/").pop());
1205
+ }
1206
+
1207
+ // ---- sync from Python (Examples / backend updates) ----
1208
+ let last = props.value;
1209
+ const syncFromProps = () => {
1210
+ const v = props.value;
1211
+
1212
+ if (v !== last) {
1213
+ last = v;
1214
+ if (!v || v === "__CLEAR__") setPreviewFromPath(null);
1215
+ else setPreviewFromPath(String(v));
1216
+ }
1217
+ requestAnimationFrame(syncFromProps);
1218
+ };
1219
+ requestAnimationFrame(syncFromProps);
1220
+
1221
+
1222
+ }})();
1223
+ """
1224
+ js_on_load = js_on_load.replace("__TARGET_ID__", target_audio_elem_id)
1225
+
1226
+ super().__init__(
1227
+ value=value,
1228
+ html_template=html_template,
1229
+ js_on_load=js_on_load,
1230
+ **kwargs
1231
+ )
1232
+
1233
+
1234
+ ####################################################################################################
1235
+ ### PART 17: Wrapper Functions (Resolution, Duration, Examples)
1236
+ ####################################################################################################
1237
+ def generate_video_example(first_frame, prompt, camera_lora, resolution, radioanimated_mode, input_video, input_audio, end_frame, progress=gr.Progress(track_tqdm=True)):
1238
+
1239
+ w, h = apply_resolution(resolution)
1240
+
1241
+ # We pass input_video (from example) to generate_video, though the logic inside generate_video
1242
+ # might ignore it since we removed the Motion Control block.
1243
+ # We keep the argument in this function signature to align with the examples_list columns.
1244
+
1245
+ # Default duration for examples
1246
+ duration_s = 5
1247
+
1248
+ with timer(f'generating with LoRA:{camera_lora} in {w}x{h}'):
1249
+ output_video = generate_video(
1250
+ first_frame,
1251
+ end_frame,
1252
+ prompt,
1253
+ 10,
1254
+ input_video,
1255
+ radioanimated_mode,
1256
+ True,
1257
+ 42,
1258
+ True,
1259
+ h,
1260
+ w,
1261
+ camera_lora,
1262
+ input_audio,
1263
+ progress
1264
+ )
1265
+ return output_video
1266
+
1267
+ def get_duration(
1268
+ first_frame,
1269
+ end_frame,
1270
+ prompt,
1271
+ duration,
1272
+ input_video,
1273
+ radioanimated_mode,
1274
+ enhance_prompt,
1275
+ seed,
1276
+ randomize_seed,
1277
+ height,
1278
+ width,
1279
+ camera_lora,
1280
+ audio_path,
1281
+ progress
1282
+ ):
1283
+ extra_time = 0
1284
+
1285
+ if audio_path is not None:
1286
+ extra_time += 10
1287
+
1288
+ if input_video is not None:
1289
+ extra_time += 60
1290
+
1291
+ if duration <= 3:
1292
+ return 60 + extra_time
1293
+ elif duration <= 5:
1294
+ return 80 + extra_time
1295
+ elif duration <= 10:
1296
+ return 120 + extra_time
1297
+ else:
1298
+ return 180 + extra_time
1299
+
1300
+
1301
+ ####################################################################################################
1302
+ ### PART 18: Main Generation Function (Modified)
1303
+ ####################################################################################################
1304
+ @spaces.GPU(duration=get_duration)
1305
+ def generate_video(
1306
+ first_frame,
1307
+ end_frame,
1308
+ prompt: str,
1309
+ duration: float,
1310
+ input_video = None,
1311
+ generation_mode = "تبدیل تصویر به ویدیو", # Default changed to Persian
1312
+ enhance_prompt: bool = True,
1313
+ seed: int = 42,
1314
+ randomize_seed: bool = True,
1315
+ height: int = DEFAULT_1_STAGE_HEIGHT,
1316
+ width: int = DEFAULT_1_STAGE_WIDTH,
1317
+ camera_lora: str = "No LoRA",
1318
+ audio_path = None,
1319
+ progress=gr.Progress(track_tqdm=True),
1320
+ ):
1321
+ """
1322
+ Generate a short cinematic video from a text prompt and optional input image using the LTX-2 distilled pipeline.
1323
+ """
1324
+
1325
+ # Removed the 15s warning check since 15s option is removed from UI
1326
+
1327
+ if audio_path is None:
1328
+ print(f'generating with duration:{duration} and LoRA:{camera_lora} in {width}x{height}')
1329
+ else:
1330
+ print(f'generating with duration:{duration} and audio in {width}x{height}')
1331
+
1332
+ # Randomize seed if checkbox is enabled
1333
+ current_seed = random.randint(0, MAX_SEED) if randomize_seed else int(seed)
1334
+
1335
+ # Calculate num_frames from duration (using fixed 24 fps)
1336
+ frame_rate = 24.0
1337
+ num_frames = int(duration * frame_rate) + 1 # +1 to ensure we meet the duration
1338
+ video_seconds = int(duration)
1339
+
1340
+ with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as tmpfile:
1341
+ output_path = tmpfile.name
1342
+
1343
+
1344
+ images = []
1345
+ videos = []
1346
+
1347
+ # Removed Motion Control block
1348
+
1349
+ if first_frame is not None:
1350
+ images = []
1351
+ images.append((first_frame, 0, 1.0))
1352
+
1353
+ # Updated logic for Persian string
1354
+ if generation_mode == "تکمیل فریم‌های میانی":
1355
+ if end_frame is not None:
1356
+ end_idx = max(0, num_frames - 1)
1357
+ images.append((end_frame, end_idx, 0.5))
1358
+
1359
+ embeddings, final_prompt, status = encode_prompt(
1360
+ prompt=prompt,
1361
+ enhance_prompt=enhance_prompt,
1362
+ input_image=first_frame,
1363
+ seed=current_seed,
1364
+ negative_prompt="",
1365
+ )
1366
+
1367
+ video_context = embeddings["video_context"].to("cuda", non_blocking=True)
1368
+ audio_context = embeddings["audio_context"].to("cuda", non_blocking=True)
1369
+ print("✓ Embeddings loaded successfully")
1370
+
1371
+
1372
+ # free prompt enhancer / encoder temps ASAP
1373
+ del embeddings, final_prompt, status
1374
+ torch.cuda.empty_cache()
1375
+
1376
+ # ✅ if user provided audio, use a neutral audio_context
1377
+ n_audio_context = None
1378
+
1379
+ if audio_path is not None:
1380
+ with torch.inference_mode():
1381
+ _, n_audio_context = encode_text_simple(text_encoder, "") # returns tensors on GPU already
1382
+ del audio_context
1383
+ audio_context = n_audio_context
1384
+
1385
+ if len(videos) == 0:
1386
+ camera_lora = "Static"
1387
+
1388
+ torch.cuda.empty_cache()
1389
+
1390
+ # Map dropdown name -> adapter index
1391
+ name_to_idx = {name: idx for name, idx in RUNTIME_LORA_CHOICES}
1392
+ selected_idx = name_to_idx.get(camera_lora, -1)
1393
+
1394
+ enable_only_lora(pipeline._transformer, selected_idx)
1395
+ torch.cuda.empty_cache()
1396
+
1397
+ # True video duration in seconds based on your rounding
1398
+ video_seconds = (num_frames - 1) / frame_rate
1399
+
1400
+ if audio_path is not None:
1401
+ input_waveform, input_waveform_sample_rate = match_audio_to_duration(
1402
+ audio_path=audio_path,
1403
+ target_seconds=video_seconds,
1404
+ target_sr=48000, # pick what your model expects; 48k is common for AV models
1405
+ to_mono=True, # set False if your model wants stereo
1406
+ pad_mode="silence", # or "repeat" if you prefer looping over silence
1407
+ device="cuda",
1408
+ )
1409
+ else:
1410
+ input_waveform = None
1411
+ input_waveform_sample_rate = None
1412
+
1413
+ with timer(f'generating with LoRA:{camera_lora} in {width}x{height}'):
1414
+ with torch.inference_mode():
1415
+ pipeline(
1416
+ prompt=prompt,
1417
+ output_path=str(output_path),
1418
+ seed=current_seed,
1419
+ height=height,
1420
+ width=width,
1421
+ num_frames=num_frames,
1422
+ frame_rate=frame_rate,
1423
+ images=images,
1424
+ video_conditioning=videos,
1425
+ tiling_config=TilingConfig.default(),
1426
+ video_context=video_context,
1427
+ audio_context=audio_context,
1428
+ input_waveform=input_waveform,
1429
+ input_waveform_sample_rate=input_waveform_sample_rate,
1430
+ )
1431
+ del video_context, audio_context
1432
+ torch.cuda.empty_cache()
1433
+ print("successful generation")
1434
+
1435
+ return str(output_path)
1436
+
1437
+
1438
+ def apply_resolution(resolution: str):
1439
+
1440
+ if resolution == "16:9":
1441
+ w, h = 768, 512
1442
+ elif resolution == "1:1":
1443
+ w, h = 512, 512
1444
+ elif resolution == "9:16":
1445
+ w, h = 512, 768
1446
+
1447
+ return int(w), int(h)
1448
+
1449
+ def apply_duration(duration: str):
1450
+ duration_s = int(duration[:-1])
1451
+ return duration_s
1452
+
1453
+ def on_mode_change(selected: str):
1454
+ is_motion = False # Removed Motion Control
1455
+ # Updated logic for Persian string
1456
+ is_interpolate = (selected == "تکمیل فریم‌های میانی")
1457
+
1458
+ return (gr.update(visible=is_motion), gr.update(visible=is_interpolate))
1459
+
1460
+
1461
+ ####################################################################################################
1462
+ ### PART 19: CSS Styles
1463
+ ####################################################################################################
1464
+ css = """
1465
+
1466
+ /* Make the row behave nicely */
1467
+ #controls-row {
1468
+ display: none !important;
1469
+ align-items: center;
1470
+ gap: 12px;
1471
+ flex-wrap: nowrap; /* or wrap if you prefer on small screens */
1472
+ }
1473
+
1474
+ /* Stop these components from stretching */
1475
+ #controls-row > * {
1476
+ flex: 0 0 auto !important;
1477
+ width: auto !important;
1478
+ min-width: 0 !important;
1479
+ }
1480
+
1481
+
1482
+ #col-container {
1483
+ margin: 0 auto;
1484
+ max-width: 1600px;
1485
+ }
1486
+ #modal-container {
1487
+ width: 100vw; /* Take full viewport width */
1488
+ height: 100vh; /* Take full viewport height (optional) */
1489
+ display: flex;
1490
+ justify-content: center; /* Center content horizontally */
1491
+ align-items: center; /* Center content vertically if desired */
1492
+ }
1493
+ #modal-content {
1494
+ width: 100%;
1495
+ max-width: 700px; /* Limit content width */
1496
+ margin: 0 auto;
1497
+ border-radius: 8px;
1498
+ padding: 1.5rem;
1499
+ }
1500
+ #step-column {
1501
+ padding: 10px;
1502
+ border-radius: 8px;
1503
+ box-shadow: var(--card-shadow);
1504
+ margin: 10px;
1505
+ }
1506
+ #col-showcase {
1507
+ margin: 0 auto;
1508
+ max-width: 1100px;
1509
+ }
1510
+ .button-gradient {
1511
+ background: linear-gradient(45deg, rgb(255, 65, 108), rgb(255, 75, 43), rgb(255, 155, 0), rgb(255, 65, 108)) 0% 0% / 400% 400%;
1512
+ border: none;
1513
+ padding: 14px 28px;
1514
+ font-size: 16px;
1515
+ font-weight: bold;
1516
+ color: white;
1517
+ border-radius: 10px;
1518
+ cursor: pointer;
1519
+ transition: 0.3s ease-in-out;
1520
+ animation: 2s linear 0s infinite normal none running gradientAnimation;
1521
+ box-shadow: rgba(255, 65, 108, 0.6) 0px 4px 10px;
1522
+ }
1523
+ .toggle-container {
1524
+ display: inline-flex;
1525
+ background-color: #ffd6ff; /* light pink background */
1526
+ border-radius: 9999px;
1527
+ padding: 4px;
1528
+ position: relative;
1529
+ width: fit-content;
1530
+ font-family: sans-serif;
1531
+ }
1532
+ .toggle-container input[type="radio"] {
1533
+ display: none;
1534
+ }
1535
+ .toggle-container label {
1536
+ position: relative;
1537
+ z-index: 2;
1538
+ flex: 1;
1539
+ text-align: center;
1540
+ font-weight: 700;
1541
+ color: #4b2ab5; /* dark purple text for unselected */
1542
+ padding: 6px 22px;
1543
+ border-radius: 9999px;
1544
+ cursor: pointer;
1545
+ transition: color 0.25s ease;
1546
+ }
1547
+ /* Moving highlight */
1548
+ .toggle-highlight {
1549
+ position: absolute;
1550
+ top: 4px;
1551
+ left: 4px;
1552
+ width: calc(50% - 4px);
1553
+ height: calc(100% - 8px);
1554
+ background-color: #4b2ab5; /* dark purple background */
1555
+ border-radius: 9999px;
1556
+ transition: transform 0.25s ease;
1557
+ z-index: 1;
1558
+ }
1559
+ /* When "True" is checked */
1560
+ #true:checked ~ label[for="true"] {
1561
+ color: #ffd6ff; /* light pink text */
1562
+ }
1563
+ /* When "False" is checked */
1564
+ #false:checked ~ label[for="false"] {
1565
+ color: #ffd6ff; /* light pink text */
1566
+ }
1567
+ /* Move highlight to right side when False is checked */
1568
+ #false:checked ~ .toggle-highlight {
1569
+ transform: translateX(100%);
1570
+ }
1571
+
1572
+ /* Center items inside that row */
1573
+ #mode-row{
1574
+ justify-content: center !important;
1575
+ align-items: center !important;
1576
+ }
1577
+
1578
+ /* Center the mode row contents */
1579
+ #mode-row {
1580
+ display: flex !important;
1581
+ justify-content: center !important;
1582
+ align-items: center !important;
1583
+ width: 100% !important;
1584
+ }
1585
+
1586
+ /* Stop Gradio from making children stretch */
1587
+ #mode-row > * {
1588
+ flex: 0 0 auto !important;
1589
+ width: auto !important;
1590
+ min-width: 0 !important;
1591
+ }
1592
+
1593
+ /* Specifically ensure the HTML component wrapper doesn't take full width */
1594
+ #mode-row .gr-html,
1595
+ #mode-row .gradio-html,
1596
+ #mode-row .prose,
1597
+ #mode-row .block {
1598
+ width: auto !important;
1599
+ flex: 0 0 auto !important;
1600
+ display: inline-block !important;
1601
+ }
1602
+
1603
+ /* Center the pill itself */
1604
+ #radioanimated_mode {
1605
+ display: inline-flex !important;
1606
+ justify-content: center !important;
1607
+ width: auto !important;
1608
+ }
1609
+
1610
+ """
1611
+
1612
+ css += """
1613
+ .cd-trigger-icon{
1614
+ color: rgba(255,255,255,0.9);
1615
+ display: inline-flex;
1616
+ align-items: center;
1617
+ justify-content: center;
1618
+ width: 18px;
1619
+ height: 18px;
1620
+ }
1621
+ .cd-trigger-icon svg {
1622
+ width: 18px;
1623
+ height: 18px;
1624
+ display: block;
1625
+ }
1626
+ """
1627
+
1628
+
1629
+ css += """
1630
+ /* ---- radioanimated ---- */
1631
+ .ra-wrap{
1632
+ width: fit-content;
1633
+ }
1634
+ .ra-inner{
1635
+ position: relative;
1636
+ display: inline-flex;
1637
+ align-items: center;
1638
+ gap: 0;
1639
+ padding: 6px;
1640
+ background: #0b0b0b;
1641
+ border-radius: 9999px;
1642
+ overflow: hidden;
1643
+ user-select: none;
1644
+ }
1645
+ .ra-input{
1646
+ display: none;
1647
+ }
1648
+ .ra-label{
1649
+ position: relative;
1650
+ z-index: 2;
1651
+ padding: 10px 18px;
1652
+ font-family: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Arial;
1653
+ font-size: 14px;
1654
+ font-weight: 600;
1655
+ color: rgba(255,255,255,0.7);
1656
+ cursor: pointer;
1657
+ transition: color 180ms ease;
1658
+ white-space: nowrap;
1659
+ }
1660
+ .ra-highlight{
1661
+ position: absolute;
1662
+ z-index: 1;
1663
+ top: 6px;
1664
+ left: 6px;
1665
+ height: calc(100% - 12px);
1666
+ border-radius: 9999px;
1667
+ background: #8bff97; /* green knob */
1668
+ transition: transform 200ms ease, width 200ms ease;
1669
+ }
1670
+ /* selected label becomes darker like your screenshot */
1671
+ .ra-input:checked + .ra-label{
1672
+ color: rgba(0,0,0,0.75);
1673
+ }
1674
+ """
1675
+
1676
+ css += """
1677
+ .cd-icn svg{
1678
+ width: 18px;
1679
+ height: 18px;
1680
+ display: block;
1681
+ }
1682
+ .cd-icn svg *{
1683
+ stroke: rgba(255,255,255,0.9);
1684
+ }
1685
+ """
1686
+
1687
+
1688
+ css += """
1689
+ /* --- prompt box --- */
1690
+ .ds-prompt{
1691
+ width: 100%;
1692
+ max-width: 720px;
1693
+ margin-top: 3px;
1694
+ }
1695
+
1696
+ .ds-textarea{
1697
+ width: 100%;
1698
+ box-sizing: border-box;
1699
+ background: #2b2b2b;
1700
+ color: rgba(255,255,255,0.9);
1701
+ border: 1px solid rgba(255,255,255,0.12);
1702
+ border-radius: 14px;
1703
+ padding: 14px 16px;
1704
+ outline: none;
1705
+ font-family: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Arial;
1706
+ font-size: 15px;
1707
+ line-height: 1.35;
1708
+ resize: none;
1709
+ min-height: 210px;
1710
+ max-height: 210px;
1711
+ overflow-y: auto;
1712
+
1713
+ /* IMPORTANT: space for the footer controls */
1714
+ padding-bottom: 72px;
1715
+ }
1716
+
1717
+
1718
+ .ds-card{
1719
+ width: 100%;
1720
+ max-width: 720px;
1721
+ margin: 0 auto;
1722
+ }
1723
+ .ds-top{
1724
+ position: relative;
1725
+ }
1726
+
1727
+ /* Make room for footer inside textarea */
1728
+ .ds-textarea{
1729
+ padding-bottom: 72px;
1730
+ }
1731
+
1732
+ /* Footer positioning */
1733
+ .ds-footer{
1734
+ position: absolute;
1735
+ right: 12px;
1736
+ bottom: 10px;
1737
+ display: flex;
1738
+ gap: 8px;
1739
+ align-items: center;
1740
+ justify-content: flex-end;
1741
+ z-index: 3;
1742
+ }
1743
+
1744
+ /* Smaller pill buttons inside footer */
1745
+ .ds-footer .cd-trigger{
1746
+ min-height: 32px;
1747
+ padding: 6px 10px;
1748
+ font-size: 12px;
1749
+ gap: 6px;
1750
+ border-radius: 9999px;
1751
+ }
1752
+ .ds-footer .cd-trigger-icon,
1753
+ .ds-footer .cd-icn{
1754
+ width: 14px;
1755
+ height: 14px;
1756
+ }
1757
+ .ds-footer .cd-trigger-icon svg,
1758
+ .ds-footer .cd-icn svg{
1759
+ width: 14px;
1760
+ height: 14px;
1761
+ }
1762
+ .ds-footer .cd-caret{
1763
+ font-size: 11px;
1764
+ }
1765
+
1766
+ /* Bottom safe area bar (optional but looks nicer) */
1767
+ .ds-top::after{
1768
+ content: "";
1769
+ position: absolute;
1770
+ left: 1px;
1771
+ right: 1px;
1772
+ bottom: 1px;
1773
+ height: 56px;
1774
+ background: #2b2b2b;
1775
+ border-bottom-left-radius: 13px;
1776
+ border-bottom-right-radius: 13px;
1777
+ pointer-events: none;
1778
+ z-index: 2;
1779
+ }
1780
+
1781
+ """
1782
+
1783
+ css += """
1784
+ /* ---- camera dropdown ---- */
1785
+
1786
+ /* 1) Fix overlap: make the Gradio HTML block shrink-to-fit when it contains a CameraDropdown.
1787
+ Gradio uses .gr-html for HTML components in most versions; older themes sometimes use .gradio-html.
1788
+ This keeps your big header HTML unaffected because it doesn't contain .cd-wrap.
1789
+ */
1790
+
1791
+ /* 2) Actual dropdown layout */
1792
+ .cd-wrap{
1793
+ position: relative;
1794
+ display: inline-block;
1795
+ }
1796
+
1797
+ /* 3) Match RadioAnimated pill size/feel */
1798
+ .cd-trigger{
1799
+ margin-top: 2px;
1800
+ display: inline-flex;
1801
+ align-items: center;
1802
+ justify-content: center;
1803
+ gap: 10px;
1804
+
1805
+ border: none;
1806
+
1807
+ box-sizing: border-box;
1808
+ padding: 10px 18px;
1809
+ min-height: 52px;
1810
+ line-height: 1.2;
1811
+
1812
+ border-radius: 9999px;
1813
+ background: #0b0b0b;
1814
+
1815
+ font-family: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Arial;
1816
+ font-size: 14px;
1817
+
1818
+ /* ✅ match .ra-label exactly */
1819
+ color: rgba(255,255,255,0.7) !important;
1820
+ font-weight: 600 !important;
1821
+
1822
+ cursor: pointer;
1823
+ user-select: none;
1824
+ white-space: nowrap;
1825
+ }
1826
+
1827
+ /* Ensure inner spans match too */
1828
+ .cd-trigger .cd-trigger-text,
1829
+ .cd-trigger .cd-caret{
1830
+ color: rgba(255,255,255,0.7) !important;
1831
+ }
1832
+
1833
+ /* keep caret styling */
1834
+ .cd-caret{
1835
+ opacity: 0.8;
1836
+ font-weight: 900;
1837
+ }
1838
+
1839
+ /* 4) Ensure menu overlays neighbors and isn't clipped */
1840
+ /* Move dropdown a tiny bit up (closer to the trigger) */
1841
+ .cd-menu{
1842
+ position: absolute;
1843
+ top: calc(100% + 4px); /* was +10px */
1844
+ left: 0;
1845
+
1846
+ min-width: 240px;
1847
+ background: #2b2b2b;
1848
+ border: 1px solid rgba(255,255,255,0.14);
1849
+ border-radius: 14px;
1850
+ box-shadow: 0 18px 40px rgba(0,0,0,0.35);
1851
+ padding: 10px;
1852
+
1853
+ opacity: 0;
1854
+ transform: translateY(-6px);
1855
+ pointer-events: none;
1856
+ transition: opacity 160ms ease, transform 160ms ease;
1857
+
1858
+ z-index: 9999;
1859
+ }
1860
+
1861
+ .cd-title{
1862
+ font-size: 12px;
1863
+ font-weight: 600;
1864
+ text-transform: uppercase;
1865
+ letter-spacing: 0.04em;
1866
+
1867
+ color: rgba(255,255,255,0.45); /* 👈 muted grey */
1868
+ margin-bottom: 6px;
1869
+ padding: 0 6px;
1870
+ pointer-events: none; /* title is non-interactive */
1871
+ }
1872
+
1873
+
1874
+ .cd-menu.open{
1875
+ opacity: 1;
1876
+ transform: translateY(0);
1877
+ pointer-events: auto;
1878
+ }
1879
+
1880
+ .cd-items{
1881
+ display: flex;
1882
+ flex-direction: column;
1883
+ gap: 0px; /* tighter, more like a native menu */
1884
+ }
1885
+
1886
+ /* Items: NO "boxed" buttons by default */
1887
+ .cd-item{
1888
+ width: 100%;
1889
+ text-align: left;
1890
+ border: none;
1891
+ background: transparent; /* ✅ removes box look */
1892
+ color: rgba(255,255,255,0.92);
1893
+ padding: 8px 34px 8px 12px; /* right padding leaves room for tick */
1894
+ border-radius: 10px; /* only matters on hover */
1895
+ cursor: pointer;
1896
+
1897
+ font-size: 14px;
1898
+ font-weight: 700;
1899
+
1900
+ position: relative;
1901
+ transition: background 120ms ease;
1902
+ }
1903
+
1904
+ /* “Box effect” only on hover (not always) */
1905
+ .cd-item:hover{
1906
+ background: rgba(255,255,255,0.08);
1907
+ }
1908
+
1909
+ /* Tick on the right ONLY on hover */
1910
+ .cd-item::after{
1911
+ content: "✓";
1912
+ position: absolute;
1913
+ right: 12px;
1914
+ top: 50%;
1915
+ transform: translateY(-50%);
1916
+ opacity: 0; /* hidden by default */
1917
+ transition: opacity 120ms ease;
1918
+ color: rgba(255,255,255,0.9);
1919
+ font-weight: 900;
1920
+ }
1921
+
1922
+ /* show tick ONLY for selected item */
1923
+ .cd-item[data-selected="true"]::after{
1924
+ opacity: 1;
1925
+ }
1926
+
1927
+ /* keep hover box effect, but no tick change */
1928
+ .cd-item:hover{
1929
+ background: rgba(255,255,255,0.08);
1930
+ }
1931
+
1932
+
1933
+ /* Kill any old “selected” styling just in case */
1934
+ .cd-item.selected{
1935
+ background: transparent !important;
1936
+ border: none !important;
1937
+ }
1938
+
1939
+
1940
+ """
1941
+
1942
+ css += """
1943
+ /* icons in dropdown items */
1944
+ .cd-item{
1945
+ display: flex;
1946
+ align-items: center;
1947
+ gap: 10px;
1948
+ }
1949
+ .cd-icn{
1950
+ display: inline-flex;
1951
+ align-items: center;
1952
+ justify-content: center;
1953
+ width: 18px;
1954
+ height: 18px;
1955
+ flex: 0 0 18px;
1956
+ }
1957
+ .cd-label{
1958
+ flex: 1;
1959
+ }
1960
+
1961
+ /* =========================
1962
+ FIX: prompt border + scrollbar bleed
1963
+ ========================= */
1964
+
1965
+ /* Put the border + background on the wrapper, not the textarea */
1966
+ .ds-top{
1967
+ position: relative;
1968
+ background: #2b2b2b;
1969
+ border: 1px solid rgba(255,255,255,0.12);
1970
+ border-radius: 14px;
1971
+ overflow: hidden; /* ensures the footer bar is clipped to rounded corners */
1972
+ }
1973
+
1974
+ /* Make textarea "transparent" so wrapper owns the border/background */
1975
+ .ds-textarea{
1976
+ background: transparent !important;
1977
+ border: none !important;
1978
+ border-radius: 0 !important; /* wrapper handles radius */
1979
+ outline: none;
1980
+
1981
+ /* keep your spacing */
1982
+ padding: 14px 16px;
1983
+ padding-bottom: 72px; /* room for footer */
1984
+ width: 100%;
1985
+ box-sizing: border-box;
1986
+
1987
+ /* keep scroll behavior */
1988
+ overflow-y: auto;
1989
+
1990
+ /* prevent scrollbar bleed by hiding native scrollbar */
1991
+ scrollbar-width: none; /* Firefox */
1992
+ }
1993
+ .ds-textarea::-webkit-scrollbar{ /* Chrome/Safari */
1994
+ width: 0;
1995
+ height: 0;
1996
+ }
1997
+
1998
+ /* Safe-area bar: now it matches perfectly because it's inside the same bordered wrapper */
1999
+ .ds-top::after{
2000
+ content: "";
2001
+ position: absolute;
2002
+ left: 0;
2003
+ right: 0;
2004
+ bottom: 0;
2005
+ height: 56px;
2006
+ background: #2b2b2b;
2007
+ pointer-events: none;
2008
+ z-index: 2;
2009
+ }
2010
+
2011
+ /* Footer above the bar */
2012
+ .ds-footer{
2013
+ position: absolute;
2014
+ right: 12px;
2015
+ bottom: 10px;
2016
+ display: flex;
2017
+ gap: 8px;
2018
+ align-items: center;
2019
+ justify-content: flex-end;
2020
+ z-index: 3;
2021
+ }
2022
+
2023
+ /* Ensure textarea content sits below overlays */
2024
+ .ds-textarea{
2025
+ position: relative;
2026
+ z-index: 1;
2027
+ }
2028
+
2029
+ /* ===== FIX dropdown menu being clipped/behind ===== */
2030
+
2031
+ /* Let the dropdown menu escape the prompt wrapper */
2032
+ .ds-top{
2033
+ overflow: visible !important; /* IMPORTANT: do not clip the menu */
2034
+ }
2035
+
2036
+ /* Keep the rounded "safe area" look without clipping the menu */
2037
+ .ds-top::after{
2038
+ left: 0 !important;
2039
+ right: 0 !important;
2040
+ bottom: 0 !important;
2041
+ border-bottom-left-radius: 14px !important;
2042
+ border-bottom-right-radius: 14px !important;
2043
+ }
2044
+
2045
+ /* Ensure the footer stays above the safe-area bar */
2046
+ .ds-footer{
2047
+ z-index: 20 !important;
2048
+ }
2049
+
2050
+ /* Make sure the opened menu is above EVERYTHING */
2051
+ .ds-footer .cd-menu{
2052
+ z-index: 999999 !important;
2053
+ }
2054
+
2055
+ /* Sometimes Gradio/columns/cards create stacking contexts;
2056
+ force the whole prompt card above nearby panels */
2057
+ .ds-card{
2058
+ position: relative;
2059
+ z-index: 50;
2060
+ }
2061
+
2062
+ /* --- Fix focus highlight shape (make it match rounded container) --- */
2063
+
2064
+ /* Kill any theme focus ring on the textarea itself */
2065
+ .ds-textarea:focus,
2066
+ .ds-textarea:focus-visible{
2067
+ outline: none !important;
2068
+ box-shadow: none !important;
2069
+ }
2070
+
2071
+ /* Optional: if some themes apply it even when not focused */
2072
+ .ds-textarea{
2073
+ outline: none !important;
2074
+ }
2075
+
2076
+ /* Apply the focus ring to the rounded wrapper instead */
2077
+ .ds-top:focus-within{
2078
+ border-color: rgba(255,255,255,0.22) !important;
2079
+ box-shadow: 0 0 0 3px rgba(255,255,255,0.06) !important;
2080
+ border-radius: 14px !important;
2081
+ }
2082
+
2083
+ /* If you see any tiny square corners, ensure the wrapper clips its own shadow properly */
2084
+ .ds-top{
2085
+ border-radius: 14px !important;
2086
+ }
2087
+
2088
+ /* =========================
2089
+ CameraDropdown: force readable menu text in BOTH themes
2090
+ ========================= */
2091
+
2092
+ /* Menu surface */
2093
+ .cd-menu{
2094
+ background: #2b2b2b !important;
2095
+ border: 1px solid rgba(255,255,255,0.14) !important;
2096
+ }
2097
+
2098
+ /* Title */
2099
+ .cd-title{
2100
+ color: rgba(255,255,255,0.55) !important;
2101
+ }
2102
+
2103
+ /* Items + all descendants (fixes spans / inherited theme colors) */
2104
+ .cd-item,
2105
+ .cd-item *{
2106
+ color: rgba(255,255,255,0.92) !important;
2107
+ }
2108
+
2109
+ /* Hover state */
2110
+ .cd-item:hover{
2111
+ background: rgba(255,255,255,0.10) !important;
2112
+ }
2113
+
2114
+ /* Checkmark */
2115
+ .cd-item::after{
2116
+ color: rgba(255,255,255,0.92) !important;
2117
+ }
2118
+
2119
+ /* (Optional) make sure the trigger stays readable too */
2120
+ .cd-trigger,
2121
+ .cd-trigger *{
2122
+ color: rgba(255,255,255,0.75) !important;
2123
+ }
2124
+
2125
+ /* ---- preset gallery ---- */
2126
+ .pg-wrap{
2127
+ width: 100%;
2128
+ max-width: 1100px;
2129
+ margin: 18px auto 0 auto;
2130
+ }
2131
+ .pg-title{
2132
+ text-align: center;
2133
+ margin-bottom: 14px;
2134
+ }
2135
+ .pg-h1{
2136
+ font-size: 34px;
2137
+ font-weight: 800;
2138
+ line-height: 1.1;
2139
+
2140
+ /* ✅ theme-aware */
2141
+ color: var(--body-text-color);
2142
+ }
2143
+ .pg-h2{
2144
+ font-size: 14px;
2145
+ font-weight: 600;
2146
+ color: var(--body-text-color-subdued);
2147
+ margin-top: 6px;
2148
+ }
2149
+
2150
+ .pg-grid{
2151
+ display: grid;
2152
+ grid-template-columns: repeat(3, minmax(0, 1fr)); /* 3 per row */
2153
+ gap: 18px;
2154
+ }
2155
+
2156
+ .pg-card{
2157
+ border: none;
2158
+ background: transparent;
2159
+ padding: 0;
2160
+ cursor: pointer;
2161
+ border-radius: 12px;
2162
+ overflow: hidden;
2163
+ position: relative;
2164
+ transform: translateZ(0);
2165
+ }
2166
+
2167
+ .pg-img{
2168
+ width: 100%;
2169
+ height: 220px; /* adjust to match your look */
2170
+ object-fit: cover;
2171
+ display: block;
2172
+ border-radius: 12px;
2173
+ transition: transform 160ms ease, filter 160ms ease, opacity 160ms ease;
2174
+ }
2175
+
2176
+ /* hover: slight zoom on hovered card */
2177
+ .pg-card:hover .pg-img{
2178
+ transform: scale(1.02);
2179
+ }
2180
+
2181
+ /* dim others while hovering */
2182
+ .pg-card[data-dim="true"] .pg-img{
2183
+ opacity: 0.35;
2184
+ filter: saturate(0.9);
2185
+ }
2186
+
2187
+ /* keep hovered/active crisp */
2188
+ .pg-card[data-active="true"] .pg-img{
2189
+ opacity: 1.0;
2190
+ filter: none;
2191
+ }
2192
+
2193
+
2194
+ """
2195
+
2196
+
2197
+ css += """
2198
+ /* ---- AudioDropUpload ---- */
2199
+ .aud-wrap{
2200
+ width: 100%;
2201
+ max-width: 720px;
2202
+ }
2203
+ .aud-drop{
2204
+ border: 2px dashed var(--body-text-color-subdued);
2205
+ border-radius: 16px;
2206
+ padding: 18px;
2207
+ text-align: center;
2208
+ cursor: pointer;
2209
+ user-select: none;
2210
+ color: var(--body-text-color);
2211
+ background: var(--block-background-fill);
2212
+ }
2213
+ .aud-drop.dragover{
2214
+ border-color: rgba(255,255,255,0.35);
2215
+ background: rgba(255,255,255,0.06);
2216
+ }
2217
+ .aud-hint{
2218
+ color: var(--body-text-color-subdued);
2219
+ font-size: 0.95rem;
2220
+ margin-top: 6px;
2221
+ }
2222
+ /* pill row like your other controls */
2223
+ .aud-row{
2224
+ display: none;
2225
+ align-items: center;
2226
+ gap: 10px;
2227
+ background: #0b0b0b;
2228
+ border-radius: 9999px;
2229
+ padding: 8px 10px;
2230
+ }
2231
+ .aud-player{
2232
+ flex: 1;
2233
+ width: 100%;
2234
+ height: 34px;
2235
+ border-radius: 9999px;
2236
+ }
2237
+ .aud-remove{
2238
+ appearance: none;
2239
+ border: none;
2240
+ background: transparent;
2241
+ color: rgba(255,255,255);
2242
+ cursor: pointer;
2243
+ width: 36px;
2244
+ height: 36px;
2245
+ border-radius: 9999px;
2246
+ display: inline-flex;
2247
+ align-items: center;
2248
+ justify-content: center;
2249
+ padding: 0;
2250
+ transition: background 120ms ease, color 120ms ease, opacity 120ms ease;
2251
+ opacity: 0.9;
2252
+ flex: 0 0 auto;
2253
+ }
2254
+ .aud-remove:hover{
2255
+ background: rgba(255,255,255,0.08);
2256
+ color: rgb(255,255,255);
2257
+ opacity: 1;
2258
+ }
2259
+ .aud-filelabel{
2260
+ margin: 10px 6px 0;
2261
+ color: var(--body-text-color-subdued);
2262
+ font-size: 0.95rem;
2263
+ display: none;
2264
+ }
2265
+ #audio_input_hidden { display: none !important; }
2266
+ """
2267
+
2268
+ # Hiding Gradio Footer, Branding and Settings
2269
+ css += """
2270
+ footer {
2271
+ display: none !important;
2272
+ }
2273
+ .gradio-container footer {
2274
+ display: none !important;
2275
+ }
2276
+ div.footer {
2277
+ display: none !important;
2278
+ }
2279
+ .flagging {
2280
+ display: none !important;
2281
+ }
2282
+ /* Hide the 'Use via API' link if visible */
2283
+ .api-logo, .built-with {
2284
+ display: none !important;
2285
+ }
2286
+ """
2287
+
2288
+
2289
+ def apply_example(idx: str):
2290
+ idx = int(idx)
2291
+
2292
+ # Read the example row from your list
2293
+ img, prompt_txt, cam, res, mode, vid, aud, end_img = examples_list[idx]
2294
+
2295
+ img_path = img if img else None
2296
+ vid_path = vid if vid else None
2297
+ aud_path = aud if aud else None
2298
+
2299
+ input_image_update = img_path
2300
+ prompt_update = prompt_txt
2301
+ camera_update = cam
2302
+ resolution_update = res
2303
+ mode_update = mode
2304
+ video_update = gr.update(value=vid_path, visible=(mode == "Motion Control"))
2305
+ audio_update = aud_path
2306
+ end_image = end_img
2307
+
2308
+ return (
2309
+ input_image_update,
2310
+ prompt_update,
2311
+ camera_update,
2312
+ resolution_update,
2313
+ mode_update,
2314
+ video_update,
2315
+ audio_update,
2316
+ audio_update,
2317
+ end_image,
2318
+ )
2319
+
2320
+
2321
+ ####################################################################################################
2322
+ ### PART 20: Gradio UI Layout & Launch
2323
+ ####################################################################################################
2324
+ def apply_example(idx: str):
2325
+ idx = int(idx)
2326
+
2327
+ # Read the example row from your list
2328
+ img, prompt_txt, cam, res, mode, vid, aud, end_img = examples_list[idx]
2329
+
2330
+ img_path = img if img else None
2331
+ vid_path = vid if vid else None
2332
+ aud_path = aud if aud else None
2333
+
2334
+ input_image_update = img_path
2335
+ prompt_update = prompt_txt
2336
+ camera_update = cam
2337
+ resolution_update = res
2338
+ mode_update = mode
2339
+ video_update = gr.update(value=vid_path, visible=(mode == "Motion Control"))
2340
+ audio_update = aud_path
2341
+ end_image = end_img
2342
+
2343
+ # Clear the output video when loading a new example
2344
+ output_video_update = gr.update(value=None)
2345
+
2346
+ return (
2347
+ input_image_update,
2348
+ prompt_update,
2349
+ camera_update,
2350
+ resolution_update,
2351
+ mode_update,
2352
+ video_update,
2353
+ audio_update,
2354
+ audio_update,
2355
+ end_image,
2356
+ output_video_update
2357
+ )
2358
+
2359
+
2360
+ with gr.Blocks(title="LTX-2 Video Distilled 🎥🔈") as demo:
2361
+
2362
+ # Updated Header to Persian
2363
+ gr.HTML(
2364
+ """
2365
+ <div style="text-align: center; padding: 20px;">
2366
+ <h1 style="font-size: 28px; font-weight: bold; margin-bottom: 10px; color: var(--body-text-color);">
2367
+ ساخت ویدیو با هوش مصنوعی
2368
+ </h1>
2369
+ <p style="font-size: 18px; color: var(--body-text-color-subdued); margin: 0;">
2370
+ با پشتیبانی از صدا و دو تصویر
2371
+ </p>
2372
+ </div>
2373
+ """
2374
+ )
2375
+
2376
+ with gr.Column(elem_id="col-container"):
2377
+ with gr.Row(elem_id="mode-row"):
2378
+ # Updated choices to Persian
2379
+ radioanimated_mode = RadioAnimated(
2380
+ choices=["تبدیل تصویر به ویدیو", "تکمیل فریم‌های میانی"],
2381
+ value="تبدیل تصویر به ویدیو",
2382
+ elem_id="radioanimated_mode"
2383
+ )
2384
+ with gr.Row():
2385
+ with gr.Column(elem_id="step-column"):
2386
+
2387
+ with gr.Row():
2388
+
2389
+ first_frame = gr.Image(
2390
+ label="تصویر اول (اختیاری)",
2391
+ type="filepath",
2392
+ height=256
2393
+ )
2394
+
2395
+ end_frame = gr.Image(
2396
+ label="تصویر آخر (اختیاری)",
2397
+ type="filepath",
2398
+ height=256,
2399
+ visible=False,
2400
+ )
2401
+
2402
+ # input_video is defined but hidden
2403
+ input_video = gr.Video(
2404
+ label="Motion Reference Video",
2405
+ height=256,
2406
+ visible=False,
2407
+ )
2408
+
2409
+ relocate = gr.HTML(
2410
+ value="",
2411
+ html_template="<div></div>",
2412
+ js_on_load=r"""
2413
+ (() => {
2414
+ function moveIntoFooter() {
2415
+ const promptRoot = document.querySelector("#prompt_ui");
2416
+ if (!promptRoot) return false;
2417
+
2418
+ const footer = promptRoot.querySelector(".ds-footer");
2419
+ if (!footer) return false;
2420
+
2421
+ const dur = document.querySelector("#duration_ui .cd-wrap");
2422
+ const res = document.querySelector("#resolution_ui .cd-wrap");
2423
+ const cam = document.querySelector("#camera_ui .cd-wrap");
2424
+
2425
+ if (!dur || !res || !cam) return false;
2426
+
2427
+ footer.appendChild(dur);
2428
+ footer.appendChild(res);
2429
+ footer.appendChild(cam);
2430
+
2431
+ return true;
2432
+ }
2433
+
2434
+ const tick = () => {
2435
+ if (!moveIntoFooter()) requestAnimationFrame(tick);
2436
+ };
2437
+ requestAnimationFrame(tick);
2438
+ })();
2439
+ """
2440
+ )
2441
+
2442
+
2443
+ prompt_ui = PromptBox(
2444
+ value="این تصویر را با حرکت سینمایی و انیمیشن روان زنده کن",
2445
+ elem_id="prompt_ui",
2446
+ )
2447
+
2448
+ # Hidden real audio input (backend value)
2449
+ audio_input = gr.File(
2450
+ label="Audio (Optional)",
2451
+ file_types=["audio"],
2452
+ type="filepath",
2453
+ elem_id="audio_input_hidden",
2454
+ )
2455
+
2456
+ # Custom UI that feeds the hidden gr.Audio above
2457
+ audio_ui = AudioDropUpload(
2458
+ target_audio_elem_id="audio_input_hidden",
2459
+ elem_id="audio_ui",
2460
+ )
2461
+
2462
+ prompt = gr.Textbox(
2463
+ label="Prompt",
2464
+ value="این تصویر را با حرکت سینمایی و انیمیشن روان زنده کن",
2465
+ lines=3,
2466
+ max_lines=3,
2467
+ placeholder="حرکت و انیمیشن مورد نظر خود را توصیف کنید...",
2468
+ visible=False
2469
+ )
2470
+
2471
+ enhance_prompt = gr.Checkbox(
2472
+ label="Enhance Prompt",
2473
+ value=True,
2474
+ visible=False
2475
+ )
2476
+
2477
+ with gr.Accordion("تنظیمات پیشرفته", open=False, visible=False):
2478
+ seed = gr.Slider(
2479
+ label="سید (Seed)",
2480
+ minimum=0,
2481
+ maximum=MAX_SEED,
2482
+ value=DEFAULT_SEED,
2483
+ step=1
2484
+ )
2485
+
2486
+ randomize_seed = gr.Checkbox(label="استفاده از سید تصادفی", value=True)
2487
+
2488
+
2489
+ with gr.Column(elem_id="step-column"):
2490
+ output_video = gr.Video(label="ویدیوی ساخته شده", autoplay=True, height=512)
2491
+
2492
+ with gr.Row(elem_id="controls-row"):
2493
+
2494
+ duration_ui = CameraDropdown(
2495
+ choices=["3s", "5s", "10s"],
2496
+ value="5s",
2497
+ title="مدت زمان ویدیو",
2498
+ elem_id="duration_ui"
2499
+ )
2500
+
2501
+ duration = gr.Slider(
2502
+ label="Duration (seconds)",
2503
+ minimum=1.0,
2504
+ maximum=10.0,
2505
+ value=5.0,
2506
+ step=0.1,
2507
+ visible=False
2508
+ )
2509
+
2510
+ ICON_16_9 = """<svg viewBox="0 0 24 24" fill="none" aria-hidden="true">
2511
+ <rect x="3" y="7" width="18" height="10" rx="2" stroke="currentColor" stroke-width="2"/>
2512
+ </svg>"""
2513
+
2514
+ ICON_1_1 = """<svg viewBox="0 0 24 24" fill="none" aria-hidden="true">
2515
+ <rect x="6" y="6" width="12" height="12" rx="2" stroke="currentColor" stroke-width="2"/>
2516
+ </svg>"""
2517
+
2518
+ ICON_9_16 = """<svg viewBox="0 0 24 24" fill="none" aria-hidden="true">
2519
+ <rect x="7" y="3" width="10" height="18" rx="2" stroke="currentColor" stroke-width="2"/>
2520
+ </svg>"""
2521
+
2522
+
2523
+ resolution_ui = CameraDropdown(
2524
+ choices=[
2525
+ {"label": "16:9", "value": "16:9", "icon": ICON_16_9},
2526
+ {"label": "1:1", "value": "1:1", "icon": ICON_1_1},
2527
+ {"label": "9:16", "value": "9:16", "icon": ICON_9_16},
2528
+ ],
2529
+ value="16:9",
2530
+ title="ابعاد تصویر",
2531
+ elem_id="resolution_ui"
2532
+ )
2533
+
2534
+
2535
+ width = gr.Number(label="Width", value=DEFAULT_1_STAGE_WIDTH, precision=0, visible=False)
2536
+ height = gr.Number(label="Height", value=DEFAULT_1_STAGE_HEIGHT, precision=0, visible=False)
2537
+
2538
+ camera_ui = CameraDropdown(
2539
+ choices=[name for name, _ in VISIBLE_RUNTIME_LORA_CHOICES],
2540
+ value="No LoRA",
2541
+ title="افکت دوربین (LoRA)",
2542
+ elem_id="camera_ui",
2543
+ )
2544
+
2545
+ # Hidden real dropdown (backend value)
2546
+ camera_lora = gr.Dropdown(
2547
+ label="Camera Control LoRA",
2548
+ choices=[name for name, _ in VISIBLE_RUNTIME_LORA_CHOICES],
2549
+ value="No LoRA",
2550
+ visible=False
2551
+ )
2552
+
2553
+ generate_btn = gr.Button("🤩 ساخت ویدیو", variant="primary", elem_classes="button-gradient")
2554
+
2555
+
2556
+ camera_ui.change(
2557
+ fn=lambda x: x,
2558
+ inputs=camera_ui,
2559
+ outputs=camera_lora,
2560
+ api_visibility="private"
2561
+ )
2562
+
2563
+ radioanimated_mode.change(
2564
+ fn=on_mode_change,
2565
+ inputs=radioanimated_mode,
2566
+ outputs=[input_video, end_frame],
2567
+ api_visibility="private",
2568
+ )
2569
+
2570
+
2571
+ duration_ui.change(
2572
+ fn=apply_duration,
2573
+ inputs=duration_ui,
2574
+ outputs=[duration],
2575
+ api_visibility="private"
2576
+ )
2577
+ resolution_ui.change(
2578
+ fn=apply_resolution,
2579
+ inputs=resolution_ui,
2580
+ outputs=[width, height],
2581
+ api_visibility="private"
2582
+ )
2583
+ prompt_ui.change(
2584
+ fn=lambda x: x,
2585
+ inputs=prompt_ui,
2586
+ outputs=prompt,
2587
+ api_visibility="private"
2588
+ )
2589
+
2590
+
2591
+ generate_btn.click(
2592
+ fn=generate_video,
2593
+ inputs=[
2594
+ first_frame,
2595
+ end_frame,
2596
+ prompt,
2597
+ duration,
2598
+ input_video,
2599
+ radioanimated_mode,
2600
+ enhance_prompt,
2601
+ seed,
2602
+ randomize_seed,
2603
+ height,
2604
+ width,
2605
+ camera_lora,
2606
+ audio_input
2607
+ ],
2608
+ outputs=[output_video]
2609
+ )
2610
+
2611
+ # Updated Examples to use Persian modes
2612
+ examples_list = [
2613
+ [
2614
+ "examples/supergirl-2.png",
2615
+ "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",
2616
+ "Static",
2617
+ "16:9",
2618
+ "تبدیل تصویر به ویدیو",
2619
+ None,
2620
+ "examples/supergirl.m4a",
2621
+ None,
2622
+ ],
2623
+ [
2624
+ "examples/frame3.png",
2625
+ "a woman in a white dress standing in a supermarket, looking at a stack of pomegranates, she picks one and takes a bite, the camera zooms in to a close up of the pomegranate seeds. A calm music is playing in the supermarket and you can hear her taking a bite.",
2626
+ "Zoom In",
2627
+ "16:9",
2628
+ "تکمیل فریم‌های میانی",
2629
+ None,
2630
+ None,
2631
+ "examples/frame4.png",
2632
+ ],
2633
+ [
2634
+ "examples/supergirl.png",
2635
+ "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",
2636
+ "No LoRA",
2637
+ "16:9",
2638
+ "تبدیل تصویر به ویدیو",
2639
+ None,
2640
+ None,
2641
+ None,
2642
+ ],
2643
+ [
2644
+ "examples/highland.png",
2645
+ "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.",
2646
+ "No LoRA",
2647
+ "16:9",
2648
+ "تبدیل تصویر به ویدیو",
2649
+ None,
2650
+ None,
2651
+ None,
2652
+ ],
2653
+ [
2654
+ "examples/wednesday.png",
2655
+ "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.",
2656
+ "Zoom Out",
2657
+ "16:9",
2658
+ "تبدیل تصویر به ویدیو",
2659
+ None,
2660
+ None,
2661
+ None,
2662
+ ],
2663
+ [
2664
+ "examples/astronaut.png",
2665
+ "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.",
2666
+ "Static",
2667
+ "1:1",
2668
+ "تبدیل تصویر به ویدیو",
2669
+ None,
2670
+ None,
2671
+ None,
2672
+ ],
2673
+ ]
2674
+
2675
+ examples_obj = create_examples(
2676
+ examples=examples_list,
2677
+ fn=generate_video_example,
2678
+ inputs=[first_frame, prompt_ui, camera_ui, resolution_ui, radioanimated_mode, input_video, audio_input, end_frame],
2679
+ outputs = [output_video],
2680
+ label="نمونه‌ها",
2681
+ cache_examples=True,
2682
+ visible=False
2683
+ )
2684
+
2685
+ preset_gallery = PresetGallery(
2686
+ items=[
2687
+ {"thumb": "examples/supergirl-2.png", "label": "تصویر و صدا به ویدیو"},
2688
+ {"thumb": "examples/frame3.png", "label": "تصویر اول و آخر"},
2689
+ {"thumb": "examples/supergirl.png", "label": "تصویر به ویدیو (عروسک)"},
2690
+ {"thumb": "examples/highland.png", "label": "تصویر به ویدیو (گاو)"},
2691
+ {"thumb": "examples/wednesday.png", "label": "تصویر به ویدیو (ونزدی)"},
2692
+ {"thumb": "examples/astronaut.png", "label": "تصویر به ویدیو (فضانورد)"},
2693
+ ],
2694
+ title="برای شروع روی یکی از نمونه‌ها کلیک کنید",
2695
+ )
2696
+
2697
+ def on_audio_ui_change(v):
2698
+ # Our JS sends "__CLEAR__" when the user presses the X
2699
+ if v == "__CLEAR__" or v is None or v == "":
2700
+ return None
2701
+ # For normal events (uploads), do nothing (keep whatever gr.File already has)
2702
+ return gr.update()
2703
+
2704
+ audio_ui.change(
2705
+ fn=on_audio_ui_change,
2706
+ inputs=audio_ui,
2707
+ outputs=audio_input,
2708
+ api_visibility="private",
2709
+ )
2710
+
2711
+ preset_gallery.change(
2712
+ fn=apply_example,
2713
+ inputs=preset_gallery,
2714
+ outputs=[
2715
+ first_frame,
2716
+ prompt_ui,
2717
+ camera_ui,
2718
+ resolution_ui,
2719
+ radioanimated_mode,
2720
+ input_video,
2721
+ audio_input,
2722
+ audio_ui,
2723
+ end_frame,
2724
+ output_video # Clears the output video
2725
+ ],
2726
+ api_visibility="private",
2727
+ )
2728
+
2729
+ if __name__ == "__main__":
2730
+ demo.launch(ssr_mode=False, mcp_server=True, css=css, allowed_paths=["./examples"])