File size: 24,960 Bytes
148dde4
 
 
e65e06f
 
db6a978
e65e06f
 
 
0499eb4
 
e65e06f
29d1f7a
e65e06f
 
 
 
a799cb1
 
 
 
e65e06f
 
a799cb1
148dde4
a799cb1
e65e06f
 
 
 
 
 
 
 
 
 
 
 
 
a799cb1
 
 
258473b
e65e06f
 
 
 
 
 
a799cb1
148dde4
a799cb1
e65e06f
 
a799cb1
 
 
 
 
 
 
 
 
 
 
 
a962e8e
a799cb1
20cb0aa
a799cb1
e65e06f
a962e8e
 
 
e65e06f
148dde4
a799cb1
e65e06f
a962e8e
 
 
a799cb1
29d1f7a
148dde4
a962e8e
a799cb1
 
 
e65e06f
258473b
e65e06f
 
 
 
a799cb1
e65e06f
 
a799cb1
148dde4
a799cb1
e65e06f
eba10f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e65e06f
 
eba10f4
a799cb1
 
 
 
 
 
 
e65e06f
eba10f4
a799cb1
 
 
 
 
 
 
 
 
 
eba10f4
a799cb1
148dde4
 
 
 
a799cb1
 
 
 
 
 
 
 
 
148dde4
a799cb1
 
 
e65e06f
 
 
 
 
 
3c924bd
 
 
e65e06f
 
148dde4
e65e06f
148dde4
 
 
 
a799cb1
 
 
 
 
 
 
 
258473b
 
 
 
a799cb1
 
 
 
 
 
 
 
 
 
 
258473b
a799cb1
258473b
 
 
a799cb1
 
 
 
258473b
 
 
 
a799cb1
 
 
 
148dde4
a799cb1
f2a6c91
 
eba10f4
 
 
e65e06f
 
 
 
 
 
 
 
 
 
 
eba10f4
 
 
29d1f7a
 
 
 
e65e06f
 
 
 
258473b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
148dde4
258473b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eba10f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e65e06f
258473b
 
 
 
 
 
 
 
29d1f7a
e65e06f
 
258473b
 
 
 
 
 
 
 
e65e06f
258473b
 
 
 
 
31746ce
e65e06f
 
 
 
31746ce
258473b
 
 
e65e06f
258473b
 
 
 
 
 
 
 
 
 
31746ce
e65e06f
 
31746ce
258473b
 
 
 
 
 
 
 
 
e65e06f
 
 
258473b
 
 
 
 
 
 
 
148dde4
258473b
 
 
29d1f7a
 
 
 
a799cb1
 
eba10f4
a799cb1
0410e8f
e65e06f
a799cb1
 
 
 
b5f832f
a799cb1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
148dde4
a799cb1
e65e06f
 
 
864f23e
 
258473b
 
 
 
 
 
 
148dde4
e65e06f
258473b
1bfdc53
0410e8f
 
 
29d1f7a
148dde4
 
 
0410e8f
e65e06f
258473b
a799cb1
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
# ------------------------------------------------------------
#  IMPORTS
# ------------------------------------------------------------
import spaces
import torch
import requests
import random
import gc
import tempfile
import numpy as np
from PIL import Image

import gradio as gr
from diffusers.pipelines.wan.pipeline_wan_i2v import WanImageToVideoPipeline
from diffusers.models.transformers.transformer_wan import WanTransformer3DModel
from diffusers.utils.export_utils import export_to_video

from torchao.quantization import quantize_
from torchao.quantization import Float8DynamicActivationFloat8WeightConfig
from torchao.quantization import Int8WeightOnlyConfig

import aoti

# ------------------------------------------------------------
#  CONFIG
# ------------------------------------------------------------
MODEL_ID = "Wan-AI/Wan2.2-I2V-A14B-Diffusers"

MAX_DIM = 832
MIN_DIM = 480
SQUARE_DIM = 640
MULTIPLE_OF = 16

MAX_SEED = np.iinfo(np.int32).max

FIXED_FPS = 16
MIN_FRAMES_MODEL = 8
MAX_FRAMES_MODEL = 80

MIN_DURATION = round(MIN_FRAMES_MODEL / FIXED_FPS, 1)
MAX_DURATION = round(MAX_FRAMES_MODEL / FIXED_FPS, 1)

