Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,11 +9,15 @@ from huggingface_hub import hf_hub_download
|
|
| 9 |
import numpy as np
|
| 10 |
from PIL import Image
|
| 11 |
import random
|
|
|
|
| 12 |
|
| 13 |
model_id = "Wan-AI/Wan2.1-VACE-14B-diffusers"
|
| 14 |
vae = AutoencoderKLWan.from_pretrained(model_id, subfolder="vae", torch_dtype=torch.float32)
|
| 15 |
pipe = WanVACEPipeline.from_pretrained(model_id, vae=vae, torch_dtype=torch.bfloat16).to("cuda")
|
| 16 |
|
|
|
|
|
|
|
|
|
|
| 17 |
pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config, flow_shift=2.0)
|
| 18 |
|
| 19 |
pipe.load_lora_weights(
|
|
@@ -89,6 +93,91 @@ def remove_alpha_channel(image: Image.Image) -> Image.Image:
|
|
| 89 |
return image
|
| 90 |
|
| 91 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
def _calculate_new_dimensions_wan(pil_image, mod_val, calculation_max_area,
|
| 93 |
min_slider_h, max_slider_h,
|
| 94 |
min_slider_w, max_slider_w,
|
|
@@ -228,20 +317,26 @@ def prepare_video_and_mask_Random2V(images: List[Image.Image], frame_indices: Li
|
|
| 228 |
def get_duration(gallery_images, mode, prompt, height, width,
|
| 229 |
negative_prompt, duration_seconds,
|
| 230 |
guidance_scale, steps,
|
| 231 |
-
seed, randomize_seed,
|
| 232 |
progress):
|
|
|
|
|
|
|
| 233 |
if steps > 4 and duration_seconds > 2:
|
| 234 |
-
|
| 235 |
elif steps > 4 or duration_seconds > 2:
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 239 |
|
| 240 |
@spaces.GPU(duration=get_duration)
|
| 241 |
def generate_video(gallery_images, mode, prompt, height, width,
|
| 242 |
negative_prompt=default_negative_prompt, duration_seconds = 2,
|
| 243 |
guidance_scale = 1, steps = 4,
|
| 244 |
-
seed = 42, randomize_seed = False,
|
| 245 |
progress=gr.Progress(track_tqdm=True)):
|
| 246 |
"""
|
| 247 |
Generate a video from gallery images using the selected mode.
|
|
@@ -258,6 +353,7 @@ def generate_video(gallery_images, mode, prompt, height, width,
|
|
| 258 |
steps (int): Number of inference steps
|
| 259 |
seed (int): Random seed for reproducible results
|
| 260 |
randomize_seed (bool): Whether to use a random seed
|
|
|
|
| 261 |
progress (gr.Progress): Gradio progress tracker
|
| 262 |
|
| 263 |
Returns:
|
|
@@ -266,8 +362,20 @@ def generate_video(gallery_images, mode, prompt, height, width,
|
|
| 266 |
if gallery_images is None or len(gallery_images) == 0:
|
| 267 |
raise gr.Error("Please upload at least one image to the gallery.")
|
| 268 |
else:
|
| 269 |
-
#
|
| 270 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 271 |
|
| 272 |
if mode == "FLF2V" and len(gallery_images) >= 2:
|
| 273 |
gallery_images = gallery_images[:2]
|
|
@@ -377,6 +485,14 @@ with gr.Blocks() as demo:
|
|
| 377 |
|
| 378 |
with gr.Accordion("Advanced Settings", open=False):
|
| 379 |
negative_prompt_input = gr.Textbox(label="Negative Prompt", value=default_negative_prompt, lines=3)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 380 |
seed_input = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=42, interactive=True)
|
| 381 |
randomize_seed_checkbox = gr.Checkbox(label="Randomize seed", value=True, interactive=True)
|
| 382 |
with gr.Row():
|
|
@@ -390,6 +506,10 @@ with gr.Blocks() as demo:
|
|
| 390 |
with gr.Column():
|
| 391 |
video_output = gr.Video(label="Generated Video", autoplay=True, interactive=False)
|
| 392 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 393 |
# Update prompt when mode changes
|
| 394 |
mode_radio.change(
|
| 395 |
fn=update_prompt_from_mode,
|
|
@@ -397,6 +517,13 @@ with gr.Blocks() as demo:
|
|
| 397 |
outputs=[prompt_input]
|
| 398 |
)
|
| 399 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 400 |
# Update dimensions when gallery changes
|
| 401 |
gallery_component.change(
|
| 402 |
fn=handle_gallery_upload_for_dims_wan,
|
|
@@ -407,7 +534,7 @@ with gr.Blocks() as demo:
|
|
| 407 |
ui_inputs = [
|
| 408 |
gallery_component, mode_radio, prompt_input, height_input, width_input,
|
| 409 |
negative_prompt_input, duration_seconds_input,
|
| 410 |
-
guidance_scale_input, steps_slider, seed_input, randomize_seed_checkbox
|
| 411 |
]
|
| 412 |
|
| 413 |
generate_button.click(fn=generate_video, inputs=ui_inputs, outputs=[video_output, seed_input])
|
|
|
|
| 9 |
import numpy as np
|
| 10 |
from PIL import Image
|
| 11 |
import random
|
| 12 |
+
from briarmbg import BriaRMBG
|
| 13 |
|
| 14 |
model_id = "Wan-AI/Wan2.1-VACE-14B-diffusers"
|
| 15 |
vae = AutoencoderKLWan.from_pretrained(model_id, subfolder="vae", torch_dtype=torch.float32)
|
| 16 |
pipe = WanVACEPipeline.from_pretrained(model_id, vae=vae, torch_dtype=torch.bfloat16).to("cuda")
|
| 17 |
|
| 18 |
+
# Initialize background removal model
|
| 19 |
+
rmbg = BriaRMBG.from_pretrained("briaai/RMBG-1.4").to("cuda", dtype=torch.float32)
|
| 20 |
+
|
| 21 |
pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config, flow_shift=2.0)
|
| 22 |
|
| 23 |
pipe.load_lora_weights(
|
|
|
|
| 93 |
return image
|
| 94 |
|
| 95 |
|
| 96 |
+
@torch.inference_mode()
|
| 97 |
+
def numpy2pytorch(imgs):
|
| 98 |
+
h = torch.from_numpy(np.stack(imgs, axis=0)).float() / 127.0 - 1.0 # so that 127 must be strictly 0.0
|
| 99 |
+
h = h.movedim(-1, 1)
|
| 100 |
+
return h
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
@torch.inference_mode()
|
| 104 |
+
def pytorch2numpy(imgs, quant=True):
|
| 105 |
+
results = []
|
| 106 |
+
for x in imgs:
|
| 107 |
+
y = x.movedim(0, -1)
|
| 108 |
+
|
| 109 |
+
if quant:
|
| 110 |
+
y = y * 127.5 + 127.5
|
| 111 |
+
y = y.detach().float().cpu().numpy().clip(0, 255).astype(np.uint8)
|
| 112 |
+
else:
|
| 113 |
+
y = y * 0.5 + 0.5
|
| 114 |
+
y = y.detach().float().cpu().numpy().clip(0, 1).astype(np.float32)
|
| 115 |
+
|
| 116 |
+
results.append(y)
|
| 117 |
+
return results
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
def resize_without_crop(image, target_width, target_height):
|
| 121 |
+
pil_image = Image.fromarray(image)
|
| 122 |
+
resized_image = pil_image.resize((target_width, target_height), Image.LANCZOS)
|
| 123 |
+
return np.array(resized_image)
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
@torch.inference_mode()
|
| 127 |
+
def run_rmbg(img, sigma=0.0):
|
| 128 |
+
"""
|
| 129 |
+
Remove background from image using BriaRMBG model.
|
| 130 |
+
|
| 131 |
+
Args:
|
| 132 |
+
img (np.ndarray): Input image as numpy array (H, W, C)
|
| 133 |
+
sigma (float): Noise parameter for blending
|
| 134 |
+
|
| 135 |
+
Returns:
|
| 136 |
+
tuple: (result_image, alpha_mask) where result_image is the image with background removed
|
| 137 |
+
"""
|
| 138 |
+
H, W, C = img.shape
|
| 139 |
+
assert C == 3
|
| 140 |
+
k = (256.0 / float(H * W)) ** 0.5
|
| 141 |
+
feed = resize_without_crop(img, int(64 * round(W * k)), int(64 * round(H * k)))
|
| 142 |
+
feed = numpy2pytorch([feed]).to(device="cuda", dtype=torch.float32)
|
| 143 |
+
alpha = rmbg(feed)[0][0]
|
| 144 |
+
alpha = torch.nn.functional.interpolate(alpha, size=(H, W), mode="bilinear")
|
| 145 |
+
alpha = alpha.movedim(1, -1)[0]
|
| 146 |
+
alpha = alpha.detach().float().cpu().numpy().clip(0, 1)
|
| 147 |
+
result = 127 + (img.astype(np.float32) - 127 + sigma) * alpha
|
| 148 |
+
return result.clip(0, 255).astype(np.uint8), alpha
|
| 149 |
+
|
| 150 |
+
|
| 151 |
+
def remove_background_from_image(image: Image.Image) -> Image.Image:
|
| 152 |
+
"""
|
| 153 |
+
Remove background from PIL Image using RMBG model.
|
| 154 |
+
|
| 155 |
+
Args:
|
| 156 |
+
image (Image.Image): Input PIL image
|
| 157 |
+
|
| 158 |
+
Returns:
|
| 159 |
+
Image.Image: Image with background removed (transparent background)
|
| 160 |
+
"""
|
| 161 |
+
# Convert PIL to numpy array
|
| 162 |
+
img_array = np.array(image)
|
| 163 |
+
|
| 164 |
+
# Remove background using RMBG
|
| 165 |
+
result_array, alpha_mask = run_rmbg(img_array)
|
| 166 |
+
|
| 167 |
+
# Convert back to PIL with alpha channel
|
| 168 |
+
result_image = Image.fromarray(result_array)
|
| 169 |
+
|
| 170 |
+
# Create RGBA image with alpha mask
|
| 171 |
+
if result_image.mode != 'RGBA':
|
| 172 |
+
result_image = result_image.convert('RGBA')
|
| 173 |
+
|
| 174 |
+
# Apply alpha mask
|
| 175 |
+
alpha_pil = Image.fromarray((alpha_mask * 255).astype(np.uint8), mode='L')
|
| 176 |
+
result_image.putalpha(alpha_pil)
|
| 177 |
+
|
| 178 |
+
return result_image
|
| 179 |
+
|
| 180 |
+
|
| 181 |
def _calculate_new_dimensions_wan(pil_image, mod_val, calculation_max_area,
|
| 182 |
min_slider_h, max_slider_h,
|
| 183 |
min_slider_w, max_slider_w,
|
|
|
|
| 317 |
def get_duration(gallery_images, mode, prompt, height, width,
|
| 318 |
negative_prompt, duration_seconds,
|
| 319 |
guidance_scale, steps,
|
| 320 |
+
seed, randomize_seed, remove_bg,
|
| 321 |
progress):
|
| 322 |
+
# Add extra time if background removal is enabled
|
| 323 |
+
base_duration = 60
|
| 324 |
if steps > 4 and duration_seconds > 2:
|
| 325 |
+
base_duration = 90
|
| 326 |
elif steps > 4 or duration_seconds > 2:
|
| 327 |
+
base_duration = 75
|
| 328 |
+
|
| 329 |
+
# Add extra time for background removal processing
|
| 330 |
+
if mode == "Ref2V" and remove_bg:
|
| 331 |
+
base_duration += 30
|
| 332 |
+
|
| 333 |
+
return base_duration
|
| 334 |
|
| 335 |
@spaces.GPU(duration=get_duration)
|
| 336 |
def generate_video(gallery_images, mode, prompt, height, width,
|
| 337 |
negative_prompt=default_negative_prompt, duration_seconds = 2,
|
| 338 |
guidance_scale = 1, steps = 4,
|
| 339 |
+
seed = 42, randomize_seed = False, remove_bg = False,
|
| 340 |
progress=gr.Progress(track_tqdm=True)):
|
| 341 |
"""
|
| 342 |
Generate a video from gallery images using the selected mode.
|
|
|
|
| 353 |
steps (int): Number of inference steps
|
| 354 |
seed (int): Random seed for reproducible results
|
| 355 |
randomize_seed (bool): Whether to use a random seed
|
| 356 |
+
remove_bg (bool): Whether to remove background from images (Ref2V mode only)
|
| 357 |
progress (gr.Progress): Gradio progress tracker
|
| 358 |
|
| 359 |
Returns:
|
|
|
|
| 362 |
if gallery_images is None or len(gallery_images) == 0:
|
| 363 |
raise gr.Error("Please upload at least one image to the gallery.")
|
| 364 |
else:
|
| 365 |
+
# Process images: remove background if requested (Ref2V mode only), then remove alpha channels
|
| 366 |
+
processed_images = []
|
| 367 |
+
for img in gallery_images:
|
| 368 |
+
image = img[0] # Extract PIL image from gallery format
|
| 369 |
+
|
| 370 |
+
# Apply background removal only for Ref2V mode if checkbox is checked
|
| 371 |
+
if mode == "Ref2V" and remove_bg:
|
| 372 |
+
image = remove_background_from_image(image)
|
| 373 |
+
|
| 374 |
+
# Always remove alpha channels to ensure RGB format
|
| 375 |
+
image = remove_alpha_channel(image)
|
| 376 |
+
processed_images.append(image)
|
| 377 |
+
|
| 378 |
+
gallery_images = processed_images
|
| 379 |
|
| 380 |
if mode == "FLF2V" and len(gallery_images) >= 2:
|
| 381 |
gallery_images = gallery_images[:2]
|
|
|
|
| 485 |
|
| 486 |
with gr.Accordion("Advanced Settings", open=False):
|
| 487 |
negative_prompt_input = gr.Textbox(label="Negative Prompt", value=default_negative_prompt, lines=3)
|
| 488 |
+
|
| 489 |
+
# Background removal checkbox (only for Ref2V mode)
|
| 490 |
+
remove_bg_checkbox = gr.Checkbox(
|
| 491 |
+
label="Remove Background (Ref2V mode only)",
|
| 492 |
+
value=False,
|
| 493 |
+
info="Automatically remove background from input images when using Ref2V mode"
|
| 494 |
+
)
|
| 495 |
+
|
| 496 |
seed_input = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=42, interactive=True)
|
| 497 |
randomize_seed_checkbox = gr.Checkbox(label="Randomize seed", value=True, interactive=True)
|
| 498 |
with gr.Row():
|
|
|
|
| 506 |
with gr.Column():
|
| 507 |
video_output = gr.Video(label="Generated Video", autoplay=True, interactive=False)
|
| 508 |
|
| 509 |
+
# Function to update checkbox visibility based on mode
|
| 510 |
+
def update_bg_removal_visibility(mode):
|
| 511 |
+
return gr.update(visible=(mode == "Ref2V"))
|
| 512 |
+
|
| 513 |
# Update prompt when mode changes
|
| 514 |
mode_radio.change(
|
| 515 |
fn=update_prompt_from_mode,
|
|
|
|
| 517 |
outputs=[prompt_input]
|
| 518 |
)
|
| 519 |
|
| 520 |
+
# Update background removal checkbox visibility when mode changes
|
| 521 |
+
mode_radio.change(
|
| 522 |
+
fn=update_bg_removal_visibility,
|
| 523 |
+
inputs=[mode_radio],
|
| 524 |
+
outputs=[remove_bg_checkbox]
|
| 525 |
+
)
|
| 526 |
+
|
| 527 |
# Update dimensions when gallery changes
|
| 528 |
gallery_component.change(
|
| 529 |
fn=handle_gallery_upload_for_dims_wan,
|
|
|
|
| 534 |
ui_inputs = [
|
| 535 |
gallery_component, mode_radio, prompt_input, height_input, width_input,
|
| 536 |
negative_prompt_input, duration_seconds_input,
|
| 537 |
+
guidance_scale_input, steps_slider, seed_input, randomize_seed_checkbox, remove_bg_checkbox
|
| 538 |
]
|
| 539 |
|
| 540 |
generate_button.click(fn=generate_video, inputs=ui_inputs, outputs=[video_output, seed_input])
|