Views
No views yet
![]() |
![]() |
1import torch
2
3from diffusers import AnimateDiffSparseControlNetPipeline
4from diffusers.models import AutoencoderKL, MotionAdapter, SparseControlNetModel
5from diffusers.schedulers import DPMSolverMultistepScheduler
6from diffusers.utils import export_to_gif, load_image
7
8
9model_id = "SG161222/Realistic_Vision_V5.1_noVAE"
10motion_adapter_id = "guoyww/animatediff-motion-adapter-v1-5-3"
11controlnet_id = "guoyww/animatediff-sparsectrl-rgb"
12lora_adapter_id = "guoyww/animatediff-motion-lora-v1-5-3"
13vae_id = "stabilityai/sd-vae-ft-mse"
14device = "cuda"
15
16motion_adapter = MotionAdapter.from_pretrained(motion_adapter_id, torch_dtype=torch.float16).to(device)
17controlnet = SparseControlNetModel.from_pretrained(controlnet_id, torch_dtype=torch.float16).to(device)
18vae = AutoencoderKL.from_pretrained(vae_id, torch_dtype=torch.float16).to(device)
19scheduler = DPMSolverMultistepScheduler.from_pretrained(
20 model_id,
21 subfolder="scheduler",
22 beta_schedule="linear",
23 algorithm_type="dpmsolver++",
24 use_karras_sigmas=True,
25)
26pipe = AnimateDiffSparseControlNetPipeline.from_pretrained(
27 model_id,
28 motion_adapter=motion_adapter,
29 controlnet=controlnet,
30 vae=vae,
31 scheduler=scheduler,
32 torch_dtype=torch.float16,
33).to(device)
34pipe.load_lora_weights(lora_adapter_id, adapter_name="motion_lora")
35
36image = load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/animatediff-firework.png")
37
38video = pipe(
39 prompt="closeup face photo of man in black clothes, night city street, bokeh, fireworks in background",
40 negative_prompt="low quality, worst quality",
41 num_inference_steps=25,
42 conditioning_frames=image,
43 controlnet_frame_indices=[0],
44 controlnet_conditioning_scale=1.0,
45 generator=torch.Generator().manual_seed(42),
46).frames[0]
47export_to_gif(video, "output.gif")