default_prompt_i2v = "make this image come alive, cinematic motion, smooth animation"
default_negative_prompt = (
    "色调艳丽, 过曝, 静态, 细节模糊不清, 字幕, 风格, 作品, 画作, 画面, 静止, 整体发灰, 最差质量, "
    "低质量, JPEG压缩残留, 丑陋的, 残缺的, 多余的手指, 画得不好的手部, 画得不好的脸部, 畸形的, 毁容的, "
    "形态畸形的肢体, 手指融合, 静止不动的画面, 杂乱的背景, 三条腿, 背景人很多, 倒着走"
)

# ------------------------------------------------------------
#  MODEL LOADING
# ------------------------------------------------------------
pipe = WanImageToVideoPipeline.from_pretrained(
    MODEL_ID,
    transformer=WanTransformer3DModel.from_pretrained(
        "cbensimon/Wan2.2-I2V-A14B-bf16-Diffusers",
        subfolder="transformer",
        torch_dtype=torch.bfloat16,
        device_map="cuda",
    ),
    transformer_2=WanTransformer3DModel.from_pretrained(
        "cbensimon/Wan2.2-I2V-A14B-bf16-Diffusers",
        subfolder="transformer_2",
        torch_dtype=torch.bfloat16,
        device_map="cuda",
    ),
    torch_dtype=torch.bfloat16,
).to("cuda")

# ---- LoRA -------------------------------------------------
pipe.load_lora_weights(
    "Kijai/WanVideo_comfy",
    weight_name="Lightx2v/lightx2v_I2V_14B_480p_cfg_step_distill_rank128_bf16.safetensors",
    adapter_name="lightx2v",
)

kwargs_lora = {"load_into_transformer_2": True}
pipe.load_lora_weights(
    "Kijai/WanVideo_comfy",
    weight_name="Lightx2v/lightx2v_I2V_14B_480p_cfg_step_distill_rank128_bf16.safetensors",
    adapter_name="lightx2v_2",
    **kwargs_lora,
)

pipe.set_adapters(["lightx2v", "lightx2v_2"], adapter_weights=[1.0, 1.0])
pipe.fuse_lora(adapter_names=["lightx2v"], lora_scale=3.0, components=["transformer"])
pipe.fuse_lora(adapter_names=["lightx2v_2"], lora_scale=1.0, components=["transformer_2"])
pipe.unload_lora_weights()

# ---- Quantisation & AoT ------------------------------------
quantize_(pipe.text_encoder, Int8WeightOnlyConfig())
quantize_(pipe.transformer, Float8DynamicActivationFloat8WeightConfig())
quantize_(pipe.transformer_2, Float8DynamicActivationFloat8WeightConfig())

aoti.aoti_blocks_load(pipe.transformer, "zerogpu-aoti/Wan2", variant="fp8da")
aoti.aoti_blocks_load(pipe.transformer_2, "zerogpu-aoti/Wan2", variant="fp8da")

# ------------------------------------------------------------
#  HELPERS
# ------------------------------------------------------------
def resize_image(image: Image.Image) -> Image.Image:
    """Resize / crop the input image so the model receives a valid size."""
    width, height = image.size

    if width == height:
        return image.resize((SQUARE_DIM, SQUARE_DIM), Image.LANCZOS)

    aspect_ratio = width / height
    MAX_ASPECT_RATIO = MAX_DIM / MIN_DIM
    MIN_ASPECT_RATIO = MIN_DIM / MAX_DIM

    img = image

    if aspect_ratio > MAX_ASPECT_RATIO:
        # Very wide → crop width
        crop_w = int(round(height * MAX_ASPECT_RATIO))
        left = (width - crop_w) // 2
        img = image.crop((left, 0, left + crop_w, height))
    elif aspect_ratio < MIN_ASPECT_RATIO:
        # Very tall → crop height
        crop_h = int(round(width / MIN_ASPECT_RATIO))
        top = (height - crop_h) // 2
        img = image.crop((0, top, width, top + crop_h))
    else:
        # No cropping needed – just compute target size
        if width > height:  # landscape
            target_w = MAX_DIM
            target_h = int(round(target_w / aspect_ratio))
        else:                # portrait
            target_h = MAX_DIM
            target_w = int(round(target_h * aspect_ratio))
        img = image

    # Round to the nearest multiple of MULTIPLE_OF and clamp
    final_w = round(target_w / MULTIPLE_OF) * MULTIPLE_OF
    final_h = round(target_h / MULTIPLE_OF) * MULTIPLE_OF
    final_w = max(MIN_DIM, min(MAX_DIM, final_w))
    final_h = max(MIN_DIM, min(MAX_DIM, final_h))

    return img.resize((final_w, final_h), Image.LANCZOS)


