eeuuia commited on
Commit
b743563
·
verified ·
1 Parent(s): e5a215d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +87 -38
app.py CHANGED
@@ -108,19 +108,12 @@ def prepare_and_generate_video(
108
  progress=gr.Progress(track_tqdm=True)
109
  ):
110
  try:
111
- conditions_data = [
112
- (condition_image_1, condition_strength_1, condition_frame_index_1),
113
- (condition_image_2, condition_strength_2, condition_frame_index_2)
114
- ]
115
-
116
- if randomize_seed:
117
- seed = random.randint(0, 2**32 - 1)
118
-
119
  num_frames = int(duration * FPS) + 1
120
  temporal_compression = pipeline.vae_temporal_compression_ratio
121
  num_frames = ((num_frames - 1) // temporal_compression) * temporal_compression + 1
122
 
123
- # Etapa 1: Preparar condições para baixa resolução
124
  downscale_factor = 2 / 3
125
  downscaled_height = int(height * downscale_factor)
126
  downscaled_width = int(width * downscale_factor)
@@ -128,45 +121,103 @@ def prepare_and_generate_video(
128
  downscaled_height, downscaled_width, pipeline.vae_temporal_compression_ratio
129
  )
130
 
131
- conditions_low_res = []
132
- for image, strength, frame_index in conditions_data:
133
- if image is not None:
134
- processed_image = ImageOps.fit(image, (downscaled_width, downscaled_height), Image.LANCZOS)
135
- conditions_low_res.append(LTXVideoCondition(
136
- image=processed_image, strength=strength, frame_index=int(frame_index)
137
- ))
138
 
139
- pipeline_args_low_res = {"conditions": conditions_low_res} if conditions_low_res else {}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
140
 
 
 
 
 
 
 
141
  latents = pipeline(
142
- prompt=prompt, negative_prompt=negative_prompt, width=downscaled_width, height=downscaled_height,
143
- num_frames=num_frames, generator=torch.Generator().manual_seed(seed),
144
- output_type="latent", **pipeline_args_low_res
 
 
 
 
 
 
 
 
 
 
 
145
  ).frames
146
 
147
- # Etapa 2: Upscale
148
  upscaled_height, upscaled_width = downscaled_height * 2, downscaled_width * 2
149
- upscaled_latents = pipe_upsample(latents=latents, output_type="latent").frames
150
-
151
- # Etapa 3: Preparar condições para alta resolução (para manter frames imutáveis)
152
- conditions_high_res = []
153
- for image, strength, frame_index in conditions_data:
154
- if image is not None:
155
- processed_image_high_res = ImageOps.fit(image, (upscaled_width, upscaled_height), Image.LANCZOS)
156
- conditions_high_res.append(LTXVideoCondition(
157
- image=processed_image_high_res, strength=strength, frame_index=int(frame_index)
158
- ))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
159
 
160
- pipeline_args_high_res = {"conditions": conditions_high_res} if conditions_high_res else {}
161
 
 
162
  final_video_frames_np = pipeline(
163
- prompt=prompt, negative_prompt=negative_prompt, width=upscaled_width, height=upscaled_height,
164
- num_frames=num_frames, denoise_strength=0.999, latents=upscaled_latents,
 
 
 
 
 
 
 
 
 
 
 
165
  generator=torch.Generator(device="cuda").manual_seed(seed),
166
- output_type="np", **pipeline_args_high_res
 
167
  ).frames[0]
168
 
169
- # Etapa 4: Exportação
170
  video_uint8_frames = [(frame * 255).astype(np.uint8) for frame in final_video_frames_np]
171
  output_filename = "output.mp4"
172
  with imageio.get_writer(output_filename, fps=FPS, quality=8, macro_block_size=1) as writer:
@@ -178,8 +229,6 @@ def prepare_and_generate_video(
178
 
179
  except Exception as e:
180
  print(f"Ocorreu um erro: {e}")
181
- import traceback
182
- traceback.print_exc()
183
  return None, seed
184
 
185
  # --- Interface Gráfica com Gradio ---
 
108
  progress=gr.Progress(track_tqdm=True)
109
  ):
110
  try:
111
+ # Lógica para agrupar as condições *dentro* da função
112
+ # Cálculo de frames e resolução
 
 
 
 
 
 
113
  num_frames = int(duration * FPS) + 1
114
  temporal_compression = pipeline.vae_temporal_compression_ratio
115
  num_frames = ((num_frames - 1) // temporal_compression) * temporal_compression + 1
116
 
 
117
  downscale_factor = 2 / 3
118
  downscaled_height = int(height * downscale_factor)
119
  downscaled_width = int(width * downscale_factor)
 
121
  downscaled_height, downscaled_width, pipeline.vae_temporal_compression_ratio
122
  )
123
 
 
 
 
 
 
 
 
124
 
125
+
126
+ conditions = []
127
+ if condition_image_1 is not None:
128
+ condition_image_1 = ImageOps.fit(condition_image_1, (downscaled_width, downscaled_height), Image.LANCZOS)
129
+ conditions.append(LTXVideoCondition(
130
+ image=condition_image_1,
131
+ strength=condition_strength_1,
132
+ frame_index=int(condition_frame_index_1)
133
+ ))
134
+ if condition_image_2 is not None:
135
+ condition_image_2 = ImageOps.fit(condition_image_2, (downscaled_width, downscaled_height), Image.LANCZOS)
136
+ conditions.append(LTXVideoCondition(
137
+ image=condition_image_2,
138
+ strength=condition_strength_2,
139
+ frame_index=int(condition_frame_index_2)
140
+ ))
141
+
142
+ pipeline_args = {}
143
+ if conditions:
144
+ pipeline_args["conditions"] = conditions
145
 
146
+ # Manipulação da seed
147
+ if randomize_seed:
148
+ seed = random.randint(0, 2**32 - 1)
149
+
150
+
151
+ # ETAPA 1: Geração do vídeo em baixa resolução
152
  latents = pipeline(
153
+ prompt=prompt,
154
+ negative_prompt=negative_prompt,
155
+ width=downscaled_width,
156
+ height=downscaled_height,
157
+ num_frames=num_frames,
158
+ timesteps=[1000, 993, 987, 981, 975, 909, 725, 0.03],
159
+ decode_timestep=0.05,
160
+ decode_noise_scale=0.025,
161
+ image_cond_noise_scale=0.0,
162
+ guidance_scale=guidance_scale,
163
+ guidance_rescale=0.7,
164
+ generator=torch.Generator().manual_seed(seed),
165
+ output_type="latent",
166
+ **pipeline_args
167
  ).frames
168
 
169
+ # ETAPA 2: Upscale dos latentes
170
  upscaled_height, upscaled_width = downscaled_height * 2, downscaled_width * 2
171
+ upscaled_latents = pipe_upsample(
172
+ latents=latents,
173
+ output_type="latent"
174
+ ).frames
175
+
176
+
177
+
178
+ conditions = []
179
+ if condition_image_1 is not None:
180
+ condition_image_1 = ImageOps.fit(condition_image_1, (upscaled_width, upscaled_height), Image.LANCZOS)
181
+ conditions.append(LTXVideoCondition(
182
+ image=condition_image_1,
183
+ strength=condition_strength_1,
184
+ frame_index=int(condition_frame_index_1)
185
+ ))
186
+ if condition_image_2 is not None:
187
+ condition_image_2 = ImageOps.fit(condition_image_2, (upscaled_width, upscaled_height), Image.LANCZOS)
188
+ conditions.append(LTXVideoCondition(
189
+ image=condition_image_2,
190
+ strength=condition_strength_2,
191
+ frame_index=int(condition_frame_index_2)
192
+ ))
193
+
194
+ pipeline_args = {}
195
+ if conditions:
196
+ pipeline_args["conditions"] = conditions
197
+
198
 
 
199
 
200
+ # ETAPA 3: Denoise final em alta resolução
201
  final_video_frames_np = pipeline(
202
+ prompt=prompt,
203
+ negative_prompt=negative_prompt,
204
+ width=upscaled_width,
205
+ height=upscaled_height,
206
+ num_frames=num_frames,
207
+ denoise_strength=0.999,
208
+ timesteps=[1000, 909, 725, 421, 0],
209
+ latents=upscaled_latents,
210
+ decode_timestep=0.05,
211
+ decode_noise_scale=0.025,
212
+ image_cond_noise_scale=0.0,
213
+ guidance_scale=guidance_scale,
214
+ guidance_rescale=0.7,
215
  generator=torch.Generator(device="cuda").manual_seed(seed),
216
+ output_type="np",
217
+ **pipeline_args
218
  ).frames[0]
219
 
220
+ # Exportação para arquivo MP4
221
  video_uint8_frames = [(frame * 255).astype(np.uint8) for frame in final_video_frames_np]
222
  output_filename = "output.mp4"
223
  with imageio.get_writer(output_filename, fps=FPS, quality=8, macro_block_size=1) as writer:
 
229
 
230
  except Exception as e:
231
  print(f"Ocorreu um erro: {e}")
 
 
232
  return None, seed
233
 
234
  # --- Interface Gráfica com Gradio ---