File size: 10,623 Bytes
fe0873a
fd36c7b
fe0873a
 
 
 
 
 
 
f357636
fe0873a
 
 
 
 
 
 
 
4b1abd9
fe0873a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f357636
fe0873a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4b1abd9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe0873a
 
 
 
 
 
4b1abd9
fe0873a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f5e92ed
 
fe0873a
f5e92ed
 
 
 
fe0873a
 
4b1abd9
 
fe0873a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4b1abd9
fe0873a
 
 
 
 
 
f5e92ed
4b1abd9
 
 
 
fe0873a
 
 
fd36c7b
 
 
 
 
 
fe0873a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f5e92ed
fe0873a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4b1abd9
 
 
 
 
 
 
 
fe0873a
9d89e9f
 
fe0873a
 
 
 
 
 
 
 
 
 
4b1abd9
 
 
 
 
 
 
 
 
 
 
fe0873a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import gc
import os
import random
import threading
import uuid
from pathlib import Path

import gradio as gr
import numpy as np
import spaces
import torch
from diffusers import AutoencoderKLWan, WanImageToVideoPipeline
from diffusers.utils import export_to_video
from PIL import Image, ImageOps
from transformers import CLIPVisionModel


MODEL_ID = "Wan-AI/Wan2.1-FLF2V-14B-720P-diffusers"
VIDEO_FPS = 16
OUTPUT_DIR = Path("outputs")
OUTPUT_DIR.mkdir(exist_ok=True)

NEGATIVE_PROMPT = (
    "low quality, worst quality, blurry, overexposed, static, distorted, "
    "deformed, disfigured, duplicate, watermark, text, logo, artifacts"
)

RESOLUTIONS = {
    "480p (faster)": 480 * 832,
    "720p (best quality)": 720 * 1280,
}

pipe = None
model_lock = threading.Lock()


def load_pipeline():
    """Load once, on the first request, to keep Space startup responsive."""
    global pipe
    if pipe is not None:
        return pipe

    with model_lock:
        if pipe is not None:
            return pipe

        if not torch.cuda.is_available():
            raise gr.Error(
                "A CUDA GPU is required. In the Space settings, select an A100 80GB "
                "or another GPU with enough memory."
            )

        image_encoder = CLIPVisionModel.from_pretrained(
            MODEL_ID,
            subfolder="image_encoder",
            torch_dtype=torch.float32,
        )
        vae = AutoencoderKLWan.from_pretrained(
            MODEL_ID,
            subfolder="vae",
            torch_dtype=torch.float32,
        )
        loaded_pipe = WanImageToVideoPipeline.from_pretrained(
            MODEL_ID,
            image_encoder=image_encoder,
            vae=vae,
            torch_dtype=torch.bfloat16,
        )
        loaded_pipe.vae.enable_tiling()
        loaded_pipe.to("cuda")
        pipe = loaded_pipe

    return pipe


def prepare_frame(image: Image.Image, max_area: int, size=None):
    if image is None:
        return None, None

    image = ImageOps.exif_transpose(image).convert("RGB")

    if size is None:
        aspect = image.height / image.width
        height = max(128, round(np.sqrt(max_area * aspect) / 16) * 16)
        width = max(128, round(np.sqrt(max_area / aspect) / 16) * 16)
        size = (width, height)

    # Crop only the overflow so both conditioning frames have identical geometry.
    return ImageOps.fit(image, size, method=Image.Resampling.LANCZOS), size


def seconds_to_frames(duration_seconds):
    """Wan accepts 4k+1 frame counts; whole seconds at 16 fps fit exactly."""
    seconds = max(1, min(3, int(duration_seconds)))
    return seconds * VIDEO_FPS + 1


def estimate_gpu_duration(
    _start_image,
    _end_image,
    _prompt,
    _negative_prompt,
    resolution,
    duration_seconds,
    steps,
    _guidance,
    _seed,
):
    """Reserve only the free ZeroGPU time appropriate for this request."""
    seconds = max(1, min(3, int(duration_seconds)))
    resolution_factor = 1.6 if resolution == "720p (best quality)" else 1.0
    estimate = (8 + 7 * seconds) * (int(steps) / 8) * resolution_factor
    return max(12, min(60, int(round(estimate))))


@spaces.GPU(size="xlarge", duration=estimate_gpu_duration)
def generate_video(
    start_image,
    end_image,
    prompt,
    negative_prompt,
    resolution,
    duration_seconds,
    steps,
    guidance,
    seed,
    progress=gr.Progress(track_tqdm=False),
):
    if start_image is None:
        raise gr.Error("Please upload a start image.")
    if not prompt or not prompt.strip():
        raise gr.Error("Please describe the motion or scene in the prompt.")

    progress(0, desc="Loading the video model…")
    pipeline = load_pipeline()

    max_area = RESOLUTIONS[resolution]
    first_frame, target_size = prepare_frame(start_image, max_area)
    has_end_frame = end_image is not None
    if has_end_frame:
        last_frame, _ = prepare_frame(end_image, max_area, target_size)
    else:
        # The FLF2V checkpoint always expects two CLIP frame embeddings. Reusing
        # the first frame keeps the end-image input optional and creates a loop.
        last_frame = first_frame.copy()

    width, height = target_size
    duration_seconds = max(1, min(3, int(duration_seconds)))
    num_frames = seconds_to_frames(duration_seconds)
    actual_seed = random.randint(0, 2**31 - 1) if int(seed) < 0 else int(seed)
    generator = torch.Generator(device="cpu").manual_seed(actual_seed)

    def update_progress(_pipeline, step_index, _timestep, callback_kwargs):
        progress((step_index + 1) / int(steps), desc=f"Generating frame sequence · step {step_index + 1}/{steps}")
        return callback_kwargs

    output_path = OUTPUT_DIR / f"wan_{actual_seed}_{uuid.uuid4().hex[:8]}_{width}x{height}.mp4"

    try:
        with model_lock, torch.inference_mode():
            frames = pipeline(
                image=first_frame,
                last_image=last_frame,
                prompt=prompt.strip(),
                negative_prompt=(negative_prompt or "").strip(),
                height=height,
                width=width,
                num_frames=int(num_frames),
                num_inference_steps=int(steps),
                guidance_scale=float(guidance),
                generator=generator,
                callback_on_step_end=update_progress,
            ).frames[0]
        export_to_video(frames, str(output_path), fps=VIDEO_FPS)
    except torch.cuda.OutOfMemoryError as exc:
        gc.collect()
        torch.cuda.empty_cache()
        raise gr.Error("The GPU ran out of memory. Try 480p, fewer frames, or an A100 80GB GPU.") from exc

    progress(1, desc="Video ready")
    mode = "start → end" if has_end_frame else "loop"
    info = (
        f"Seed **{actual_seed}** · {width}×{height} · "
        f"{duration_seconds}s ({num_frames} frames at {VIDEO_FPS} fps) · {mode} mode"
    )
    return str(output_path), info, actual_seed


