observantdistressed commited on
Commit
f5259fc
·
verified ·
1 Parent(s): 8f58600

UI: blue→green→purple gradient theme with frosted glass panels

Browse files
Files changed (1) hide show
  1. app.py +1119 -799
app.py CHANGED
@@ -1,800 +1,1120 @@
1
- import os; os.system('pip install --upgrade --no-deps spaces')
2
- import spaces
3
- import shutil
4
- import subprocess
5
- import sys
6
- import copy
7
- import random
8
- import tempfile
9
- import warnings
10
- import time
11
- import gc
12
- import uuid
13
- from tqdm import tqdm
14
- import cv2
15
- import numpy as np
16
- import torch
17
- import torch._dynamo
18
- from torch.nn import functional as F
19
- from PIL import Image
20
-
21
- import gradio as gr
22
- from diffusers import (
23
- FlowMatchEulerDiscreteScheduler,
24
- SASolverScheduler,
25
- DEISMultistepScheduler,
26
- DPMSolverMultistepInverseScheduler,
27
- UniPCMultistepScheduler,
28
- DPMSolverMultistepScheduler,
29
- DPMSolverSinglestepScheduler,
30
- )
31
- from diffusers.pipelines.wan.pipeline_wan_i2v import WanImageToVideoPipeline
32
- from diffusers.utils.export_utils import export_to_video
33
-
34
- from torchao.quantization import quantize_, Float8DynamicActivationFloat8WeightConfig, Int8WeightOnlyConfig
35
- import aoti
36
- import lora_loader
37
-
38
- os.environ["TOKENIZERS_PARALLELISM"] = "true"
39
- warnings.filterwarnings("ignore")
40
- IS_ZERO_GPU = bool(os.getenv("SPACES_ZERO_GPU"))
41
-
42
- # if IS_ZERO_GPU:
43
- # print("Loading...")
44
- # subprocess.run("rm -rf /data-nvme/zerogpu-offload/*", env={}, shell=True)
45
-
46
- # --- FRAME EXTRACTION JS & LOGIC ---
47
-
48
- # JS to grab timestamp from the output video
49
- get_timestamp_js = """
50
- function() {
51
- // Select the video element specifically inside the component with id 'generated-video'
52
- const video = document.querySelector('#generated-video video');
53
-
54
- if (video) {
55
- console.log("Video found! Time: " + video.currentTime);
56
- return video.currentTime;
57
- } else {
58
- console.log("No video element found.");
59
- return 0;
60
- }
61
- }
62
- """
63
-
64
-
65
- def extract_frame(video_path, timestamp):
66
- # Safety check: if no video is present
67
- if not video_path:
68
- return None
69
-
70
- print(f"Extracting frame at timestamp: {timestamp}")
71
-
72
- cap = cv2.VideoCapture(video_path)
73
-
74
- if not cap.isOpened():
75
- return None
76
-
77
- # Calculate frame number
78
- fps = cap.get(cv2.CAP_PROP_FPS)
79
- target_frame_num = int(float(timestamp) * fps)
80
-
81
- # Cap total frames to prevent errors at the very end of video
82
- total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
83
- if target_frame_num >= total_frames:
84
- target_frame_num = total_frames - 1
85
-
86
- # Set position
87
- cap.set(cv2.CAP_PROP_POS_FRAMES, target_frame_num)
88
- ret, frame = cap.read()
89
- cap.release()
90
-
91
- if ret:
92
- # Convert from BGR (OpenCV) to RGB (Gradio)
93
- # Gradio Image component handles Numpy array -> PIL conversion automatically
94
- return cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
95
-
96
- return None
97
-
98
- # --- END FRAME EXTRACTION LOGIC ---
99
-
100
-
101
- def clear_vram():
102
- gc.collect()
103
- torch.cuda.empty_cache()
104
-
105
-
106
- # RIFE
107
- if not os.path.exists("RIFEv4.26_0921.zip"):
108
- print("Downloading RIFE Model...")
109
- subprocess.run([
110
- "wget", "-q",
111
- "https://huggingface.co/thornmaze/RIFE/resolve/main/RIFEv4.26_0921.zip",
112
- "-O", "RIFEv4.26_0921.zip"
113
- ], check=True)
114
- subprocess.run(["unzip", "-o", "RIFEv4.26_0921.zip"], check=True)
115
-
116
- # sys.path.append(os.getcwd())
117
-
118
- from train_log.RIFE_HDv3 import Model
119
- device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
120
- rife_model = Model()
121
- rife_model.load_model("train_log", -1)
122
- rife_model.eval()
123
-
124
-
125
- @torch.no_grad()
126
- def interpolate_bits(frames_np, multiplier=2, scale=1.0):
127
- """
128
- Interpolation maintaining Numpy Float 0-1 format.
129
- Args:
130
- frames_np: Numpy Array (Time, Height, Width, Channels) - Float32 [0.0, 1.0]
131
- multiplier: int (2, 4, 8)
132
- Returns:
133
- List of Numpy Arrays (Height, Width, Channels) - Float32 [0.0, 1.0]
134
- """
135
-
136
- # Handle input shape
137
- if isinstance(frames_np, list):
138
- # Convert list of arrays to one big array for easier shape handling if needed,
139
- # but here we just grab dims from first frame
140
- T = len(frames_np)
141
- H, W, C = frames_np[0].shape
142
- else:
143
- T, H, W, C = frames_np.shape
144
-
145
- # 1. No Interpolation Case
146
- if multiplier < 2:
147
- # Just convert 4D array to list of 3D arrays
148
- if isinstance(frames_np, np.ndarray):
149
- return list(frames_np)
150
- return frames_np
151
-
152
- n_interp = multiplier - 1
153
-
154
- # Pre-calc padding for RIFE (requires dimensions divisible by 32/scale)
155
- tmp = max(128, int(128 / scale))
156
- ph = ((H - 1) // tmp + 1) * tmp
157
- pw = ((W - 1) // tmp + 1) * tmp
158
- padding = (0, pw - W, 0, ph - H)
159
-
160
- # Helper: Numpy (H, W, C) Float -> Tensor (1, C, H, W) Half
161
- def to_tensor(frame_np):
162
- # frame_np is float32 0-1
163
- t = torch.from_numpy(frame_np).to(device)
164
- # HWC -> CHW
165
- t = t.permute(2, 0, 1).unsqueeze(0)
166
- return F.pad(t, padding).half()
167
-
168
- # Helper: Tensor (1, C, H, W) Half -> Numpy (H, W, C) Float
169
- def from_tensor(tensor):
170
- # Crop padding
171
- t = tensor[0, :, :H, :W]
172
- # CHW -> HWC
173
- t = t.permute(1, 2, 0)
174
- # Keep as float32, range 0-1
175
- return t.float().cpu().numpy()
176
-
177
- def make_inference(I0, I1, n):
178
- if rife_model.version >= 3.9:
179
- res = []
180
- for i in range(n):
181
- res.append(rife_model.inference(I0, I1, (i+1) * 1. / (n+1), scale))
182
- return res
183
- else:
184
- middle = rife_model.inference(I0, I1, scale)
185
- if n == 1:
186
- return [middle]
187
- first_half = make_inference(I0, middle, n=n//2)
188
- second_half = make_inference(middle, I1, n=n//2)
189
- if n % 2:
190
- return [*first_half, middle, *second_half]
191
- else:
192
- return [*first_half, *second_half]
193
-
194
- output_frames = []
195
-
196
- # Process Frames
197
- # Load first frame into GPU
198
- I1 = to_tensor(frames_np[0])
199
-
200
- total_steps = T - 1
201
-
202
- with tqdm(total=total_steps, desc="Interpolating", unit="frame") as pbar:
203
-
204
- for i in range(total_steps):
205
- I0 = I1
206
- # Add original frame to output
207
- output_frames.append(from_tensor(I0))
208
-
209
- # Load next frame
210
- I1 = to_tensor(frames_np[i+1])
211
-
212
- # Generate intermediate frames
213
- mid_tensors = make_inference(I0, I1, n_interp)
214
-
215
- # Append intermediate frames
216
- for mid in mid_tensors:
217
- output_frames.append(from_tensor(mid))
218
-
219
- if (i + 1) % 50 == 0:
220
- pbar.update(50)
221
- pbar.update(total_steps % 50)
222
-
223
- # Add the very last frame
224
- output_frames.append(from_tensor(I1))
225
-
226
- # Cleanup
227
- del I0, I1, mid_tensors
228
- torch.cuda.empty_cache()
229
-
230
- return output_frames
231
-
232
-
233
- # WAN
234
-
235
- MODEL_ID = "thornmaze/WAMU_v3_WAN2.2_I2V_LIGHTNING"
236
-
237
- LORA_MODELS = []
238
-
239
- MAX_DIM = 832
240
- MIN_DIM = 480
241
- SQUARE_DIM = 640
242
- MULTIPLE_OF = 16
243
- MAX_SEED = np.iinfo(np.int32).max
244
-
245
- FIXED_FPS = 16
246
- MIN_FRAMES_MODEL = 8
247
- MAX_FRAMES_MODEL = 321
248
-
249
- MIN_DURATION = round(MIN_FRAMES_MODEL / FIXED_FPS, 1)
250
- MAX_DURATION = round(MAX_FRAMES_MODEL / FIXED_FPS, 1)
251
-
252
- SCHEDULER_MAP = {
253
- "FlowMatchEulerDiscrete": FlowMatchEulerDiscreteScheduler,
254
- "SASolver": SASolverScheduler,
255
- "DEISMultistep": DEISMultistepScheduler,
256
- "DPMSolverMultistepInverse": DPMSolverMultistepInverseScheduler,
257
- "UniPCMultistep": UniPCMultistepScheduler,
258
- "DPMSolverMultistep": DPMSolverMultistepScheduler,
259
- "DPMSolverSinglestep": DPMSolverSinglestepScheduler,
260
- }
261
-
262
- pipe = WanImageToVideoPipeline.from_pretrained(
263
- MODEL_ID,
264
- torch_dtype=torch.bfloat16,
265
- ).to('cuda')
266
- original_scheduler = copy.deepcopy(pipe.scheduler)
267
-
268
- for i, lora in enumerate(LORA_MODELS):
269
- name_high_tr = lora["high_tr"].split(".")[0].split("/")[-1] + "Hh"
270
- name_low_tr = lora["low_tr"].split(".")[0].split("/")[-1] + "Ll"
271
-
272
- try:
273
- pipe.load_lora_weights(
274
- lora["repo_id"],
275
- weight_name=lora["high_tr"],
276
- adapter_name=name_high_tr
277
- )
278
-
279
- kwargs_lora = {"load_into_transformer_2": True}
280
- pipe.load_lora_weights(
281
- lora["repo_id"],
282
- weight_name=lora["low_tr"],
283
- adapter_name=name_low_tr,
284
- **kwargs_lora
285
- )
286
-
287
- pipe.set_adapters([name_high_tr, name_low_tr], adapter_weights=[1.0, 1.0])
288
-
289
- pipe.fuse_lora(adapter_names=[name_high_tr], lora_scale=lora["high_scale"], components=["transformer"])
290
- pipe.fuse_lora(adapter_names=[name_low_tr], lora_scale=lora["low_scale"], components=["transformer_2"])
291
-
292
- pipe.unload_lora_weights()
293
-
294
- print(f"Applied: {lora['high_tr']}, hs={lora['high_scale']}/ls={lora['low_scale']}, {i+1}/{len(LORA_MODELS)}")
295
- except Exception as e:
296
- print("Error:", str(e))
297
- print("Failed LoRA:", name_high_tr)
298
- pipe.unload_lora_weights()
299
-
300
- # if os.path.exists(CACHE_DIR):
301
- # shutil.rmtree(CACHE_DIR)
302
- # print("Deleted Hugging Face cache.")
303
- # else:
304
- # print("No hub cache found.")
305
-
306
- quantize_(pipe.text_encoder, Int8WeightOnlyConfig())
307
- torch._dynamo.reset()
308
- quantize_(pipe.transformer, Float8DynamicActivationFloat8WeightConfig())
309
- torch._dynamo.reset()
310
- quantize_(pipe.transformer_2, Float8DynamicActivationFloat8WeightConfig())
311
- torch._dynamo.reset()
312
-
313
- spaces.aoti_load(
314
- module=pipe.transformer,
315
- repo_id='thornmaze/WanTransformer3DModel-sm120-cu130-raa',
316
- )
317
- spaces.aoti_load(
318
- module=pipe.transformer_2,
319
- repo_id='thornmaze/WanTransformer3DModel-sm120-cu130-raa',
320
- )
321
-
322
- # pipe.vae.enable_slicing()
323
- # pipe.vae.enable_tiling()
324
-
325
- default_prompt_i2v = "make this image come alive, cinematic motion, smooth animation"
326
- default_negative_prompt = "色调艳丽, 过曝, 静态, 细节模糊不清, 字幕, 风格, 作品, 画作, 画面, 静止, 整体发灰, 最差质量, 低质量, JPEG压缩残留, 丑陋的, 残缺的, 多余的手指, 画得不好的手部, 画得不好的脸部, 畸形的, 毁容的, 形态畸形的肢体, 手指融合, 静止不动的画面, 杂乱的背景, 三条腿, 背景人很��, 倒着走"
327
-
328
-
329
- def model_title():
330
- return "## Wan 2.2 I2V 14B Lightning — NSFW"
331
-
332
-
333
- def resize_image(image: Image.Image) -> Image.Image:
334
- width, height = image.size
335
- if width == height:
336
- return image.resize((SQUARE_DIM, SQUARE_DIM), Image.LANCZOS)
337
-
338
- aspect_ratio = width / height
339
- MAX_ASPECT_RATIO = MAX_DIM / MIN_DIM
340
- MIN_ASPECT_RATIO = MIN_DIM / MAX_DIM
341
-
342
- image_to_resize = image
343
- if aspect_ratio > MAX_ASPECT_RATIO:
344
- target_w, target_h = MAX_DIM, MIN_DIM
345
- crop_width = int(round(height * MAX_ASPECT_RATIO))
346
- left = (width - crop_width) // 2
347
- image_to_resize = image.crop((left, 0, left + crop_width, height))
348
- elif aspect_ratio < MIN_ASPECT_RATIO:
349
- target_w, target_h = MIN_DIM, MAX_DIM
350
- crop_height = int(round(width / MIN_ASPECT_RATIO))
351
- top = (height - crop_height) // 2
352
- image_to_resize = image.crop((0, top, width, top + crop_height))
353
- else:
354
- if width > height:
355
- target_w = MAX_DIM
356
- target_h = int(round(target_w / aspect_ratio))
357
- else:
358
- target_h = MAX_DIM
359
- target_w = int(round(target_h * aspect_ratio))
360
-
361
- final_w = round(target_w / MULTIPLE_OF) * MULTIPLE_OF
362
- final_h = round(target_h / MULTIPLE_OF) * MULTIPLE_OF
363
- final_w = max(MIN_DIM, min(MAX_DIM, final_w))
364
- final_h = max(MIN_DIM, min(MAX_DIM, final_h))
365
- return image_to_resize.resize((final_w, final_h), Image.LANCZOS)
366
-
367
-
368
- def resize_and_crop_to_match(target_image, reference_image):
369
- ref_width, ref_height = reference_image.size
370
- target_width, target_height = target_image.size
371
- scale = max(ref_width / target_width, ref_height / target_height)
372
- new_width, new_height = int(target_width * scale), int(target_height * scale)
373
- resized = target_image.resize((new_width, new_height), Image.Resampling.LANCZOS)
374
- left, top = (new_width - ref_width) // 2, (new_height - ref_height) // 2
375
- return resized.crop((left, top, left + ref_width, top + ref_height))
376
-
377
-
378
- def get_num_frames(duration_seconds: float):
379
- raw = int(round(duration_seconds * FIXED_FPS))
380
- raw = max(MIN_FRAMES_MODEL, min(MAX_FRAMES_MODEL, raw))
381
- return ((raw - 1) // 4) * 4 + 1
382
-
383
-
384
- def get_inference_duration(
385
- resized_image,
386
- processed_last_image,
387
- prompt,
388
- steps,
389
- negative_prompt,
390
- num_frames,
391
- guidance_scale,
392
- guidance_scale_2,
393
- current_seed,
394
- scheduler_name,
395
- flow_shift,
396
- frame_multiplier,
397
- quality,
398
- duration_seconds,
399
- safe_mode,
400
- lora_groups,
401
- progress
402
- ):
403
- BASE_FRAMES_HEIGHT_WIDTH = 81 * 832 * 624
404
- BASE_STEP_DURATION = 5.
405
- width, height = resized_image.size
406
- factor = num_frames * width * height / BASE_FRAMES_HEIGHT_WIDTH
407
- step_duration = BASE_STEP_DURATION * factor ** 1.5
408
- gen_time = int(steps) * step_duration
409
-
410
- if guidance_scale > 1:
411
- gen_time = gen_time * 2.4
412
-
413
- frame_factor = frame_multiplier // FIXED_FPS
414
- if frame_factor > 1:
415
- total_out_frames = (num_frames * frame_factor) - num_frames
416
- inter_time = (total_out_frames * 0.02)
417
- gen_time += inter_time
418
-
419
- total_time = 15 + gen_time
420
- if safe_mode:
421
- total_time = total_time * 1.30
422
-
423
- return total_time
424
-
425
-
426
- @spaces.GPU(duration=get_inference_duration, size='xlarge')
427
- def run_inference(
428
- resized_image,
429
- processed_last_image,
430
- prompt,
431
- steps,
432
- negative_prompt,
433
- num_frames,
434
- guidance_scale,
435
- guidance_scale_2,
436
- current_seed,
437
- scheduler_name,
438
- flow_shift,
439
- frame_multiplier,
440
- quality,
441
- duration_seconds,
442
- safe_mode=False,
443
- lora_groups=None,
444
- progress=gr.Progress(track_tqdm=True),
445
- ):
446
- scheduler_class = SCHEDULER_MAP.get(scheduler_name)
447
- if scheduler_class.__name__ != pipe.scheduler.config._class_name or flow_shift != pipe.scheduler.config.get("flow_shift", "shift"):
448
- config = copy.deepcopy(original_scheduler.config)
449
- if scheduler_class == FlowMatchEulerDiscreteScheduler:
450
- config['shift'] = flow_shift
451
- else:
452
- config['flow_shift'] = flow_shift
453
- pipe.scheduler = scheduler_class.from_config(config)
454
-
455
- clear_vram()
456
-
457
- task_name = str(uuid.uuid4())[:8]
458
- print(f"Generating {num_frames} frames, task: {task_name}, {duration_seconds}, {resized_image.size}, lora={lora_groups}")
459
- start = time.time()
460
-
461
- lora_loaded = False
462
- lora_adapter_names = []
463
- if lora_groups:
464
- try:
465
- for idx, name in enumerate(lora_groups):
466
- if name and name != "(None)":
467
- lora_adapter_names += lora_loader.load_lora_to_pipe(
468
- pipe, name, adapter_name=f"lora_{idx}"
469
- )
470
- # Activate ALL loaded adapters together (HIGH in transformer, LOW in
471
- # transformer_2); each load_lora_weights alone would leave only the
472
- # last adapter active.
473
- if lora_adapter_names:
474
- pipe.set_adapters(lora_adapter_names, adapter_weights=[1.0] * len(lora_adapter_names))
475
- lora_loaded = True
476
- print(f"LoRA loaded: {lora_groups}")
477
- except Exception as e:
478
- print(f"LoRA warning: {e}")
479
- lora_loader.unload_lora(pipe)
480
-
481
- # LoRA prompt + user prompt combined — lora triggers first for activation
482
- try:
483
- additions = lora_loader.get_lora_prompt_additions(lora_groups)
484
- except Exception:
485
- additions = ""
486
- if additions:
487
- prompt = f"{additions} {prompt}"
488
- print(f"Combined prompt: {prompt}")
489
-
490
- result = pipe(
491
- image=resized_image,
492
- last_image=processed_last_image,
493
- prompt=prompt,
494
- negative_prompt=negative_prompt,
495
- height=resized_image.height,
496
- width=resized_image.width,
497
- num_frames=num_frames,
498
- guidance_scale=float(guidance_scale),
499
- guidance_scale_2=float(guidance_scale_2),
500
- num_inference_steps=int(steps),
501
- generator=torch.Generator(device="cuda").manual_seed(current_seed),
502
- output_type="np"
503
- )
504
-
505
- if lora_loaded:
506
- lora_loader.unload_lora(pipe)
507
-
508
- print("gen time passed:", time.time() - start)
509
-
510
- raw_frames_np = result.frames[0] # Returns (T, H, W, C) float32
511
- pipe.scheduler = original_scheduler
512
-
513
- frame_factor = frame_multiplier // FIXED_FPS
514
- if frame_factor > 1:
515
- start = time.time()
516
- print(f"Processing frames (RIFE Multiplier: {frame_factor}x)...")
517
- rife_model.device()
518
- rife_model.flownet = rife_model.flownet.half()
519
- final_frames = interpolate_bits(raw_frames_np, multiplier=int(frame_factor))
520
- print("Interpolation time passed:", time.time() - start)
521
- else:
522
- final_frames = list(raw_frames_np)
523
-
524
- final_fps = FIXED_FPS * int(frame_factor)
525
-
526
- with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as tmpfile:
527
- video_path = tmpfile.name
528
-
529
- start = time.time()
530
- with tqdm(total=3, desc="Rendering Media", unit="clip") as pbar:
531
- pbar.update(2)
532
- export_to_video(final_frames, video_path, fps=final_fps, quality=quality)
533
- pbar.update(1)
534
- print(f"Export time passed, {final_fps} FPS:", time.time() - start)
535
-
536
- return video_path, task_name
537
-
538
-
539
- def generate_video(
540
- input_image,
541
- last_image,
542
- prompt,
543
- steps=4,
544
- negative_prompt=default_negative_prompt,
545
- duration_seconds=MAX_DURATION,
546
- guidance_scale=1,
547
- guidance_scale_2=1,
548
- seed=42,
549
- randomize_seed=False,
550
- quality=5,
551
- scheduler="UniPCMultistep",
552
- flow_shift=6.0,
553
- frame_multiplier=16,
554
- safe_mode=False,
555
- lora_groups=None,
556
- auto_lora_enabled=True,
557
- video_component=True,
558
- progress=gr.Progress(track_tqdm=True),
559
- ):
560
- """
561
- Generate a video from an input image using the Wan 2.2 14B I2V model with Lightning LoRA.
562
- This function takes an input image and generates a video animation based on the provided
563
- prompt and parameters. It uses an FP8 qunatized Wan 2.2 14B Image-to-Video model in with Lightning LoRA
564
- for fast generation in 4-8 steps.
565
- Args:
566
- input_image (PIL.Image): The input image to animate. Will be resized to target dimensions.
567
- last_image (PIL.Image, optional): The optional last image for the video.
568
- prompt (str): Text prompt describing the desired animation or motion.
569
- steps (int, optional): Number of inference steps. More steps = higher quality but slower.
570
- Defaults to 4. Range: 1-30.
571
- negative_prompt (str, optional): Negative prompt to avoid unwanted elements.
572
- Defaults to default_negative_prompt (contains unwanted visual artifacts).
573
- duration_seconds (float, optional): Duration of the generated video in seconds.
574
- Defaults to 2. Clamped between MIN_FRAMES_MODEL/FIXED_FPS and MAX_FRAMES_MODEL/FIXED_FPS.
575
- guidance_scale (float, optional): Controls adherence to the prompt. Higher values = more adherence.
576
- Defaults to 1.0. Range: 0.0-20.0.
577
- guidance_scale_2 (float, optional): Controls adherence to the prompt. Higher values = more adherence.
578
- Defaults to 1.0. Range: 0.0-20.0.
579
- seed (int, optional): Random seed for reproducible results. Defaults to 42.
580
- Range: 0 to MAX_SEED (2147483647).
581
- randomize_seed (bool, optional): Whether to use a random seed instead of the provided seed.
582
- Defaults to False.
583
- quality (float, optional): Video output quality. Default is 5. Uses variable bit rate.
584
- Highest quality is 10, lowest is 1.
585
- scheduler (str, optional): The name of the scheduler to use for inference. Defaults to "UniPCMultistep".
586
- flow_shift (float, optional): The flow shift value for compatible schedulers. Defaults to 6.0.
587
- frame_multiplier (int, optional): The int value for fps enhancer
588
- video_component(bool, optional): Show video player in output.
589
- Defaults to True.
590
- progress (gr.Progress, optional): Gradio progress tracker. Defaults to gr.Progress(track_tqdm=True).
591
- Returns:
592
- tuple: A tuple containing:
593
- - video_path (str): Path for the video component.
594
- - video_path (str): Path for the file download component. Attempt to avoid reconversion in video component.
595
- - current_seed (int): The seed used for generation.
596
- Raises:
597
- gr.Error: If input_image is None (no image uploaded).
598
- Note:
599
- - Frame count is calculated as duration_seconds * FIXED_FPS (24)
600
- - Output dimensions are adjusted to be multiples of MOD_VALUE (32)
601
- - The function uses GPU acceleration via the @spaces.GPU decorator
602
- - Generation time varies based on steps and duration (see get_duration function)
603
- """
604
-
605
- if input_image is None:
606
- raise gr.Error("Please upload an input image.")
607
-
608
- num_frames = get_num_frames(duration_seconds)
609
- current_seed = random.randint(0, MAX_SEED) if randomize_seed else int(seed)
610
- resized_image = resize_image(input_image)
611
-
612
- processed_last_image = None
613
- if last_image:
614
- processed_last_image = resize_and_crop_to_match(last_image, resized_image)
615
-
616
- # Auto-activate LoRAs from the prompt when enabled, merging with manual picks
617
- if auto_lora_enabled and prompt:
618
- try:
619
- auto_loras = lora_loader.auto_activate_loras(prompt)
620
- except Exception as e:
621
- auto_loras = []
622
- print(f"Auto-LoRA detection error: {e}")
623
- if auto_loras:
624
- print(f"Auto-activated LoRAs: {auto_loras}")
625
- manual = list(lora_groups or [])
626
- merged = list(auto_loras)
627
- for m in manual:
628
- if m not in merged:
629
- merged.append(m)
630
- lora_groups = merged
631
-
632
- video_path, task_n = run_inference(
633
- resized_image,
634
- processed_last_image,
635
- prompt,
636
- steps,
637
- negative_prompt,
638
- num_frames,
639
- guidance_scale,
640
- guidance_scale_2,
641
- current_seed,
642
- scheduler,
643
- flow_shift,
644
- frame_multiplier,
645
- quality,
646
- duration_seconds,
647
- safe_mode,
648
- lora_groups,
649
- progress,
650
- )
651
- print(f"GPU complete: {task_n}")
652
-
653
- return (video_path if video_component else None), video_path, current_seed
654
-
655
-
656
- def _update_auto_lora_preview(prompt, auto_lora_enabled):
657
- if not auto_lora_enabled or not prompt:
658
- return ""
659
- try:
660
- details = lora_loader.auto_activation_details(prompt)
661
- except Exception as e:
662
- return f"<span style='color:#888'>auto-detection error: {e}</span>"
663
- if not details:
664
- return "<span style='color:#888'>No LoRAs matched.</span>"
665
- items = []
666
- for d in details:
667
- phrases = ", ".join(d.get("matched", [])) or "—"
668
- items.append(f"<li><b>{d['group']}</b> <span style='color:#888'>(score {d['score']})</span><br><small>{phrases}</small></li>")
669
- return "<b>Auto-activated:</b><ul style='margin:2px 0;padding-left:18px'>" + "".join(items) + "</ul>"
670
-
671
-
672
- CSS = """
673
- #hidden-timestamp {
674
- opacity: 0;
675
- height: 0px;
676
- width: 0px;
677
- margin: 0px;
678
- padding: 0px;
679
- overflow: hidden;
680
- position: absolute;
681
- pointer-events: none;
682
- }
683
- """
684
-
685
-
686
- with gr.Blocks(delete_cache=(3600, 10800)) as demo:
687
- gr.Markdown(model_title())
688
- gr.Markdown("Run Wan 2.2 in just 4-8 steps, fp8 quantization & AoT compilation - compatible with 🧨 diffusers and ZeroGPU")
689
-
690
- with gr.Row():
691
- with gr.Column():
692
- input_image_component = gr.Image(type="pil", label="Input Image", sources=["upload", "clipboard"])
693
- prompt_input = gr.Textbox(label="Prompt", value=default_prompt_i2v)
694
- auto_lora_checkbox = gr.Checkbox(
695
- label="Auto-activate LoRAs from prompt",
696
- value=True,
697
- interactive=True,
698
- info="Automatically detect matching LoRAs from your prompt text (deduplicated by concept).",
699
- )
700
- auto_lora_preview = gr.HTML(
701
- label="Auto-detected LoRAs",
702
- value="",
703
- elem_id="auto-lora-preview",
704
- )
705
- duration_seconds_input = gr.Slider(minimum=MIN_DURATION, maximum=MAX_DURATION, step=0.1, value=3.5, label="Duration (seconds)", info=f"Clamped to model's {MIN_FRAMES_MODEL}-{MAX_FRAMES_MODEL} frames at {FIXED_FPS}fps.")
706
- frame_multi = gr.Dropdown(
707
- choices=[FIXED_FPS, FIXED_FPS*2, FIXED_FPS*4, FIXED_FPS*8],
708
- value=FIXED_FPS,
709
- label="Video Fluidity (Frames per Second)",
710
- info="Extra frames will be generated using flow estimation, which estimates motion between frames to make the video smoother."
711
- )
712
- safe_mode_checkbox = gr.Checkbox(
713
- label="🛠️ Safe Mode",
714
- value=True,
715
- info="Requests 30% extra processing time to try to prevent unfinished tasks when the server is busy."
716
- )
717
- with gr.Accordion("Advanced Settings", open=False):
718
- last_image_component = gr.Image(type="pil", label="Last Image (Optional)", sources=["upload", "clipboard"])
719
- negative_prompt_input = gr.Textbox(label="Negative Prompt", value=default_negative_prompt, info="Used if any Guidance Scale > 1.", lines=3)
720
- quality_slider = gr.Slider(minimum=1, maximum=10, step=1, value=6, label="Video Quality", info="If set to 10, the generated video may be too large and won't play in the Gradio preview.")
721
- seed_input = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=42, interactive=True)
722
- randomize_seed_checkbox = gr.Checkbox(label="Randomize seed", value=True, interactive=True)
723
- steps_slider = gr.Slider(minimum=1, maximum=30, step=1, value=6, label="Inference Steps")
724
- guidance_scale_input = gr.Slider(minimum=0.0, maximum=10.0, step=0.5, value=1, label="Guidance Scale - high noise stage", info="Values above 1 increase GPU usage and may take longer to process.")
725
- guidance_scale_2_input = gr.Slider(minimum=0.0, maximum=10.0, step=0.5, value=1, label="Guidance Scale 2 - low noise stage")
726
- scheduler_dropdown = gr.Dropdown(
727
- label="Scheduler",
728
- choices=list(SCHEDULER_MAP.keys()),
729
- value="UniPCMultistep",
730
- info="Select a custom scheduler."
731
- )
732
- flow_shift_slider = gr.Slider(minimum=0.5, maximum=15.0, step=0.1, value=3.0, label="Flow Shift")
733
- lora_dropdown = gr.Dropdown(choices=lora_loader.get_lora_choices(), label="LoRA (NSFW)", multiselect=True, info="Select scenario LoRAs")
734
- play_result_video = gr.Checkbox(label="Display result", value=True, interactive=True)
735
-
736
- generate_button = gr.Button("Generate Video", variant="primary")
737
-
738
- with gr.Column():
739
- # ASSIGNED elem_id="generated-video" so JS can find it
740
- video_output = gr.Video(label="Generated Video", autoplay=True, sources=["upload"], buttons=["download", "share"], interactive=True, elem_id="generated-video")
741
-
742
- # --- Frame Grabbing UI ---
743
- with gr.Row():
744
- grab_frame_btn = gr.Button("📸 Use Current Frame as Input", variant="secondary")
745
- timestamp_box = gr.Number(value=0, label="Timestamp", visible=True, elem_id="hidden-timestamp")
746
- # -------------------------
747
-
748
- file_output = gr.File(label="Download Video")
749
-
750
- ui_inputs = [
751
- input_image_component, last_image_component, prompt_input, steps_slider,
752
- negative_prompt_input, duration_seconds_input,
753
- guidance_scale_input, guidance_scale_2_input, seed_input, randomize_seed_checkbox,
754
- quality_slider, scheduler_dropdown, flow_shift_slider, frame_multi,
755
- safe_mode_checkbox,
756
- lora_dropdown,
757
- auto_lora_checkbox,
758
- play_result_video
759
- ]
760
-
761
- generate_button.click(
762
- fn=generate_video,
763
- inputs=ui_inputs,
764
- outputs=[video_output, file_output, seed_input]
765
- )
766
-
767
- # --- Auto-LoRA Preview Events ---
768
- prompt_input.change(
769
- fn=_update_auto_lora_preview,
770
- inputs=[prompt_input, auto_lora_checkbox],
771
- outputs=[auto_lora_preview],
772
- )
773
- auto_lora_checkbox.change(
774
- fn=_update_auto_lora_preview,
775
- inputs=[prompt_input, auto_lora_checkbox],
776
- outputs=[auto_lora_preview],
777
- )
778
-
779
- # --- Frame Grabbing Events ---
780
- # 1. Click button -> JS runs -> puts time in hidden number box
781
- grab_frame_btn.click(
782
- fn=None,
783
- inputs=None,
784
- outputs=[timestamp_box],
785
- js=get_timestamp_js
786
- )
787
-
788
- # 2. Hidden number box changes -> Python runs -> puts frame in Input Image
789
- timestamp_box.change(
790
- fn=extract_frame,
791
- inputs=[video_output, timestamp_box],
792
- outputs=[input_image_component]
793
- )
794
-
795
- if __name__ == "__main__":
796
- demo.queue().launch(
797
- mcp_server=True,
798
- css=CSS,
799
- show_error=True,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
800
  )
 
1
+ import os; os.system('pip install --upgrade --no-deps spaces')
2
+ import spaces
3
+ import shutil
4
+ import subprocess
5
+ import sys
6
+ import copy
7
+ import random
8
+ import tempfile
9
+ import warnings
10
+ import time
11
+ import gc
12
+ import uuid
13
+ from tqdm import tqdm
14
+ import cv2
15
+ import numpy as np
16
+ import torch
17
+ import torch._dynamo
18
+ from torch.nn import functional as F
19
+ from PIL import Image
20
+
21
+ import gradio as gr
22
+ from diffusers import (
23
+ FlowMatchEulerDiscreteScheduler,
24
+ SASolverScheduler,
25
+ DEISMultistepScheduler,
26
+ DPMSolverMultistepInverseScheduler,
27
+ UniPCMultistepScheduler,
28
+ DPMSolverMultistepScheduler,
29
+ DPMSolverSinglestepScheduler,
30
+ )
31
+ from diffusers.pipelines.wan.pipeline_wan_i2v import WanImageToVideoPipeline
32
+ from diffusers.utils.export_utils import export_to_video
33
+
34
+ from torchao.quantization import quantize_, Float8DynamicActivationFloat8WeightConfig, Int8WeightOnlyConfig
35
+ import aoti
36
+ import lora_loader
37
+
38
+ os.environ["TOKENIZERS_PARALLELISM"] = "true"
39
+ warnings.filterwarnings("ignore")
40
+ IS_ZERO_GPU = bool(os.getenv("SPACES_ZERO_GPU"))
41
+
42
+ # if IS_ZERO_GPU:
43
+ # print("Loading...")
44
+ # subprocess.run("rm -rf /data-nvme/zerogpu-offload/*", env={}, shell=True)
45
+
46
+ # --- FRAME EXTRACTION JS & LOGIC ---
47
+
48
+ # JS to grab timestamp from the output video
49
+ get_timestamp_js = """
50
+ function() {
51
+ // Select the video element specifically inside the component with id 'generated-video'
52
+ const video = document.querySelector('#generated-video video');
53
+
54
+ if (video) {
55
+ console.log("Video found! Time: " + video.currentTime);
56
+ return video.currentTime;
57
+ } else {
58
+ console.log("No video element found.");
59
+ return 0;
60
+ }
61
+ }
62
+ """
63
+
64
+
65
+ def extract_frame(video_path, timestamp):
66
+ # Safety check: if no video is present
67
+ if not video_path:
68
+ return None
69
+
70
+ print(f"Extracting frame at timestamp: {timestamp}")
71
+
72
+ cap = cv2.VideoCapture(video_path)
73
+
74
+ if not cap.isOpened():
75
+ return None
76
+
77
+ # Calculate frame number
78
+ fps = cap.get(cv2.CAP_PROP_FPS)
79
+ target_frame_num = int(float(timestamp) * fps)
80
+
81
+ # Cap total frames to prevent errors at the very end of video
82
+ total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
83
+ if target_frame_num >= total_frames:
84
+ target_frame_num = total_frames - 1
85
+
86
+ # Set position
87
+ cap.set(cv2.CAP_PROP_POS_FRAMES, target_frame_num)
88
+ ret, frame = cap.read()
89
+ cap.release()
90
+
91
+ if ret:
92
+ # Convert from BGR (OpenCV) to RGB (Gradio)
93
+ # Gradio Image component handles Numpy array -> PIL conversion automatically
94
+ return cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
95
+
96
+ return None
97
+
98
+ # --- END FRAME EXTRACTION LOGIC ---
99
+
100
+
101
+ def clear_vram():
102
+ gc.collect()
103
+ torch.cuda.empty_cache()
104
+
105
+
106
+ # RIFE
107
+ if not os.path.exists("RIFEv4.26_0921.zip"):
108
+ print("Downloading RIFE Model...")
109
+ subprocess.run([
110
+ "wget", "-q",
111
+ "https://huggingface.co/thornmaze/RIFE/resolve/main/RIFEv4.26_0921.zip",
112
+ "-O", "RIFEv4.26_0921.zip"
113
+ ], check=True)
114
+ subprocess.run(["unzip", "-o", "RIFEv4.26_0921.zip"], check=True)
115
+
116
+ # sys.path.append(os.getcwd())
117
+
118
+ from train_log.RIFE_HDv3 import Model
119
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
120
+ rife_model = Model()
121
+ rife_model.load_model("train_log", -1)
122
+ rife_model.eval()
123
+
124
+
125
+ @torch.no_grad()
126
+ def interpolate_bits(frames_np, multiplier=2, scale=1.0):
127
+ """
128
+ Interpolation maintaining Numpy Float 0-1 format.
129
+ Args:
130
+ frames_np: Numpy Array (Time, Height, Width, Channels) - Float32 [0.0, 1.0]
131
+ multiplier: int (2, 4, 8)
132
+ Returns:
133
+ List of Numpy Arrays (Height, Width, Channels) - Float32 [0.0, 1.0]
134
+ """
135
+
136
+ # Handle input shape
137
+ if isinstance(frames_np, list):
138
+ # Convert list of arrays to one big array for easier shape handling if needed,
139
+ # but here we just grab dims from first frame
140
+ T = len(frames_np)
141
+ H, W, C = frames_np[0].shape
142
+ else:
143
+ T, H, W, C = frames_np.shape
144
+
145
+ # 1. No Interpolation Case
146
+ if multiplier < 2:
147
+ # Just convert 4D array to list of 3D arrays
148
+ if isinstance(frames_np, np.ndarray):
149
+ return list(frames_np)
150
+ return frames_np
151
+
152
+ n_interp = multiplier - 1
153
+
154
+ # Pre-calc padding for RIFE (requires dimensions divisible by 32/scale)
155
+ tmp = max(128, int(128 / scale))
156
+ ph = ((H - 1) // tmp + 1) * tmp
157
+ pw = ((W - 1) // tmp + 1) * tmp
158
+ padding = (0, pw - W, 0, ph - H)
159
+
160
+ # Helper: Numpy (H, W, C) Float -> Tensor (1, C, H, W) Half
161
+ def to_tensor(frame_np):
162
+ # frame_np is float32 0-1
163
+ t = torch.from_numpy(frame_np).to(device)
164
+ # HWC -> CHW
165
+ t = t.permute(2, 0, 1).unsqueeze(0)
166
+ return F.pad(t, padding).half()
167
+
168
+ # Helper: Tensor (1, C, H, W) Half -> Numpy (H, W, C) Float
169
+ def from_tensor(tensor):
170
+ # Crop padding
171
+ t = tensor[0, :, :H, :W]
172
+ # CHW -> HWC
173
+ t = t.permute(1, 2, 0)
174
+ # Keep as float32, range 0-1
175
+ return t.float().cpu().numpy()
176
+
177
+ def make_inference(I0, I1, n):
178
+ if rife_model.version >= 3.9:
179
+ res = []
180
+ for i in range(n):
181
+ res.append(rife_model.inference(I0, I1, (i+1) * 1. / (n+1), scale))
182
+ return res
183
+ else:
184
+ middle = rife_model.inference(I0, I1, scale)
185
+ if n == 1:
186
+ return [middle]
187
+ first_half = make_inference(I0, middle, n=n//2)
188
+ second_half = make_inference(middle, I1, n=n//2)
189
+ if n % 2:
190
+ return [*first_half, middle, *second_half]
191
+ else:
192
+ return [*first_half, *second_half]
193
+
194
+ output_frames = []
195
+
196
+ # Process Frames
197
+ # Load first frame into GPU
198
+ I1 = to_tensor(frames_np[0])
199
+
200
+ total_steps = T - 1
201
+
202
+ with tqdm(total=total_steps, desc="Interpolating", unit="frame") as pbar:
203
+
204
+ for i in range(total_steps):
205
+ I0 = I1
206
+ # Add original frame to output
207
+ output_frames.append(from_tensor(I0))
208
+
209
+ # Load next frame
210
+ I1 = to_tensor(frames_np[i+1])
211
+
212
+ # Generate intermediate frames
213
+ mid_tensors = make_inference(I0, I1, n_interp)
214
+
215
+ # Append intermediate frames
216
+ for mid in mid_tensors:
217
+ output_frames.append(from_tensor(mid))
218
+
219
+ if (i + 1) % 50 == 0:
220
+ pbar.update(50)
221
+ pbar.update(total_steps % 50)
222
+
223
+ # Add the very last frame
224
+ output_frames.append(from_tensor(I1))
225
+
226
+ # Cleanup
227
+ del I0, I1, mid_tensors
228
+ torch.cuda.empty_cache()
229
+
230
+ return output_frames
231
+
232
+
233
+ # WAN
234
+
235
+ MODEL_ID = "thornmaze/WAMU_v3_WAN2.2_I2V_LIGHTNING"
236
+
237
+ LORA_MODELS = []
238
+
239
+ MAX_DIM = 832
240
+ MIN_DIM = 480
241
+ SQUARE_DIM = 640
242
+ MULTIPLE_OF = 16
243
+ MAX_SEED = np.iinfo(np.int32).max
244
+
245
+ FIXED_FPS = 16
246
+ MIN_FRAMES_MODEL = 8
247
+ MAX_FRAMES_MODEL = 321
248
+
249
+ MIN_DURATION = round(MIN_FRAMES_MODEL / FIXED_FPS, 1)
250
+ MAX_DURATION = round(MAX_FRAMES_MODEL / FIXED_FPS, 1)
251
+
252
+ SCHEDULER_MAP = {
253
+ "FlowMatchEulerDiscrete": FlowMatchEulerDiscreteScheduler,
254
+ "SASolver": SASolverScheduler,
255
+ "DEISMultistep": DEISMultistepScheduler,
256
+ "DPMSolverMultistepInverse": DPMSolverMultistepInverseScheduler,
257
+ "UniPCMultistep": UniPCMultistepScheduler,
258
+ "DPMSolverMultistep": DPMSolverMultistepScheduler,
259
+ "DPMSolverSinglestep": DPMSolverSinglestepScheduler,
260
+ }
261
+
262
+ pipe = WanImageToVideoPipeline.from_pretrained(
263
+ MODEL_ID,
264
+ torch_dtype=torch.bfloat16,
265
+ ).to('cuda')
266
+ original_scheduler = copy.deepcopy(pipe.scheduler)
267
+
268
+ for i, lora in enumerate(LORA_MODELS):
269
+ name_high_tr = lora["high_tr"].split(".")[0].split("/")[-1] + "Hh"
270
+ name_low_tr = lora["low_tr"].split(".")[0].split("/")[-1] + "Ll"
271
+
272
+ try:
273
+ pipe.load_lora_weights(
274
+ lora["repo_id"],
275
+ weight_name=lora["high_tr"],
276
+ adapter_name=name_high_tr
277
+ )
278
+
279
+ kwargs_lora = {"load_into_transformer_2": True}
280
+ pipe.load_lora_weights(
281
+ lora["repo_id"],
282
+ weight_name=lora["low_tr"],
283
+ adapter_name=name_low_tr,
284
+ **kwargs_lora
285
+ )
286
+
287
+ pipe.set_adapters([name_high_tr, name_low_tr], adapter_weights=[1.0, 1.0])
288
+
289
+ pipe.fuse_lora(adapter_names=[name_high_tr], lora_scale=lora["high_scale"], components=["transformer"])
290
+ pipe.fuse_lora(adapter_names=[name_low_tr], lora_scale=lora["low_scale"], components=["transformer_2"])
291
+
292
+ pipe.unload_lora_weights()
293
+
294
+ print(f"Applied: {lora['high_tr']}, hs={lora['high_scale']}/ls={lora['low_scale']}, {i+1}/{len(LORA_MODELS)}")
295
+ except Exception as e:
296
+ print("Error:", str(e))
297
+ print("Failed LoRA:", name_high_tr)
298
+ pipe.unload_lora_weights()
299
+
300
+ # if os.path.exists(CACHE_DIR):
301
+ # shutil.rmtree(CACHE_DIR)
302
+ # print("Deleted Hugging Face cache.")
303
+ # else:
304
+ # print("No hub cache found.")
305
+
306
+ quantize_(pipe.text_encoder, Int8WeightOnlyConfig())
307
+ torch._dynamo.reset()
308
+ quantize_(pipe.transformer, Float8DynamicActivationFloat8WeightConfig())
309
+ torch._dynamo.reset()
310
+ quantize_(pipe.transformer_2, Float8DynamicActivationFloat8WeightConfig())
311
+ torch._dynamo.reset()
312
+
313
+ spaces.aoti_load(
314
+ module=pipe.transformer,
315
+ repo_id='thornmaze/WanTransformer3DModel-sm120-cu130-raa',
316
+ )
317
+ spaces.aoti_load(
318
+ module=pipe.transformer_2,
319
+ repo_id='thornmaze/WanTransformer3DModel-sm120-cu130-raa',
320
+ )
321
+
322
+ # pipe.vae.enable_slicing()
323
+ # pipe.vae.enable_tiling()
324
+
325
+ default_prompt_i2v = "make this image come alive, cinematic motion, smooth animation"
326
+ default_negative_prompt = "色调艳丽, 过曝, 静态, 细节模糊不清, 字幕, 风格, 作品, 画作, 画面, 静止, 整体发灰, 最差质量, 低质量, JPEG压缩残留, 丑陋的, 残缺的, 多余的手指, 画得不好的手部, 画得不好的脸部, 畸形的, 毁容的, 形态畸形的肢体, 手指融合, 静止不动的画面, 杂乱的背景, 三条腿, 背景人很多, 倒着走"
327
+
328
+
329
+ def model_title():
330
+ return "## Wan 2.2 I2V 14B Lightning — NSFW"
331
+
332
+
333
+ def resize_image(image: Image.Image) -> Image.Image:
334
+ width, height = image.size
335
+ if width == height:
336
+ return image.resize((SQUARE_DIM, SQUARE_DIM), Image.LANCZOS)
337
+
338
+ aspect_ratio = width / height
339
+ MAX_ASPECT_RATIO = MAX_DIM / MIN_DIM
340
+ MIN_ASPECT_RATIO = MIN_DIM / MAX_DIM
341
+
342
+ image_to_resize = image
343
+ if aspect_ratio > MAX_ASPECT_RATIO:
344
+ target_w, target_h = MAX_DIM, MIN_DIM
345
+ crop_width = int(round(height * MAX_ASPECT_RATIO))
346
+ left = (width - crop_width) // 2
347
+ image_to_resize = image.crop((left, 0, left + crop_width, height))
348
+ elif aspect_ratio < MIN_ASPECT_RATIO:
349
+ target_w, target_h = MIN_DIM, MAX_DIM
350
+ crop_height = int(round(width / MIN_ASPECT_RATIO))
351
+ top = (height - crop_height) // 2
352
+ image_to_resize = image.crop((0, top, width, top + crop_height))
353
+ else:
354
+ if width > height:
355
+ target_w = MAX_DIM
356
+ target_h = int(round(target_w / aspect_ratio))
357
+ else:
358
+ target_h = MAX_DIM
359
+ target_w = int(round(target_h * aspect_ratio))
360
+
361
+ final_w = round(target_w / MULTIPLE_OF) * MULTIPLE_OF
362
+ final_h = round(target_h / MULTIPLE_OF) * MULTIPLE_OF
363
+ final_w = max(MIN_DIM, min(MAX_DIM, final_w))
364
+ final_h = max(MIN_DIM, min(MAX_DIM, final_h))
365
+ return image_to_resize.resize((final_w, final_h), Image.LANCZOS)
366
+
367
+
368
+ def resize_and_crop_to_match(target_image, reference_image):
369
+ ref_width, ref_height = reference_image.size
370
+ target_width, target_height = target_image.size
371
+ scale = max(ref_width / target_width, ref_height / target_height)
372
+ new_width, new_height = int(target_width * scale), int(target_height * scale)
373
+ resized = target_image.resize((new_width, new_height), Image.Resampling.LANCZOS)
374
+ left, top = (new_width - ref_width) // 2, (new_height - ref_height) // 2
375
+ return resized.crop((left, top, left + ref_width, top + ref_height))
376
+
377
+
378
+ def get_num_frames(duration_seconds: float):
379
+ raw = int(round(duration_seconds * FIXED_FPS))
380
+ raw = max(MIN_FRAMES_MODEL, min(MAX_FRAMES_MODEL, raw))
381
+ return ((raw - 1) // 4) * 4 + 1
382
+
383
+
384
+ def get_inference_duration(
385
+ resized_image,
386
+ processed_last_image,
387
+ prompt,
388
+ steps,
389
+ negative_prompt,
390
+ num_frames,
391
+ guidance_scale,
392
+ guidance_scale_2,
393
+ current_seed,
394
+ scheduler_name,
395
+ flow_shift,
396
+ frame_multiplier,
397
+ quality,
398
+ duration_seconds,
399
+ safe_mode,
400
+ lora_groups,
401
+ progress
402
+ ):
403
+ BASE_FRAMES_HEIGHT_WIDTH = 81 * 832 * 624
404
+ BASE_STEP_DURATION = 5.
405
+ width, height = resized_image.size
406
+ factor = num_frames * width * height / BASE_FRAMES_HEIGHT_WIDTH
407
+ step_duration = BASE_STEP_DURATION * factor ** 1.5
408
+ gen_time = int(steps) * step_duration
409
+
410
+ if guidance_scale > 1:
411
+ gen_time = gen_time * 2.4
412
+
413
+ frame_factor = frame_multiplier // FIXED_FPS
414
+ if frame_factor > 1:
415
+ total_out_frames = (num_frames * frame_factor) - num_frames
416
+ inter_time = (total_out_frames * 0.02)
417
+ gen_time += inter_time
418
+
419
+ total_time = 15 + gen_time
420
+ if safe_mode:
421
+ total_time = total_time * 1.30
422
+
423
+ return total_time
424
+
425
+
426
+ @spaces.GPU(duration=get_inference_duration, size='xlarge')
427
+ def run_inference(
428
+ resized_image,
429
+ processed_last_image,
430
+ prompt,
431
+ steps,
432
+ negative_prompt,
433
+ num_frames,
434
+ guidance_scale,
435
+ guidance_scale_2,
436
+ current_seed,
437
+ scheduler_name,
438
+ flow_shift,
439
+ frame_multiplier,
440
+ quality,
441
+ duration_seconds,
442
+ safe_mode=False,
443
+ lora_groups=None,
444
+ progress=gr.Progress(track_tqdm=True),
445
+ ):
446
+ scheduler_class = SCHEDULER_MAP.get(scheduler_name)
447
+ if scheduler_class.__name__ != pipe.scheduler.config._class_name or flow_shift != pipe.scheduler.config.get("flow_shift", "shift"):
448
+ config = copy.deepcopy(original_scheduler.config)
449
+ if scheduler_class == FlowMatchEulerDiscreteScheduler:
450
+ config['shift'] = flow_shift
451
+ else:
452
+ config['flow_shift'] = flow_shift
453
+ pipe.scheduler = scheduler_class.from_config(config)
454
+
455
+ clear_vram()
456
+
457
+ task_name = str(uuid.uuid4())[:8]
458
+ print(f"Generating {num_frames} frames, task: {task_name}, {duration_seconds}, {resized_image.size}, lora={lora_groups}")
459
+ start = time.time()
460
+
461
+ lora_loaded = False
462
+ lora_adapter_names = []
463
+ if lora_groups:
464
+ try:
465
+ for idx, name in enumerate(lora_groups):
466
+ if name and name != "(None)":
467
+ lora_adapter_names += lora_loader.load_lora_to_pipe(
468
+ pipe, name, adapter_name=f"lora_{idx}"
469
+ )
470
+ # Activate ALL loaded adapters together (HIGH in transformer, LOW in
471
+ # transformer_2); each load_lora_weights alone would leave only the
472
+ # last adapter active.
473
+ if lora_adapter_names:
474
+ pipe.set_adapters(lora_adapter_names, adapter_weights=[1.0] * len(lora_adapter_names))
475
+ lora_loaded = True
476
+ print(f"LoRA loaded: {lora_groups}")
477
+ except Exception as e:
478
+ print(f"LoRA warning: {e}")
479
+ lora_loader.unload_lora(pipe)
480
+
481
+ # LoRA prompt + user prompt combined — lora triggers first for activation
482
+ try:
483
+ additions = lora_loader.get_lora_prompt_additions(lora_groups)
484
+ except Exception:
485
+ additions = ""
486
+ if additions:
487
+ prompt = f"{additions} {prompt}"
488
+ print(f"Combined prompt: {prompt}")
489
+
490
+ result = pipe(
491
+ image=resized_image,
492
+ last_image=processed_last_image,
493
+ prompt=prompt,
494
+ negative_prompt=negative_prompt,
495
+ height=resized_image.height,
496
+ width=resized_image.width,
497
+ num_frames=num_frames,
498
+ guidance_scale=float(guidance_scale),
499
+ guidance_scale_2=float(guidance_scale_2),
500
+ num_inference_steps=int(steps),
501
+ generator=torch.Generator(device="cuda").manual_seed(current_seed),
502
+ output_type="np"
503
+ )
504
+
505
+ if lora_loaded:
506
+ lora_loader.unload_lora(pipe)
507
+
508
+ print("gen time passed:", time.time() - start)
509
+
510
+ raw_frames_np = result.frames[0] # Returns (T, H, W, C) float32
511
+ pipe.scheduler = original_scheduler
512
+
513
+ frame_factor = frame_multiplier // FIXED_FPS
514
+ if frame_factor > 1:
515
+ start = time.time()
516
+ print(f"Processing frames (RIFE Multiplier: {frame_factor}x)...")
517
+ rife_model.device()
518
+ rife_model.flownet = rife_model.flownet.half()
519
+ final_frames = interpolate_bits(raw_frames_np, multiplier=int(frame_factor))
520
+ print("Interpolation time passed:", time.time() - start)
521
+ else:
522
+ final_frames = list(raw_frames_np)
523
+
524
+ final_fps = FIXED_FPS * int(frame_factor)
525
+
526
+ with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as tmpfile:
527
+ video_path = tmpfile.name
528
+
529
+ start = time.time()
530
+ with tqdm(total=3, desc="Rendering Media", unit="clip") as pbar:
531
+ pbar.update(2)
532
+ export_to_video(final_frames, video_path, fps=final_fps, quality=quality)
533
+ pbar.update(1)
534
+ print(f"Export time passed, {final_fps} FPS:", time.time() - start)
535
+
536
+ return video_path, task_name
537
+
538
+
539
+ def generate_video(
540
+ input_image,
541
+ last_image,
542
+ prompt,
543
+ steps=4,
544
+ negative_prompt=default_negative_prompt,
545
+ duration_seconds=MAX_DURATION,
546
+ guidance_scale=1,
547
+ guidance_scale_2=1,
548
+ seed=42,
549
+ randomize_seed=False,
550
+ quality=5,
551
+ scheduler="UniPCMultistep",
552
+ flow_shift=6.0,
553
+ frame_multiplier=16,
554
+ safe_mode=False,
555
+ lora_groups=None,
556
+ auto_lora_enabled=True,
557
+ video_component=True,
558
+ progress=gr.Progress(track_tqdm=True),
559
+ ):
560
+ """
561
+ Generate a video from an input image using the Wan 2.2 14B I2V model with Lightning LoRA.
562
+ This function takes an input image and generates a video animation based on the provided
563
+ prompt and parameters. It uses an FP8 qunatized Wan 2.2 14B Image-to-Video model in with Lightning LoRA
564
+ for fast generation in 4-8 steps.
565
+ Args:
566
+ input_image (PIL.Image): The input image to animate. Will be resized to target dimensions.
567
+ last_image (PIL.Image, optional): The optional last image for the video.
568
+ prompt (str): Text prompt describing the desired animation or motion.
569
+ steps (int, optional): Number of inference steps. More steps = higher quality but slower.
570
+ Defaults to 4. Range: 1-30.
571
+ negative_prompt (str, optional): Negative prompt to avoid unwanted elements.
572
+ Defaults to default_negative_prompt (contains unwanted visual artifacts).
573
+ duration_seconds (float, optional): Duration of the generated video in seconds.
574
+ Defaults to 2. Clamped between MIN_FRAMES_MODEL/FIXED_FPS and MAX_FRAMES_MODEL/FIXED_FPS.
575
+ guidance_scale (float, optional): Controls adherence to the prompt. Higher values = more adherence.
576
+ Defaults to 1.0. Range: 0.0-20.0.
577
+ guidance_scale_2 (float, optional): Controls adherence to the prompt. Higher values = more adherence.
578
+ Defaults to 1.0. Range: 0.0-20.0.
579
+ seed (int, optional): Random seed for reproducible results. Defaults to 42.
580
+ Range: 0 to MAX_SEED (2147483647).
581
+ randomize_seed (bool, optional): Whether to use a random seed instead of the provided seed.
582
+ Defaults to False.
583
+ quality (float, optional): Video output quality. Default is 5. Uses variable bit rate.
584
+ Highest quality is 10, lowest is 1.
585
+ scheduler (str, optional): The name of the scheduler to use for inference. Defaults to "UniPCMultistep".
586
+ flow_shift (float, optional): The flow shift value for compatible schedulers. Defaults to 6.0.
587
+ frame_multiplier (int, optional): The int value for fps enhancer
588
+ video_component(bool, optional): Show video player in output.
589
+ Defaults to True.
590
+ progress (gr.Progress, optional): Gradio progress tracker. Defaults to gr.Progress(track_tqdm=True).
591
+ Returns:
592
+ tuple: A tuple containing:
593
+ - video_path (str): Path for the video component.
594
+ - video_path (str): Path for the file download component. Attempt to avoid reconversion in video component.
595
+ - current_seed (int): The seed used for generation.
596
+ Raises:
597
+ gr.Error: If input_image is None (no image uploaded).
598
+ Note:
599
+ - Frame count is calculated as duration_seconds * FIXED_FPS (24)
600
+ - Output dimensions are adjusted to be multiples of MOD_VALUE (32)
601
+ - The function uses GPU acceleration via the @spaces.GPU decorator
602
+ - Generation time varies based on steps and duration (see get_duration function)
603
+ """
604
+
605
+ if input_image is None:
606
+ raise gr.Error("Please upload an input image.")
607
+
608
+ num_frames = get_num_frames(duration_seconds)
609
+ current_seed = random.randint(0, MAX_SEED) if randomize_seed else int(seed)
610
+ resized_image = resize_image(input_image)
611
+
612
+ processed_last_image = None
613
+ if last_image:
614
+ processed_last_image = resize_and_crop_to_match(last_image, resized_image)
615
+
616
+ # Auto-activate LoRAs from the prompt when enabled, merging with manual picks
617
+ if auto_lora_enabled and prompt:
618
+ try:
619
+ auto_loras = lora_loader.auto_activate_loras(prompt)
620
+ except Exception as e:
621
+ auto_loras = []
622
+ print(f"Auto-LoRA detection error: {e}")
623
+ if auto_loras:
624
+ print(f"Auto-activated LoRAs: {auto_loras}")
625
+ manual = list(lora_groups or [])
626
+ merged = list(auto_loras)
627
+ for m in manual:
628
+ if m not in merged:
629
+ merged.append(m)
630
+ lora_groups = merged
631
+
632
+ video_path, task_n = run_inference(
633
+ resized_image,
634
+ processed_last_image,
635
+ prompt,
636
+ steps,
637
+ negative_prompt,
638
+ num_frames,
639
+ guidance_scale,
640
+ guidance_scale_2,
641
+ current_seed,
642
+ scheduler,
643
+ flow_shift,
644
+ frame_multiplier,
645
+ quality,
646
+ duration_seconds,
647
+ safe_mode,
648
+ lora_groups,
649
+ progress,
650
+ )
651
+ print(f"GPU complete: {task_n}")
652
+
653
+ return (video_path if video_component else None), video_path, current_seed
654
+
655
+
656
+ def _update_auto_lora_preview(prompt, auto_lora_enabled):
657
+ if not auto_lora_enabled or not prompt:
658
+ return ""
659
+ try:
660
+ details = lora_loader.auto_activation_details(prompt)
661
+ except Exception as e:
662
+ return f"<span style='color:#888'>auto-detection error: {e}</span>"
663
+ if not details:
664
+ return "<span style='color:#888'>No LoRAs matched.</span>"
665
+ items = []
666
+ for d in details:
667
+ phrases = ", ".join(d.get("matched", [])) or "—"
668
+ items.append(f"<li><b>{d['group']}</b> <span style='color:#888'>(score {d['score']})</span><br><small>{phrases}</small></li>")
669
+ return "<b>Auto-activated:</b><ul style='margin:2px 0;padding-left:18px'>" + "".join(items) + "</ul>"
670
+
671
+
672
+ CSS = """
673
+ /* ── Hidden timestamp (preserved) ── */
674
+ #hidden-timestamp {
675
+ opacity: 0;
676
+ height: 0px;
677
+ width: 0px;
678
+ margin: 0px;
679
+ padding: 0px;
680
+ overflow: hidden;
681
+ position: absolute;
682
+ pointer-events: none;
683
+ }
684
+
685
+ /* ── GLOBAL BACKGROUND — vivid blue→green→purple gradient ── */
686
+ .gradio-container {
687
+ background: linear-gradient(135deg,
688
+ #0a0e2a 0%,
689
+ #0d1b3e 12%,
690
+ #0f2847 25%,
691
+ #0a3d2e 40%,
692
+ #0e5135 55%,
693
+ #1a3a5c 65%,
694
+ #2d1b69 80%,
695
+ #1e1145 100%) !important;
696
+ background-attachment: fixed !important;
697
+ min-height: 100vh !important;
698
+ color: #e8ecf4 !important;
699
+ font-family: 'Segoe UI', system-ui, -apple-system, sans-serif !important;
700
+ }
701
+
702
+ /* Subtle animated grain overlay for atmosphere */
703
+ .gradio-container::before {
704
+ content: '' !important;
705
+ position: fixed !important;
706
+ inset: 0 !important;
707
+ background-image:
708
+ radial-gradient(ellipse at 20% 20%, rgba(0, 200, 180, 0.08) 0%, transparent 60%),
709
+ radial-gradient(ellipse at 80% 30%, rgba(100, 60, 220, 0.1) 0%, transparent 55%),
710
+ radial-gradient(ellipse at 50% 80%, rgba(30, 120, 200, 0.07) 0%, transparent 50%) !important;
711
+ pointer-events: none !important;
712
+ z-index: 0 !important;
713
+ }
714
+
715
+ .gradio-container > * {
716
+ position: relative !important;
717
+ z-index: 1 !important;
718
+ }
719
+
720
+ /* ── HEADER / TITLE ── */
721
+ .gradio-container h1,
722
+ .gradio-container .gr-title,
723
+ .gradio-container .prose {
724
+ background: linear-gradient(90deg, #00e5c8, #6c5ce7, #a855f7) !important;
725
+ -webkit-background-clip: text !important;
726
+ -webkit-text-fill-color: transparent !important;
727
+ background-clip: text !important;
728
+ font-weight: 800 !important;
729
+ letter-spacing: -0.02em !important;
730
+ }
731
+
732
+ /* Subtitle text */
733
+ .gradio-container .prose p,
734
+ .gradio-container > div > div > .prose:nth-child(2) {
735
+ color: #b0c4de !important;
736
+ }
737
+
738
+ /* ── PANELS / CARDS — frosted glass ── */
739
+ .gradio-container .panel,
740
+ .gradio-container .gr-group,
741
+ .gradio-container .gr-block {
742
+ background: rgba(255, 255, 255, 0.04) !important;
743
+ backdrop-filter: blur(16px) saturate(1.4) !important;
744
+ -webkit-backdrop-filter: blur(16px) saturate(1.4) !important;
745
+ border: 1px solid rgba(255, 255, 255, 0.08) !important;
746
+ border-radius: 14px !important;
747
+ box-shadow:
748
+ 0 4px 30px rgba(0, 0, 0, 0.3),
749
+ inset 0 1px 0 rgba(255, 255, 255, 0.06) !important;
750
+ }
751
+
752
+ /* Image/video upload areas */
753
+ .gradio-container .upload-container,
754
+ .gradio-container .file-upload,
755
+ .gradio-container [data-testid="image"] {
756
+ background: rgba(255, 255, 255, 0.03) !important;
757
+ border: 2px dashed rgba(100, 200, 255, 0.2) !important;
758
+ border-radius: 12px !important;
759
+ transition: border-color 0.3s ease, box-shadow 0.3s ease !important;
760
+ }
761
+ .gradio-container .upload-container:hover,
762
+ .gradio-container .file-upload:hover,
763
+ .gradio-container [data-testid="image"]:hover {
764
+ border-color: rgba(0, 229, 200, 0.5) !important;
765
+ box-shadow: 0 0 20px rgba(0, 229, 200, 0.1) !important;
766
+ }
767
+
768
+ /* ── LABELS — crisp white with subtle glow ── */
769
+ .gradio-container label,
770
+ .gradio-container .gr-label,
771
+ .gradio-container .gr-block-label {
772
+ color: #d6e4f0 !important;
773
+ font-weight: 600 !important;
774
+ text-shadow: 0 0 8px rgba(100, 180, 255, 0.15) !important;
775
+ }
776
+
777
+ /* ── TEXT INPUTS & TEXTBOX — glass borders ── */
778
+ .gradio-container input[type="text"],
779
+ .gradio-container textarea,
780
+ .gradio-container .gr-input,
781
+ .gradio-container .gr-textbox,
782
+ .gradio-container .gr-textarea {
783
+ background: rgba(10, 15, 40, 0.6) !important;
784
+ border: 1px solid rgba(100, 180, 255, 0.15) !important;
785
+ border-radius: 10px !important;
786
+ color: #e8ecf4 !important;
787
+ box-shadow: inset 0 2px 6px rgba(0, 0, 0, 0.2) !important;
788
+ transition: border-color 0.3s ease, box-shadow 0.3s ease !important;
789
+ }
790
+ .gradio-container input[type="text"]:focus,
791
+ .gradio-container textarea:focus,
792
+ .gradio-container .gr-input:focus,
793
+ .gradio-container .gr-textbox:focus {
794
+ border-color: rgba(108, 92, 231, 0.6) !important;
795
+ box-shadow:
796
+ inset 0 2px 6px rgba(0, 0, 0, 0.2),
797
+ 0 0 12px rgba(108, 92, 231, 0.2) !important;
798
+ outline: none !important;
799
+ }
800
+
801
+ /* ── SLIDERS — glowing track ── */
802
+ .gradio-container input[type="range"],
803
+ .gradio-container .range-slider,
804
+ .gradio-container .gr-slider {
805
+ accent-color: #00d4aa !important;
806
+ }
807
+
808
+ .gradio-container input[type="range"]::-webkit-slider-runnable-track {
809
+ background: linear-gradient(90deg, #00d4aa, #6c5ce7) !important;
810
+ height: 6px !important;
811
+ border-radius: 3px !important;
812
+ }
813
+ .gradio-container input[type="range"]::-webkit-slider-thumb {
814
+ -webkit-appearance: none !important;
815
+ width: 18px !important;
816
+ height: 18px !important;
817
+ border-radius: 50% !important;
818
+ background: linear-gradient(135deg, #00e5c8, #6c5ce7) !important;
819
+ box-shadow: 0 0 10px rgba(0, 229, 200, 0.5), 0 2px 6px rgba(0, 0, 0, 0.3) !important;
820
+ cursor: pointer !important;
821
+ border: 2px solid rgba(255, 255, 255, 0.3) !important;
822
+ margin-top: -6px !important;
823
+ transition: transform 0.2s ease, box-shadow 0.2s ease !important;
824
+ }
825
+ .gradio-container input[type="range"]::-webkit-slider-thumb:hover {
826
+ transform: scale(1.15) !important;
827
+ box-shadow: 0 0 18px rgba(0, 229, 200, 0.7), 0 2px 8px rgba(0, 0, 0, 0.4) !important;
828
+ }
829
+
830
+ /* Number inputs */
831
+ .gradio-container input[type="number"],
832
+ .gradio-container .gr-number {
833
+ background: rgba(10, 15, 40, 0.6) !important;
834
+ border: 1px solid rgba(100, 180, 255, 0.15) !important;
835
+ border-radius: 8px !important;
836
+ color: #e8ecf4 !important;
837
+ box-shadow: inset 0 2px 6px rgba(0, 0, 0, 0.2) !important;
838
+ }
839
+
840
+ /* ── DROPDOWN / SELECT ── */
841
+ .gradio-container select,
842
+ .gradio-container .gr-dropdown,
843
+ .gradio-container .multiselect {
844
+ background: rgba(10, 15, 40, 0.7) !important;
845
+ border: 1px solid rgba(100, 180, 255, 0.15) !important;
846
+ border-radius: 10px !important;
847
+ color: #e8ecf4 !important;
848
+ box-shadow: inset 0 2px 6px rgba(0, 0, 0, 0.2) !important;
849
+ transition: border-color 0.3s ease, box-shadow 0.3s ease !important;
850
+ }
851
+ .gradio-container select:focus,
852
+ .gradio-container .gr-dropdown:focus {
853
+ border-color: rgba(108, 92, 231, 0.6) !important;
854
+ box-shadow: 0 0 12px rgba(108, 92, 231, 0.2) !important;
855
+ }
856
+
857
+ /* ── CHECKBOXES ── */
858
+ .gradio-container input[type="checkbox"] {
859
+ accent-color: #00d4aa !important;
860
+ width: 18px !important;
861
+ height: 18px !important;
862
+ cursor: pointer !important;
863
+ }
864
+ .gradio-container input[type="checkbox"]:checked {
865
+ accent-color: #00e5c8 !important;
866
+ }
867
+
868
+ /* ── ACCORDION ── */
869
+ .gradio-container .accordion,
870
+ .gradio-container details {
871
+ background: rgba(255, 255, 255, 0.03) !important;
872
+ border: 1px solid rgba(100, 180, 255, 0.1) !important;
873
+ border-radius: 12px !important;
874
+ overflow: hidden !important;
875
+ }
876
+ .gradio-container .accordion summary,
877
+ .gradio-container details summary {
878
+ color: #c8d8e8 !important;
879
+ font-weight: 600 !important;
880
+ padding: 12px 16px !important;
881
+ cursor: pointer !important;
882
+ transition: color 0.2s ease !important;
883
+ }
884
+ .gradio-container .accordion summary:hover,
885
+ .gradio-container details summary:hover {
886
+ color: #00e5c8 !important;
887
+ }
888
+
889
+ /* ── GENERATE BUTTON — vibrant gradient with glow ── */
890
+ .gradio-container button.primary,
891
+ .gradio-container button.lg.primary,
892
+ .gradio-container .gr-button-primary,
893
+ .gradio-container button[data-variant="primary"] {
894
+ background: linear-gradient(135deg, #00b894, #0984e3, #6c5ce7) !important;
895
+ background-size: 200% 200% !important;
896
+ animation: btn-shimmer 4s ease-in-out infinite !important;
897
+ color: #ffffff !important;
898
+ font-weight: 700 !important;
899
+ font-size: 1.05em !important;
900
+ letter-spacing: 0.03em !important;
901
+ border: none !important;
902
+ border-radius: 12px !important;
903
+ padding: 14px 32px !important;
904
+ box-shadow:
905
+ 0 4px 20px rgba(0, 184, 148, 0.35),
906
+ 0 0 40px rgba(108, 92, 231, 0.15),
907
+ inset 0 1px 0 rgba(255, 255, 255, 0.15) !important;
908
+ transition: transform 0.25s ease, box-shadow 0.3s ease !important;
909
+ text-shadow: 0 1px 3px rgba(0, 0, 0, 0.3) !important;
910
+ cursor: pointer !important;
911
+ }
912
+ .gradio-container button.primary:hover,
913
+ .gradio-container button.lg.primary:hover,
914
+ .gradio-container .gr-button-primary:hover,
915
+ .gradio-container button[data-variant="primary"]:hover {
916
+ transform: translateY(-2px) !important;
917
+ box-shadow:
918
+ 0 8px 32px rgba(0, 184, 148, 0.5),
919
+ 0 0 60px rgba(108, 92, 231, 0.25),
920
+ inset 0 1px 0 rgba(255, 255, 255, 0.2) !important;
921
+ }
922
+ .gradio-container button.primary:active,
923
+ .gradio-container button.lg.primary:active {
924
+ transform: translateY(0px) !important;
925
+ box-shadow:
926
+ 0 2px 12px rgba(0, 184, 148, 0.3),
927
+ inset 0 2px 4px rgba(0, 0, 0, 0.2) !important;
928
+ }
929
+
930
+ @keyframes btn-shimmer {
931
+ 0%, 100% { background-position: 0% 50%; }
932
+ 50% { background-position: 100% 50%; }
933
+ }
934
+
935
+ /* ── SECONDARY BUTTONS ── */
936
+ .gradio-container button.secondary,
937
+ .gradio-container button.lg.secondary,
938
+ .gradio-container .gr-button-secondary,
939
+ .gradio-container button[data-variant="secondary"] {
940
+ background: rgba(255, 255, 255, 0.06) !important;
941
+ backdrop-filter: blur(8px) !important;
942
+ border: 1px solid rgba(100, 180, 255, 0.2) !important;
943
+ color: #b8c8e0 !important;
944
+ border-radius: 10px !important;
945
+ font-weight: 600 !important;
946
+ transition: all 0.25s ease !important;
947
+ }
948
+ .gradio-container button.secondary:hover,
949
+ .gradio-container button.lg.secondary:hover,
950
+ .gradio-container .gr-button-secondary:hover,
951
+ .gradio-container button[data-variant="secondary"]:hover {
952
+ background: rgba(108, 92, 231, 0.15) !important;
953
+ border-color: rgba(108, 92, 231, 0.4) !important;
954
+ color: #e0e8f8 !important;
955
+ box-shadow: 0 0 20px rgba(108, 92, 231, 0.15) !important;
956
+ }
957
+
958
+ /* ── FILE OUTPUT ── */
959
+ .gradio-container .file-preview,
960
+ .gradio-container [data-testid="file"] {
961
+ background: rgba(10, 15, 40, 0.5) !important;
962
+ border: 1px solid rgba(100, 180, 255, 0.1) !important;
963
+ border-radius: 10px !important;
964
+ }
965
+
966
+ /* ── INFO TEXT ── */
967
+ .gradio-container .prose small,
968
+ .gradio-container small,
969
+ .gradio-container .gr-info {
970
+ color: #7a8fa8 !important;
971
+ }
972
+
973
+ /* ── SCROLLBAR — themed ── */
974
+ .gradio-container ::-webkit-scrollbar {
975
+ width: 8px !important;
976
+ height: 8px !important;
977
+ }
978
+ .gradio-container ::-webkit-scrollbar-track {
979
+ background: rgba(10, 15, 40, 0.4) !important;
980
+ border-radius: 4px !important;
981
+ }
982
+ .gradio-container ::-webkit-scrollbar-thumb {
983
+ background: linear-gradient(180deg, #00b894, #6c5ce7) !important;
984
+ border-radius: 4px !important;
985
+ }
986
+ .gradio-container ::-webkit-scrollbar-thumb:hover {
987
+ background: linear-gradient(180deg, #00d4aa, #a855f7) !important;
988
+ }
989
+
990
+ /* ── AUTO-DETECTED LORA PREVIEW ── */
991
+ #auto-lora-preview {
992
+ color: #b8d4e8 !important;
993
+ }
994
+ #auto-lora-preview b {
995
+ color: #00e5c8 !important;
996
+ }
997
+
998
+ /* ── COLUMN SEPARATOR — subtle glow line ── */
999
+ .gradio-container .gr-row > .gr-col,
1000
+ .gradio-container .gr-row > div {
1001
+ border-left: 1px solid rgba(108, 92, 231, 0.08) !important;
1002
+ }
1003
+ """
1004
+
1005
+
1006
+ with gr.Blocks(delete_cache=(3600, 10800)) as demo:
1007
+ gr.Markdown(model_title())
1008
+ gr.Markdown("Run Wan 2.2 in just 4-8 steps, fp8 quantization & AoT compilation - compatible with 🧨 diffusers and ZeroGPU")
1009
+
1010
+ with gr.Row():
1011
+ with gr.Column():
1012
+ input_image_component = gr.Image(type="pil", label="Input Image", sources=["upload", "clipboard"])
1013
+ prompt_input = gr.Textbox(label="Prompt", value=default_prompt_i2v)
1014
+ auto_lora_checkbox = gr.Checkbox(
1015
+ label="Auto-activate LoRAs from prompt",
1016
+ value=True,
1017
+ interactive=True,
1018
+ info="Automatically detect matching LoRAs from your prompt text (deduplicated by concept).",
1019
+ )
1020
+ auto_lora_preview = gr.HTML(
1021
+ label="Auto-detected LoRAs",
1022
+ value="",
1023
+ elem_id="auto-lora-preview",
1024
+ )
1025
+ duration_seconds_input = gr.Slider(minimum=MIN_DURATION, maximum=MAX_DURATION, step=0.1, value=3.5, label="Duration (seconds)", info=f"Clamped to model's {MIN_FRAMES_MODEL}-{MAX_FRAMES_MODEL} frames at {FIXED_FPS}fps.")
1026
+ frame_multi = gr.Dropdown(
1027
+ choices=[FIXED_FPS, FIXED_FPS*2, FIXED_FPS*4, FIXED_FPS*8],
1028
+ value=FIXED_FPS,
1029
+ label="Video Fluidity (Frames per Second)",
1030
+ info="Extra frames will be generated using flow estimation, which estimates motion between frames to make the video smoother."
1031
+ )
1032
+ safe_mode_checkbox = gr.Checkbox(
1033
+ label="🛠️ Safe Mode",
1034
+ value=True,
1035
+ info="Requests 30% extra processing time to try to prevent unfinished tasks when the server is busy."
1036
+ )
1037
+ with gr.Accordion("Advanced Settings", open=False):
1038
+ last_image_component = gr.Image(type="pil", label="Last Image (Optional)", sources=["upload", "clipboard"])
1039
+ negative_prompt_input = gr.Textbox(label="Negative Prompt", value=default_negative_prompt, info="Used if any Guidance Scale > 1.", lines=3)
1040
+ quality_slider = gr.Slider(minimum=1, maximum=10, step=1, value=6, label="Video Quality", info="If set to 10, the generated video may be too large and won't play in the Gradio preview.")
1041
+ seed_input = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=42, interactive=True)
1042
+ randomize_seed_checkbox = gr.Checkbox(label="Randomize seed", value=True, interactive=True)
1043
+ steps_slider = gr.Slider(minimum=1, maximum=30, step=1, value=6, label="Inference Steps")
1044
+ guidance_scale_input = gr.Slider(minimum=0.0, maximum=10.0, step=0.5, value=1, label="Guidance Scale - high noise stage", info="Values above 1 increase GPU usage and may take longer to process.")
1045
+ guidance_scale_2_input = gr.Slider(minimum=0.0, maximum=10.0, step=0.5, value=1, label="Guidance Scale 2 - low noise stage")
1046
+ scheduler_dropdown = gr.Dropdown(
1047
+ label="Scheduler",
1048
+ choices=list(SCHEDULER_MAP.keys()),
1049
+ value="UniPCMultistep",
1050
+ info="Select a custom scheduler."
1051
+ )
1052
+ flow_shift_slider = gr.Slider(minimum=0.5, maximum=15.0, step=0.1, value=3.0, label="Flow Shift")
1053
+ lora_dropdown = gr.Dropdown(choices=lora_loader.get_lora_choices(), label="LoRA (NSFW)", multiselect=True, info="Select scenario LoRAs")
1054
+ play_result_video = gr.Checkbox(label="Display result", value=True, interactive=True)
1055
+
1056
+ generate_button = gr.Button("Generate Video", variant="primary")
1057
+
1058
+ with gr.Column():
1059
+ # ASSIGNED elem_id="generated-video" so JS can find it
1060
+ video_output = gr.Video(label="Generated Video", autoplay=True, sources=["upload"], buttons=["download", "share"], interactive=True, elem_id="generated-video")
1061
+
1062
+ # --- Frame Grabbing UI ---
1063
+ with gr.Row():
1064
+ grab_frame_btn = gr.Button("📸 Use Current Frame as Input", variant="secondary")
1065
+ timestamp_box = gr.Number(value=0, label="Timestamp", visible=True, elem_id="hidden-timestamp")
1066
+ # -------------------------
1067
+
1068
+ file_output = gr.File(label="Download Video")
1069
+
1070
+ ui_inputs = [
1071
+ input_image_component, last_image_component, prompt_input, steps_slider,
1072
+ negative_prompt_input, duration_seconds_input,
1073
+ guidance_scale_input, guidance_scale_2_input, seed_input, randomize_seed_checkbox,
1074
+ quality_slider, scheduler_dropdown, flow_shift_slider, frame_multi,
1075
+ safe_mode_checkbox,
1076
+ lora_dropdown,
1077
+ auto_lora_checkbox,
1078
+ play_result_video
1079
+ ]
1080
+
1081
+ generate_button.click(
1082
+ fn=generate_video,
1083
+ inputs=ui_inputs,
1084
+ outputs=[video_output, file_output, seed_input]
1085
+ )
1086
+
1087
+ # --- Auto-LoRA Preview Events ---
1088
+ prompt_input.change(
1089
+ fn=_update_auto_lora_preview,
1090
+ inputs=[prompt_input, auto_lora_checkbox],
1091
+ outputs=[auto_lora_preview],
1092
+ )
1093
+ auto_lora_checkbox.change(
1094
+ fn=_update_auto_lora_preview,
1095
+ inputs=[prompt_input, auto_lora_checkbox],
1096
+ outputs=[auto_lora_preview],
1097
+ )
1098
+
1099
+ # --- Frame Grabbing Events ---
1100
+ # 1. Click button -> JS runs -> puts time in hidden number box
1101
+ grab_frame_btn.click(
1102
+ fn=None,
1103
+ inputs=None,
1104
+ outputs=[timestamp_box],
1105
+ js=get_timestamp_js
1106
+ )
1107
+
1108
+ # 2. Hidden number box changes -> Python runs -> puts frame in Input Image
1109
+ timestamp_box.change(
1110
+ fn=extract_frame,
1111
+ inputs=[video_output, timestamp_box],
1112
+ outputs=[input_image_component]
1113
+ )
1114
+
1115
+ if __name__ == "__main__":
1116
+ demo.queue().launch(
1117
+ mcp_server=True,
1118
+ css=CSS,
1119
+ show_error=True,
1120
  )