import gradio as gr import torch from diffusers import I2VGenXLPipeline from diffusers.utils import export_to_gif, load_image import tempfile import spaces @spaces.GPU def initialize_pipeline(): # Check if CUDA is available and set the device device = "cuda" if torch.cuda.is_available() else "cpu" # Initialize the pipeline with CUDA support pipeline = I2VGenXLPipeline.from_pretrained("ali-vilab/i2vgen-xl", torch_dtype=torch.float16, variant="fp16") pipeline.to(device) return pipeline, device def generate_gif(prompt, image, negative_prompt, num_inference_steps, guidance_scale, seed): # Initialize the pipeline and device within the function pipeline, device = initialize_pipeline() # Set the generator seed generator = torch.Generator(device=device).manual_seed(seed) # Check if an image is provided if image is not None: image = load_image(image).convert("RGB") frames = pipeline( prompt=prompt, image=image, num_inference_steps=num_inference_steps, negative_prompt=negative_prompt, guidance_scale=guidance_scale, generator=generator ).frames[0] else: frames = pipeline( prompt=prompt, num_inference_steps=num_inference_steps, negative_prompt=negative_prompt, guidance_scale=guidance_scale, generator=generator ).frames[0] # Export to GIF with tempfile.NamedTemporaryFile(delete=False, suffix=".gif") as tmp_gif: gif_path = tmp_gif.name export_to_gif(frames, gif_path) return gif_path # Create the Gradio interface with tabs with gr.Blocks() as demo: with gr.TabItem("Generate from Text or Image"): with gr.Row(): with gr.Column(): prompt = gr.Textbox(lines=2, placeholder="Enter your prompt here...", label="Prompt") image = gr.Image(type="filepath", label="Input Image (optional)") negative_prompt = gr.Textbox(lines=2, placeholder="Enter your negative prompt here...", label="Negative Prompt") num_inference_steps = gr.Slider(1, 100, step=1, value=50, label="Number of Inference Steps") guidance_scale = gr.Slider(1, 20, step=0.1, value=9.0, label="Guidance Scale") seed = gr.Number(label="Seed", value=8888) generate_button = gr.Button("Generate GIF") with gr.Column(): output_video = gr.Video(label="Generated GIF") generate_button.click( fn=generate_gif, inputs=[prompt, image, negative_prompt, num_inference_steps, guidance_scale, seed], outputs=output_video ) # Launch the interface demo.launch()