Spaces:
Runtime error
Runtime error
app
Browse files
app.py
CHANGED
|
@@ -5,7 +5,6 @@ from enum import Enum
|
|
| 5 |
import db_examples
|
| 6 |
import cv2
|
| 7 |
|
| 8 |
-
import spaces
|
| 9 |
|
| 10 |
from demo_utils1 import *
|
| 11 |
|
|
@@ -24,6 +23,7 @@ from torchvision.transforms import functional as F
|
|
| 24 |
from torch.hub import download_url_to_file
|
| 25 |
|
| 26 |
import os
|
|
|
|
| 27 |
|
| 28 |
# 推理设置
|
| 29 |
from pl_trainer.inference.inference import InferenceIP2PVideo
|
|
@@ -224,9 +224,28 @@ inf_pipe = InferenceIP2PVideo(
|
|
| 224 |
num_ddim_steps=20
|
| 225 |
)
|
| 226 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 227 |
# 伪函数占位(生成空白视频)
|
| 228 |
@spaces.GPU
|
| 229 |
-
def dummy_process(input_fg, input_bg):
|
| 230 |
# import pdb; pdb.set_trace()
|
| 231 |
|
| 232 |
diffusion_model.to(torch.float16)
|
|
@@ -240,7 +259,8 @@ def dummy_process(input_fg, input_bg):
|
|
| 240 |
# 初始化潜变量
|
| 241 |
init_latent = torch.randn_like(cond_fg_tensor)
|
| 242 |
|
| 243 |
-
EDIT_PROMPT = 'change the background'
|
|
|
|
| 244 |
VIDEO_CFG = 1.2
|
| 245 |
TEXT_CFG = 7.5
|
| 246 |
text_cond = diffusion_model.encode_text([EDIT_PROMPT]) # (1, 77, 768)
|
|
@@ -295,13 +315,35 @@ def dummy_process(input_fg, input_bg):
|
|
| 295 |
class BGSource(Enum):
|
| 296 |
UPLOAD = "Use Background Video"
|
| 297 |
UPLOAD_FLIP = "Use Flipped Background Video"
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
TOP = "Top Light"
|
| 301 |
-
BOTTOM = "Bottom Light"
|
| 302 |
-
GREY = "Ambient"
|
| 303 |
|
| 304 |
# Quick prompts 示例
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 305 |
quick_prompts = [
|
| 306 |
'beautiful woman',
|
| 307 |
'handsome man',
|
|
@@ -309,62 +351,96 @@ quick_prompts = [
|
|
| 309 |
'handsome man, cinematic lighting',
|
| 310 |
'beautiful woman, natural lighting',
|
| 311 |
'handsome man, natural lighting',
|
| 312 |
-
'beautiful woman,
|
| 313 |
-
'handsome man,
|
|
|
|
| 314 |
]
|
|
|
|
|
|
|
| 315 |
quick_prompts = [[x] for x in quick_prompts]
|
| 316 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 317 |
# Gradio UI 结构
|
| 318 |
block = gr.Blocks().queue()
|
| 319 |
with block:
|
| 320 |
with gr.Row():
|
| 321 |
-
gr.Markdown("##
|
|
|
|
| 322 |
|
| 323 |
with gr.Row():
|
| 324 |
with gr.Column():
|
| 325 |
with gr.Row():
|
| 326 |
-
input_fg = gr.Video(label="Foreground Video", height=
|
| 327 |
-
input_bg = gr.Video(label="Background Video", height=
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
|
|
|
|
|
|
|
|
|
| 333 |
|
| 334 |
-
|
| 335 |
bg_gallery = gr.Gallery(height=450, object_fit='contain', label='Background Quick List', value=db_examples.bg_samples, columns=5, allow_preview=False)
|
| 336 |
-
|
| 337 |
|
| 338 |
with gr.Group():
|
|
|
|
|
|
|
|
|
|
| 339 |
with gr.Row():
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
a_prompt = gr.Textbox(label="Added Prompt", value='best quality')
|
| 352 |
-
n_prompt = gr.Textbox(label="Negative Prompt", value='lowres, bad anatomy, bad hands, cropped, worst quality')
|
| 353 |
-
normal_button = gr.Button(value="Compute Normal (4x Slower)")
|
| 354 |
|
| 355 |
with gr.Column():
|
| 356 |
-
result_video = gr.Video(label='Output Video', height=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 357 |
|
| 358 |
# 输入列表
|
| 359 |
# ips = [input_fg, input_bg, prompt, video_width, video_height, num_samples, seed, steps, a_prompt, n_prompt, cfg, highres_scale, highres_denoise, bg_source]
|
| 360 |
-
ips = [input_fg, input_bg]
|
| 361 |
|
| 362 |
# 按钮绑定处理函数
|
| 363 |
# relight_button.click(fn=lambda: None, inputs=[], outputs=[result_video])
|
| 364 |
|
| 365 |
relight_button.click(fn=dummy_process, inputs=ips, outputs=[result_video])
|
| 366 |
|
| 367 |
-
normal_button.click(fn=dummy_process, inputs=ips, outputs=[result_video])
|
| 368 |
|
| 369 |
# 背景库选择
|
| 370 |
def bg_gallery_selected(gal, evt: gr.SelectData):
|
|
@@ -376,13 +452,31 @@ with block:
|
|
| 376 |
|
| 377 |
bg_gallery.select(bg_gallery_selected, inputs=bg_gallery, outputs=input_bg)
|
| 378 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 379 |
# 示例
|
| 380 |
# dummy_video_for_outputs = gr.Video(visible=False, label='Result')
|
| 381 |
gr.Examples(
|
| 382 |
-
fn=lambda *args: args[-1],
|
|
|
|
| 383 |
examples=db_examples.background_conditioned_examples,
|
| 384 |
-
inputs=[
|
| 385 |
-
|
|
|
|
| 386 |
run_on_click=True, examples_per_page=1024
|
| 387 |
)
|
| 388 |
|
|
|
|
| 5 |
import db_examples
|
| 6 |
import cv2
|
| 7 |
|
|
|
|
| 8 |
|
| 9 |
from demo_utils1 import *
|
| 10 |
|
|
|
|
| 23 |
from torch.hub import download_url_to_file
|
| 24 |
|
| 25 |
import os
|
| 26 |
+
import spaces
|
| 27 |
|
| 28 |
# 推理设置
|
| 29 |
from pl_trainer.inference.inference import InferenceIP2PVideo
|
|
|
|
| 224 |
num_ddim_steps=20
|
| 225 |
)
|
| 226 |
|
| 227 |
+
|
| 228 |
+
def process_example(*args):
|
| 229 |
+
v_index = args[0]
|
| 230 |
+
select_e = db_examples.background_conditioned_examples[int(v_index)-1]
|
| 231 |
+
input_fg_path = select_e[1]
|
| 232 |
+
input_bg_path = select_e[2]
|
| 233 |
+
result_video_path = select_e[-1]
|
| 234 |
+
# input_fg_img = args[1] # 第 0 个参数
|
| 235 |
+
# input_bg_img = args[2] # 第 1 个参数
|
| 236 |
+
# result_video_img = args[-1] # 最后一个参数
|
| 237 |
+
|
| 238 |
+
input_fg = input_fg_path.replace("frames/0000.png", "cropped_video.mp4")
|
| 239 |
+
input_bg = input_bg_path.replace("frames/0000.png", "cropped_video.mp4")
|
| 240 |
+
result_video = result_video_path.replace(".png", ".mp4")
|
| 241 |
+
|
| 242 |
+
return input_fg, input_bg, result_video
|
| 243 |
+
|
| 244 |
+
|
| 245 |
+
|
| 246 |
# 伪函数占位(生成空白视频)
|
| 247 |
@spaces.GPU
|
| 248 |
+
def dummy_process(input_fg, input_bg, prompt):
|
| 249 |
# import pdb; pdb.set_trace()
|
| 250 |
|
| 251 |
diffusion_model.to(torch.float16)
|
|
|
|
| 259 |
# 初始化潜变量
|
| 260 |
init_latent = torch.randn_like(cond_fg_tensor)
|
| 261 |
|
| 262 |
+
# EDIT_PROMPT = 'change the background'
|
| 263 |
+
EDIT_PROMPT = prompt
|
| 264 |
VIDEO_CFG = 1.2
|
| 265 |
TEXT_CFG = 7.5
|
| 266 |
text_cond = diffusion_model.encode_text([EDIT_PROMPT]) # (1, 77, 768)
|
|
|
|
| 315 |
class BGSource(Enum):
|
| 316 |
UPLOAD = "Use Background Video"
|
| 317 |
UPLOAD_FLIP = "Use Flipped Background Video"
|
| 318 |
+
UPLOAD_REVERSE = "Use Reversed Background Video"
|
| 319 |
+
|
|
|
|
|
|
|
|
|
|
| 320 |
|
| 321 |
# Quick prompts 示例
|
| 322 |
+
# quick_prompts = [
|
| 323 |
+
# 'beautiful woman, fantasy setting',
|
| 324 |
+
# 'beautiful woman, neon dynamic lighting',
|
| 325 |
+
# 'man in suit, tunel lighting',
|
| 326 |
+
# 'animated mouse, aesthetic lighting',
|
| 327 |
+
# 'robot warrior, a sunset background',
|
| 328 |
+
# 'yellow cat, reflective wet beach',
|
| 329 |
+
# 'camera, dock, calm sunset',
|
| 330 |
+
# 'astronaut, dim lighting',
|
| 331 |
+
# 'astronaut, colorful balloons',
|
| 332 |
+
# 'astronaut, desert landscape'
|
| 333 |
+
# ]
|
| 334 |
+
|
| 335 |
+
# quick_prompts = [
|
| 336 |
+
# 'beautiful woman',
|
| 337 |
+
# 'handsome man',
|
| 338 |
+
# 'beautiful woman, cinematic lighting',
|
| 339 |
+
# 'handsome man, cinematic lighting',
|
| 340 |
+
# 'beautiful woman, natural lighting',
|
| 341 |
+
# 'handsome man, natural lighting',
|
| 342 |
+
# 'beautiful woman, neo punk lighting, cyberpunk',
|
| 343 |
+
# 'handsome man, neo punk lighting, cyberpunk',
|
| 344 |
+
# ]
|
| 345 |
+
|
| 346 |
+
|
| 347 |
quick_prompts = [
|
| 348 |
'beautiful woman',
|
| 349 |
'handsome man',
|
|
|
|
| 351 |
'handsome man, cinematic lighting',
|
| 352 |
'beautiful woman, natural lighting',
|
| 353 |
'handsome man, natural lighting',
|
| 354 |
+
'beautiful woman, warm lighting',
|
| 355 |
+
'handsome man, soft lighting',
|
| 356 |
+
'change the background lighting',
|
| 357 |
]
|
| 358 |
+
|
| 359 |
+
|
| 360 |
quick_prompts = [[x] for x in quick_prompts]
|
| 361 |
|
| 362 |
+
# css = """
|
| 363 |
+
# #foreground-gallery {
|
| 364 |
+
# width: 700 !important; /* 限制最大宽度 */
|
| 365 |
+
# max-width: 700px !important; /* 避免它自动变宽 */
|
| 366 |
+
# flex: none !important; /* 让它不自动扩展 */
|
| 367 |
+
# }
|
| 368 |
+
# """
|
| 369 |
+
|
| 370 |
# Gradio UI 结构
|
| 371 |
block = gr.Blocks().queue()
|
| 372 |
with block:
|
| 373 |
with gr.Row():
|
| 374 |
+
# gr.Markdown("## RelightVid (Relighting with Foreground and Background Video Condition)")
|
| 375 |
+
gr.Markdown("# 💡RelightVid \n### Relighting with Foreground and Background Video Condition")
|
| 376 |
|
| 377 |
with gr.Row():
|
| 378 |
with gr.Column():
|
| 379 |
with gr.Row():
|
| 380 |
+
input_fg = gr.Video(label="Foreground Video", height=380, width=420, visible=True)
|
| 381 |
+
input_bg = gr.Video(label="Background Video", height=380, width=420, visible=True)
|
| 382 |
+
|
| 383 |
+
segment_button = gr.Button(value="Video Segmentation")
|
| 384 |
+
with gr.Accordion("Segmentation Options", open=False):
|
| 385 |
+
# 如果用户不使用 point_prompt,而是直接提供坐标,则使用 x, y
|
| 386 |
+
with gr.Row():
|
| 387 |
+
x_coord = gr.Slider(label="X Coordinate (Point Prompt Ratio)", minimum=0.0, maximum=1.0, value=0.5, step=0.01)
|
| 388 |
+
y_coord = gr.Slider(label="Y Coordinate (Point Prompt Ratio)", minimum=0.0, maximum=1.0, value=0.5, step=0.01)
|
| 389 |
+
|
| 390 |
|
| 391 |
+
fg_gallery = gr.Gallery(height=150, object_fit='contain', label='Foreground Quick List', value=db_examples.fg_samples, columns=5, allow_preview=False)
|
| 392 |
bg_gallery = gr.Gallery(height=450, object_fit='contain', label='Background Quick List', value=db_examples.bg_samples, columns=5, allow_preview=False)
|
| 393 |
+
|
| 394 |
|
| 395 |
with gr.Group():
|
| 396 |
+
# with gr.Row():
|
| 397 |
+
# num_samples = gr.Slider(label="Videos", minimum=1, maximum=12, value=1, step=1)
|
| 398 |
+
# seed = gr.Number(label="Seed", value=12345, precision=0)
|
| 399 |
with gr.Row():
|
| 400 |
+
video_width = gr.Slider(label="Video Width", minimum=256, maximum=1024, value=512, step=64, visible=False)
|
| 401 |
+
video_height = gr.Slider(label="Video Height", minimum=256, maximum=1024, value=512, step=64, visible=False)
|
| 402 |
+
|
| 403 |
+
# with gr.Accordion("Advanced options", open=False):
|
| 404 |
+
# steps = gr.Slider(label="Steps", minimum=1, maximum=100, value=20, step=1)
|
| 405 |
+
# cfg = gr.Slider(label="CFG Scale", minimum=1.0, maximum=32.0, value=7.0, step=0.01)
|
| 406 |
+
# highres_scale = gr.Slider(label="Highres Scale", minimum=1.0, maximum=3.0, value=1.5, step=0.01)
|
| 407 |
+
# highres_denoise = gr.Slider(label="Highres Denoise", minimum=0.1, maximum=0.9, value=0.5, step=0.01)
|
| 408 |
+
# a_prompt = gr.Textbox(label="Added Prompt", value='best quality')
|
| 409 |
+
# n_prompt = gr.Textbox(label="Negative Prompt", value='lowres, bad anatomy, bad hands, cropped, worst quality')
|
| 410 |
+
# normal_button = gr.Button(value="Compute Normal (4x Slower)")
|
|
|
|
|
|
|
|
|
|
| 411 |
|
| 412 |
with gr.Column():
|
| 413 |
+
result_video = gr.Video(label='Output Video', height=700, width=700, visible=True)
|
| 414 |
+
|
| 415 |
+
prompt = gr.Textbox(label="Prompt")
|
| 416 |
+
bg_source = gr.Radio(choices=[e.value for e in BGSource],
|
| 417 |
+
value=BGSource.UPLOAD.value,
|
| 418 |
+
label="Background Source", type='value')
|
| 419 |
+
|
| 420 |
+
example_prompts = gr.Dataset(samples=quick_prompts, label='Prompt Quick List', components=[prompt])
|
| 421 |
+
relight_button = gr.Button(value="Relight")
|
| 422 |
+
# fg_gallery = gr.Gallery(witdth=400, object_fit='contain', label='Foreground Quick List', value=db_examples.bg_samples, columns=4, allow_preview=False)
|
| 423 |
+
# fg_gallery = gr.Gallery(
|
| 424 |
+
# height=380,
|
| 425 |
+
# object_fit='contain',
|
| 426 |
+
# label='Foreground Quick List',
|
| 427 |
+
# value=db_examples.fg_samples,
|
| 428 |
+
# columns=4,
|
| 429 |
+
# allow_preview=False,
|
| 430 |
+
# elem_id="foreground-gallery" # 👈 添加 elem_id
|
| 431 |
+
# )
|
| 432 |
+
|
| 433 |
|
| 434 |
# 输入列表
|
| 435 |
# ips = [input_fg, input_bg, prompt, video_width, video_height, num_samples, seed, steps, a_prompt, n_prompt, cfg, highres_scale, highres_denoise, bg_source]
|
| 436 |
+
ips = [input_fg, input_bg, prompt]
|
| 437 |
|
| 438 |
# 按钮绑定处理函数
|
| 439 |
# relight_button.click(fn=lambda: None, inputs=[], outputs=[result_video])
|
| 440 |
|
| 441 |
relight_button.click(fn=dummy_process, inputs=ips, outputs=[result_video])
|
| 442 |
|
| 443 |
+
# normal_button.click(fn=dummy_process, inputs=ips, outputs=[result_video])
|
| 444 |
|
| 445 |
# 背景库选择
|
| 446 |
def bg_gallery_selected(gal, evt: gr.SelectData):
|
|
|
|
| 452 |
|
| 453 |
bg_gallery.select(bg_gallery_selected, inputs=bg_gallery, outputs=input_bg)
|
| 454 |
|
| 455 |
+
def fg_gallery_selected(gal, evt: gr.SelectData):
|
| 456 |
+
# import pdb; pdb.set_trace()
|
| 457 |
+
# img_path = gal[evt.index][0]
|
| 458 |
+
img_path = db_examples.fg_samples[evt.index]
|
| 459 |
+
video_path = img_path.replace('frames/0000.png', 'cropped_video.mp4')
|
| 460 |
+
return video_path
|
| 461 |
+
|
| 462 |
+
fg_gallery.select(fg_gallery_selected, inputs=fg_gallery, outputs=input_fg)
|
| 463 |
+
|
| 464 |
+
input_fg_img = gr.Image(label="Foreground Video", visible=False)
|
| 465 |
+
input_bg_img = gr.Image(label="Background Video", visible=False)
|
| 466 |
+
result_video_img = gr.Image(label="Output Video", visible=False)
|
| 467 |
+
|
| 468 |
+
v_index = gr.Textbox(label="ID", visible=False)
|
| 469 |
+
example_prompts.click(lambda x: x[0], inputs=example_prompts, outputs=prompt, show_progress=False, queue=False)
|
| 470 |
+
|
| 471 |
# 示例
|
| 472 |
# dummy_video_for_outputs = gr.Video(visible=False, label='Result')
|
| 473 |
gr.Examples(
|
| 474 |
+
# fn=lambda *args: args[-1],
|
| 475 |
+
fn=process_example,
|
| 476 |
examples=db_examples.background_conditioned_examples,
|
| 477 |
+
# inputs=[v_index, input_fg_img, input_bg_img, prompt, bg_source, video_width, video_height, result_video_img],
|
| 478 |
+
inputs=[v_index, input_fg_img, input_bg_img, prompt, bg_source, result_video_img],
|
| 479 |
+
outputs=[input_fg, input_bg, result_video],
|
| 480 |
run_on_click=True, examples_per_page=1024
|
| 481 |
)
|
| 482 |
|