Spaces:
Runtime error

File size: 1,338 Bytes
292cddb
 
 
cd97c88
1e835d1
292cddb
1e835d1
dead101
1e835d1
292cddb
1ce57f1
be98c35
cd97c88
1e835d1
292cddb
1ce57f1
292cddb
1e835d1
292cddb
 
1ce57f1
 
1e835d1
1ce57f1
 
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
import torch
from diffusers import StableVideoDiffusionPipeline
from diffusers.utils import load_image, export_to_video

# Load the StableVideoDiffusionPipeline with optimizations for lower memory usage
pipe = StableVideoDiffusionPipeline.from_pretrained(
    "stabilityai/stable-video-diffusion-img2vid-xt",
    torch_dtype=torch.float32,  # Use float32 since float16 is not supported on CPU
    low_cpu_mem_usage=True      # Reduce CPU memory usage
)
pipe.enable_attention_slicing()  # Enable memory-efficient attention
pipe.to("cpu")  # Explicitly move the pipeline to CPU

# Load the conditioning image and resize to a smaller resolution for memory optimization
image = load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/svd/rocket.png")
image = image.resize((256, 144))  # Further reduce the resolution to minimize memory usage

# Set a manual seed for reproducibility
generator = torch.manual_seed(42)

# Generate video frames with a reduced decode_chunk_size and fewer inference steps to lower memory consumption
frames = pipe(image, decode_chunk_size=2, generator=generator, num_inference_steps=10).frames[0]

# Export the generated frames to a video file with a lower FPS for memory optimization
export_to_video(frames, "generated.mp4", fps=4)  # Lower FPS to reduce memory usage