linoyts HF Staff commited on
Commit
8e2aeca
Β·
verified Β·
1 Parent(s): b640355
Files changed (1) hide show
  1. app.py +17 -16
app.py CHANGED
@@ -15,7 +15,7 @@ from controlnet_aux import CannyDetector
15
  from PIL import Image
16
  import cv2
17
 
18
-
19
  dtype = torch.bfloat16
20
  device = "cuda" if torch.cuda.is_available() else "cpu"
21
 
@@ -185,10 +185,9 @@ def process_input_video(reference_video, width, height):
185
  processed_video = process_video_for_canny(video, width, height)
186
 
187
  # Create a preview video file for display
188
- fps = 24
189
  with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as tmp_file:
190
  preview_path = tmp_file.name
191
- export_to_video(processed_video, preview_path, fps=fps)
192
 
193
  return preview_path
194
 
@@ -242,9 +241,8 @@ def generate_video(
242
  if randomize_seed:
243
  seed = random.randint(0, 2**32 - 1)
244
 
245
- # Calculate number of frames from duration (24 fps)
246
- fps = 24
247
- num_frames = int(duration * fps) + 1 # +1 for proper frame count
248
  # Ensure num_frames is valid for the model (multiple of temporal compression + 1)
249
  temporal_compression = pipeline.vae_temporal_compression_ratio
250
  num_frames = ((num_frames - 1) // temporal_compression) * temporal_compression + 1
@@ -305,7 +303,7 @@ def generate_video(
305
  progress(0.8, desc="Final denoising and processing...")
306
 
307
  # 3. Denoise the upscaled video
308
- video_output = pipeline(
309
  prompt=prompt,
310
  negative_prompt=negative_prompt,
311
  width=upscaled_width,
@@ -319,22 +317,24 @@ def generate_video(
319
  decode_noise_scale = 0.025,
320
  image_cond_noise_scale=0.025,
321
  generator=torch.Generator(device="cuda").manual_seed(seed),
322
- output_type="pil",
323
  ).frames[0]
324
 
325
  progress(0.9, desc="Finalizing output...")
326
 
327
- # 4. Downscale to expected resolution
328
- video_output = [frame.resize((width, height)) for frame in video_output]
329
 
330
  # Export to temporary file
331
- with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as tmp_file:
332
- output_path = tmp_file.name
333
- export_to_video(video_output, output_path, fps=fps)
 
 
 
 
334
 
335
  progress(1.0, desc="Complete!")
336
 
337
- return output_path, seed
338
 
339
  except Exception as e:
340
  print(e)
@@ -346,8 +346,9 @@ with gr.Blocks(theme=gr.themes.Ocean(font=[gr.themes.GoogleFont("Lexend Deca"),
346
  """
347
  # Canny Control LTX Video Distilled
348
 
349
- LTX Video 0.9.7 Distilled with [control canny ICLoRA](https://huggingface.co/Lightricks/LTX-Video-ICLoRA-canny-13b-0.9.7):
350
- control AI video generation - by concatenation of control signals and Canny LoRA trained on just a few samples ✨
 
351
  """
352
  )
353
 
 
15
  from PIL import Image
16
  import cv2
17
 
18
+ FPS = 24
19
  dtype = torch.bfloat16
20
  device = "cuda" if torch.cuda.is_available() else "cpu"
21
 
 
185
  processed_video = process_video_for_canny(video, width, height)
186
 
187
  # Create a preview video file for display
 
188
  with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as tmp_file:
189
  preview_path = tmp_file.name
190
+ export_to_video(processed_video, preview_path, fps=FPS)
191
 
192
  return preview_path
193
 
 
241
  if randomize_seed:
242
  seed = random.randint(0, 2**32 - 1)
243
 
244
+ # Calculate number of frames from duration
245
+ num_frames = int(duration * FPS) + 1 # +1 for proper frame count
 
246
  # Ensure num_frames is valid for the model (multiple of temporal compression + 1)
247
  temporal_compression = pipeline.vae_temporal_compression_ratio
248
  num_frames = ((num_frames - 1) // temporal_compression) * temporal_compression + 1
 
303
  progress(0.8, desc="Final denoising and processing...")
304
 
305
  # 3. Denoise the upscaled video
306
+ final_video_frames_np = pipeline(
307
  prompt=prompt,
308
  negative_prompt=negative_prompt,
309
  width=upscaled_width,
 
317
  decode_noise_scale = 0.025,
318
  image_cond_noise_scale=0.025,
319
  generator=torch.Generator(device="cuda").manual_seed(seed),
320
+ output_type="np",
321
  ).frames[0]
322
 
323
  progress(0.9, desc="Finalizing output...")
324
 
 
 
325
 
326
  # Export to temporary file
327
+ video_uint8_frames = [(frame * 255).astype(np.uint8) for frame in final_video_frames_np]
328
+ output_filename = "output.mp4"
329
+ with imageio.get_writer(output_filename, fps=FPS, quality=8, macro_block_size=1) as writer:
330
+ for frame_idx, frame_data in enumerate(video_uint8_frames):
331
+ progress((frame_idx + 1) / len(video_uint8_frames), desc="Encoding video frames...")
332
+ writer.append_data(frame_data)
333
+
334
 
335
  progress(1.0, desc="Complete!")
336
 
337
+ return output_filename, seed
338
 
339
  except Exception as e:
340
  print(e)
 
346
  """
347
  # Canny Control LTX Video Distilled
348
 
349
+ **Fast & canny-controlled video generation using LTX Video 0.9.7 Distilled with [ICLoRA]**(https://huggingface.co/Lightricks/LTX-Video-ICLoRA-canny-13b-0.9.7)
350
+
351
+ achieved by concatenation of control signals and Canny LoRA trained on just a few samples ✨
352
  """
353
  )
354