Zrelv commited on
Commit
d144ea2
·
1 Parent(s): 8a3705e

Make identical to wan777: base Wan2.2-I2V-A14B + wan777 code/deps/gradio

Browse files

User wants all 5 like wan777 (base Wan2.2). Copy wan777's exact stack (app.py,
release requirements, gradio 5.44.1) and unset REPO_ID so it uses the hardcoded
base model. README title/emoji preserved.

Files changed (2) hide show
  1. README.md +2 -1
  2. app.py +35 -80
README.md CHANGED
@@ -4,7 +4,8 @@ emoji: 🐌
4
  colorFrom: yellow
5
  colorTo: pink
6
  sdk: gradio
7
- sdk_version: 6.0.1
 
8
  app_file: app.py
9
  pinned: false
10
  short_description: generate a video from an image with a text prompt
 
4
  colorFrom: yellow
5
  colorTo: pink
6
  sdk: gradio
7
+ sdk_version: 5.44.1
8
+ python_version: "3.10"
9
  app_file: app.py
10
  pinned: false
11
  short_description: generate a video from an image with a text prompt
app.py CHANGED
@@ -11,11 +11,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 huggingface_hub import list_models
19
  from torch.nn import functional as F
20
  from PIL import Image
21
 
@@ -127,10 +126,10 @@ 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
@@ -176,7 +175,7 @@ def interpolate_bits(frames_np, multiplier=2, scale=1.0):
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
@@ -187,11 +186,11 @@ def interpolate_bits(frames_np, multiplier=2, scale=1.0):
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
@@ -232,13 +231,9 @@ def interpolate_bits(frames_np, multiplier=2, scale=1.0):
232
 
233
  # WAN
234
 
235
- ORG_NAME = "TestOrganizationPleaseIgnore"
236
- # MODEL_ID = "Wan-AI/Wan2.2-I2V-A14B-Diffusers"
237
- MODEL_ID = os.getenv("REPO_ID") or "TestOrganizationPleaseIgnore/WAMU_v3_WAN2.2_I2V_LIGHTNING"
238
  CACHE_DIR = os.path.expanduser("~/.cache/huggingface/")
239
 
240
- LORA_MODELS = [] # WAMU is a pre-merged lightning model — no extra LoRA needed
241
-
242
  MAX_DIM = 832
243
  MIN_DIM = 480
244
  SQUARE_DIM = 640
@@ -247,7 +242,7 @@ MAX_SEED = np.iinfo(np.int32).max
247
 
248
  FIXED_FPS = 16
249
  MIN_FRAMES_MODEL = 8
250
- MAX_FRAMES_MODEL = 1280
251
 
252
  MIN_DURATION = round(MIN_FRAMES_MODEL / FIXED_FPS, 1)
253
  MAX_DURATION = round(MAX_FRAMES_MODEL / FIXED_FPS, 1)
@@ -263,43 +258,11 @@ SCHEDULER_MAP = {
263
  }
264
 
265
  pipe = WanImageToVideoPipeline.from_pretrained(
266
- MODEL_ID,
267
  torch_dtype=torch.bfloat16,
268
  ).to('cuda')
269
  original_scheduler = copy.deepcopy(pipe.scheduler)
270
 
271
- for i, lora in enumerate(LORA_MODELS):
272
- name_high_tr = lora["high_tr"].split(".")[0].split("/")[-1] + "Hh"
273
- name_low_tr = lora["low_tr"].split(".")[0].split("/")[-1] + "Ll"
274
-
275
- try:
276
- pipe.load_lora_weights(
277
- lora["repo_id"],
278
- weight_name=lora["high_tr"],
279
- adapter_name=name_high_tr
280
- )
281
-
282
- kwargs_lora = {"load_into_transformer_2": True}
283
- pipe.load_lora_weights(
284
- lora["repo_id"],
285
- weight_name=lora["low_tr"],
286
- adapter_name=name_low_tr,
287
- **kwargs_lora
288
- )
289
-
290
- pipe.set_adapters([name_high_tr, name_low_tr], adapter_weights=[1.0, 1.0])
291
-
292
- pipe.fuse_lora(adapter_names=[name_high_tr], lora_scale=lora["high_scale"], components=["transformer"])
293
- pipe.fuse_lora(adapter_names=[name_low_tr], lora_scale=lora["low_scale"], components=["transformer_2"])
294
-
295
- pipe.unload_lora_weights()
296
-
297
- print(f"Applied: {lora['high_tr']}, hs={lora['high_scale']}/ls={lora['low_scale']}, {i+1}/{len(LORA_MODELS)}")
298
- except Exception as e:
299
- print("Error:", str(e))
300
- print("Failed LoRA:", name_high_tr)
301
- pipe.unload_lora_weights()
302
-
303
  # if os.path.exists(CACHE_DIR):
