observantdistressed commited on
Commit
7970a56
·
verified ·
1 Parent(s): 42cbeea

Revert: restore original app.py (undo CSS gradient changes)

Browse files
Files changed (1) hide show
  1. app.py +799 -1083
app.py CHANGED
@@ -1,1084 +1,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
- #060a1c 0%,
689
- #0e1d5c 12%,
690
- #14408f 25%,
691
- #0a7a4e 40%,
692
- #12b367 55%,
693
- #1f4fa8 65%,
694
- #5b2bd0 80%,
695
- #33107a 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 atmosphere overlay */
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, 220, 190, 0.12) 0%, transparent 60%),
709
- radial-gradient(ellipse at 80% 30%, rgba(130, 80, 250, 0.16) 0%, transparent 55%),
710
- radial-gradient(ellipse at 50% 80%, rgba(40, 140, 230, 0.1) 0%, transparent 50%) !important;
711
- pointer-events: none !important;
712
- z-index: 0 !important;
713
- }
714
-
715
- /* NOTE: Do NOT force z-index on .gradio-container > * — it clips dropdown popups */
716
-
717
- /* ── HEADER / TITLE — gradient text on markdown headings ── */
718
- .gradio-container .prose h1,
719
- .gradio-container .prose h2 {
720
- background: linear-gradient(90deg, #00e5c8, #6c5ce7, #a855f7) !important;
721
- -webkit-background-clip: text !important;
722
- -webkit-text-fill-color: transparent !important;
723
- background-clip: text !important;
724
- font-weight: 800 !important;
725
- letter-spacing: -0.02em !important;
726
- }
727
-
728
- /* Subtitle text */
729
- .gradio-container .prose p {
730
- color: #b0c4de !important;
731
- }
732
-
733
- /* ── CARDS / BLOCKS — frosted glass (Gradio 6: .block) ── */
734
- .gradio-container .block {
735
- background: rgba(255, 255, 255, 0.05) !important;
736
- backdrop-filter: blur(20px) saturate(1.5) !important;
737
- -webkit-backdrop-filter: blur(20px) saturate(1.5) !important;
738
- border: 1px solid rgba(255, 255, 255, 0.1) !important;
739
- border-radius: 14px !important;
740
- box-shadow: 0 6px 24px rgba(0, 0, 0, 0.3) !important;
741
- }
742
-
743
- /* ── UPLOAD DROP ZONES — dashed borders inside image blocks ── */
744
- .gradio-container [data-testid="image"],
745
- .gradio-container .upload-container {
746
- background: rgba(255, 255, 255, 0.04) !important;
747
- border: 2px dashed rgba(100, 200, 255, 0.3) !important;
748
- border-radius: 12px !important;
749
- transition: border-color 0.3s ease, box-shadow 0.3s ease !important;
750
- }
751
- .gradio-container [data-testid="image"]:hover,
752
- .gradio-container .upload-container:hover {
753
- border-color: rgba(0, 229, 200, 0.6) !important;
754
- box-shadow: 0 0 24px rgba(0, 229, 200, 0.12) !important;
755
- }
756
-
757
- /* ── LAYOUT SPACING ── */
758
- .gradio-container .row {
759
- gap: 12px !important;
760
- }
761
- .gradio-container .column {
762
- gap: 8px !important;
763
- }
764
-
765
- /* ── LABELS ── */
766
- .gradio-container label {
767
- color: #d6e4f0 !important;
768
- font-weight: 600 !important;
769
- }
770
-
771
- /* ── TEXTBOX — use data-testid (stable in Gradio 6) ── */
772
- .gradio-container [data-testid="textbox"] {
773
- background: rgba(12, 18, 50, 0.8) !important;
774
- border: 1px solid rgba(100, 180, 255, 0.3) !important;
775
- border-radius: 10px !important;
776
- color: #e8ecf4 !important;
777
- box-shadow: inset 0 2px 8px rgba(0, 0, 0, 0.3) !important;
778
- transition: border-color 0.3s ease, box-shadow 0.3s ease !important;
779
- }
780
- .gradio-container [data-testid="textbox"]:focus {
781
- border-color: rgba(108, 92, 231, 0.7) !important;
782
- box-shadow: inset 0 2px 8px rgba(0, 0, 0, 0.3), 0 0 16px rgba(108, 92, 231, 0.25) !important;
783
- outline: none !important;
784
- }
785
-
786
- /* ── SLIDER — use data-testid ── */
787
- .gradio-container [data-testid="range-input"] {
788
- accent-color: #00d4aa !important;
789
- cursor: pointer !important;
790
- }
791
- .gradio-container [data-testid="range-input"]::-webkit-slider-runnable-track {
792
- background: linear-gradient(90deg, #00d4aa, #6c5ce7) !important;
793
- height: 6px !important;
794
- border-radius: 3px !important;
795
- }
796
- .gradio-container [data-testid="range-input"]::-webkit-slider-thumb {
797
- -webkit-appearance: none !important;
798
- appearance: none !important;
799
- width: 18px !important;
800
- height: 18px !important;
801
- border-radius: 50% !important;
802
- background: linear-gradient(135deg, #00e5c8, #6c5ce7) !important;
803
- box-shadow: 0 0 10px rgba(0, 229, 200, 0.5), 0 2px 6px rgba(0, 0, 0, 0.3) !important;
804
- cursor: pointer !important;
805
- border: 2px solid rgba(255, 255, 255, 0.3) !important;
806
- margin-top: -6px !important;
807
- transition: transform 0.2s ease, box-shadow 0.2s ease !important;
808
- }
809
- .gradio-container [data-testid="range-input"]::-webkit-slider-thumb:hover {
810
- transform: scale(1.15) !important;
811
- box-shadow: 0 0 18px rgba(0, 229, 200, 0.7), 0 2px 8px rgba(0, 0, 0, 0.4) !important;
812
- }
813
- .gradio-container [data-testid="range-input"]::-moz-range-track {
814
- background: linear-gradient(90deg, #00d4aa, #6c5ce7) !important;
815
- height: 6px !important;
816
- border-radius: 3px !important;
817
- }
818
- .gradio-container [data-testid="range-input"]::-moz-range-thumb {
819
- width: 18px !important;
820
- height: 18px !important;
821
- border-radius: 50% !important;
822
- background: linear-gradient(135deg, #00e5c8, #6c5ce7) !important;
823
- border: 2px solid rgba(255, 255, 255, 0.3) !important;
824
- box-shadow: 0 0 10px rgba(0, 229, 200, 0.5), 0 2px 6px rgba(0, 0, 0, 0.3) !important;
825
- cursor: pointer !important;
826
- }
827
-
828
- /* ── NUMBER INPUT — use data-testid or direct element ── */
829
- .gradio-container input[type="number"] {
830
- background: rgba(12, 18, 50, 0.8) !important;
831
- border: 1px solid rgba(100, 180, 255, 0.3) !important;
832
- border-radius: 8px !important;
833
- color: #e8ecf4 !important;
834
- box-shadow: inset 0 2px 8px rgba(0, 0, 0, 0.3) !important;
835
- padding: 8px 10px !important;
836
- transition: border-color 0.3s ease, box-shadow 0.3s ease !important;
837
- }
838
- .gradio-container input[type="number"]:focus {
839
- border-color: rgba(108, 92, 231, 0.7) !important;
840
- box-shadow: inset 0 2px 8px rgba(0, 0, 0, 0.3), 0 0 16px rgba(108, 92, 231, 0.25) !important;
841
- outline: none !important;
842
- }
843
-
844
- /* ── CHECKBOX — use data-testid (stable) ── */
845
- .gradio-container [data-testid="checkbox"] {
846
- accent-color: #00d4aa !important;
847
- width: 18px !important;
848
- height: 18px !important;
849
- cursor: pointer !important;
850
- }
851
- .gradio-container [data-testid="checkbox"]:checked {
852
- accent-color: #00e5c8 !important;
853
- }
854
-
855
- /* ── ACCORDION — .label-wrap header, data-testid for content ── */
856
- .gradio-container .label-wrap {
857
- background: rgba(255, 255, 255, 0.03) !important;
858
- border: 1px solid rgba(100, 180, 255, 0.1) !important;
859
- border-radius: 12px !important;
860
- color: #c8d8e8 !important;
861
- font-weight: 600 !important;
862
- padding: 10px 14px !important;
863
- cursor: pointer !important;
864
- transition: color 0.2s ease !important;
865
- }
866
- .gradio-container .label-wrap:hover {
867
- color: #00e5c8 !important;
868
- }
869
- .gradio-container .label-wrap.open {
870
- color: #00e5c8 !important;
871
- border-color: rgba(0, 229, 200, 0.35) !important;
872
- border-radius: 12px 12px 0 0 !important;
873
- }
874
- .gradio-container [data-testid="accordion-content"] {
875
- background: rgba(255, 255, 255, 0.02) !important;
876
- border: 1px solid rgba(100, 180, 255, 0.1) !important;
877
- border-top: none !important;
878
- border-radius: 0 0 12px 12px !important;
879
- padding: 8px 12px !important;
880
- }
881
-
882
- /* ── GENERATE BUTTON — vibrant gradient with glow ── */
883
- .gradio-container button.primary,
884
- .gradio-container button[data-variant="primary"] {
885
- background: linear-gradient(135deg, #00b894, #0984e3, #6c5ce7) !important;
886
- background-size: 200% 200% !important;
887
- animation: btn-shimmer 4s ease-in-out infinite !important;
888
- color: #ffffff !important;
889
- font-weight: 700 !important;
890
- font-size: 1.05em !important;
891
- letter-spacing: 0.03em !important;
892
- border: none !important;
893
- border-radius: 12px !important;
894
- padding: 14px 32px !important;
895
- box-shadow: 0 4px 20px rgba(0, 184, 148, 0.35), 0 0 40px rgba(108, 92, 231, 0.15) !important;
896
- transition: transform 0.25s ease, box-shadow 0.3s ease !important;
897
- text-shadow: 0 1px 3px rgba(0, 0, 0, 0.3) !important;
898
- cursor: pointer !important;
899
- }
900
- .gradio-container button.primary:hover,
901
- .gradio-container button[data-variant="primary"]:hover {
902
- transform: translateY(-2px) !important;
903
- box-shadow: 0 8px 32px rgba(0, 184, 148, 0.5), 0 0 60px rgba(108, 92, 231, 0.25) !important;
904
- }
905
- .gradio-container button.primary:active {
906
- transform: translateY(0px) !important;
907
- }
908
-
909
- @keyframes btn-shimmer {
910
- 0%, 100% { background-position: 0% 50%; }
911
- 50% { background-position: 100% 50%; }
912
- }
913
-
914
- /* ── SECONDARY BUTTONS ── */
915
- .gradio-container button.secondary,
916
- .gradio-container button[data-variant="secondary"] {
917
- background: rgba(255, 255, 255, 0.06) !important;
918
- backdrop-filter: blur(8px) !important;
919
- border: 1px solid rgba(100, 180, 255, 0.2) !important;
920
- color: #b8c8e0 !important;
921
- border-radius: 10px !important;
922
- font-weight: 600 !important;
923
- transition: all 0.25s ease !important;
924
- }
925
- .gradio-container button.secondary:hover,
926
- .gradio-container button[data-variant="secondary"]:hover {
927
- background: rgba(108, 92, 231, 0.15) !important;
928
- border-color: rgba(108, 92, 231, 0.4) !important;
929
- color: #e0e8f8 !important;
930
- }
931
-
932
- /* ── FILE / VIDEO OUTPUT ── */
933
- .gradio-container [data-testid="file"],
934
- .gradio-container [data-testid="video"] {
935
- background: rgba(10, 15, 40, 0.5) !important;
936
- border: 1px solid rgba(100, 180, 255, 0.1) !important;
937
- border-radius: 10px !important;
938
- }
939
-
940
- /* ── INFO TEXT ── */
941
- .gradio-container .prose small,
942
- .gradio-container small {
943
- color: #7a8fa8 !important;
944
- }
945
-
946
- /* ── SCROLLBAR — themed ── */
947
- .gradio-container ::-webkit-scrollbar {
948
- width: 8px !important;
949
- height: 8px !important;
950
- }
951
- .gradio-container ::-webkit-scrollbar-track {
952
- background: rgba(10, 15, 40, 0.4) !important;
953
- border-radius: 4px !important;
954
- }
955
- .gradio-container ::-webkit-scrollbar-thumb {
956
- background: linear-gradient(180deg, #00b894, #6c5ce7) !important;
957
- border-radius: 4px !important;
958
- }
959
-
960
- /* ── AUTO-DETECTED LORA PREVIEW ── */
961
- #auto-lora-preview {
962
- color: #b8d4e8 !important;
963
- }
964
- #auto-lora-preview b {
965
- color: #00e5c8 !important;
966
- }
967
- """
968
-
969
-
970
- with gr.Blocks(delete_cache=(3600, 10800)) as demo:
971
- gr.Markdown(model_title())
972
- gr.Markdown("Run Wan 2.2 in just 4-8 steps, fp8 quantization & AoT compilation - compatible with 🧨 diffusers and ZeroGPU")
973
-
974
- with gr.Row():
975
- with gr.Column():
976
- input_image_component = gr.Image(type="pil", label="Input Image", sources=["upload", "clipboard"])
977
- prompt_input = gr.Textbox(label="Prompt", value=default_prompt_i2v)
978
- auto_lora_checkbox = gr.Checkbox(
979
- label="Auto-activate LoRAs from prompt",
980
- value=True,
981
- interactive=True,
982
- info="Automatically detect matching LoRAs from your prompt text (deduplicated by concept).",
983
- )
984
- auto_lora_preview = gr.HTML(
985
- label="Auto-detected LoRAs",
986
- value="",
987
- elem_id="auto-lora-preview",
988
- )
989
- 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.")
990
- frame_multi = gr.Dropdown(
991
- choices=[FIXED_FPS, FIXED_FPS*2, FIXED_FPS*4, FIXED_FPS*8],
992
- value=FIXED_FPS,
993
- label="Video Fluidity (Frames per Second)",
994
- info="Extra frames will be generated using flow estimation, which estimates motion between frames to make the video smoother."
995
- )
996
- safe_mode_checkbox = gr.Checkbox(
997
- label="🛠️ Safe Mode",
998
- value=True,
999
- info="Requests 30% extra processing time to try to prevent unfinished tasks when the server is busy."
1000
- )
1001
- with gr.Accordion("Advanced Settings", open=False):
1002
- last_image_component = gr.Image(type="pil", label="Last Image (Optional)", sources=["upload", "clipboard"])
1003
- negative_prompt_input = gr.Textbox(label="Negative Prompt", value=default_negative_prompt, info="Used if any Guidance Scale > 1.", lines=3)
1004
- 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.")
1005
- seed_input = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=42, interactive=True)
1006
- randomize_seed_checkbox = gr.Checkbox(label="Randomize seed", value=True, interactive=True)
1007
- steps_slider = gr.Slider(minimum=1, maximum=30, step=1, value=6, label="Inference Steps")
1008
- 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.")
1009
- guidance_scale_2_input = gr.Slider(minimum=0.0, maximum=10.0, step=0.5, value=1, label="Guidance Scale 2 - low noise stage")
1010
- scheduler_dropdown = gr.Dropdown(
1011
- label="Scheduler",
1012
- choices=list(SCHEDULER_MAP.keys()),
1013
- value="UniPCMultistep",
1014
- info="Select a custom scheduler."
1015
- )
1016
- flow_shift_slider = gr.Slider(minimum=0.5, maximum=15.0, step=0.1, value=3.0, label="Flow Shift")
1017
- lora_dropdown = gr.Dropdown(choices=lora_loader.get_lora_choices(), label="LoRA (NSFW)", multiselect=True, info="Select scenario LoRAs")
1018
- play_result_video = gr.Checkbox(label="Display result", value=True, interactive=True)
1019
-
1020
- generate_button = gr.Button("Generate Video", variant="primary")
1021
-
1022
- with gr.Column():
1023
- # ASSIGNED elem_id="generated-video" so JS can find it
1024
- video_output = gr.Video(label="Generated Video", autoplay=True, sources=["upload"], buttons=["download", "share"], interactive=True, elem_id="generated-video")
1025
-
1026
- # --- Frame Grabbing UI ---
1027
- with gr.Row():
1028
- grab_frame_btn = gr.Button("📸 Use Current Frame as Input", variant="secondary")
1029
- timestamp_box = gr.Number(value=0, label="Timestamp", visible=True, elem_id="hidden-timestamp")
1030
- # -------------------------
1031
-
1032
- file_output = gr.File(label="Download Video")
1033
-
1034
- ui_inputs = [
1035
- input_image_component, last_image_component, prompt_input, steps_slider,
1036
- negative_prompt_input, duration_seconds_input,
1037
- guidance_scale_input, guidance_scale_2_input, seed_input, randomize_seed_checkbox,
1038
- quality_slider, scheduler_dropdown, flow_shift_slider, frame_multi,
1039
- safe_mode_checkbox,
1040
- lora_dropdown,
1041
- auto_lora_checkbox,
1042
- play_result_video
1043
- ]
1044
-
1045
- generate_button.click(
1046
- fn=generate_video,
1047
- inputs=ui_inputs,
1048
- outputs=[video_output, file_output, seed_input]
1049
- )
1050
-
1051
- # --- Auto-LoRA Preview Events ---
1052
- prompt_input.change(
1053
- fn=_update_auto_lora_preview,
1054
- inputs=[prompt_input, auto_lora_checkbox],
1055
- outputs=[auto_lora_preview],
1056
- )
1057
- auto_lora_checkbox.change(
1058
- fn=_update_auto_lora_preview,
1059
- inputs=[prompt_input, auto_lora_checkbox],
1060
- outputs=[auto_lora_preview],
1061
- )
1062
-
1063
- # --- Frame Grabbing Events ---
1064
- # 1. Click button -> JS runs -> puts time in hidden number box
1065
- grab_frame_btn.click(
1066
- fn=None,
1067
- inputs=None,
1068
- outputs=[timestamp_box],
1069
- js=get_timestamp_js
1070
- )
1071
-
1072
- # 2. Hidden number box changes -> Python runs -> puts frame in Input Image
1073
- timestamp_box.change(
1074
- fn=extract_frame,
1075
- inputs=[video_output, timestamp_box],
1076
- outputs=[input_image_component]
1077
- )
1078
-
1079
- if __name__ == "__main__":
1080
- demo.queue().launch(
1081
- mcp_server=True,
1082
- css=CSS,
1083
- show_error=True,
1084
  )
 
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
  )