K00B404 commited on
Commit
a174f44
·
verified ·
1 Parent(s): a623d98

Create app_3.py

Browse files
Files changed (1) hide show
  1. app_3.py +67 -0
app_3.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from diffusers import AnimateDiffSparseControlNetPipeline
3
+ from diffusers.models import AutoencoderKL, MotionAdapter, SparseControlNetModel
4
+ from diffusers.schedulers import DPMSolverMultistepScheduler
5
+ from diffusers.utils import export_to_gif, load_image
6
+
7
+ torch.backends.cuda.matmul.allow_tf32 = True # Enable TF32 for speed
8
+ device = "cuda"
9
+ dtype = torch.float16
10
+
11
+ # Model IDs
12
+ model_id = "SG161222/Realistic_Vision_V5.1_noVAE"
13
+ motion_adapter_id = "guoyww/animatediff-motion-adapter-v1-5-3"
14
+ controlnet_id = "guoyww/animatediff-sparsectrl-scribble"
15
+ lora_adapter_id = "guoyww/animatediff-motion-lora-v1-5-3"
16
+ vae_id = "stabilityai/sd-vae-ft-mse"
17
+
18
+ # Load models to device once
19
+ motion_adapter = MotionAdapter.from_pretrained(motion_adapter_id, torch_dtype=dtype, device_map="auto")
20
+ controlnet = SparseControlNetModel.from_pretrained(controlnet_id, torch_dtype=dtype, device_map="auto")
21
+ vae = AutoencoderKL.from_pretrained(vae_id, torch_dtype=dtype, device_map="auto")
22
+
23
+ # Use DPMSolverMultistepScheduler with optimizations
24
+ scheduler = DPMSolverMultistepScheduler.from_pretrained(
25
+ model_id, subfolder="scheduler", beta_schedule="linear",
26
+ algorithm_type="dpmsolver++", use_karras_sigmas=True,
27
+ )
28
+
29
+ pipe = AnimateDiffSparseControlNetPipeline.from_pretrained(
30
+ model_id, motion_adapter=motion_adapter, controlnet=controlnet,
31
+ vae=vae, scheduler=scheduler, torch_dtype=dtype,
32
+ ).to(device)
33
+
34
+ # Enable memory optimizations
35
+ pipe.enable_xformers_memory_efficient_attention()
36
+ pipe.load_lora_weights(lora_adapter_id, adapter_name="motion_lora")
37
+ pipe.fuse_lora(lora_scale=1.0)
38
+
39
+ # Preload conditioning frames
40
+ image_files = [
41
+ "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/animatediff-scribble-1.png",
42
+ "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/animatediff-scribble-2.png",
43
+ "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/animatediff-scribble-3.png"
44
+ ]
45
+ condition_frame_indices = [0, 8, 15]
46
+ conditioning_frames = [load_image(img) for img in image_files]
47
+
48
+ # Generator for reproducibility
49
+ generator = torch.Generator(device).manual_seed(1337)
50
+
51
+ # Inference with memory optimizations
52
+ with torch.inference_mode():
53
+ video = pipe(
54
+ prompt="an aerial view of a cyberpunk city, night time, neon lights, masterpiece, high quality",
55
+ negative_prompt="low quality, worst quality, letterboxed",
56
+ num_inference_steps=25,
57
+ conditioning_frames=conditioning_frames,
58
+ controlnet_conditioning_scale=1.0,
59
+ controlnet_frame_indices=condition_frame_indices,
60
+ generator=generator,
61
+ ).frames[0]
62
+
63
+ export_to_gif(video, "output.gif")
64
+
65
+ # Free memory
66
+ del pipe, motion_adapter, controlnet, vae
67
+ torch.cuda.empty_cache()