304
  # shutil.rmtree(CACHE_DIR)
305
  # print("Deleted Hugging Face cache.")
@@ -329,13 +292,10 @@ default_prompt_i2v = "make this image come alive, cinematic motion, smooth anima
329
  default_negative_prompt = "色调艳丽, 过曝, 静态, 细节模糊不清, 字幕, 风格, 作品, 画作, 画面, 静止, 整体发灰, 最差质量, 低质量, JPEG压缩残留, 丑陋的, 残缺的, 多余的手指, 画得不好的手部, 画得不好的脸部, 畸形的, 毁容的, 形态畸形的肢体, 手指融合, 静止不动的画面, 杂乱的背景, 三条腿, 背景人很多, 倒着走"
330
 
331
 
332
- def model_title():
333
- repo_name = MODEL_ID.split('/')[-1].replace("_", " ")
334
- url = f"https://huggingface.co/{MODEL_ID}"
335
- return f"## This space is currently running [{repo_name}]({url}) 🐢"
336
-
337
-
338
  def resize_image(image: Image.Image) -> Image.Image:
 
 
 
339
  width, height = image.size
340
  if width == height:
341
  return image.resize((SQUARE_DIM, SQUARE_DIM), Image.LANCZOS)
@@ -371,6 +331,7 @@ def resize_image(image: Image.Image) -> Image.Image:
371
 
372
 
373
  def resize_and_crop_to_match(target_image, reference_image):
 
374
  ref_width, ref_height = reference_image.size
375
  target_width, target_height = target_image.size
376
  scale = max(ref_width / target_width, ref_height / target_height)
