signsur4739379373 commited on
Commit
c5459d5
·
1 Parent(s): ca7d4ee

Add first pass preview output

Browse files
Files changed (1) hide show
  1. app.py +198 -19
app.py CHANGED
@@ -46,6 +46,7 @@ from huggingface_hub import hf_hub_download, snapshot_download
46
 
47
  from ltx_core.components.diffusion_steps import EulerDiffusionStep
48
  from ltx_core.components.noisers import GaussianNoiser
 
49
  from ltx_core.model.audio_vae import encode_audio as vae_encode_audio
50
  from ltx_core.model.upsampler import upsample_video
51
  from ltx_core.model.video_vae import TilingConfig, get_video_chunks_number, decode_video as vae_decode_video
@@ -58,6 +59,7 @@ from ltx_pipelines.utils.constants import DISTILLED_SIGMA_VALUES, STAGE_2_DISTIL
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,
@@ -99,22 +101,171 @@ class LTX23DistilledA2VPipeline(DistilledPipeline):
99
  audio_path: str | None = None,
100
  tiling_config: TilingConfig | None = None,
101
  enhance_prompt: bool = False,
 
102
  ):
103
- # Standard path when no audio input is provided.
104
  print(prompt)
105
  if audio_path is None:
106
- return super().__call__(
107
- prompt=prompt,
108
- seed=seed,
109
- height=height,
110
- width=width,
111
- num_frames=num_frames,
112
- frame_rate=frame_rate,
113
- images=images,
114
- tiling_config=tiling_config,
115
- enhance_prompt=enhance_prompt,
 
116
  )
117
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
118
  generator = torch.Generator(device=self.device).manual_seed(seed)
119
  noiser = GaussianNoiser(generator=generator)
120
  stepper = EulerDiffusionStep()
@@ -195,6 +346,7 @@ class LTX23DistilledA2VPipeline(DistilledPipeline):
195
  device=self.device,
196
  initial_audio_latent=encoded_audio_latent,
197
  )
 
198
 
199
  torch.cuda.synchronize()
200
  cleanup_memory()
@@ -234,9 +386,10 @@ class LTX23DistilledA2VPipeline(DistilledPipeline):
234
  del video_encoder
235
  cleanup_memory()
236
 
 
237
  decoded_video = vae_decode_video(
238
  video_state.latent,
239
- self.model_ledger.video_decoder(),
240
  tiling_config,
241
  generator,
242
  )
@@ -244,7 +397,11 @@ class LTX23DistilledA2VPipeline(DistilledPipeline):
244
  waveform=decoded_audio.waveform.squeeze(0),
245
  sampling_rate=decoded_audio.sampling_rate,
246
  )
247
- return decoded_video, original_audio
 
 
 
 
248
 
249
 
250
  # Model repos
