Update app.py
Browse files
app.py
CHANGED
@@ -2,104 +2,51 @@ import gradio as gr
|
|
2 |
import torch
|
3 |
from diffusers import I2VGenXLPipeline
|
4 |
from diffusers.utils import export_to_gif, load_image
|
5 |
-
import tempfile
|
6 |
import spaces
|
7 |
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
pipeline = I2VGenXLPipeline.from_pretrained("ali-vilab/i2vgen-xl", torch_dtype=torch.float16, variant="fp16")
|
12 |
-
return pipeline
|
13 |
-
|
14 |
-
import gradio as gr
|
15 |
-
import torch
|
16 |
-
from diffusers import I2VGenXLPipeline
|
17 |
-
from diffusers.utils import export_to_gif, load_image
|
18 |
-
import tempfile
|
19 |
-
import spaces
|
20 |
|
21 |
@spaces.GPU
|
22 |
-
def
|
23 |
-
#
|
24 |
-
|
25 |
-
return pipeline
|
26 |
-
|
27 |
-
def generate_gif(prompt, image, negative_prompt, num_inference_steps, guidance_scale, seed):
|
28 |
-
# Initialize the pipeline within the function
|
29 |
-
pipeline = initialize_pipeline()
|
30 |
|
31 |
# Set the generator seed
|
32 |
-
generator = torch.
|
33 |
-
|
34 |
-
#
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
generator=generator
|
44 |
-
).frames[0]
|
45 |
-
else:
|
46 |
-
frames = pipeline(
|
47 |
-
prompt=prompt,
|
48 |
-
num_inference_steps=num_inference_steps,
|
49 |
-
negative_prompt=negative_prompt,
|
50 |
-
guidance_scale=guidance_scale,
|
51 |
-
generator=generator
|
52 |
-
).frames[0]
|
53 |
|
54 |
# Export to GIF
|
55 |
-
|
56 |
-
|
57 |
-
export_to_gif(frames, gif_path)
|
58 |
|
59 |
return gif_path
|
60 |
|
61 |
-
# Create the Gradio interface
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
# When generating from text, pass an empty string as the image input
|
78 |
-
text_generate_button.click(
|
79 |
-
fn=generate_gif,
|
80 |
-
inputs=[text_prompt, "", text_negative_prompt, text_num_inference_steps, text_guidance_scale, text_seed],
|
81 |
-
outputs=text_output_video
|
82 |
-
)
|
83 |
-
|
84 |
-
with gr.TabItem("Generate from Image"):
|
85 |
-
with gr.Row():
|
86 |
-
with gr.Column():
|
87 |
-
image_prompt = gr.Textbox(lines=2, placeholder="Enter your prompt here...", label="Prompt")
|
88 |
-
image_input = gr.Image(type="filepath", label="Input Image")
|
89 |
-
image_negative_prompt = gr.Textbox(lines=2, placeholder="Enter your negative prompt here...", label="Negative Prompt")
|
90 |
-
image_num_inference_steps = gr.Slider(1, 100, step=1, value=50, label="Number of Inference Steps")
|
91 |
-
image_guidance_scale = gr.Slider(1, 20, step=0.1, value=9.0, label="Guidance Scale")
|
92 |
-
image_seed = gr.Number(label="Seed", value=8888)
|
93 |
-
image_generate_button = gr.Button("Generate GIF")
|
94 |
-
|
95 |
-
with gr.Column():
|
96 |
-
image_output_video = gr.Video(label="Generated GIF")
|
97 |
-
|
98 |
-
image_generate_button.click(
|
99 |
-
fn=generate_gif,
|
100 |
-
inputs=[image_prompt, image_input, image_negative_prompt, image_num_inference_steps, image_guidance_scale, image_seed],
|
101 |
-
outputs=image_output_video
|
102 |
-
)
|
103 |
|
104 |
# Launch the interface
|
105 |
-
|
|
|
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, value=50, label="Number of Inference Steps"),
|
43 |
+
gr.Slider(1, 20, step=0.1, value=9.0, label="Guidance Scale"),
|
44 |
+
gr.Number(label="Seed", value=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()
|