mrcuddle's picture
Update app.py
fe77a8e verified
raw
history blame
2.41 kB
import gradio as gr
import torch
from diffusers import I2VGenXLPipeline
from diffusers.utils import export_to_gif, load_image
import tempfile
# 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)
def generate_gif(prompt, image, negative_prompt, num_inference_steps, guidance_scale, seed):
# 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.Tabs() as demo:
with gr.TabItem("Generate from Text or Image"):
interface = gr.Interface(
fn=generate_gif,
inputs=[
gr.Textbox(lines=2, placeholder="Enter your prompt here...", label="Prompt"),
gr.Image(type="filepath", label="Input Image (optional)"),
gr.Textbox(lines=2, placeholder="Enter your negative prompt here...", label="Negative Prompt"),
gr.Slider(1, 100, step=1, value=50, label="Number of Inference Steps"),
gr.Slider(1, 20, step=0.1, value=9.0, label="Guidance Scale"),
gr.Number(label="Seed", value=8888)
],
outputs=gr.Video(label="Generated GIF"),
title="I2VGen-XL GIF Generator",
description="Generate a GIF from a text prompt and/or an image using the I2VGen-XL model."
)
# Launch the interface
demo.launch()