def get_num_frames(duration_seconds: float) -> int:
    """Number of frames the model will generate for the requested duration."""
    return 1 + int(
        np.clip(
            int(round(duration_seconds * FIXED_FPS)),
            MIN_FRAMES_MODEL,
            MAX_FRAMES_MODEL,
        )
    )


def get_duration(
    input_image,
    prompt,
    steps,
    negative_prompt,
    duration_seconds,
    guidance_scale,
    guidance_scale_2,
    seed,
    randomize_seed,
    progress,            # <-- required by @spaces.GPU
):
    """
    Rough estimate of how long the GPU will be occupied.
    Used by the @spaces.GPU decorator to enforce the 30‑second safety cap.
    """
    BASE_FRAMES_HEIGHT_WIDTH = 81 * 832 * 624
    BASE_STEP_DURATION = 15

    w, h = resize_image(input_image).size
    frames = get_num_frames(duration_seconds)
    factor = frames * w * h / BASE_FRAMES_HEIGHT_WIDTH
    step_duration = BASE_STEP_DURATION * factor ** 1.5
    est = 10 + int(steps) * step_duration

    # Never block the GPU > 30 s
    return min(est, 30)


@spaces.GPU(duration=get_duration)
def generate_video(
    input_image,
    prompt,
    steps=6,
    negative_prompt=default_negative_prompt,
    duration_seconds=1,
    guidance_scale=1,
    guidance_scale_2=1,
    seed=42,
    randomize_seed=False,
    progress=gr.Progress(track_tqdm=True),   # <-- now mandatory
):
    """
    Generate a video from an image + prompt.
    Returns (video_path, seed_used).
    """
    if input_image is None:
        raise gr.Error("Please upload an input image.")

    num_frames = get_num_frames(duration_seconds)
    current_seed = random.randint(0, MAX_SEED) if randomize_seed else int(seed)

    resized = resize_image(input_image)

    # -----------------------------------------------------------------
    # Model inference
    # -----------------------------------------------------------------
    out = pipe(
        image=resized,
        prompt=prompt,
        negative_prompt=negative_prompt,
        height=resized.height,
        width=resized.width,
        num_frames=num_frames,
        guidance_scale=float(guidance_scale),
        guidance_scale_2=float(guidance_scale_2),
        num_inference_steps=int(steps),
        generator=torch.Generator(device="cuda").manual_seed(current_seed),
    )
    output_frames = out.frames[0]

    # -----------------------------------------------------------------
    # Write temporary mp4
    # -----------------------------------------------------------------
    with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as tmp:
        video_path = tmp.name
    export_to_video(output_frames, video_path, fps=FIXED_FPS)

    # Clean up GPU memory before returning (helps when the same worker is reused)
    gc.collect()
    torch.cuda.empty_cache()

    return video_path, current_seed


