Spaces:
Paused
Paused
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python
|
2 |
+
"""
|
3 |
+
Gradio demo for Wan2.1 First-Last-Frame-to-Video (FLF2V)
|
4 |
+
Author: <your-handle>
|
5 |
+
"""
|
6 |
+
|
7 |
+
import os, tempfile, numpy as np, torch, gradio as gr
|
8 |
+
from diffusers import WanImageToVideoPipeline, AutoencoderKLWan
|
9 |
+
from diffusers.utils import export_to_video
|
10 |
+
from transformers import CLIPVisionModel
|
11 |
+
from PIL import Image
|
12 |
+
import torchvision.transforms.functional as TF
|
13 |
+
|
14 |
+
# ---------------------------------------------------------------------
|
15 |
+
# CONFIG ----------------------------------------------------------------
|
16 |
+
MODEL_ID = "Wan-AI/Wan2.1-FLF2V-14B-720P-diffusers" # switch to 1.3B if needed
|
17 |
+
DTYPE = torch.float16 # or torch.bfloat16 on AMP-friendly GPUs
|
18 |
+
MAX_AREA = 1280 * 720 # keep ≤ 720 p
|
19 |
+
DEFAULT_FRAMES = 81 # ≈ 5 s at 16 fps
|
20 |
+
# ----------------------------------------------------------------------
|
21 |
+
|
22 |
+
def load_pipeline():
|
23 |
+
"""Lazy-load the huge model once per process."""
|
24 |
+
image_encoder = CLIPVisionModel.from_pretrained(
|
25 |
+
MODEL_ID, subfolder="image_encoder", torch_dtype=torch.float32
|
26 |
+
)
|
27 |
+
vae = AutoencoderKLWan.from_pretrained(
|
28 |
+
MODEL_ID, subfolder="vae", torch_dtype=DTYPE
|
29 |
+
)
|
30 |
+
pipe = WanImageToVideoPipeline.from_pretrained(
|
31 |
+
MODEL_ID,
|
32 |
+
vae=vae,
|
33 |
+
image_encoder=image_encoder,
|
34 |
+
torch_dtype=DTYPE,
|
35 |
+
)
|
36 |
+
|
37 |
+
# memory helpers for ≤ 24 GB cards / HF T4-medium
|
38 |
+
pipe.enable_model_cpu_offload() # paged UNet blocks
|
39 |
+
pipe.enable_vae_slicing() # reduces VAE RAM spikes
|
40 |
+
# Optional (needs xformers): pipe.enable_xformers_memory_efficient_attention()
|
41 |
+
return pipe.to("cuda" if torch.cuda.is_available() else "cpu")
|
42 |
+
|
43 |
+
PIPE = load_pipeline()
|
44 |
+
|
45 |
+
# ----------------------------------------------------------------------
|
46 |
+
# UTILS ----------------------------------------------------------------
|
47 |
+
def aspect_resize(img: Image.Image, max_area=MAX_AREA):
|
48 |
+
"""Resize while respecting model patch size (multiple of 8*transformer patch)."""
|
49 |
+
ar = img.height / img.width
|
50 |
+
mod = PIPE.vae_scale_factor_spatial * PIPE.transformer.config.patch_size[1]
|
51 |
+
h = round(np.sqrt(max_area * ar)) // mod * mod
|
52 |
+
w = round(np.sqrt(max_area / ar)) // mod * mod
|
53 |
+
return img.resize((w, h), Image.LANCZOS), h, w
|
54 |
+
|
55 |
+
def center_crop_resize(img: Image.Image, h, w):
|
56 |
+
ratio = max(w / img.width, h / img.height)
|
57 |
+
img = img.resize((round(img.width * ratio), round(img.height * ratio)), Image.LANCZOS)
|
58 |
+
img = TF.center_crop(img, [h, w])
|
59 |
+
return img
|
60 |
+
|
61 |
+
# ----------------------------------------------------------------------
|
62 |
+
# GENERATE --------------------------------------------------------------
|
63 |
+
def generate(first_frame, last_frame, prompt, negative_prompt, steps,
|
64 |
+
guidance, num_frames, seed, fps):
|
65 |
+
|
66 |
+
if seed == -1:
|
67 |
+
seed = torch.seed()
|
68 |
+
generator = torch.Generator(device=PIPE.device).manual_seed(seed)
|
69 |
+
|
70 |
+
first_frame, h, w = aspect_resize(first_frame)
|
71 |
+
if last_frame.size != first_frame.size:
|
72 |
+
last_frame = center_crop_resize(last_frame, h, w)
|
73 |
+
|
74 |
+
out = PIPE(
|
75 |
+
image=first_frame,
|
76 |
+
last_image=last_frame,
|
77 |
+
prompt=prompt,
|
78 |
+
negative_prompt=negative_prompt or None,
|
79 |
+
height=h,
|
80 |
+
width=w,
|
81 |
+
num_frames=num_frames,
|
82 |
+
num_inference_steps=steps,
|
83 |
+
guidance_scale=guidance,
|
84 |
+
generator=generator,
|
85 |
+
).frames[0] # list[pillow]
|
86 |
+
|
87 |
+
video_path = export_to_video(out, fps=fps)
|
88 |
+
return video_path, seed
|
89 |
+
|
90 |
+
# ----------------------------------------------------------------------
|
91 |
+
# UI --------------------------------------------------------------------
|
92 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
93 |
+
gr.Markdown("## Wan 2.1 FLF2V – First & Last Frame → Video")
|
94 |
+
|
95 |
+
with gr.Row():
|
96 |
+
first_img = gr.Image(label="First frame", type="pil")
|
97 |
+
last_img = gr.Image(label="Last frame", type="pil")
|
98 |
+
|
99 |
+
prompt = gr.Textbox(label="Prompt", placeholder="A blue bird takes off…")
|
100 |
+
negative = gr.Textbox(label="Negative prompt (optional)", placeholder="ugly, blurry")
|
101 |
+
with gr.Accordion("Advanced parameters", open=False):
|
102 |
+
steps = gr.Slider(10, 50, value=30, step=1, label="Sampling steps")
|
103 |
+
guidance = gr.Slider(0.0, 10.0, value=5.5, step=0.1, label="Guidance scale")
|
104 |
+
num_frames = gr.Slider(16, 129, value=DEFAULT_FRAMES, step=1, label="Frames")
|
105 |
+
fps = gr.Slider(4, 30, value=16, step=1, label="FPS (export)")
|
106 |
+
seed = gr.Number(value=-1, precision=0, label="Seed (-1 = random)")
|
107 |
+
|
108 |
+
run_btn = gr.Button("Generate")
|
109 |
+
video = gr.Video(label="Result (.mp4)")
|
110 |
+
used_seed = gr.Number(label="Seed used", interactive=False)
|
111 |
+
|
112 |
+
run_btn.click(
|
113 |
+
fn=generate,
|
114 |
+
inputs=[first_img, last_img, prompt, negative, steps, guidance, num_frames, seed, fps],
|
115 |
+
outputs=[video, used_seed]
|
116 |
+
)
|
117 |
+
|
118 |
+
if __name__ == "__main__":
|
119 |
+
demo.launch()
|