@@ -424,14 +385,17 @@ def get_inference_duration(
424
 
425
  total_time = 15 + gen_time
426
  if safe_mode:
427
- total_time = total_time * 1.30
428
-
429
- # Cap reserved GPU duration so the reported reservation (×2 for xlarge) stays
430
- # under free-tier ZeroGPU's ~300s per-call limit — the padded worst-case was
431
- # tripping "requested GPU duration larger than the maximum allowed". 140 →
432
- # ~280s reported, still covers real p90 (~107s) and the 150s caller timeout.
 
 
433
  total_time = min(total_time, 145)
434
  return total_time
 
435
 
436
 
437
  @spaces.GPU(duration=get_inference_duration, size='xlarge')
@@ -450,7 +414,7 @@ def run_inference(
450
  frame_multiplier,
451
  quality,
452
  duration_seconds,
453
- safe_mode=False,
454
  progress=gr.Progress(track_tqdm=True),
455
  ):
456
  scheduler_class = SCHEDULER_MAP.get(scheduler_name)
@@ -465,7 +429,7 @@ def run_inference(
465
  clear_vram()
466
 
467
  task_name = str(uuid.uuid4())[:8]
468
- print(f"Generating {num_frames} frames, task: {task_name}, {duration_seconds}, {resized_image.size}")
469
  start = time.time()
470
  result = pipe(
471
  image=resized_image,
@@ -481,7 +445,6 @@ def run_inference(
481
  generator=torch.Generator(device="cuda").manual_seed(current_seed),
482
  output_type="np"
483
  )
484
- print("gen time passed:", time.time() - start)
485
 
486
  raw_frames_np = result.frames[0] # Returns (T, H, W, C) float32
487
  pipe.scheduler = original_scheduler
@@ -489,11 +452,9 @@ def run_inference(
489
  frame_factor = frame_multiplier // FIXED_FPS
490
  if frame_factor > 1:
491
  start = time.time()
492
- print(f"Processing frames (RIFE Multiplier: {frame_factor}x)...")
493
  rife_model.device()
494
  rife_model.flownet = rife_model.flownet.half()
495
  final_frames = interpolate_bits(raw_frames_np, multiplier=int(frame_factor))
496
- print("Interpolation time passed:", time.time() - start)
497
  else:
498
  final_frames = list(raw_frames_np)
499
 
@@ -507,7 +468,6 @@ def run_inference(
507
  pbar.update(2)
508
  export_to_video(final_frames, video_path, fps=final_fps, quality=quality)
509
  pbar.update(1)
510
- print(f"Export time passed, {final_fps} FPS:", time.time() - start)
511
 
512
  return video_path, task_name
513
 
@@ -527,8 +487,8 @@ def generate_video(
527
  scheduler="UniPCMultistep",
528
  flow_shift=6.0,
529
  frame_multiplier=16,
530
- safe_mode=False,
531
  video_component=True,
 
532
  progress=gr.Progress(track_tqdm=True),
533
  ):
534
  """
@@ -605,6 +565,7 @@ def generate_video(
605
  safe_mode,
606
  progress,
607
  )
 
608
  print(f"GPU complete: {task_n}")
609
 
610
  return (video_path if video_component else None), video_path, current_seed
@@ -624,9 +585,9 @@ CSS = """
624
  """
625
 
626
 
627
- with gr.Blocks(delete_cache=(3600, 10800)) as demo:
628
- gr.Markdown(model_title())
629
- gr.Markdown("Run Wan 2.2 in just 4-8 steps, fp8 quantization & AoT compilation - compatible with 🧨 diffusers and ZeroGPU")
630
 
631
  with gr.Row():
632
  with gr.Column():
@@ -642,7 +603,7 @@ with gr.Blocks(delete_cache=(3600, 10800)) as demo:
642
  safe_mode_checkbox = gr.Checkbox(
643
  label="🛠️ Safe Mode",
644
  value=True,
645
- info="Requests 30% extra processing time to try to prevent unfinished tasks when the server is busy."
646
  )
647
  with gr.Accordion("Advanced Settings", open=False):
648
  last_image_component = gr.Image(type="pil", label="Last Image (Optional)", sources=["upload", "clipboard"])
@@ -661,17 +622,12 @@ with gr.Blocks(delete_cache=(3600, 10800)) as demo:
661
  )
662
  flow_shift_slider = gr.Slider(minimum=0.5, maximum=15.0, step=0.1, value=3.0, label="Flow Shift")
663
  play_result_video = gr.Checkbox(label="Display result", value=True, interactive=True)
664
- gr.Markdown(f"[ZeroGPU help, tips and troubleshooting](https://huggingface.co/datasets/{ORG_NAME}/help/blob/main/gpu_help.md)")
665
- gr.Markdown( # TestOrganizationPleaseIgnore/wamu-tools
666
- "To use a different model, **duplicate this Space** first, then change the `REPO_ID` environment variable. "
667
- "[See compatible models here](https://huggingface.co/models?other=diffusers:WanImageToVideoPipeline&sort=trending&search=WAN2.2_I2V_LIGHTNING)."
668
- )
669
 
670
  generate_button = gr.Button("Generate Video", variant="primary")
671
 
672
  with gr.Column():
673
  # ASSIGNED elem_id="generated-video" so JS can find it
674
- video_output = gr.Video(label="Generated Video", autoplay=True, sources=["upload"], buttons=["download", "share"], interactive=True, elem_id="generated-video")
675
 
676
  # --- Frame Grabbing UI ---
677
  with gr.Row():
@@ -681,13 +637,12 @@ with gr.Blocks(delete_cache=(3600, 10800)) as demo:
681
 
682
  file_output = gr.File(label="Download Video")
683
 
684
- ui_inputs = [
685
  input_image_component, last_image_component, prompt_input, steps_slider,
686
  negative_prompt_input, duration_seconds_input,
687
  guidance_scale_input, guidance_scale_2_input, seed_input, randomize_seed_checkbox,
688
  quality_slider, scheduler_dropdown, flow_shift_slider, frame_multi,
689
- safe_mode_checkbox,
690
- play_result_video
691
  ]
692
 
693
  generate_button.click(
@@ -715,6 +670,6 @@ with gr.Blocks(delete_cache=(3600, 10800)) as demo:
715
  if __name__ == "__main__":
716
  demo.queue().launch(
717
  mcp_server=True,
718
- css=CSS,
719
  show_error=True,
720
  )
 
11
  import gc
12
  import uuid
13
  from tqdm import tqdm
14
+
15
  import cv2
16
  import numpy as np
17
  import torch
 
 
18
  from torch.nn import functional as F
19
  from PIL import Image
20
 
 
126
  """
127
  Interpolation maintaining Numpy Float 0-1 format.
128
  Args:
129
+ frames_np: Numpy Array (Time, Height, Width, Channels) - Float32[0.0, 1.0]
130
  multiplier: int (2, 4, 8)
131
  Returns:
132
+ List of Numpy Arrays (Height, Width, Channels) - Float32[0.0, 1.0]
133
  """
134
 
135
  # Handle input shape
 
175
 
176
  def make_inference(I0, I1, n):
177
  if rife_model.version >= 3.9:
178
+ res =[]
179
  for i in range(n):
180
  res.append(rife_model.inference(I0, I1, (i+1) * 1. / (n+1), scale))
181
  return res
 
186
  first_half = make_inference(I0, middle, n=n//2)
187
  second_half = make_inference(middle, I1, n=n//2)
188
  if n % 2:
189
+ return[*first_half, middle, *second_half]
190
  else:
191
+ return[*first_half, *second_half]
192
 
193
+ output_frames =[]
194
 
195
  # Process Frames
196
  # Load first frame into GPU
 
231
 
232
  # WAN
233
 
234
+ MODEL_ID = "Wan-AI/Wan2.2-I2V-A14B-Diffusers"
 
 
235
  CACHE_DIR = os.path.expanduser("~/.cache/huggingface/")
236
 
 
 
237
  MAX_DIM = 832
238
  MIN_DIM = 480
239
  SQUARE_DIM = 640
 
242
 
243
  FIXED_FPS = 16
244
  MIN_FRAMES_MODEL = 8
245
+ MAX_FRAMES_MODEL = 160
246
 
247
  MIN_DURATION = round(MIN_FRAMES_MODEL / FIXED_FPS, 1)
248
  MAX_DURATION = round(MAX_FRAMES_MODEL / FIXED_FPS, 1)
 
258
  }
259
 
260
  pipe = WanImageToVideoPipeline.from_pretrained(
261
+ "TestOrganizationPleaseIgnore/WAMU_v3_WAN2.2_I2V_LIGHTNING",
262
  torch_dtype=torch.bfloat16,
263
  ).to('cuda')
264
  original_scheduler = copy.deepcopy(pipe.scheduler)
265
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
266
  # if os.path.exists(CACHE_DIR):
267
  # shutil.rmtree(CACHE_DIR)
268
  # print("Deleted Hugging Face cache.")
 
292
  default_negative_prompt = "色调艳丽, 过曝, 静态, 细节模糊不清, 字幕, 风格, 作品, 画作, 画面, 静止, 整体发灰, 最差质量, 低质量, JPEG压缩残留, 丑陋的, 残缺的, 多余的手指, 画得不好的手部, 画得不好的脸部, 畸形的, 毁容的, 形态畸形的肢体, 手指融合, 静止不动的画面, 杂乱的背景, 三条腿, 背景人很多, 倒着走"
293
 
294
 
 
 
 
 
 
 
295
  def resize_image(image: Image.Image) -> Image.Image:
296
+ """
297
+ Resizes an image to fit within the model's constraints, preserving aspect ratio as much as possible.
298
+ """
299
  width, height = image.size
300
  if width == height:
301
  return image.resize((SQUARE_DIM, SQUARE_DIM), Image.LANCZOS)
 
331
 
332
 
333
  def resize_and_crop_to_match(target_image, reference_image):
334
+ """Resizes and center-crops the target image to match the reference image's dimensions."""
335
  ref_width, ref_height = reference_image.size
336
  target_width, target_height = target_image.size
337
  scale = max(ref_width / target_width, ref_height / target_height)
 
385
 
386
  total_time = 15 + gen_time
387
  if safe_mode:
388
+ total_time = total_time * 1.3
389
+
390
+ # Cap the reserved GPU duration so the reported reservation (×2 for xlarge)
391
+ # stays under free-tier ZeroGPU's ~300s per-call limit. The padded worst-case
392
+ # estimate ran 3-10x the real p90 (~107s) and tripped "The requested GPU
393
+ # duration (…) is larger than the maximum allowed". 145 → ~290s reported,
394
+ # still covers p90 and matches the caller's 150s hard timeout. Revert to 140
395
+ # if the cap turns out stricter than 300s.
396
  total_time = min(total_time, 145)
397
  return total_time
398
+
399
 
400
 
401
  @spaces.GPU(duration=get_inference_duration, size='xlarge')
 
414
  frame_multiplier,
415
  quality,
416
  duration_seconds,
417
+ safe_mode,
418
  progress=gr.Progress(track_tqdm=True),
419
  ):
420
  scheduler_class = SCHEDULER_MAP.get(scheduler_name)
 
429
  clear_vram()
430
 
431
  task_name = str(uuid.uuid4())[:8]
432
+ print(f"Task: {task_name}, {duration_seconds}, {resized_image.size}, FM={frame_multiplier}")
433
  start = time.time()
434
  result = pipe(
435
  image=resized_image,
 
445
  generator=torch.Generator(device="cuda").manual_seed(current_seed),
446
  output_type="np"
447
  )
 
448
 
449
  raw_frames_np = result.frames[0] # Returns (T, H, W, C) float32
450
  pipe.scheduler = original_scheduler
 
452
  frame_factor = frame_multiplier // FIXED_FPS
453
  if frame_factor > 1:
454
  start = time.time()
 
455
  rife_model.device()
456
  rife_model.flownet = rife_model.flownet.half()
457
  final_frames = interpolate_bits(raw_frames_np, multiplier=int(frame_factor))
 
458
  else:
459
  final_frames = list(raw_frames_np)
460
 
 
468
  pbar.update(2)
469
  export_to_video(final_frames, video_path, fps=final_fps, quality=quality)
470
  pbar.update(1)
 
471
 
472
  return video_path, task_name
473
 
 
487
  scheduler="UniPCMultistep",
488
  flow_shift=6.0,
489
  frame_multiplier=16,
 
490
  video_component=True,
491
+ safe_mode=False,
492
  progress=gr.Progress(track_tqdm=True),
493
  ):
494
  """
 
565
  safe_mode,
566
  progress,
567
  )
568
+
569
  print(f"GPU complete: {task_n}")
570
 
571
  return (video_path if video_component else None), video_path, current_seed
 
585
  """
586
 
587
 
588
+ with gr.Blocks(theme=gr.themes.Soft(), css=CSS, delete_cache=(3600, 3700)) as demo:
589
+ gr.Markdown("## Wan 2.2 I2V (14B) — Fast ⚡")
590
+ gr.Markdown("Run Wan 2.2 in just 4-8 steps, fp8 quantization & AoT compilation - compatible with 🧨 diffusers and ZeroGPU.")
591
 
592
  with gr.Row():
593
  with gr.Column():
 
603
  safe_mode_checkbox = gr.Checkbox(
604
  label="🛠️ Safe Mode",
605
  value=True,
606
+ info="Safe Mode: Requests 30% extra processing time to try to prevent unfinished tasks when the server is busy."
607
  )
608
  with gr.Accordion("Advanced Settings", open=False):
609
  last_image_component = gr.Image(type="pil", label="Last Image (Optional)", sources=["upload", "clipboard"])
 
622
  )
623
  flow_shift_slider = gr.Slider(minimum=0.5, maximum=15.0, step=0.1, value=3.0, label="Flow Shift")
624
  play_result_video = gr.Checkbox(label="Display result", value=True, interactive=True)
 
 
 
 
 
625
 
626
  generate_button = gr.Button("Generate Video", variant="primary")
627
 
628
  with gr.Column():
629
  # ASSIGNED elem_id="generated-video" so JS can find it
630
+ video_output = gr.Video(label="Generated Video", autoplay=True, sources=["upload"], show_download_button=True, show_share_button=True, interactive=False, elem_id="generated-video")
631
 
632
  # --- Frame Grabbing UI ---
633
  with gr.Row():
 
637
 
638
  file_output = gr.File(label="Download Video")
639
 
640
+ ui_inputs =[
641
  input_image_component, last_image_component, prompt_input, steps_slider,
642
  negative_prompt_input, duration_seconds_input,
643
  guidance_scale_input, guidance_scale_2_input, seed_input, randomize_seed_checkbox,
644
  quality_slider, scheduler_dropdown, flow_shift_slider, frame_multi,
645
+ play_result_video, safe_mode_checkbox
 
646
  ]
647
 
648
  generate_button.click(
 
670
  if __name__ == "__main__":
671
  demo.queue().launch(
672
  mcp_server=True,
673
+ ssr_mode=False,
674
  show_error=True,
675
  )