File size: 1,416 Bytes
3b59f84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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")