@@ -541,6 +698,7 @@ def get_gpu_duration(
541
  duration: float,
542
  gpu_duration: float,
543
  enhance_prompt: bool = True,
 
544
  seed: int = 42,
545
  randomize_seed: bool = True,
546
  height: int = 1024,
@@ -562,6 +720,7 @@ def generate_video(
562
  duration: float,
563
  gpu_duration: float,
564
  enhance_prompt: bool = True,
 
565
  seed: int = 42,
566
  randomize_seed: bool = True,
567
  height: int = 1024,
@@ -610,7 +769,7 @@ def generate_video(
610
 
611
  apply_prepared_lora_state_to_pipeline()
612
 
613
- video, audio = pipeline(
614
  prompt=prompt,
615
  seed=current_seed,
616
  height=int(height),
@@ -621,7 +780,13 @@ def generate_video(
621
  audio_path=input_audio,
622
  tiling_config=tiling_config,
623
  enhance_prompt=enhance_prompt,
 
624
  )
 
 
 
 
 
625
 
626
  log_memory("after pipeline call")
627
 
@@ -634,14 +799,25 @@ def generate_video(
634
  video_chunks_number=video_chunks_number,
635
  )
636
 
 
 
 
 
 
 
 
 
 
 
 
637
  log_memory("after encode_video")
638
- return str(output_path), current_seed
639
 
640
  except Exception as e:
641
  import traceback
642
  log_memory("on error")
643
  print(f"Error: {str(e)}\n{traceback.format_exc()}")
644
- return None, current_seed
645
  with gr.Blocks(title="LTX-2.3 Image-to-Video") as demo:
646
  gr.Markdown("# LTX-2.3 Image-to-Video")
647
 
@@ -673,6 +849,7 @@ with gr.Blocks(title="LTX-2.3 Image-to-Video") as demo:
673
  with gr.Row():
674
  enhance_prompt = gr.Checkbox(label="Enhance Prompt", value=False)
675
  high_res = gr.Checkbox(label="High Resolution", value=True)
 
676
  with gr.Column():
677
  gr.Markdown("### LoRA adapter strengths (set to 0 to disable)")
678
  distilled_strength = gr.Slider(label="Distilled LoRA strength", minimum=0.0, maximum=2.0, value=0.6, step=0.01)
@@ -688,6 +865,7 @@ with gr.Blocks(title="LTX-2.3 Image-to-Video") as demo:
688
 
689
  with gr.Column():
690
  output_video = gr.Video(label="Generated Video", autoplay=False)
 
691
  gpu_duration = gr.Slider(
692
  label="ZeroGPU duration (seconds)",
693
  minimum=40.0,
@@ -706,6 +884,7 @@ with gr.Blocks(title="LTX-2.3 Image-to-Video") as demo:
706
  3.0,
707
  80.0,
708
  False,
 
709
  42,
710
  True,
711
  1024,
@@ -717,7 +896,7 @@ with gr.Blocks(title="LTX-2.3 Image-to-Video") as demo:
717
  ],
718
  inputs=[
719
  first_image, last_image, input_audio, prompt, duration, gpu_duration,
720
- enhance_prompt, seed, randomize_seed, height, width,
721
  distilled_strength, quality_strength, reasoning_strength,
722
  ],
723
  )
@@ -769,11 +948,11 @@ with gr.Blocks(title="LTX-2.3 Image-to-Video") as demo:
769
  generate_btn.click(
770
  fn=generate_video,
771
  inputs=[
772
- first_image, last_image, input_audio, prompt, duration, gpu_duration, enhance_prompt,
773
  seed, randomize_seed, height, width,
774
  distilled_strength, quality_strength, reasoning_strength,
775
  ],
776
- outputs=[output_video, seed],
777
  )
778
 
779
 
 
46
 
47
  from ltx_core.components.diffusion_steps import EulerDiffusionStep
48
  from ltx_core.components.noisers import GaussianNoiser
49
+ from ltx_core.model.audio_vae import decode_audio as vae_decode_audio
50
  from ltx_core.model.audio_vae import encode_audio as vae_encode_audio
51
  from ltx_core.model.upsampler import upsample_video
52
  from ltx_core.model.video_vae import TilingConfig, get_video_chunks_number, decode_video as vae_decode_video
 
59
  from ltx_pipelines.utils.helpers import (
60
  cleanup_memory,
61
  combined_image_conditionings,
62
+ denoise_audio_video,
63
  denoise_video_only,
64
  encode_prompts,
65
  simple_denoising_func,
 
101
  audio_path: str | None = None,
102
  tiling_config: TilingConfig | None = None,
103
  enhance_prompt: bool = False,
104
+ return_first_pass: bool = False,
105
  ):
 
106
  print(prompt)
107
  if audio_path is None:
108
+ return self._call_without_audio(
109
+ prompt,
110
+ seed,
111
+ height,
112
+ width,
113
+ num_frames,
114
+ frame_rate,
115
+ images,
116
+ tiling_config,
117
+ enhance_prompt,
118
+ return_first_pass,
119
  )
120
 
121
+ return self._call_with_audio(
122
+ prompt,
123
+ seed,
124
+ height,
125
+ width,
126
+ num_frames,
127
+ frame_rate,
128
+ images,
129
+ audio_path,
130
+ tiling_config,
131
+ enhance_prompt,
132
+ return_first_pass,
133
+ )
134
+
135
+ def _call_without_audio(
136
+ self,
137
+ prompt: str,
138
+ seed: int,
139
+ height: int,
140
+ width: int,
141
+ num_frames: int,
142
+ frame_rate: float,
143
+ images: list[ImageConditioningInput],
144
+ tiling_config: TilingConfig | None,
145
+ enhance_prompt: bool,
146
+ return_first_pass: bool,
147
+ ):
148
+ generator = torch.Generator(device=self.device).manual_seed(seed)
149
+ noiser = GaussianNoiser(generator=generator)
150
+ stepper = EulerDiffusionStep()
151
+ dtype = torch.bfloat16
152
+
153
+ (ctx_p,) = encode_prompts(
154
+ [prompt],
155
+ self.model_ledger,
156
+ enhance_first_prompt=enhance_prompt,
157
+ enhance_prompt_image=images[0].path if len(images) > 0 else None,
158
+ )
159
+ video_context, audio_context = ctx_p.video_encoding, ctx_p.audio_encoding
160
+
161
+ video_encoder = self.model_ledger.video_encoder()
162
+ transformer = self.model_ledger.transformer()
163
+ stage_1_sigmas = torch.tensor(DISTILLED_SIGMA_VALUES, device=self.device)
164
+
165
+ def denoising_loop(sigmas, video_state, audio_state, stepper):
166
+ return euler_denoising_loop(
167
+ sigmas=sigmas,
168
+ video_state=video_state,
169
+ audio_state=audio_state,
170
+ stepper=stepper,
171
+ denoise_fn=simple_denoising_func(
172
+ video_context=video_context,
173
+ audio_context=audio_context,
174
+ transformer=transformer,
175
+ ),
176
+ )
177
+
178
+ stage_1_output_shape = VideoPixelShape(
179
+ batch=1,
180
+ frames=num_frames,
181
+ width=width // 2,
182
+ height=height // 2,
183
+ fps=frame_rate,
184
+ )
185
+ stage_1_conditionings = combined_image_conditionings(
186
+ images=images,
187
+ height=stage_1_output_shape.height,
188
+ width=stage_1_output_shape.width,
189
+ video_encoder=video_encoder,
190
+ dtype=dtype,
191
+ device=self.device,
192
+ )
193
+ video_state, audio_state = denoise_audio_video(
194
+ output_shape=stage_1_output_shape,
195
+ conditionings=stage_1_conditionings,
196
+ noiser=noiser,
197
+ sigmas=stage_1_sigmas,
198
+ stepper=stepper,
199
+ denoising_loop_fn=denoising_loop,
200
+ components=self.pipeline_components,
201
+ dtype=dtype,
202
+ device=self.device,
203
+ )
204
+ first_pass_latent = video_state.latent.detach().clone() if return_first_pass else None
205
+
206
+ upscaled_video_latent = upsample_video(
207
+ latent=video_state.latent[:1],
208
+ video_encoder=video_encoder,
209
+ upsampler=self.model_ledger.spatial_upsampler(),
210
+ )
211
+
212
+ torch.cuda.synchronize()
213
+ cleanup_memory()
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, audio_state = denoise_audio_video(
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=audio_state.latent,
238
+ )
239
+
240
+ torch.cuda.synchronize()
241
+ del transformer
242
+ del video_encoder
243
+ cleanup_memory()
244
+
245
+ video_decoder = self.model_ledger.video_decoder()
246
+ decoded_video = vae_decode_video(video_state.latent, video_decoder, tiling_config, generator)
247
+ decoded_audio = vae_decode_audio(audio_state.latent, self.model_ledger.audio_decoder(), self.model_ledger.vocoder())
248
+ if not return_first_pass:
249
+ return decoded_video, decoded_audio
250
+
251
+ decoded_first_pass = vae_decode_video(first_pass_latent, video_decoder, tiling_config, generator)
252
+ return decoded_video, decoded_audio, decoded_first_pass
253
+
254
+ def _call_with_audio(
255
+ self,
256
+ prompt: str,
257
+ seed: int,
258
+ height: int,
259
+ width: int,
260
+ num_frames: int,
261
+ frame_rate: float,
262
+ images: list[ImageConditioningInput],
263
+ audio_path: str,
264
+ tiling_config: TilingConfig | None,
265
+ enhance_prompt: bool,
266
+ return_first_pass: bool,
267
+ ):
268
+
269
  generator = torch.Generator(device=self.device).manual_seed(seed)
270
  noiser = GaussianNoiser(generator=generator)
271
  stepper = EulerDiffusionStep()
 
346
  device=self.device,
347
  initial_audio_latent=encoded_audio_latent,
348
  )
349
+ first_pass_latent = video_state.latent.detach().clone() if return_first_pass else None
350
 
351
  torch.cuda.synchronize()
352
  cleanup_memory()
 
386
  del video_encoder
387
  cleanup_memory()
388
 
389
+ video_decoder = self.model_ledger.video_decoder()
390
  decoded_video = vae_decode_video(
391
  video_state.latent,
392
+ video_decoder,
393
  tiling_config,
394
  generator,
395
  )
 
397
  waveform=decoded_audio.waveform.squeeze(0),
398
  sampling_rate=decoded_audio.sampling_rate,
399
  )
400
+ if not return_first_pass:
401
+ return decoded_video, original_audio
402
+
403
+ decoded_first_pass = vae_decode_video(first_pass_latent, video_decoder, tiling_config, generator)
404
+ return decoded_video, original_audio, decoded_first_pass
405
 
406
 
407
  # Model repos
 
698
  duration: float,
699
  gpu_duration: float,
700
  enhance_prompt: bool = True,
701
+ return_first_pass: bool = False,
702
  seed: int = 42,
703
  randomize_seed: bool = True,
704
  height: int = 1024,
 
720
  duration: float,
721
  gpu_duration: float,
722
  enhance_prompt: bool = True,
723
+ return_first_pass: bool = False,
724
  seed: int = 42,
725
  randomize_seed: bool = True,
726
  height: int = 1024,
 
769
 
770
  apply_prepared_lora_state_to_pipeline()
771
 
772
+ pipeline_result = pipeline(
773
  prompt=prompt,
774
  seed=current_seed,
775
  height=int(height),
 
780
  audio_path=input_audio,
781
  tiling_config=tiling_config,
782
  enhance_prompt=enhance_prompt,
783
+ return_first_pass=return_first_pass,
784
  )
785
+ if return_first_pass:
786
+ video, audio, first_pass_video = pipeline_result
787
+ else:
788
+ video, audio = pipeline_result
789
+ first_pass_video = None
790
 
791
  log_memory("after pipeline call")
792
 
 
799
  video_chunks_number=video_chunks_number,
800
  )
