dagloop5 commited on
Commit
0a75f93
·
verified ·
1 Parent(s): 066348b

Upload app(3).py

Browse files
Files changed (1) hide show
  1. app(3).py +652 -0
app(3).py ADDED
@@ -0,0 +1,652 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import subprocess
3
+ import sys
4
+
5
+ # Disable torch.compile / dynamo before any torch import
6
+ os.environ["TORCH_COMPILE_DISABLE"] = "1"
7
+ os.environ["TORCHDYNAMO_DISABLE"] = "1"
8
+
9
+ # Install xformers for memory-efficient attention
10
+ subprocess.run([sys.executable, "-m", "pip", "install", "xformers==0.0.32.post2", "--no-build-isolation"], check=False)
11
+
12
+ # Clone LTX-2 repo and install packages
13
+ LTX_REPO_URL = "https://github.com/Lightricks/LTX-2.git"
14
+ LTX_REPO_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "LTX-2")
15
+
16
+ if not os.path.exists(LTX_REPO_DIR):
17
+ print(f"Cloning {LTX_REPO_URL}...")
18
+ subprocess.run(["git", "clone", "--depth", "1", LTX_REPO_URL, LTX_REPO_DIR], check=True)
19
+
20
+ print("Installing ltx-core and ltx-pipelines from cloned repo...")
21
+ subprocess.run(
22
+ [sys.executable, "-m", "pip", "install", "--force-reinstall", "--no-deps", "-e",
23
+ os.path.join(LTX_REPO_DIR, "packages", "ltx-core"),
24
+ "-e", os.path.join(LTX_REPO_DIR, "packages", "ltx-pipelines")],
25
+ check=True,
26
+ )
27
+
28
+ sys.path.insert(0, os.path.join(LTX_REPO_DIR, "packages", "ltx-pipelines", "src"))
29
+ sys.path.insert(0, os.path.join(LTX_REPO_DIR, "packages", "ltx-core", "src"))
30
+
31
+ import logging
32
+ import random
33
+ import tempfile
34
+ from pathlib import Path
35
+
36
+ import torch
37
+ torch._dynamo.config.suppress_errors = True
38
+ torch._dynamo.config.disable = True
39
+
40
+ import spaces
41
+ import gradio as gr
42
+ import numpy as np
43
+ from huggingface_hub import hf_hub_download, snapshot_download
44
+
45
+ from ltx_core.components.diffusion_steps import EulerDiffusionStep
46
+ from ltx_core.components.noisers import GaussianNoiser
47
+ from ltx_core.model.audio_vae import encode_audio as vae_encode_audio
48
+ from ltx_core.model.upsampler import upsample_video
49
+ from ltx_core.model.video_vae import TilingConfig, get_video_chunks_number, decode_video as vae_decode_video
50
+ # >>> ADD these imports (place immediately after your video_vae import)
51
+ from ltx_core.loader import LoraPathStrengthAndSDOps, LTXV_LORA_COMFY_RENAMING_MAP
52
+ from ltx_core.quantization import QuantizationPolicy
53
+ from ltx_core.types import Audio, AudioLatentShape, VideoPixelShape
54
+ from ltx_pipelines.distilled import DistilledPipeline
55
+ from ltx_pipelines.utils import euler_denoising_loop
56
+ from ltx_pipelines.utils.args import ImageConditioningInput
57
+ from ltx_pipelines.utils.constants import DISTILLED_SIGMA_VALUES, STAGE_2_DISTILLED_SIGMA_VALUES
58
+ from ltx_pipelines.utils.helpers import (
59
+ cleanup_memory,
60
+ combined_image_conditionings,
61
+ denoise_video_only,
62
+ encode_prompts,
63
+ simple_denoising_func,
64
+ )
65
+ from ltx_pipelines.utils.media_io import decode_audio_from_file, encode_video
66
+
67
+ # Force-patch xformers attention into the LTX attention module.
68
+ from ltx_core.model.transformer import attention as _attn_mod
69
+ print(f"[ATTN] Before patch: memory_efficient_attention={_attn_mod.memory_efficient_attention}")
70
+ try:
71
+ from xformers.ops import memory_efficient_attention as _mea
72
+ _attn_mod.memory_efficient_attention = _mea
73
+ print(f"[ATTN] After patch: memory_efficient_attention={_attn_mod.memory_efficient_attention}")
74
+ except Exception as e:
75
+ print(f"[ATTN] xformers patch FAILED: {type(e).__name__}: {e}")
76
+
77
+ logging.getLogger().setLevel(logging.INFO)
78
+
79
+ MAX_SEED = np.iinfo(np.int32).max
80
+ DEFAULT_PROMPT = (
81
+ "An astronaut hatches from a fragile egg on the surface of the Moon, "
82
+ "the shell cracking and peeling apart in gentle low-gravity motion. "
83
+ "Fine lunar dust lifts and drifts outward with each movement, floating "
84
+ "in slow arcs before settling back onto the ground."
85
+ )
86
+ DEFAULT_FRAME_RATE = 24.0
87
+
88
+ # Resolution presets: (width, height)
89
+ RESOLUTIONS = {
90
+ "high": {"16:9": (1536, 1024), "9:16": (1024, 1536), "1:1": (1024, 1024)},
91
+ "low": {"16:9": (768, 512), "9:16": (512, 768), "1:1": (768, 768)},
92
+ }
93
+
94
+
95
+ class LTX23DistilledA2VPipeline(DistilledPipeline):
96
+ """DistilledPipeline with optional audio conditioning."""
97
+
98
+ def __call__(
99
+ self,
100
+ prompt: str,
101
+ seed: int,
102
+ height: int,
103
+ width: int,
104
+ num_frames: int,
105
+ frame_rate: float,
106
+ images: list[ImageConditioningInput],
107
+ audio_path: str | None = None,
108
+ tiling_config: TilingConfig | None = None,
109
+ enhance_prompt: bool = False,
110
+ ):
111
+ # Standard path when no audio input is provided.
112
+ print(prompt)
113
+ if audio_path is None:
114
+ return super().__call__(
115
+ prompt=prompt,
116
+ seed=seed,
117
+ height=height,
118
+ width=width,
119
+ num_frames=num_frames,
120
+ frame_rate=frame_rate,
121
+ images=images,
122
+ tiling_config=tiling_config,
123
+ enhance_prompt=enhance_prompt,
124
+ )
125
+
126
+ generator = torch.Generator(device=self.device).manual_seed(seed)
127
+ noiser = GaussianNoiser(generator=generator)
128
+ stepper = EulerDiffusionStep()
129
+ dtype = torch.bfloat16
130
+
131
+ (ctx_p,) = encode_prompts(
132
+ [prompt],
133
+ self.model_ledger,
134
+ enhance_first_prompt=enhance_prompt,
135
+ enhance_prompt_image=images[0].path if len(images) > 0 else None,
136
+ )
137
+ video_context, audio_context = ctx_p.video_encoding, ctx_p.audio_encoding
138
+
139
+ video_duration = num_frames / frame_rate
140
+ decoded_audio = decode_audio_from_file(audio_path, self.device, 0.0, video_duration)
141
+ if decoded_audio is None:
142
+ raise ValueError(f"Could not extract audio stream from {audio_path}")
143
+
144
+ encoded_audio_latent = vae_encode_audio(decoded_audio, self.model_ledger.audio_encoder())
145
+ audio_shape = AudioLatentShape.from_duration(batch=1, duration=video_duration, channels=8, mel_bins=16)
146
+ expected_frames = audio_shape.frames
147
+ actual_frames = encoded_audio_latent.shape[2]
148
+
149
+ if actual_frames > expected_frames:
150
+ encoded_audio_latent = encoded_audio_latent[:, :, :expected_frames, :]
151
+ elif actual_frames < expected_frames:
152
+ pad = torch.zeros(
153
+ encoded_audio_latent.shape[0],
154
+ encoded_audio_latent.shape[1],
155
+ expected_frames - actual_frames,
156
+ encoded_audio_latent.shape[3],
157
+ device=encoded_audio_latent.device,
158
+ dtype=encoded_audio_latent.dtype,
159
+ )
160
+ encoded_audio_latent = torch.cat([encoded_audio_latent, pad], dim=2)
161
+
162
+ video_encoder = self.model_ledger.video_encoder()
163
+ transformer = self.model_ledger.transformer()
164
+ stage_1_sigmas = torch.tensor(DISTILLED_SIGMA_VALUES, device=self.device)
165
+
166
+ def denoising_loop(sigmas, video_state, audio_state, stepper):
167
+ return euler_denoising_loop(
168
+ sigmas=sigmas,
169
+ video_state=video_state,
170
+ audio_state=audio_state,
171
+ stepper=stepper,
172
+ denoise_fn=simple_denoising_func(
173
+ video_context=video_context,
174
+ audio_context=audio_context,
175
+ transformer=transformer,
176
+ ),
177
+ )
178
+
179
+ stage_1_output_shape = VideoPixelShape(
180
+ batch=1,
181
+ frames=num_frames,
182
+ width=width // 2,
183
+ height=height // 2,
184
+ fps=frame_rate,
185
+ )
186
+ stage_1_conditionings = combined_image_conditionings(
187
+ images=images,
188
+ height=stage_1_output_shape.height,
189
+ width=stage_1_output_shape.width,
190
+ video_encoder=video_encoder,
191
+ dtype=dtype,
192
+ device=self.device,
193
+ )
194
+ video_state = denoise_video_only(
195
+ output_shape=stage_1_output_shape,
196
+ conditionings=stage_1_conditionings,
197
+ noiser=noiser,
198
+ sigmas=stage_1_sigmas,
199
+ stepper=stepper,
200
+ denoising_loop_fn=denoising_loop,
201
+ components=self.pipeline_components,
202
+ dtype=dtype,
203
+ device=self.device,
204
+ initial_audio_latent=encoded_audio_latent,
205
+ )
206
+
207
+ torch.cuda.synchronize()
208
+ cleanup_memory()
209
+
210
+ upscaled_video_latent = upsample_video(
211
+ latent=video_state.latent[:1],
212
+ video_encoder=video_encoder,
213
+ upsampler=self.model_ledger.spatial_upsampler(),
214
+ )
215
+ stage_2_sigmas = torch.tensor(STAGE_2_DISTILLED_SIGMA_VALUES, device=self.device)
216
+ stage_2_output_shape = VideoPixelShape(batch=1, frames=num_frames, width=width, height=height, fps=frame_rate)
217
+ stage_2_conditionings = combined_image_conditionings(
218
+ images=images,
219
+ height=stage_2_output_shape.height,
220
+ width=stage_2_output_shape.width,
221
+ video_encoder=video_encoder,
222
+ dtype=dtype,
223
+ device=self.device,
224
+ )
225
+ video_state = denoise_video_only(
226
+ output_shape=stage_2_output_shape,
227
+ conditionings=stage_2_conditionings,
228
+ noiser=noiser,
229
+ sigmas=stage_2_sigmas,
230
+ stepper=stepper,
231
+ denoising_loop_fn=denoising_loop,
232
+ components=self.pipeline_components,
233
+ dtype=dtype,
234
+ device=self.device,
235
+ noise_scale=stage_2_sigmas[0],
236
+ initial_video_latent=upscaled_video_latent,
237
+ initial_audio_latent=encoded_audio_latent,
238
+ )
239
+
240
+ torch.cuda.synchronize()
241
+ del transformer
242
+ del video_encoder
243
+ cleanup_memory()
244
+
245
+ decoded_video = vae_decode_video(
246
+ video_state.latent,
247
+ self.model_ledger.video_decoder(),
248
+ tiling_config,
249
+ generator,
250
+ )
251
+ original_audio = Audio(
252
+ waveform=decoded_audio.waveform.squeeze(0),
253
+ sampling_rate=decoded_audio.sampling_rate,
254
+ )
255
+ return decoded_video, original_audio
256
+
257
+
258
+ # Model repos
259
+ LTX_MODEL_REPO = "Lightricks/LTX-2.3"
260
+ GEMMA_REPO ="google/gemma-3-12b-it-qat-q4_0-unquantized"
261
+
262
+
263
+ # Download model checkpoints
264
+ print("=" * 80)
265
+ print("Downloading LTX-2.3 distilled model + Gemma...")
266
+ print("=" * 80)
267
+
268
+ checkpoint_path = hf_hub_download(repo_id=LTX_MODEL_REPO, filename="ltx-2.3-22b-distilled.safetensors")
269
+ spatial_upsampler_path = hf_hub_download(repo_id=LTX_MODEL_REPO, filename="ltx-2.3-spatial-upscaler-x2-1.0.safetensors")
270
+ gemma_root = snapshot_download(repo_id=GEMMA_REPO)
271
+
272
+ # >>> ADD: download and prepare LoRA descriptor
273
+ print("Downloading LoRA for this Space (dagloop5/LoRA:LoRA.safetensors)...")
274
+ lora_path = hf_hub_download(repo_id="dagloop5/LoRA", filename="LoRA.safetensors")
275
+ # Create a descriptor object that the LTX loader expects.
276
+ # initial strength is set to 1.0; we'll mutate `.strength` at runtime from the UI slider.
277
+ lora_descriptor = LoraPathStrengthAndSDOps(lora_path, 1.0, LTXV_LORA_COMFY_RENAMING_MAP)
278
+
279
+ print(f"LoRA: {lora_path}")
280
+
281
+ print(f"Checkpoint: {checkpoint_path}")
282
+ print(f"Spatial upsampler: {spatial_upsampler_path}")
283
+ print(f"Gemma root: {gemma_root}")
284
+
285
+ # Initialize pipeline WITH text encoder and optional audio support
286
+ pipeline = LTX23DistilledA2VPipeline(
287
+ distilled_checkpoint_path=checkpoint_path,
288
+ spatial_upsampler_path=spatial_upsampler_path,
289
+ gemma_root=gemma_root,
290
+ loras=[lora_descriptor],
291
+ quantization=QuantizationPolicy.fp8_cast(),
292
+ )
293
+
294
+ # Preload all models for ZeroGPU tensor packing.
295
+ # >>> REPLACE the "Preload all models" block with this one:
296
+ print("Preloading models (pinning decoders/encoders but leaving transformer dynamic)...")
297
+ ledger = pipeline.model_ledger
298
+
299
+ # NOTE: do NOT call ledger.transformer() here. We keep the transformer's construction dynamic
300
+ # so that changes to lora_descriptor.strength (made at runtime) are applied when the transformer
301
+ # is built. We DO preload other components that are safe to pin.
302
+ _video_encoder = ledger.video_encoder()
303
+ _video_decoder = ledger.video_decoder()
304
+ _audio_encoder = ledger.audio_encoder()
305
+ _audio_decoder = ledger.audio_decoder()
306
+ _vocoder = ledger.vocoder()
307
+ _spatial_upsampler = ledger.spatial_upsampler()
308
+ _text_encoder = ledger.text_encoder()
309
+ _embeddings_processor = ledger.gemma_embeddings_processor()
310
+
311
+ # Replace ledger methods to return the pinned objects for those components.
312
+ # Intentionally do NOT override ledger.transformer so transformer is built when needed.
313
+ ledger.video_encoder = lambda: _video_encoder
314
+ ledger.video_decoder = lambda: _video_decoder
315
+ ledger.audio_encoder = lambda: _audio_encoder
316
+ ledger.audio_decoder = lambda: _audio_decoder
317
+ ledger.vocoder = lambda: _vocoder
318
+ ledger.spatial_upsampler = lambda: _spatial_upsampler
319
+ ledger.text_encoder = lambda: _text_encoder
320
+ ledger.gemma_embeddings_processor = lambda: _embeddings_processor
321
+
322
+ print("Selected models pinned. Transformer remains dynamic to reflect runtime LoRA strength.")
323
+ print("Preload complete.")
324
+
325
+ print("=" * 80)
326
+ print("Pipeline ready!")
327
+ print("=" * 80)
328
+
329
+
330
+ def log_memory(tag: str):
331
+ if torch.cuda.is_available():
332
+ allocated = torch.cuda.memory_allocated() / 1024**3
333
+ peak = torch.cuda.max_memory_allocated() / 1024**3
334
+ free, total = torch.cuda.mem_get_info()
335
+ print(f"[VRAM {tag}] allocated={allocated:.2f}GB peak={peak:.2f}GB free={free / 1024**3:.2f}GB total={total / 1024**3:.2f}GB")
336
+
337
+
338
+ def detect_aspect_ratio(image) -> str:
339
+ if image is None:
340
+ return "16:9"
341
+ if hasattr(image, "size"):
342
+ w, h = image.size
343
+ elif hasattr(image, "shape"):
344
+ h, w = image.shape[:2]
345
+ else:
346
+ return "16:9"
347
+ ratio = w / h
348
+ candidates = {"16:9": 16 / 9, "9:16": 9 / 16, "1:1": 1.0}
349
+ return min(candidates, key=lambda k: abs(ratio - candidates[k]))
350
+
351
+
352
+ def on_image_upload(first_image, last_image, high_res):
353
+ ref_image = first_image if first_image is not None else last_image
354
+ aspect = detect_aspect_ratio(ref_image)
355
+ tier = "high" if high_res else "low"
356
+ w, h = RESOLUTIONS[tier][aspect]
357
+ return gr.update(value=w), gr.update(value=h)
358
+
359
+
360
+ def on_highres_toggle(first_image, last_image, high_res):
361
+ ref_image = first_image if first_image is not None else last_image
362
+ aspect = detect_aspect_ratio(ref_image)
363
+ tier = "high" if high_res else "low"
364
+ w, h = RESOLUTIONS[tier][aspect]
365
+ return gr.update(value=w), gr.update(value=h)
366
+
367
+
368
+ @spaces.GPU(duration=75)
369
+ @torch.inference_mode()
370
+ def generate_video(
371
+ first_image,
372
+ last_image,
373
+ input_audio,
374
+ prompt: str,
375
+ duration: float,
376
+ enhance_prompt: bool = True,
377
+ seed: int = 42,
378
+ randomize_seed: bool = True,
379
+ height: int = 1024,
380
+ width: int = 1536,
381
+ lora_strength: float = 1.0,
382
+ progress=gr.Progress(track_tqdm=True),
383
+ ):
384
+ try:
385
+ global pipeline # <<< ADD THIS LINE HERE (VERY TOP of try block)
386
+ torch.cuda.reset_peak_memory_stats()
387
+ log_memory("start")
388
+
389
+ current_seed = random.randint(0, MAX_SEED) if randomize_seed else int(seed)
390
+
391
+ frame_rate = DEFAULT_FRAME_RATE
392
+ num_frames = int(duration * frame_rate) + 1
393
+ num_frames = ((num_frames - 1 + 7) // 8) * 8 + 1
394
+
395
+ print(f"Generating: {height}x{width}, {num_frames} frames ({duration}s), seed={current_seed}")
396
+
397
+ images = []
398
+ output_dir = Path("outputs")
399
+ output_dir.mkdir(exist_ok=True)
400
+
401
+ if first_image is not None:
402
+ temp_first_path = output_dir / f"temp_first_{current_seed}.jpg"
403
+ if hasattr(first_image, "save"):
404
+ first_image.save(temp_first_path)
405
+ else:
406
+ temp_first_path = Path(first_image)
407
+ images.append(ImageConditioningInput(path=str(temp_first_path), frame_idx=0, strength=1.0))
408
+
409
+ if last_image is not None:
410
+ temp_last_path = output_dir / f"temp_last_{current_seed}.jpg"
411
+ if hasattr(last_image, "save"):
412
+ last_image.save(temp_last_path)
413
+ else:
414
+ temp_last_path = Path(last_image)
415
+ images.append(ImageConditioningInput(path=str(temp_last_path), frame_idx=num_frames - 1, strength=1.0))
416
+
417
+ tiling_config = TilingConfig.default()
418
+ video_chunks_number = get_video_chunks_number(num_frames, tiling_config)
419
+
420
+ # >>> RUNTIME LoRA application (robust, multi-fallback)
421
+ # We cannot rely on mutating the original descriptor (some implementations are immutable),
422
+ # so create a fresh runtime descriptor and try multiple ways to install it.
423
+ runtime_strength = float(lora_strength)
424
+ replaced = False
425
+
426
+ # 1) Try simple approach: build a new LoraPathStrengthAndSDOps
427
+ runtime_lora = LoraPathStrengthAndSDOps(lora_path, runtime_strength, LTXV_LORA_COMFY_RENAMING_MAP)
428
+ print(f"[LoRA] attempting to apply runtime LoRA (strength={runtime_strength})")
429
+
430
+ # Try a few likely places to replace the descriptor used by the pipeline/ledger.
431
+ try:
432
+ # common attribute on pipeline
433
+ if hasattr(pipeline, "loras"):
434
+ try:
435
+ pipeline.loras = [runtime_lora]
436
+ replaced = True
437
+ print("[LoRA] replaced pipeline.loras")
438
+ except Exception as e:
439
+ print(f"[LoRA] pipeline.loras assignment failed: {e}")
440
+ except Exception:
441
+ pass
442
+
443
+ try:
444
+ # common attribute on the model ledger
445
+ if hasattr(pipeline, "model_ledger") and hasattr(pipeline.model_ledger, "loras"):
446
+ try:
447
+ pipeline.model_ledger.loras = [runtime_lora]
448
+ replaced = True
449
+ print("[LoRA] replaced pipeline.model_ledger.loras")
450
+ except Exception as e:
451
+ print(f"[LoRA] pipeline.model_ledger.loras assignment failed: {e}")
452
+ except Exception:
453
+ pass
454
+
455
+ try:
456
+ # some internals use a private _loras list
457
+ if hasattr(pipeline, "model_ledger") and hasattr(pipeline.model_ledger, "_loras"):
458
+ try:
459
+ pipeline.model_ledger._loras = [runtime_lora]
460
+ replaced = True
461
+ print("[LoRA] replaced pipeline.model_ledger._loras")
462
+ except Exception as e:
463
+ print(f"[LoRA] pipeline.model_ledger._loras assignment failed: {e}")
464
+ except Exception:
465
+ pass
466
+
467
+ # 2) If we succeeded replacing the descriptor in-place, clear transformer cache so it will rebuild
468
+ if replaced:
469
+ try:
470
+ if hasattr(pipeline.model_ledger, "_transformer"):
471
+ pipeline.model_ledger._transformer = None
472
+ # also clear potential caches named similar to 'transformer_cache' if present
473
+ if hasattr(pipeline.model_ledger, "transformer_cache"):
474
+ try:
475
+ pipeline.model_ledger.transformer_cache = {}
476
+ except Exception:
477
+ pass
478
+ print("[LoRA] in-place descriptor replacement done; transformer cache cleared")
479
+ except Exception as e:
480
+ print(f"[LoRA] replacement succeeded but cache clearing failed: {e}")
481
+
482
+ # 3) FINAL FALLBACK - if none of the in-place replacements worked, rebuild the pipeline
483
+ if not replaced:
484
+ print("[LoRA] in-place replacement FAILED; rebuilding pipeline with runtime LoRA (this is slow)")
485
+ try:
486
+ # Rebuild pipeline object with the new LoRA descriptor
487
+ # NOTE: this replaces the global `pipeline`. We must declare global to reassign it.
488
+ pipeline = LTX23DistilledA2VPipeline(
489
+ distilled_checkpoint_path=checkpoint_path,
490
+ spatial_upsampler_path=spatial_upsampler_path,
491
+ gemma_root=gemma_root,
492
+ loras=[runtime_lora],
493
+ quantization=QuantizationPolicy.fp8_cast(),
494
+ )
495
+
496
+ # After rebuilding, we *do not* re-run the original module-level preloads here,
497
+ # because re-pinning may be complex; the rebuilt pipeline will construct its
498
+ # own ledger as part of the first call. This is slower but reliable.
499
+ # Clear any transformer caches if they exist on the new ledger as well.
500
+ try:
501
+ if hasattr(pipeline.model_ledger, "_transformer"):
502
+ pipeline.model_ledger._transformer = None
503
+ except Exception:
504
+ pass
505
+
506
+ print("[LoRA] pipeline rebuilt with runtime LoRA")
507
+ except Exception as e:
508
+ print(f"[LoRA] pipeline rebuild FAILED: {e}")
509
+
510
+ # Finally, log memory then proceed
511
+ log_memory("before pipeline call")
512
+
513
+ video, audio = pipeline(
514
+ prompt=prompt,
515
+ seed=current_seed,
516
+ height=int(height),
517
+ width=int(width),
518
+ num_frames=num_frames,
519
+ frame_rate=frame_rate,
520
+ images=images,
521
+ audio_path=input_audio,
522
+ tiling_config=tiling_config,
523
+ enhance_prompt=enhance_prompt,
524
+ )
525
+
526
+ log_memory("after pipeline call")
527
+
528
+ output_path = tempfile.mktemp(suffix=".mp4")
529
+ encode_video(
530
+ video=video,
531
+ fps=frame_rate,
532
+ audio=audio,
533
+ output_path=output_path,
534
+ video_chunks_number=video_chunks_number,
535
+ )
536
+
537
+ log_memory("after encode_video")
538
+ return str(output_path), current_seed
539
+
540
+ except Exception as e:
541
+ import traceback
542
+ log_memory("on error")
543
+ print(f"Error: {str(e)}\n{traceback.format_exc()}")
544
+ return None, current_seed
545
+
546
+
547
+ with gr.Blocks(title="LTX-2.3 Heretic Distilled") as demo:
548
+ gr.Markdown("# LTX-2.3 F2LF:Heretic with Fast Audio-Video Generation with Frame Conditioning")
549
+
550
+
551
+ with gr.Row():
552
+ with gr.Column():
553
+ with gr.Row():
554
+ first_image = gr.Image(label="First Frame (Optional)", type="pil")
555
+ last_image = gr.Image(label="Last Frame (Optional)", type="pil")
556
+ input_audio = gr.Audio(label="Audio Input (Optional)", type="filepath")
557
+ prompt = gr.Textbox(
558
+ label="Prompt",
559
+ info="for best results - make it as elaborate as possible",
560
+ value="Make this image come alive with cinematic motion, smooth animation",
561
+ lines=3,
562
+ placeholder="Describe the motion and animation you want...",
563
+ )
564
+ duration = gr.Slider(label="Duration (seconds)", minimum=1.0, maximum=30.0, value=10.0, step=0.1)
565
+
566
+
567
+ generate_btn = gr.Button("Generate Video", variant="primary", size="lg")
568
+
569
+ with gr.Accordion("Advanced Settings", open=False):
570
+ seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, value=10, step=1)
571
+ randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
572
+ with gr.Row():
573
+ width = gr.Number(label="Width", value=1536, precision=0)
574
+ height = gr.Number(label="Height", value=1024, precision=0)
575
+ with gr.Row():
576
+ enhance_prompt = gr.Checkbox(label="Enhance Prompt", value=False)
577
+ high_res = gr.Checkbox(label="High Resolution", value=True)
578
+
579
+ # >>> MOVE slider OUTSIDE the row
580
+ lora_strength = gr.Slider(
581
+ label="LoRA Strength",
582
+ info="Scale for the LoRA weights (0.0 = off). Set near 1.0 for full effect.",
583
+ minimum=0.0,
584
+ maximum=2.0,
585
+ value=1.0,
586
+ step=0.01,
587
+ )
588
+
589
+ with gr.Column():
590
+ output_video = gr.Video(label="Generated Video", autoplay=False)
591
+
592
+ gr.Examples(
593
+ examples=[
594
+ [
595
+ None,
596
+ "pinkknit.jpg",
597
+ None,
598
+ "The camera falls downward through darkness as if dropped into a tunnel. "
599
+ "As it slows, five friends wearing pink knitted hats and sunglasses lean "
600
+ "over and look down toward the camera with curious expressions. The lens "
601
+ "has a strong fisheye effect, creating a circular frame around them. They "
602
+ "crowd together closely, forming a symmetrical cluster while staring "
603
+ "directly into the lens.",
604
+ 3.0,
605
+ False,
606
+ 42,
607
+ True,
608
+ 1024,
609
+ 1024,
610
+ 1.0,
611
+ ],
612
+ ],
613
+ inputs=[
614
+ first_image, last_image, input_audio, prompt, duration,
615
+ enhance_prompt, seed, randomize_seed, height, width, lora_strength
616
+ ],
617
+ )
618
+
619
+ first_image.change(
620
+ fn=on_image_upload,
621
+ inputs=[first_image, last_image, high_res],
622
+ outputs=[width, height],
623
+ )
624
+
625
+ last_image.change(
626
+ fn=on_image_upload,
627
+ inputs=[first_image, last_image, high_res],
628
+ outputs=[width, height],
629
+ )
630
+
631
+ high_res.change(
632
+ fn=on_highres_toggle,
633
+ inputs=[first_image, last_image, high_res],
634
+ outputs=[width, height],
635
+ )
636
+
637
+ generate_btn.click(
638
+ fn=generate_video,
639
+ inputs=[
640
+ first_image, last_image, input_audio, prompt, duration, enhance_prompt,
641
+ seed, randomize_seed, height, width, lora_strength
642
+ ],
643
+ outputs=[output_video, seed],
644
+ )
645
+
646
+
647
+ css = """
648
+ .fillable{max-width: 1200px !important}
649
+ """
650
+
651
+ if __name__ == "__main__":
652
+ demo.launch(theme=gr.themes.Citrus(), css=css)