# ZeroGPU emulates CUDA during startup, allowing weights to be prepared before
# a real GPU is assigned to a generation request.
if os.getenv("SPACE_ID"):
    load_pipeline()


CSS = """
:root { --ink: #171512; --paper: #f6f2e9; --accent: #ee5b35; }
.gradio-container { max-width: 1180px !important; margin: 0 auto !important; background: var(--paper); }
.hero { padding: 2.25rem 0 1rem; }
.hero h1 { font-size: clamp(2.3rem, 6vw, 5rem); line-height: .92; letter-spacing: -.055em; color: var(--ink); margin: 0; }
.hero p { max-width: 650px; font-size: 1.05rem; color: #5b554c; margin-top: 1.1rem; }
.eyebrow { color: var(--accent); font-weight: 750; letter-spacing: .15em; text-transform: uppercase; font-size: .76rem; }
.frame-card { border: 1px solid #d9d1c3 !important; border-radius: 18px !important; background: rgba(255,255,255,.48) !important; }
.generate-btn { background: var(--accent) !important; color: white !important; border: none !important; font-weight: 750 !important; }
.output-video { border-radius: 18px; overflow: hidden; }
.footer-note { color: #766e62; font-size: .83rem; text-align: center; padding: 1rem; }
"""


with gr.Blocks(css=CSS, title="Between Frames · Image to Video") as demo:
    gr.HTML(
        """
        <section class="hero">
          <div class="eyebrow">Wan 2.1 · First / Last Frame to Video</div>
          <h1>Turn two stills<br>into one moving moment.</h1>
          <p>Choose where the shot begins, optionally choose where it ends, and describe what happens between them. Without an end image, the shot loops back to its first frame.</p>
        </section>
        """
    )

    with gr.Row(equal_height=False):
        with gr.Column(scale=6):
            with gr.Row():
                start_image = gr.Image(
                    type="pil",
                    image_mode="RGB",
                    label="01 · Start image",
                    sources=["upload", "clipboard"],
                    elem_classes="frame-card",
                    height=330,
                )
                end_image = gr.Image(
                    type="pil",
                    image_mode="RGB",
                    label="02 · End image (optional)",
                    sources=["upload", "clipboard"],
                    elem_classes="frame-card",
                    height=330,
                )

            prompt = gr.Textbox(
                label="03 · Describe the movement",
                placeholder="The camera slowly pushes in as wind moves through her hair; cinematic light shifts from dusk to night…",
                lines=4,
            )

            with gr.Accordion("Generation controls", open=False):
                negative_prompt = gr.Textbox(label="Negative prompt", value=NEGATIVE_PROMPT, lines=2)
                with gr.Row():
                    resolution = gr.Radio(list(RESOLUTIONS), value="480p (faster)", label="Resolution")
                    duration_seconds = gr.Slider(
                        1,
                        3,
                        value=1,
                        step=1,
                        label="Video duration (seconds)",
                        info="Longer videos use more of the free daily GPU quota.",
                    )
                with gr.Row():
                    steps = gr.Slider(8, 16, value=8, step=1, label="Inference steps")
                    guidance = gr.Slider(1, 6, value=1.0, step=0.1, label="Prompt guidance")
                    seed = gr.Number(value=-1, precision=0, label="Seed (−1 = random)")

            generate_btn = gr.Button("Generate video", variant="primary", size="lg", elem_classes="generate-btn")

        with gr.Column(scale=5):
            video = gr.Video(label="Generated video", autoplay=True, elem_classes="output-video")
            generation_info = gr.Markdown("Your generation details will appear here.")

    gr.HTML('<div class="footer-note">Large video models need a GPU. 720p generation can take several minutes.</div>')

    inputs = [
        start_image,
        end_image,
        prompt,
        negative_prompt,
        resolution,
        duration_seconds,
        steps,
        guidance,
        seed,
    ]
    generate_btn.click(
        fn=generate_video,
        inputs=inputs,
        outputs=[video, generation_info, seed],
        api_name="generate",
        concurrency_id="gpu_queue",
        concurrency_limit=1,
    )
    prompt.submit(
        fn=generate_video,
        inputs=inputs,
        outputs=[video, generation_info, seed],
        concurrency_id="gpu_queue",
        concurrency_limit=1,
    )


if __name__ == "__main__":
    demo.queue(default_concurrency_limit=1, max_size=8).launch()