import torch from diffusers import AnimateDiffPipeline, DDIMScheduler, MotionAdapter from diffusers.utils import export_to_gif # Load motion adapter adapter = MotionAdapter.from_pretrained("guoyww/animatediff-motion-adapter-v1-5-2", torch_dtype=torch.float16) # Load SD 1.5-based model model_id = "SG161222/Realistic_Vision_V5.1_noVAE" pipe = AnimateDiffPipeline.from_pretrained(model_id, torch_dtype=torch.float16) # Attach motion adapter pipe.motion_adapter = adapter # Configure scheduler properly pipe.scheduler = DDIMScheduler.from_config(pipe.scheduler.config) # Enable memory-efficient settings pipe.enable_xformers_memory_efficient_attention() # Speeds up inference pipe.enable_vae_slicing() # Saves VRAM pipe.enable_model_cpu_offload() # Offloads to CPU when VRAM is low # Set up inference generator = torch.manual_seed(42) output = pipe( prompt=( "masterpiece, best quality, highly detailed, ultradetailed, sunset, " "orange sky, warm lighting, fishing boats, ocean waves, seagulls, " "rippling water, wharf, silhouette, serene atmosphere, dusk, evening glow, " "golden hour, coastal landscape, seaside scenery" ), negative_prompt="low quality, blurry, distorted, deformed, ugly", num_frames=16, guidance_scale=7.5, num_inference_steps=25, generator=generator, ) # Save animation as GIF export_to_gif(output.frames[0], "animation.gif")