|
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(): |
|
|
|
device = "cuda" if torch.cuda.is_available() else "cpu" |
|
|
|
|
|
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): |
|
|
|
pipeline, device = initialize_pipeline() |
|
|
|
|
|
generator = torch.Generator(device=device).manual_seed(seed) |
|
|
|
|
|
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] |
|
|
|
|
|
with tempfile.NamedTemporaryFile(delete=False, suffix=".gif") as tmp_gif: |
|
gif_path = tmp_gif.name |
|
export_to_gif(frames, gif_path) |
|
|
|
return gif_path |
|
|
|
|
|
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 |
|
) |
|
|
|
|
|
demo.launch() |
|
|