801
 
802
+ first_pass_path = None
803
+ if return_first_pass and first_pass_video is not None:
804
+ first_pass_path = tempfile.mktemp(suffix=".mp4")
805
+ encode_video(
806
+ video=first_pass_video,
807
+ fps=frame_rate,
808
+ audio=audio,
809
+ output_path=first_pass_path,
810
+ video_chunks_number=video_chunks_number,
811
+ )
812
+
813
  log_memory("after encode_video")
814
+ return str(output_path), first_pass_path, current_seed
815
 
816
  except Exception as e:
817
  import traceback
818
  log_memory("on error")
819
  print(f"Error: {str(e)}\n{traceback.format_exc()}")
820
+ return None, None, current_seed
821
  with gr.Blocks(title="LTX-2.3 Image-to-Video") as demo:
822
  gr.Markdown("# LTX-2.3 Image-to-Video")
823
 
 
849
  with gr.Row():
850
  enhance_prompt = gr.Checkbox(label="Enhance Prompt", value=False)
851
  high_res = gr.Checkbox(label="High Resolution", value=True)
852
+ return_first_pass = gr.Checkbox(label="Return first pass preview", value=False)
853
  with gr.Column():
854
  gr.Markdown("### LoRA adapter strengths (set to 0 to disable)")
855
  distilled_strength = gr.Slider(label="Distilled LoRA strength", minimum=0.0, maximum=2.0, value=0.6, step=0.01)
 
865
 
866
  with gr.Column():
867
  output_video = gr.Video(label="Generated Video", autoplay=False)
868
+ first_pass_video = gr.Video(label="First Pass Preview", autoplay=False)
869
  gpu_duration = gr.Slider(
870
  label="ZeroGPU duration (seconds)",
871
  minimum=40.0,
 
884
  3.0,
885
  80.0,
886
  False,
887
+ False,
888
  42,
889
  True,
890
  1024,
 
896
  ],
897
  inputs=[
898
  first_image, last_image, input_audio, prompt, duration, gpu_duration,
899
+ enhance_prompt, return_first_pass, seed, randomize_seed, height, width,
900
  distilled_strength, quality_strength, reasoning_strength,
901
  ],
902
  )
 
948
  generate_btn.click(
949
  fn=generate_video,
950
  inputs=[
951
+ first_image, last_image, input_audio, prompt, duration, gpu_duration, enhance_prompt, return_first_pass,
952
  seed, randomize_seed, height, width,
953
  distilled_strength, quality_strength, reasoning_strength,
954
  ],
955
+ outputs=[output_video, first_pass_video, seed],
956
  )
957
 
958