import gradio as gr import spaces import torch import tempfile from diffusers import AutoencoderKLWan, WanTransformer3DModel, WanPipeline from diffusers.utils import export_to_video MODEL_ID = "Wan-AI/Wan2.2-TI2V-5B" @spaces.GPU(duration=60) def generate_video(prompt): if not prompt or not prompt.strip(): raise gr.Error("Please enter a prompt.") if not torch.cuda.is_available(): raise gr.Error("CUDA GPU is not available.") try: pipe = WanPipeline.from_pretrained( MODEL_ID, torch_dtype=torch.bfloat16 ) pipe.enable_model_cpu_offload() result = pipe( prompt=prompt, height=480, width=832, num_frames=49, num_inference_steps=20, guidance_scale=5.0 ) frames = result.frames[0] output_file = tempfile.NamedTemporaryFile( suffix=".mp4", delete=False ).name export_to_video( frames, output_file, fps=16 ) del pipe torch.cuda.empty_cache() return output_file except Exception as e: torch.cuda.empty_cache() raise gr.Error(f"Generation failed: {str(e)}") demo = gr.Interface( fn=generate_video, inputs=gr.Textbox( label="Prompt", lines=5, placeholder="Describe the video you want to generate..." ), outputs=gr.Video(label="Generated Video"), title="Wan 2.2 Video Generator", description="Experimental Wan2.2-TI2V-5B generation using Hugging Face ZeroGPU." ) if __name__ == "__main__": demo.launch()