linoyts HF Staff commited on
Commit
c4c8fb3
·
verified ·
1 Parent(s): 2addde8

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +13 -15
app.py CHANGED
@@ -405,7 +405,7 @@ def _generate(request, steps, seed):
405
  return videos, None if audio is None else audio[0].cpu(), sr
406
 
407
 
408
- def replace_background(clip, prompt, relight, budget, steps, seed, keep_audio, harmonize=True, remove_only=False, progress=None):
409
  """Clip + a background description -> the subject on that new background. With remove_only, the subject on black
410
  (no new background, no H3 - just the matte)."""
411
  progress = progress or (lambda *a, **k: None)
@@ -450,7 +450,7 @@ def replace_background(clip, prompt, relight, budget, steps, seed, keep_audio, h
450
  green, matte = green[:n], matte[:n]
451
  clip_h, clip_w = green.shape[1], green.shape[2]
452
 
453
- mask = background_mask(matte, float(relight))[:n]
454
  ref_path = gray_reference(green[0], matte[0])
455
 
456
  short_edge, max_pixels = BUDGETS[budget]
@@ -478,8 +478,8 @@ def replace_background(clip, prompt, relight, budget, steps, seed, keep_audio, h
478
  progress(0.95, desc="Finishing ...")
479
  raw = np.stack([np.asarray(f) for f in generated])
480
  gen = np.stack([np.asarray(Image.fromarray(f).resize((clip_w, clip_h), Image.Resampling.LANCZOS)) for f in raw])
481
- if bool(harmonize) or float(relight) > 0:
482
- # harmonize (full-frame mask-drop refine) or relight produced the final subject in H3's own frame; use it
483
  frames = gen
484
  else:
485
  # Full-frame composite: restore the exact subject pixels, feathering ONLY the subject boundary. paste_back's
@@ -507,17 +507,17 @@ def replace_background(clip, prompt, relight, budget, steps, seed, keep_audio, h
507
  _HERE = os.path.dirname(os.path.abspath(__file__))
508
  EXAMPLES = [
509
  [os.path.join(_HERE, "examples/subject.mp4"),
510
- "a sunny tropical beach, white sand, palm trees and turquoise ocean waves behind the subject, bright warm daylight, static camera", 0.0],
511
  [os.path.join(_HERE, "examples/subject.mp4"),
512
- "a neon-lit cyberpunk city street at night, rain and glowing signs reflecting on wet asphalt behind the subject, cool moody light", 0.0],
513
  [os.path.join(_HERE, "examples/dance.mp4"),
514
- "a concert stage with dramatic blue and magenta spotlights and haze, dark background, cinematic", 0.0],
515
  ]
516
 
517
- def run_classic(clip, prompt, relight, budget, steps, seed, keep_audio, harmonize, remove_only, progress=gr.Progress()):
518
  """Wraps replace_background so gradio injects a progress tracker - the handler then reports its stages
519
  (removing the background, then generating the new one) on the UI progress bar."""
520
- return replace_background(clip, prompt, relight, budget, steps, seed, keep_audio, harmonize, remove_only,
521
  progress=progress)
522
 
523
 
@@ -529,8 +529,8 @@ with gr.Blocks(title="Video Replace Background") as demo:
529
  "the background around it with [MiniMax-H3](https://huggingface.co/multimodalart/MiniMax-H3-Pruned) "
530
  "([inpainting modular blocks](https://huggingface.co/diffusers-modular/minimax-h3-inpainting), "
531
  "[turbo LoRA](https://huggingface.co/lightx2v/Minimax-h3-Turbo)) - or just remove the background. "
532
- "**Harmonize** (on) runs a second refinement pass so the subject's lighting blends into the new scene "
533
- "(the main cure for the cutout look); **relight** is an alternative lever that adjusts only the subject."
534
  )
535
  with gr.Row():
536
  with gr.Column():
@@ -540,8 +540,6 @@ with gr.Blocks(title="Video Replace Background") as demo:
540
  harmonize = gr.Checkbox(value=True, label="Harmonize the subject into the scene",
541
  info="A second refinement pass blends the subject's lighting into the new "
542
  "background (recommended). Costs roughly 2x the time.")
543
- relight = gr.Slider(0.0, 0.4, value=0.0, step=0.05, label="Relight strength",
544
- info="Alternative to harmonize - adjusts only the subject. 0 holds it exactly.")
545
  with gr.Accordion("Settings", open=False):
546
  budget = gr.Dropdown(list(BUDGETS), value=DEFAULT_BUDGET, label="Quality / speed")
547
  steps = gr.Slider(4, 28, value=DEFAULT_STEPS, step=1, label="Steps (turbo)")
@@ -554,9 +552,9 @@ with gr.Blocks(title="Video Replace Background") as demo:
554
  matte_view = gr.Video(label="Alpha matte")
555
  mask_view = gr.Video(label="Background mask (white = repainted)")
556
 
557
- gr.Examples(examples=EXAMPLES, inputs=[clip, prompt, relight], label="Examples", cache_examples=False)
558
 
559
- run.click(run_classic, [clip, prompt, relight, budget, steps, seed, keep_audio, harmonize, remove_bg],
560
  [result, matte_view, mask_view], api_name="replace_background")
561
 
562
 
 
405
  return videos, None if audio is None else audio[0].cpu(), sr
406
 
407
 
408
+ def replace_background(clip, prompt, budget, steps, seed, keep_audio, harmonize=True, remove_only=False, progress=None):
409
  """Clip + a background description -> the subject on that new background. With remove_only, the subject on black
410
  (no new background, no H3 - just the matte)."""
411
  progress = progress or (lambda *a, **k: None)
 
450
  green, matte = green[:n], matte[:n]
451
  clip_h, clip_w = green.shape[1], green.shape[2]
452
 
453
+ mask = background_mask(matte, 0.0)[:n]
454
  ref_path = gray_reference(green[0], matte[0])
455
 
456
  short_edge, max_pixels = BUDGETS[budget]
 
478
  progress(0.95, desc="Finishing ...")
479
  raw = np.stack([np.asarray(f) for f in generated])
480
  gen = np.stack([np.asarray(Image.fromarray(f).resize((clip_w, clip_h), Image.Resampling.LANCZOS)) for f in raw])
481
+ if bool(harmonize):
482
+ # the harmonize (full-frame mask-drop) pass produced the final subject in H3's own frame; use it directly
483
  frames = gen
484
  else:
485
  # Full-frame composite: restore the exact subject pixels, feathering ONLY the subject boundary. paste_back's
 
507
  _HERE = os.path.dirname(os.path.abspath(__file__))
508
  EXAMPLES = [
509
  [os.path.join(_HERE, "examples/subject.mp4"),
510
+ "a sunny tropical beach, white sand, palm trees and turquoise ocean waves behind the subject, bright warm daylight, static camera"],
511
  [os.path.join(_HERE, "examples/subject.mp4"),
512
+ "a neon-lit cyberpunk city street at night, rain and glowing signs reflecting on wet asphalt behind the subject, cool moody light"],
513
  [os.path.join(_HERE, "examples/dance.mp4"),
514
+ "a concert stage with dramatic blue and magenta spotlights and haze, dark background, cinematic"],
515
  ]
516
 
517
+ def run_classic(clip, prompt, budget, steps, seed, keep_audio, harmonize, remove_only, progress=gr.Progress()):
518
  """Wraps replace_background so gradio injects a progress tracker - the handler then reports its stages
519
  (removing the background, then generating the new one) on the UI progress bar."""
520
+ return replace_background(clip, prompt, budget, steps, seed, keep_audio, harmonize, remove_only,
521
  progress=progress)
522
 
523
 
 
529
  "the background around it with [MiniMax-H3](https://huggingface.co/multimodalart/MiniMax-H3-Pruned) "
530
  "([inpainting modular blocks](https://huggingface.co/diffusers-modular/minimax-h3-inpainting), "
531
  "[turbo LoRA](https://huggingface.co/lightx2v/Minimax-h3-Turbo)) - or just remove the background. "
532
+ "**Harmonize** (on) runs a second refinement pass so the subject's lighting blends into the new scene, "
533
+ "the main cure for the cutout look."
534
  )
535
  with gr.Row():
536
  with gr.Column():
 
540
  harmonize = gr.Checkbox(value=True, label="Harmonize the subject into the scene",
541
  info="A second refinement pass blends the subject's lighting into the new "
542
  "background (recommended). Costs roughly 2x the time.")
 
 
543
  with gr.Accordion("Settings", open=False):
544
  budget = gr.Dropdown(list(BUDGETS), value=DEFAULT_BUDGET, label="Quality / speed")
545
  steps = gr.Slider(4, 28, value=DEFAULT_STEPS, step=1, label="Steps (turbo)")
 
552
  matte_view = gr.Video(label="Alpha matte")
553
  mask_view = gr.Video(label="Background mask (white = repainted)")
554
 
555
+ gr.Examples(examples=EXAMPLES, inputs=[clip, prompt], label="Examples", cache_examples=False)
556
 
557
+ run.click(run_classic, [clip, prompt, budget, steps, seed, keep_audio, harmonize, remove_bg],
558
  [result, matte_view, mask_view], api_name="replace_background")
559
 
560