Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files46 unmodified lines4748495051525354214 unmodified lines2692702712722732742752762772782792802812822833 unmodified lines2872882892792902912922932942842952962972862982993003013023033043 unmodified lines3083093102963113123133143152 unmodified lines31831932030532132232332432532651 unmodified lines37837938038138238338431 unmodified lines41641741841942042142242342442542642742842943061 unmodified lines49249349449549649749849950050150250350450546950650750850913 unmodified lines52352452552652752852953049353153253353419 unmodified lines55455555651955755855956046 unmodified linesfrom ltx_core.model.audio_vae import encode_audio as vae_encode_audiofrom ltx_core.model.upsampler import upsample_videofrom ltx_core.model.video_vae import TilingConfig, get_video_chunks_number, decode_video as vae_decode_video# >>> ADD these imports (place immediately after your video_vae import)from ltx_core.loader import LoraPathStrengthAndSDOps, LTXV_LORA_COMFY_RENAMING_MAPfrom ltx_core.quantization import QuantizationPolicyfrom ltx_core.types import Audio, AudioLatentShape, VideoPixelShapefrom ltx_pipelines.distilled import DistilledPipeline214 unmodified linesspatial_upsampler_path = hf_hub_download(repo_id=LTX_MODEL_REPO, filename="ltx-2.3-spatial-upscaler-x2-1.0.safetensors")gemma_root = snapshot_download(repo_id=GEMMA_REPO)
# >>> ADD: download and prepare LoRA descriptorprint("Downloading LoRA for this Space (dagloop5/LoRA:LoRA.safetensors)...")lora_path = hf_hub_download(repo_id="dagloop5/LoRA", filename="LoRA.safetensors")# Create a descriptor object that the LTX loader expects.# initial strength is set to 1.0; we'll mutate `.strength` at runtime from the UI slider.lora_descriptor = LoraPathStrengthAndSDOps(lora_path, 1.0, LTXV_LORA_COMFY_RENAMING_MAP)
print(f"LoRA: {lora_path}")
print(f"Checkpoint: {checkpoint_path}")print(f"Spatial upsampler: {spatial_upsampler_path}")print(f"Gemma root: {gemma_root}")3 unmodified lines distilled_checkpoint_path=checkpoint_path, spatial_upsampler_path=spatial_upsampler_path, gemma_root=gemma_root, loras=[], loras=[lora_descriptor], quantization=QuantizationPolicy.fp8_cast(),)
# Preload all models for ZeroGPU tensor packing.print("Preloading all models (including Gemma and audio components)...")# >>> REPLACE the "Preload all models" block with this one:print("Preloading models (pinning decoders/encoders but leaving transformer dynamic)...")ledger = pipeline.model_ledger_transformer = ledger.transformer()
# NOTE: do NOT call ledger.transformer() here. We keep the transformer's construction dynamic# so that changes to lora_descriptor.strength (made at runtime) are applied when the transformer# is built. We DO preload other components that are safe to pin._video_encoder = ledger.video_encoder()_video_decoder = ledger.video_decoder()_audio_encoder = ledger.audio_encoder()3 unmodified lines_text_encoder = ledger.text_encoder()_embeddings_processor = ledger.gemma_embeddings_processor()
ledger.transformer = lambda: _transformer# Replace ledger methods to return the pinned objects for those components.# Intentionally do NOT override ledger.transformer so transformer is built when needed.ledger.video_encoder = lambda: _video_encoderledger.video_decoder = lambda: _video_decoderledger.audio_encoder = lambda: _audio_encoder2 unmodified linesledger.spatial_upsampler = lambda: _spatial_upsamplerledger.text_encoder = lambda: _text_encoderledger.gemma_embeddings_processor = lambda: _embeddings_processorprint("All models preloaded (including Gemma text encoder and audio encoder)!")
print("Selected models pinned. Transformer remains dynamic to reflect runtime LoRA strength.")print("Preload complete.")
print("=" * 80)print("Pipeline ready!")51 unmodified lines randomize_seed: bool = True, height: int = 1024, width: int = 1536, lora_strength: float = 1.0, progress=gr.Progress(track_tqdm=True),): try:31 unmodified lines tiling_config = TilingConfig.default() video_chunks_number = get_video_chunks_number(num_frames, tiling_config)
# >>> SET the LoRA descriptor strength at runtime try: # lora_descriptor was created at module scope during init lora_descriptor.strength = float(lora_strength) print(f"[LoRA] set strength = {lora_descriptor.strength}") except Exception as e: # Defensive: if the descriptor isn't present, continue quietly print(f"[LoRA] could not set strength: {e}") log_memory("before pipeline call")
video, audio = pipeline(61 unmodified lines with gr.Row(): enhance_prompt = gr.Checkbox(label="Enhance Prompt", value=False) high_res = gr.Checkbox(label="High Resolution", value=True) # >>> ADD this line inside the Advanced Settings (e.g., after randomize_seed) lora_strength = gr.Slider( label="LoRA Strength", info="Scale for the LoRA weights (0.0 = off). Set near 1.0 for full effect.", minimum=0.0, maximum=1.5, value=1.0, step=0.01, )
with gr.Column(): output_video = gr.Video(label="Generated Video", autoplay=True) output_video = gr.Video(label="Generated Video", autoplay=False)
gr.Examples( examples=[13 unmodified lines True, 1024, 1024, 1, ], ], inputs=[ first_image, last_image, input_audio, prompt, duration, enhance_prompt, seed, randomize_seed, height, width, enhance_prompt, seed, randomize_seed, height, width, lora_strength ], )
19 unmodified lines fn=generate_video, inputs=[ first_image, last_image, input_audio, prompt, duration, enhance_prompt, seed, randomize_seed, height, width, seed, randomize_seed, height, width, lora_strength ], outputs=[output_video, seed], )app.py+46-8