Delete import torch import numpy as np from diffusers import StableVideoDiffusionPipeline from diffusers.utils import load_image, export_to_video from PIL import Image # --- Configuration --- MODEL_ID = "stabilityai/stable-video-diffusion-img2vid-xt" INPUT_IMAGE_PATH = "1000179121.jpg" # Make sure your image is in the same directory OUTPUT_VIDEO_PATH = "svd_cpu_output.mp4" DEVICE = "cpu" # Recommended resolution for SVD TARGET_SIZE = (1024, 576) # Generation settings for CPU
Create import torch import numpy as np from diffusers import StableVideoDiffusionPipeline from diffusers.utils import load_image, export_to_video from PIL import Image # --- Configuration --- MODEL_ID = "stabilityai/stable-video-diffusion-img2vid-xt" INPUT_IMAGE_PATH = "1000179121.jpg" # Make sure your image is in the same directory OUTPUT_VIDEO_PATH = "svd_cpu_output.mp4" DEVICE = "cpu" # Recommended resolution for SVD TARGET_SIZE = (1024, 576) # Generation settings for CPU/Low-Memory: # decode_chunk_size=1 is the most memory-efficient setting, crucial for CPU. # It decodes one frame at a time. This will make generation EXTREMELY SLOW. DECODE_CHUNK_SIZE = 1 NUM_FRAMES = 25 MOTION_BUCKET_ID = 127 SEED = 42 def preprocess_image(image_path): """Loads and resizes the input image to the required resolution.""" try: image = load_image(image_path) image = image.resize(TARGET_SIZE) return image except FileNotFoundError: print(f"Error: Input image not found at {image_path}") exit() except Exception as e: print(f"Error loading image: {e}") exit() def generate_video(): print(f"Loading model on {DEVICE}...") # Load the pipeline. We do not use torch_dtype=torch.float16 as it can be slower on CPU. pipe = StableVideoDiffusionPipeline.from_pretrained(MODEL_ID) # --- CRITICAL CPU LINE --- pipe.to(DEVICE) print("Model loaded successfully.") image = preprocess_image(INPUT_IMAGE_PATH) generator = torch.manual_seed(SEED) print(f"\nStarting video generation on CPU... (This will take a very long time)") try: # Run the generation video_frames = pipe( image, decode_chunk_size=DECODE_CHUNK_SIZE, generator=generator, motion_bucket_id=MOTION_BUCKET_ID, num_frames=NUM_FRAMES ).frames[0] # Save the video export_to_video(video_frames, OUTPUT_VIDEO_PATH) print(f"\n--- Generation Complete ---") print(f"Output saved to: {OUTPUT_VIDEO_PATH}") print(f"Frames: {NUM_FRAMES}, Resolution: {TARGET_SIZE[0]}x{TARGET_SIZE[1]}") except Exception as e: print(f"\nAn error occurred during generation: {e}") print("Tip: If this is a memory error, you may need more system RAM (32GB+ recommended) or you may need to reduce NUM_FRAMES.") if __name__ == "__main__": generate_video()