helloName / app.py
sushann222's picture
Update app.py
dead101 verified
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