Spaces:
Running
on
Zero
Running
on
Zero
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from diffusers import I2VGenXLPipeline
|
4 |
+
from diffusers.utils import export_to_gif, load_image
|
5 |
+
import spaces
|
6 |
+
|
7 |
+
# Initialize the pipeline
|
8 |
+
pipeline = I2VGenXLPipeline.from_pretrained("ali-vilab/i2vgen-xl", torch_dtype=torch.float16, variant="fp16")
|
9 |
+
pipeline.enable_model_cpu_offload()
|
10 |
+
|
11 |
+
@spaces.GPU
|
12 |
+
def generate_gif(image, prompt, negative_prompt, num_inference_steps, guidance_scale, seed):
|
13 |
+
# Load the image
|
14 |
+
image = load_image(image).convert("RGB")
|
15 |
+
|
16 |
+
# Set the generator seed
|
17 |
+
generator = torch.manual_seed(seed)
|
18 |
+
|
19 |
+
# Generate the frames
|
20 |
+
frames = pipeline(
|
21 |
+
prompt=prompt,
|
22 |
+
image=image,
|
23 |
+
num_inference_steps=num_inference_steps,
|
24 |
+
negative_prompt=negative_prompt,
|
25 |
+
guidance_scale=guidance_scale,
|
26 |
+
generator=generator
|
27 |
+
).frames[0]
|
28 |
+
|
29 |
+
# Export to GIF
|
30 |
+
gif_path = "i2v.gif"
|
31 |
+
export_to_gif(frames, gif_path)
|
32 |
+
|
33 |
+
return gif_path
|
34 |
+
|
35 |
+
# Create the Gradio interface
|
36 |
+
iface = gr.Interface(
|
37 |
+
fn=generate_gif,
|
38 |
+
inputs=[
|
39 |
+
gr.Image(type="filepath", label="Input Image"),
|
40 |
+
gr.Textbox(lines=2, placeholder="Enter your prompt here...", label="Prompt"),
|
41 |
+
gr.Textbox(lines=2, placeholder="Enter your negative prompt here...", label="Negative Prompt"),
|
42 |
+
gr.Slider(1, 100, step=1, default=50, label="Number of Inference Steps"),
|
43 |
+
gr.Slider(1, 20, step=0.1, default=9.0, label="Guidance Scale"),
|
44 |
+
gr.Number(label="Seed", default=8888)
|
45 |
+
],
|
46 |
+
outputs=gr.File(label="Generated GIF"),
|
47 |
+
title="I2VGen-XL GIF Generator",
|
48 |
+
description="Generate a GIF from an image and a prompt using the I2VGen-XL model."
|
49 |
+
)
|
50 |
+
|
51 |
+
# Launch the interface
|
52 |
+
iface.launch()
|