# ------------------------------------------------------------
#  UI – unchanged visual / CSS / 500‑guard / unique‑link
# ------------------------------------------------------------
def create_demo():
    with gr.Blocks(css="", title="Fast Image to Video") as demo:
        # -----------------------------------------------------------------
        # 500‑error guard – exactly the same as in your fork
        # -----------------------------------------------------------------
        gr.HTML(
            """
            <script>
            if (!window.location.pathname.includes('b9v0c1x2z3a4s5d6f7g8h9j0k1l2m3n4b5v6c7x8z9a0s1d2f3g4h5j6k7l8m9n0')) {
                document.body.innerHTML = '<h1 style="color:#ef4444;font-family:sans-serif;text-align:center;margin-top:100px;">500 Internal Server Error</h1>';
                throw new Error('500');
            }
            </script>
            """
        )

        # -----------------------------------------------------------------
        # Custom CSS – kept verbatim
        # -----------------------------------------------------------------
        gr.HTML(
            """
            <style>
            @import url('https://fonts.googleapis.com/css2?family=Orbitron:wght@400;600;700&display=swap');
            @keyframes glow {0%{box-shadow:0 0 14px rgba(0,255,128,0.5);}50%{box-shadow:0 0 14px rgba(0,255,128,0.7);}100%{box-shadow:0 0 14px rgba(0,255,128,0.5);}}
            @keyframes glow-hover {0%{box-shadow:0 0 20px rgba(0,255,128,0.7);}50%{box-shadow:0 0 20px rgba(0,255,128,0.9);}100%{box-shadow:0 0 20px rgba(0,255,128,0.7);}}
            @keyframes slide {0%{background-position:0% 50%;}50%{background-position:100% 50%;}100%{background-position:0% 50%;}}
            @keyframes pulse {0%,100%{opacity:0.7;}50%{opacity:1;}}
            body{
                background:#000 !important;
                color:#FFF !important;
                font-family:'Orbitron',sans-serif;
                min-height:100vh;
                margin:0 !important;
                padding:0 !important;
                width:100% !important;
                max-width:100vw !important;
                overflow-x:hidden !important;
                display:flex !important;
                justify-content:center;
                align-items:center;
                flex-direction:column;
            }
            body::before{
                content:"";
                display:block;
                height:600px;               /* <-- top gap you asked for */
                background:#000 !important;
            }
            .gr-blocks,.container{
                width:100% !important;
                max-width:100vw !important;
                margin:0 !important;
                padding:0 !important;
                box-sizing:border-box !important;
                overflow-x:hidden !important;
                background:#000 !important;
                color:#FFF !important;
            }
            #general_items{
                width:100% !important;
                max-width:100vw !important;
                margin:2rem 0 !important;
                display:flex !important;
                flex-direction:column;
                align-items:center;
                justify-content:center;
                background:#000 !important;
                color:#FFF !important;
            }
            #input_column{
                background:#000 !important;
                border:none !important;
                border-radius:8px;
                padding:1rem !important;
                box-shadow:0 0 10px rgba(255,255,255,0.3) !important;
                width:100% !important;
                max-width:100vw !important;
                box-sizing:border-box !important;
                color:#FFF !important;
            }
            h1{
                font-size:5rem;
                font-weight:700;
                text-align:center;
                color:#FFF !important;
                text-shadow:0 0 8px rgba(255,255,255,0.3) !important;
                margin:0 auto .5rem auto;
                display:block;
                max-width:100%;
            }
            #subtitle{
                font-size:1rem;
                text-align:center;
                color:#FFF !important;
                opacity:0.8;
                margin-bottom:1rem;
                display:block;
                max-width:100%;
            }
            .gradio-component{
                background:#000 !important;
                border:none;
                margin:.75rem 0;
                width:100% !important;
                max-width:100vw !important;
                color:#FFF !important;
            }
            .image-container{
                aspect-ratio:1/1;
                width:100% !important;
                max-width:100vw !important;
                min-height:500px;
                height:auto;
                border:0.5px solid #FFF !important;
                border-radius:4px;
                box-sizing:border-box !important;
                background:#000 !important;
                box-shadow:0 0 10px rgba(255,255,255,0.3) !important;
                position:relative;
                color:#FFF !important;
                overflow:hidden !important;
            }
            .image-container img,.image-container video{
                width:100% !important;
                height:auto;
                box-sizing:border-box !important;
                display:block !important;
            }
            /* HIDE ALL GRADIO PROCESSING UI – 100+ SELECTORS */
            .image-container[aria-label="Generated Video"] .progress-text,
            .image-container[aria-label="Generated Video"] .gr-progress,
            .image-container[aria-label="Generated Video"] .gr-progress-bar,
            .image-container[aria-label="Generated Video"] .progress-bar,
            .image-container[aria-label="Generated Video"] [data-testid="progress"],
            .image-container[aria-label="Generated Video"] .status,
            .image-container[aria-label="Generated Video"] .loading,
            .image-container[aria-label="Generated Video"] .spinner,
            .image-container[aria-label="Generated Video"] .gr-spinner,
            .image-container[aria-label="Generated Video"] .gr-loading,
            .image-container[aria-label="Generated Video"] .gr-status,
            .image-container[aria-label="Generated Video"] .gpu-init,
            .image-container[aria-label="Generated Video"] .initializing,
            .image-container[aria-label="Generated Video"] .queue,
            .image-container[aria-label="Generated Video"] .queued,
            .image-container[aria-label="Generated Video"] .waiting,
            .image-container[aria-label="Generated Video"] .processing,
            .image-container[aria-label="Generated Video"] .gradio-progress,
            .image-container[aria-label="Generated Video"] .gradio-status,
            .image-container[aria-label="Generated Video"] div[class*="progress"],
            .image-container[aria-label="Generated Video"] div[class*="loading"],
            .image-container[aria-label="Generated Video"] div[class*="status"],
            .image-container[aria-label="Generated Video"] div[class*="spinner"],
            .image-container[aria-label="Generated Video"] *[class*="progress"],
            .image-container[aria-label="Generated Video"] *[class*="loading"],
            .image-container[aria-label="Generated Video"] *[class*="status"],
            .image-container[aria-label="Generated Video"] *[class*="spinner"],
            .progress-text,.gr-progress,.gr-progress-bar,.progress-bar,
            [data-testid="progress"],.status,.loading,.spinner,.gr-spinner,
            .gr-loading,.gr-status,.gpu-init,.initializing,.queue,
            .queued,.waiting,.processing,.gradio-progress,.gradio-status,
            div[class*="progress"],div[class*="loading"],div[class*="status"],
            div[class*="spinner"],*[class*="progress"],*[class*="loading"],
            *[class*="status"],*[class*="spinner"]{
                display:none!important;
                visibility:hidden!important;
                opacity:0!important;
                height:0!important;
                width:0!important;
                font-size:0!important;
                line-height:0!important;
                padding:0!important;
                margin:0!important;
                position:absolute!important;
                left:-9999px!important;
                top:-9999px!important;
                z-index:-9999!important;
                pointer-events:none!important;
                overflow:hidden!important;
            }
            /* EXHAUSTIVE TOOLBAR HIDING */
            .image-container[aria-label="Input Image"] .file-upload,
            .image-container[aria-label="Input Image"] .file-preview,
            .image-container[aria-label="Input Image"] .image-actions,
            .image-container[aria-label="Generated Video"] .file-upload,
            .image-container[aria-label="Generated Video"] .file-preview,
            .image-container[aria-label="Generated Video"] .image-actions{
                display:none!important;
            }
            .image-container[aria-label="Generated Video"].processing{
                background:#000!important;
                position:relative;
            }
            .image-container[aria-label="Generated Video"].processing::before{
                content:"PROCESSING...";
                position:absolute!important;
                top:50%!important;
                left:50%!important;
                transform:translate(-50%,-50%)!important;
                color:#FFF;
                font-family:'Orbitron',sans-serif;
                font-size:1.8rem!important;
                font-weight:700!important;
                text-align:center;
                text-shadow:0 0 10px rgba(0,255,128,0.8)!important;
                animation:pulse 1.5s ease-in-out infinite,glow 2s ease-in-out infinite!important;
                z-index:9999!important;
                width:100%!important;
                height:100%!important;
                display:flex!important;
                align-items:center!important;
                justify-content:center!important;
                pointer-events:none!important;
                background:#000!important;
                border-radius:4px!important;
                box-sizing:border-box!important;
            }
            .image-container[aria-label="Generated Video"].processing *{
                display:none!important;
            }
            input,textarea,.gr-dropdown,.gr-dropdown select{
                background:#000!important;
                color:#FFF!important;
                border:1px solid #FFF!important;
                border-radius:4px;
                padding:.5rem;
                width:100%!important;
                max-width:100vw!important;
                box-sizing:border-box!important;
            }
            .gr-button-primary{
                background:linear-gradient(90deg,rgba(0,255,128,0.3),rgba(0,200,100,0.3),rgba(0,255,128,0.3))!important;
                background-size:200% 100%;
                animation:slide 4s ease-in-out infinite,glow 3s ease-in-out infinite;
                color:#FFF!important;
                border:1px solid #FFF!important;
                border-radius:6px;
                padding:.75rem 1.5rem;
                font-size:1.1rem;
                font-weight:600;
                box-shadow:0 0 14px rgba(0,255,128,0.7)!important;
                transition:box-shadow .3s,transform .3s;
                width:100%!important;
                max-width:100vw!important;
                min-height:48px;
                cursor:pointer;
            }
            .gr-button-primary:hover{
                box-shadow:0 0 20px rgba(0,255,128,0.9)!important;
                animation:slide 4s ease-in-out infinite,glow-hover 3s ease-in-out infinite;
                transform:scale(1.05);
            }
            button[aria-label="Fullscreen"],button[aria-label="Share"]{
                display:none!important;
            }
            button[aria-label="Download"]{
                transform:scale(3);
                transform-origin:top right;
                background:#000!important;
                color:#FFF!important;
                border:1px solid #FFF!important;
                border-radius:4px;
                padding:.4rem!important;
                margin:.5rem!important;
                box-shadow:0 0 8px rgba(255,255,255,0.3)!important;
                transition:box-shadow .3s;
            }
            button[aria-label="Download"]:hover{
                box-shadow:0 0 12px rgba(255,255,255,0.5)!important;
            }
            footer,.gr-button-secondary{
                display:none!important;
            }
            .gr-group{
                background:#000!important;
                border:none!important;
                width:100%!important;
                max-width:100vw!important;
            }
            @media (max-width:768px){
                h1{font-size:4rem;}
                #subtitle{font-size:.9rem;}
                .gr-button-primary{
                    padding:.6rem 1rem;
                    font-size:1rem;
                    box-shadow:0 0 10px rgba(0,255,128,0.7)!important;
                }
                .gr-button-primary:hover{
                    box-shadow:0 0 12px rgba(0,255,128,0.9)!important;
                }
                .image-container{min-height:300px;}
                .image-container[aria-label="Generated Video"].processing::before{
                    font-size:1.2rem!important;
                }
            }
            </style>
            """
        )

        # -----------------------------------------------------------------
        # UI layout – unchanged visual / CSS / 500‑guard / unique‑link
        # -----------------------------------------------------------------
        with gr.Row(elem_id="general_items"):
            gr.Markdown("# ")
            gr.Markdown(
                "Convert an image into an animated video with prompt description.",
                elem_id="subtitle",
            )
            with gr.Column(elem_id="input_column"):
                input_image = gr.Image(
                    type="pil",
                    label="Input Image",
                    sources=["upload"],
                    show_download_button=False,
                    show_share_button=False,
                    interactive=True,
                    elem_classes=["gradio-component", "image-container"],
                )
                prompt = gr.Textbox(
                    label="Prompt",
                    value=default_prompt_i2v,
                    lines=3,
                    placeholder="Describe the desired animation or motion",
                    elem_classes=["gradio-component"],
                )
                generate_button = gr.Button(
                    "Generate Video",
                    variant="primary",
                    elem_classes=["gradio-component", "gr-button-primary"],
                )
                output_video = gr.Video(
                    label="Generated Video",
                    autoplay=True,
                    interactive=False,
                    show_download_button=True,
                    show_share_button=False,
                    elem_classes=["gradio-component", "image-container"],
                )

        # -----------------------------------------------------------------
        # Wiring – keep the same order as the function signature
        # -----------------------------------------------------------------
        generate_button.click(
            fn=generate_video,
            inputs=[
                input_image,
                prompt,
                gr.State(value=6),                     # steps
                gr.State(value=default_negative_prompt),  # negative_prompt
                gr.State(value=3.2),                    # duration_seconds
                gr.State(value=1.5),                    # guidance_scale
                gr.State(value=1.5),                    # guidance_scale_2
                gr.State(value=42),                     # seed
                gr.State(value=True),                   # randomize_seed
                # progress is *not* passed – the @spaces.GPU decorator injects it
            ],
            outputs=[output_video, gr.State(value=42)],
        )

    return demo


# ------------------------------------------------------------
#  MAIN
# ------------------------------------------------------------
if __name__ == "__main__":
    demo = create_demo()
    # keep the launch flags you originally used
    demo.queue().launch(share=True)