Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -1,112 +1,113 @@
|
|
1 |
#!/usr/bin/env python
|
2 |
"""
|
3 |
Gradio demo for Wan2.1 FLF2V – First & Last Frame → Video
|
4 |
-
Streams all HF-Hub & Diffusers tqdm bars, caches the model,
|
5 |
-
and provides a direct download link for the MP4.
|
6 |
"""
|
7 |
|
8 |
-
import
|
9 |
-
import numpy as np
|
10 |
import torch
|
|
|
11 |
import gradio as gr
|
12 |
-
from PIL import Image
|
13 |
-
from transformers import CLIPVisionModel, CLIPProcessor
|
14 |
from diffusers import WanImageToVideoPipeline, AutoencoderKLWan
|
|
|
15 |
from diffusers.utils import export_to_video
|
|
|
16 |
import torchvision.transforms.functional as TF
|
17 |
|
18 |
-
#
|
19 |
# CONFIG
|
20 |
-
#
|
21 |
-
MODEL_ID
|
22 |
-
DTYPE
|
23 |
-
MAX_AREA
|
24 |
-
DEFAULT_FRAMES
|
25 |
-
|
26 |
-
#
|
27 |
-
#
|
28 |
-
#
|
29 |
-
PIPE = None
|
30 |
-
|
31 |
def load_pipeline():
|
32 |
-
|
33 |
-
|
34 |
-
vision = CLIPVisionModel.from_pretrained(
|
35 |
MODEL_ID, subfolder="image_encoder", torch_dtype=torch.float32
|
36 |
)
|
37 |
-
# 2)
|
38 |
-
processor = CLIPProcessor.from_pretrained(
|
39 |
-
MODEL_ID, subfolder="image_processor", use_fast=True
|
40 |
-
)
|
41 |
-
# 3) VAE (half precision)
|
42 |
vae = AutoencoderKLWan.from_pretrained(
|
43 |
MODEL_ID, subfolder="vae", torch_dtype=DTYPE
|
44 |
)
|
45 |
-
#
|
|
|
|
|
|
|
46 |
pipe = WanImageToVideoPipeline.from_pretrained(
|
47 |
MODEL_ID,
|
48 |
vae=vae,
|
49 |
-
image_encoder=
|
50 |
-
|
51 |
torch_dtype=DTYPE,
|
52 |
)
|
53 |
-
|
|
|
54 |
pipe.enable_model_cpu_offload()
|
55 |
-
# return on correct device
|
56 |
-
return pipe.to("cuda" if torch.cuda.is_available() else "cpu")
|
57 |
|
58 |
-
#
|
59 |
-
|
60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
def aspect_resize(img: Image.Image, max_area=MAX_AREA):
|
62 |
-
ar
|
63 |
-
|
64 |
-
|
65 |
-
|
|
|
66 |
return img.resize((w, h), Image.LANCZOS), h, w
|
67 |
|
68 |
-
def center_crop_resize(img: Image.Image, h
|
69 |
ratio = max(w / img.width, h / img.height)
|
70 |
-
|
71 |
-
|
|
|
|
|
72 |
|
73 |
-
#
|
74 |
-
# GENERATION
|
75 |
-
#
|
76 |
def generate(
|
77 |
first_frame: Image.Image,
|
78 |
-
last_frame:
|
79 |
-
prompt:
|
80 |
negative_prompt: str,
|
81 |
-
steps:
|
82 |
-
guidance:
|
83 |
-
num_frames:
|
84 |
-
seed:
|
85 |
-
fps:
|
86 |
-
progress=gr.Progress(track_tqdm=True),
|
87 |
):
|
88 |
-
|
89 |
-
# lazy load once
|
90 |
-
if PIPE is None:
|
91 |
-
progress(0, desc="Loading model…")
|
92 |
-
PIPE = load_pipeline()
|
93 |
-
|
94 |
-
# ensure reproducibility
|
95 |
if seed == -1:
|
96 |
seed = torch.seed()
|
97 |
-
gen = torch.Generator(device=
|
98 |
|
99 |
-
# preprocess
|
100 |
-
|
101 |
-
|
102 |
-
if last_frame.size != frame1.size:
|
103 |
last_frame = center_crop_resize(last_frame, h, w)
|
104 |
|
105 |
-
#
|
106 |
-
|
107 |
-
|
|
|
|
|
|
|
108 |
last_image=last_frame,
|
109 |
-
prompt=
|
110 |
negative_prompt=negative_prompt or None,
|
111 |
height=h,
|
112 |
width=w,
|
@@ -114,44 +115,45 @@ def generate(
|
|
114 |
num_inference_steps=steps,
|
115 |
guidance_scale=guidance,
|
116 |
generator=gen,
|
|
|
|
|
117 |
)
|
118 |
-
frames = result.frames[0]
|
119 |
|
120 |
-
# export to
|
121 |
-
|
122 |
-
|
123 |
-
return
|
124 |
|
125 |
-
#
|
126 |
-
# GRADIO
|
127 |
-
#
|
128 |
-
with gr.Blocks() as demo:
|
129 |
-
gr.Markdown("##
|
130 |
|
131 |
with gr.Row():
|
132 |
first_img = gr.Image(label="First frame", type="pil")
|
133 |
last_img = gr.Image(label="Last frame", type="pil")
|
134 |
|
135 |
-
prompt
|
136 |
-
negative
|
137 |
|
138 |
with gr.Accordion("Advanced parameters", open=False):
|
139 |
-
steps = gr.Slider(10, 50, value=30,
|
140 |
-
guidance = gr.Slider(0.0, 10.0, value=5.5, step=0.1,label="Guidance")
|
141 |
-
num_frames = gr.Slider(16, 129, value=DEFAULT_FRAMES,
|
142 |
-
fps = gr.Slider(4, 30, value=16,
|
143 |
-
|
144 |
|
145 |
-
run_btn
|
146 |
-
|
147 |
-
used_seed= gr.Number(label="Seed used", interactive=False)
|
148 |
|
149 |
run_btn.click(
|
150 |
fn=generate,
|
151 |
-
inputs=[first_img, last_img, prompt, negative, steps, guidance, num_frames,
|
152 |
-
outputs=[
|
153 |
-
|
154 |
)
|
155 |
|
156 |
-
#
|
157 |
-
demo.
|
|
|
1 |
#!/usr/bin/env python
|
2 |
"""
|
3 |
Gradio demo for Wan2.1 FLF2V – First & Last Frame → Video
|
|
|
|
|
4 |
"""
|
5 |
|
6 |
+
import os
|
|
|
7 |
import torch
|
8 |
+
import numpy as np
|
9 |
import gradio as gr
|
|
|
|
|
10 |
from diffusers import WanImageToVideoPipeline, AutoencoderKLWan
|
11 |
+
from transformers import CLIPProcessor, CLIPVisionModel
|
12 |
from diffusers.utils import export_to_video
|
13 |
+
from PIL import Image
|
14 |
import torchvision.transforms.functional as TF
|
15 |
|
16 |
+
# ----------------------------------------------------------------------
|
17 |
# CONFIG
|
18 |
+
# ----------------------------------------------------------------------
|
19 |
+
MODEL_ID = "Wan-AI/Wan2.1-FLF2V-14B-720P-diffusers"
|
20 |
+
DTYPE = torch.float16 # switch to torch.bfloat16 if you have AMP-friendly GPU
|
21 |
+
MAX_AREA = 1280 * 720 # ≤ 720p
|
22 |
+
DEFAULT_FRAMES = 81 # ~5s @ 16fps
|
23 |
+
|
24 |
+
# ----------------------------------------------------------------------
|
25 |
+
# PIPELINE LOADING (once)
|
26 |
+
# ----------------------------------------------------------------------
|
|
|
|
|
27 |
def load_pipeline():
|
28 |
+
# 1) image encoder in fp32 for stability
|
29 |
+
image_encoder = CLIPVisionModel.from_pretrained(
|
|
|
30 |
MODEL_ID, subfolder="image_encoder", torch_dtype=torch.float32
|
31 |
)
|
32 |
+
# 2) VAE in reduced precision
|
|
|
|
|
|
|
|
|
33 |
vae = AutoencoderKLWan.from_pretrained(
|
34 |
MODEL_ID, subfolder="vae", torch_dtype=DTYPE
|
35 |
)
|
36 |
+
# 3) use the unified CLIPProcessor (inherits ProcessorMixin) in fast mode
|
37 |
+
processor = CLIPProcessor.from_pretrained(MODEL_ID, use_fast=True)
|
38 |
+
|
39 |
+
# 4) assemble pipeline, overriding the default processor
|
40 |
pipe = WanImageToVideoPipeline.from_pretrained(
|
41 |
MODEL_ID,
|
42 |
vae=vae,
|
43 |
+
image_encoder=image_encoder,
|
44 |
+
processor=processor,
|
45 |
torch_dtype=DTYPE,
|
46 |
)
|
47 |
+
|
48 |
+
# 5) offload to CPU / reduce footprint
|
49 |
pipe.enable_model_cpu_offload()
|
|
|
|
|
50 |
|
51 |
+
# 6) safe VAE slicing if available
|
52 |
+
try:
|
53 |
+
pipe.vae.enable_slicing()
|
54 |
+
except (AttributeError, TypeError):
|
55 |
+
pass
|
56 |
+
|
57 |
+
return pipe
|
58 |
+
|
59 |
+
pipe = load_pipeline()
|
60 |
+
|
61 |
+
# ----------------------------------------------------------------------
|
62 |
+
# IMAGE RESIZING HELPERS
|
63 |
+
# ----------------------------------------------------------------------
|
64 |
def aspect_resize(img: Image.Image, max_area=MAX_AREA):
|
65 |
+
ar = img.height / img.width
|
66 |
+
# align to VAE & transformer patch grid
|
67 |
+
mod = pipe.vae_scale_factor_spatial * pipe.transformer.config.patch_size[1]
|
68 |
+
h = round(np.sqrt(max_area * ar)) // mod * mod
|
69 |
+
w = round(np.sqrt(max_area / ar)) // mod * mod
|
70 |
return img.resize((w, h), Image.LANCZOS), h, w
|
71 |
|
72 |
+
def center_crop_resize(img: Image.Image, h, w):
|
73 |
ratio = max(w / img.width, h / img.height)
|
74 |
+
img = img.resize(
|
75 |
+
(round(img.width * ratio), round(img.height * ratio)), Image.LANCZOS
|
76 |
+
)
|
77 |
+
return TF.center_crop(img, [h, w])
|
78 |
|
79 |
+
# ----------------------------------------------------------------------
|
80 |
+
# GENERATION FUNCTION
|
81 |
+
# ----------------------------------------------------------------------
|
82 |
def generate(
|
83 |
first_frame: Image.Image,
|
84 |
+
last_frame: Image.Image,
|
85 |
+
prompt: str,
|
86 |
negative_prompt: str,
|
87 |
+
steps: int,
|
88 |
+
guidance: float,
|
89 |
+
num_frames: int,
|
90 |
+
seed: int,
|
91 |
+
fps: int,
|
|
|
92 |
):
|
93 |
+
# randomize seed if requested
|
|
|
|
|
|
|
|
|
|
|
|
|
94 |
if seed == -1:
|
95 |
seed = torch.seed()
|
96 |
+
gen = torch.Generator(device=pipe.device).manual_seed(seed)
|
97 |
|
98 |
+
# preprocess inputs
|
99 |
+
first_frame, h, w = aspect_resize(first_frame)
|
100 |
+
if last_frame.size != first_frame.size:
|
|
|
101 |
last_frame = center_crop_resize(last_frame, h, w)
|
102 |
|
103 |
+
# set up streaming progress
|
104 |
+
progress = gr.Progress(track_tqdm=True)
|
105 |
+
|
106 |
+
# run the pipeline, streaming progress every step
|
107 |
+
result = pipe(
|
108 |
+
image=first_frame,
|
109 |
last_image=last_frame,
|
110 |
+
prompt=prompt,
|
111 |
negative_prompt=negative_prompt or None,
|
112 |
height=h,
|
113 |
width=w,
|
|
|
115 |
num_inference_steps=steps,
|
116 |
guidance_scale=guidance,
|
117 |
generator=gen,
|
118 |
+
callback=progress,
|
119 |
+
callback_steps=1,
|
120 |
)
|
|
|
121 |
|
122 |
+
# export to video and return path + seed used
|
123 |
+
frames = result.frames[0]
|
124 |
+
video_path = export_to_video(frames, fps=fps)
|
125 |
+
return video_path, seed
|
126 |
|
127 |
+
# ----------------------------------------------------------------------
|
128 |
+
# GRADIO APP
|
129 |
+
# ----------------------------------------------------------------------
|
130 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
131 |
+
gr.Markdown("## Wan 2.1 FLF2V – First & Last Frame → Video")
|
132 |
|
133 |
with gr.Row():
|
134 |
first_img = gr.Image(label="First frame", type="pil")
|
135 |
last_img = gr.Image(label="Last frame", type="pil")
|
136 |
|
137 |
+
prompt = gr.Textbox(label="Prompt", placeholder="A blue bird takes off…")
|
138 |
+
negative = gr.Textbox(label="Negative prompt (optional)", placeholder="ugly, blurry")
|
139 |
|
140 |
with gr.Accordion("Advanced parameters", open=False):
|
141 |
+
steps = gr.Slider(10, 50, value=30, label="Sampling steps")
|
142 |
+
guidance = gr.Slider(0.0, 10.0, value=5.5, step=0.1, label="Guidance scale")
|
143 |
+
num_frames = gr.Slider(16, 129, value=DEFAULT_FRAMES, label="Frames")
|
144 |
+
fps = gr.Slider(4, 30, value=16, label="FPS (export)")
|
145 |
+
seed_input = gr.Number(value=-1, precision=0, label="Seed (-1 = random)")
|
146 |
|
147 |
+
run_btn = gr.Button("Generate")
|
148 |
+
video_out = gr.Video(label="Result (.mp4)")
|
149 |
+
used_seed = gr.Number(label="Seed used", interactive=False)
|
150 |
|
151 |
run_btn.click(
|
152 |
fn=generate,
|
153 |
+
inputs=[first_img, last_img, prompt, negative, steps, guidance, num_frames, seed_input, fps],
|
154 |
+
outputs=[video_out, used_seed],
|
155 |
+
show_progress=True, # hook into Gradio’s built-in progress UI
|
156 |
)
|
157 |
|
158 |
+
demo.queue() # serialize GPU calls
|
159 |
+
demo.launch()
|