๐ฌ Image-to-Video AI
Transform any image into a real animated video with AI
CogVideoX-5B ยท Free ZeroGPU ยท Real Diffusion Animation
""" ๐ฌ Image-to-Video AI Studio โ Real AI Animation Uses CogVideoX-5B-I2V diffusion model to generate REAL animated videos from images. Runs on HuggingFace ZeroGPU (free) โ actual AI video generation, not zoom/warp tricks. """ import spaces import torch import gradio as gr import tempfile import gc from PIL import Image from diffusers import CogVideoXImageToVideoPipeline from diffusers.utils import export_to_video, load_image # โโโ Model Loading (CPU at startup, GPU inside @spaces.GPU) โโโโโโโโโโโโโโโโโ print("๐ฆ Loading CogVideoX-5B-I2V pipeline to CPU...") pipe = CogVideoXImageToVideoPipeline.from_pretrained( "THUDM/CogVideoX-5b-I2V", torch_dtype=torch.bfloat16, ) print("โ Pipeline loaded to CPU successfully!") # โโโ Prompt Templates โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ CAMERA_PROMPTS = { "None (Auto)": "", "Static Shot": "static camera, no camera movement, steady shot", "Slow Zoom In": "camera slowly zooms in, push in shot", "Slow Zoom Out": "camera slowly zooms out, pull back reveal", "Pan Left": "camera pans slowly to the left, horizontal tracking", "Pan Right": "camera pans slowly to the right, horizontal tracking", "Tilt Up": "camera tilts upward slowly, revealing the sky", "Tilt Down": "camera tilts downward slowly", "Orbit Shot": "camera orbits around the subject slowly", "Dolly Forward": "camera moves forward smoothly, dolly shot", "Crane Up": "camera rises upward, crane shot, aerial movement", "Handheld": "slight handheld camera movement, natural sway", } STYLE_PROMPTS = { "Cinematic": "cinematic lighting, film grain, dramatic, movie quality, 4K", "Realistic": "photorealistic, natural lighting, high detail, sharp focus", "Dreamy": "soft focus, ethereal glow, dreamy atmosphere, gentle lighting", "Dynamic": "fast motion, energetic, vibrant, dynamic movement", "Slow Motion": "slow motion, graceful movement, smooth, elegant", "Nature Documentary": "nature documentary style, wildlife, natural beauty, BBC quality", "Anime / Artistic": "anime style, artistic, vibrant colors, stylized movement", } DURATION_FRAMES = { "~3 seconds (24 frames)": 24, "~6 seconds (49 frames)": 49, } # โโโ Core Video Generation โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ @spaces.GPU(duration=300) def generate_video( image, prompt, camera_motion, style, duration, num_inference_steps, guidance_scale, seed, use_random_seed, progress=gr.Progress(track_tqdm=True), ): """Generate real animated video using CogVideoX-5B-I2V diffusion model.""" if image is None: raise gr.Error("โ ๏ธ Please upload an image first!") if not prompt or prompt.strip() == "": raise gr.Error("โ ๏ธ Please describe what animation you want!") # Move pipeline to GPU pipe.to("cuda") pipe.vae.enable_tiling() pipe.vae.enable_slicing() # Build the full prompt parts = [prompt.strip()] camera_desc = CAMERA_PROMPTS.get(camera_motion, "") if camera_desc: parts.append(camera_desc) style_desc = STYLE_PROMPTS.get(style, "") if style_desc: parts.append(style_desc) parts.append("high quality, detailed, smooth motion") full_prompt = ". ".join(parts) print(f"๐ฌ Prompt: {full_prompt}") # Seed if use_random_seed: seed = torch.randint(0, 2**32, (1,)).item() generator = torch.Generator(device="cuda").manual_seed(int(seed)) # Frames num_frames = DURATION_FRAMES.get(duration, 49) # Resize image (CogVideoX native: 720x480 landscape, 480x720 portrait) if isinstance(image, str): image = load_image(image) img_w, img_h = image.size if img_w >= img_h: target_w, target_h = 720, 480 else: target_w, target_h = 480, 720 image_resized = image.resize((target_w, target_h), Image.LANCZOS) # Generate print(f"๐ Generating: {num_frames} frames, {num_inference_steps} steps") result = pipe( prompt=full_prompt, image=image_resized, num_videos_per_prompt=1, num_inference_steps=int(num_inference_steps), num_frames=num_frames, guidance_scale=float(guidance_scale), generator=generator, ).frames[0] # Export to MP4 out_path = tempfile.mktemp(suffix=".mp4") export_to_video(result, out_path, fps=8) # Cleanup pipe.to("cpu") gc.collect() torch.cuda.empty_cache() print(f"โ Video saved: {out_path}") return out_path, f"๐ฒ Seed: {seed} | Frames: {num_frames} | Steps: {num_inference_steps}" # โโโ Gradio UI โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ CSS = """ .main-header { text-align: center; padding: 20px 0 10px 0; } .main-header h1 { background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); -webkit-background-clip: text; -webkit-text-fill-color: transparent; font-size: 2.2em; margin-bottom: 5px; } .note-box { background: linear-gradient(135deg, #fff3cd 0%, #ffeeba 100%); border-left: 4px solid #ffc107; border-radius: 8px; padding: 12px 16px; margin: 10px 0; } .info-box { background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%); border-radius: 10px; padding: 15px; margin: 10px 0; } footer { display: none !important; } """ with gr.Blocks(title="๐ฌ Image-to-Video AI", css=CSS) as demo: gr.HTML("""
Transform any image into a real animated video with AI
CogVideoX-5B ยท Free ZeroGPU ยท Real Diffusion Animation
CogVideoX-5B-I2V by Tsinghua University ยท ZeroGPU (Free A10G)