Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files46 unmodified lines474849505152214 unmodified lines2672682692702712723 unmodified lines2762772782792802812822832842852862872882893 unmodified lines2932942952962972982992 unmodified lines30230330430530630730851 unmodified lines36036136236336436531 unmodified lines39739839940040140261 unmodified lines46446546646746846947047147213 unmodified lines48648748848949049149249349449549619 unmodified lines51651751851952052152246 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_videofrom 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)print(f"Checkpoint: {checkpoint_path}")print(f"Spatial upsampler: {spatial_upsampler_path}")print(f"Gemma root: {gemma_root}")3 unmodified linesdistilled_checkpoint_path=checkpoint_path,spatial_upsampler_path=spatial_upsampler_path,gemma_root=gemma_root,loras=[],quantization=QuantizationPolicy.fp8_cast(),)# Preload all models for ZeroGPU tensor packing.print("Preloading all models (including Gemma and audio components)...")ledger = pipeline.model_ledger_transformer = ledger.transformer()_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: _transformerledger.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("=" * 80)print("Pipeline ready!")51 unmodified linesrandomize_seed: bool = True,height: int = 1024,width: int = 1536,progress=gr.Progress(track_tqdm=True),):try:31 unmodified linestiling_config = TilingConfig.default()video_chunks_number = get_video_chunks_number(num_frames, tiling_config)log_memory("before pipeline call")video, audio = pipeline(61 unmodified lineswith gr.Row():enhance_prompt = gr.Checkbox(label="Enhance Prompt", value=False)high_res = gr.Checkbox(label="High Resolution", value=True)with gr.Column():output_video = gr.Video(label="Generated Video", autoplay=True)gr.Examples(examples=[13 unmodified linesTrue,1024,1024,],],inputs=[first_image, last_image, input_audio, prompt, duration,enhance_prompt, seed, randomize_seed, height, width,],)19 unmodified linesfn=generate_video,inputs=[first_image, last_image, input_audio, prompt, duration, enhance_prompt,seed, randomize_seed, height, width,],outputs=[output_video, seed],)46 unmodified lines4748495051525354214 unmodified lines2692702712722732742752762772782792802812822833 unmodified lines2872882892902912922932942952962972982993003013023033043 unmodified lines3083093103113123133143152 unmodified lines31831932032132232332432532651 unmodified lines37837938038138238338431 unmodified lines41641741841942042142242342442542642742842943061 unmodified lines49249349449549649749849950050150250350450550650750850913 unmodified lines52352452552652752852953053153253353419 unmodified lines55455555655755855956046 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 linesdistilled_checkpoint_path=checkpoint_path,spatial_upsampler_path=spatial_upsampler_path,gemma_root=gemma_root,loras=[lora_descriptor],quantization=QuantizationPolicy.fp8_cast(),)# Preload all models for ZeroGPU tensor packing.# >>> REPLACE the "Preload all models" block with this one:print("Preloading models (pinning decoders/encoders but leaving transformer dynamic)...")ledger = pipeline.model_ledger# 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()# 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("Selected models pinned. Transformer remains dynamic to reflect runtime LoRA strength.")print("Preload complete.")print("=" * 80)print("Pipeline ready!")51 unmodified linesrandomize_seed: bool = True,height: int = 1024,width: int = 1536,lora_strength: float = 1.0,progress=gr.Progress(track_tqdm=True),):try:31 unmodified linestiling_config = TilingConfig.default()video_chunks_number = get_video_chunks_number(num_frames, tiling_config)# >>> SET the LoRA descriptor strength at runtimetry:# lora_descriptor was created at module scope during initlora_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 quietlyprint(f"[LoRA] could not set strength: {e}")log_memory("before pipeline call")video, audio = pipeline(61 unmodified lineswith 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=False)gr.Examples(examples=[13 unmodified linesTrue,1024,1024,1,],],inputs=[first_image, last_image, input_audio, prompt, duration,enhance_prompt, seed, randomize_seed, height, width, lora_strength],)19 unmodified linesfn=generate_video,inputs=[first_image, last_image, input_audio, prompt, duration, enhance_prompt,seed, randomize_seed, height, width, lora_strength],outputs=[output_video, seed],)
app.py+46-8