Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -52,6 +52,14 @@ WAN21_FILES = [
|
|
| 52 |
"Wan2.1_VAE.pth"
|
| 53 |
]
|
| 54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
RECAMMASTER_REPO_ID = "KwaiVGI/ReCamMaster-Wan2.1"
|
| 56 |
RECAMMASTER_CHECKPOINT_FILE = "step20000.ckpt"
|
| 57 |
RECAMMASTER_LOCAL_DIR = f"{MODELS_ROOT_DIR}/ReCamMaster/checkpoints"
|
|
@@ -59,6 +67,55 @@ RECAMMASTER_LOCAL_DIR = f"{MODELS_ROOT_DIR}/ReCamMaster/checkpoints"
|
|
| 59 |
# Define test data directory
|
| 60 |
TEST_DATA_DIR = "example_test_data"
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
def download_wan21_models(progress_callback=None):
|
| 63 |
"""Download Wan2.1 model files from HuggingFace"""
|
| 64 |
|
|
@@ -343,10 +400,45 @@ def load_models(progress_callback=None):
|
|
| 343 |
logger.error(error_msg)
|
| 344 |
return error_msg
|
| 345 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 346 |
# Now, load the models
|
| 347 |
if progress_callback:
|
| 348 |
progress_callback(0.4, desc="Loading model manager...")
|
| 349 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 350 |
# Load Wan2.1 pre-trained models
|
| 351 |
model_manager = ModelManager(torch_dtype=torch.bfloat16, device="cpu")
|
| 352 |
|
|
@@ -363,6 +455,13 @@ def load_models(progress_callback=None):
|
|
| 363 |
logger.error(error_msg)
|
| 364 |
return error_msg
|
| 365 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 366 |
model_manager.load_models(model_files)
|
| 367 |
|
| 368 |
if progress_callback:
|
|
@@ -581,13 +680,23 @@ def generate_recammaster_video(
|
|
| 581 |
return None, f"Error: {str(e)}"
|
| 582 |
|
| 583 |
# Create Gradio interface
|
| 584 |
-
with gr.Blocks(title="ReCamMaster") as demo:
|
| 585 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 586 |
gr.Markdown(f"""
|
| 587 |
-
# π₯ ReCamMaster
|
| 588 |
|
| 589 |
ReCamMaster allows you to re-capture videos with novel camera trajectories.
|
| 590 |
Upload a video and select a camera transformation to see the magic!
|
|
|
|
|
|
|
|
|
|
| 591 |
""")
|
| 592 |
|
| 593 |
with gr.Row():
|
|
@@ -618,8 +727,24 @@ with gr.Blocks(title="ReCamMaster") as demo:
|
|
| 618 |
# Output section
|
| 619 |
output_video = gr.Video(label="Output Video")
|
| 620 |
status_output = gr.Textbox(label="Generation Status", interactive=False)
|
| 621 |
-
|
| 622 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 623 |
# Event handlers
|
| 624 |
generate_btn.click(
|
| 625 |
fn=generate_recammaster_video,
|
|
@@ -628,5 +753,4 @@ with gr.Blocks(title="ReCamMaster") as demo:
|
|
| 628 |
)
|
| 629 |
|
| 630 |
if __name__ == "__main__":
|
| 631 |
-
load_models()
|
| 632 |
demo.launch(share=True)
|
|
|
|
| 52 |
"Wan2.1_VAE.pth"
|
| 53 |
]
|
| 54 |
|
| 55 |
+
# Define tokenizer files to download
|
| 56 |
+
UMT5_XXL_TOKENIZER_FILES = [
|
| 57 |
+
"google/umt5-xxl/special_tokens_map.json",
|
| 58 |
+
"google/umt5-xxl/spiece.model",
|
| 59 |
+
"google/umt5-xxl/tokenizer.json",
|
| 60 |
+
"google/umt5-xxl/tokenizer_config.json"
|
| 61 |
+
]
|
| 62 |
+
|
| 63 |
RECAMMASTER_REPO_ID = "KwaiVGI/ReCamMaster-Wan2.1"
|
| 64 |
RECAMMASTER_CHECKPOINT_FILE = "step20000.ckpt"
|
| 65 |
RECAMMASTER_LOCAL_DIR = f"{MODELS_ROOT_DIR}/ReCamMaster/checkpoints"
|
|
|
|
| 67 |
# Define test data directory
|
| 68 |
TEST_DATA_DIR = "example_test_data"
|
| 69 |
|
| 70 |
+
def download_umt5_xxl_tokenizer(progress_callback=None):
|
| 71 |
+
"""Download UMT5-XXL tokenizer files from HuggingFace"""
|
| 72 |
+
|
| 73 |
+
total_files = len(UMT5_XXL_TOKENIZER_FILES)
|
| 74 |
+
downloaded_paths = []
|
| 75 |
+
|
| 76 |
+
for i, file_path in enumerate(UMT5_XXL_TOKENIZER_FILES):
|
| 77 |
+
local_dir = f"{WAN21_LOCAL_DIR}/{os.path.dirname(file_path)}"
|
| 78 |
+
filename = os.path.basename(file_path)
|
| 79 |
+
full_local_path = f"{WAN21_LOCAL_DIR}/{file_path}"
|
| 80 |
+
|
| 81 |
+
# Update progress
|
| 82 |
+
if progress_callback:
|
| 83 |
+
progress_callback(i/total_files, desc=f"Checking tokenizer file {i+1}/{total_files}: {filename}")
|
| 84 |
+
|
| 85 |
+
# Check if already exists
|
| 86 |
+
if os.path.exists(full_local_path):
|
| 87 |
+
logger.info(f"β Tokenizer file {filename} already exists at {full_local_path}")
|
| 88 |
+
downloaded_paths.append(full_local_path)
|
| 89 |
+
continue
|
| 90 |
+
|
| 91 |
+
# Create directory if it doesn't exist
|
| 92 |
+
os.makedirs(local_dir, exist_ok=True)
|
| 93 |
+
|
| 94 |
+
# Download the file
|
| 95 |
+
logger.info(f"Downloading tokenizer file {filename} from {WAN21_REPO_ID}/{file_path}...")
|
| 96 |
+
|
| 97 |
+
if progress_callback:
|
| 98 |
+
progress_callback(i/total_files, desc=f"Downloading tokenizer file {i+1}/{total_files}: {filename}")
|
| 99 |
+
|
| 100 |
+
try:
|
| 101 |
+
# Download using huggingface_hub
|
| 102 |
+
downloaded_path = hf_hub_download(
|
| 103 |
+
repo_id=WAN21_REPO_ID,
|
| 104 |
+
filename=file_path,
|
| 105 |
+
local_dir=WAN21_LOCAL_DIR,
|
| 106 |
+
local_dir_use_symlinks=False
|
| 107 |
+
)
|
| 108 |
+
logger.info(f"β Successfully downloaded tokenizer file {filename} to {downloaded_path}!")
|
| 109 |
+
downloaded_paths.append(downloaded_path)
|
| 110 |
+
except Exception as e:
|
| 111 |
+
logger.error(f"β Error downloading tokenizer file {filename}: {e}")
|
| 112 |
+
raise
|
| 113 |
+
|
| 114 |
+
if progress_callback:
|
| 115 |
+
progress_callback(1.0, desc=f"All tokenizer files downloaded successfully!")
|
| 116 |
+
|
| 117 |
+
return downloaded_paths
|
| 118 |
+
|
| 119 |
def download_wan21_models(progress_callback=None):
|
| 120 |
"""Download Wan2.1 model files from HuggingFace"""
|
| 121 |
|
|
|
|
| 400 |
logger.error(error_msg)
|
| 401 |
return error_msg
|
| 402 |
|
| 403 |
+
# Fourth, download UMT5-XXL tokenizer files
|
| 404 |
+
if progress_callback:
|
| 405 |
+
progress_callback(0.3, desc="Checking for UMT5-XXL tokenizer files...")
|
| 406 |
+
|
| 407 |
+
try:
|
| 408 |
+
tokenizer_paths = download_umt5_xxl_tokenizer(progress_callback)
|
| 409 |
+
logger.info(f"Using UMT5-XXL tokenizer files: {tokenizer_paths}")
|
| 410 |
+
except Exception as e:
|
| 411 |
+
error_msg = f"Error downloading UMT5-XXL tokenizer files: {str(e)}"
|
| 412 |
+
logger.error(error_msg)
|
| 413 |
+
return error_msg
|
| 414 |
+
|
| 415 |
# Now, load the models
|
| 416 |
if progress_callback:
|
| 417 |
progress_callback(0.4, desc="Loading model manager...")
|
| 418 |
|
| 419 |
+
# Create symlink for google/umt5-xxl to handle potential path issues
|
| 420 |
+
# Some libraries might look for this in a different way
|
| 421 |
+
try:
|
| 422 |
+
google_dir = f"{MODELS_ROOT_DIR}/google"
|
| 423 |
+
if not os.path.exists(google_dir):
|
| 424 |
+
os.makedirs(google_dir, exist_ok=True)
|
| 425 |
+
|
| 426 |
+
umt5_xxl_symlink = f"{google_dir}/umt5-xxl"
|
| 427 |
+
umt5_xxl_source = f"{WAN21_LOCAL_DIR}/google/umt5-xxl"
|
| 428 |
+
|
| 429 |
+
# Create a symlink if it doesn't exist
|
| 430 |
+
if not os.path.exists(umt5_xxl_symlink) and os.path.exists(umt5_xxl_source):
|
| 431 |
+
if os.name == 'nt': # Windows
|
| 432 |
+
import ctypes
|
| 433 |
+
kdll = ctypes.windll.LoadLibrary("kernel32.dll")
|
| 434 |
+
kdll.CreateSymbolicLinkA(umt5_xxl_symlink.encode(), umt5_xxl_source.encode(), 1)
|
| 435 |
+
else: # Unix/Linux
|
| 436 |
+
os.symlink(umt5_xxl_source, umt5_xxl_symlink)
|
| 437 |
+
logger.info(f"Created symlink from {umt5_xxl_source} to {umt5_xxl_symlink}")
|
| 438 |
+
except Exception as e:
|
| 439 |
+
logger.warning(f"Could not create symlink for google/umt5-xxl: {str(e)}")
|
| 440 |
+
# This is a warning, not an error, as we'll try to proceed anyway
|
| 441 |
+
|
| 442 |
# Load Wan2.1 pre-trained models
|
| 443 |
model_manager = ModelManager(torch_dtype=torch.bfloat16, device="cpu")
|
| 444 |
|
|
|
|
| 455 |
logger.error(error_msg)
|
| 456 |
return error_msg
|
| 457 |
|
| 458 |
+
# Set environment variable for transformers to find the tokenizer
|
| 459 |
+
os.environ["TRANSFORMERS_CACHE"] = MODELS_ROOT_DIR
|
| 460 |
+
|
| 461 |
+
# Set the configuration for the text encoder to use the downloaded tokenizer path
|
| 462 |
+
# This is needed because the WanTextEncoder expects the tokenizer to be at this path
|
| 463 |
+
os.environ["TOKENIZERS_PARALLELISM"] = "false" # Disable tokenizers parallelism warning
|
| 464 |
+
|
| 465 |
model_manager.load_models(model_files)
|
| 466 |
|
| 467 |
if progress_callback:
|
|
|
|
| 680 |
return None, f"Error: {str(e)}"
|
| 681 |
|
| 682 |
# Create Gradio interface
|
| 683 |
+
with gr.Blocks(title="ReCamMaster Demo") as demo:
|
| 684 |
+
# Show loading status
|
| 685 |
+
loading_status = gr.Textbox(
|
| 686 |
+
label="Model Loading Status",
|
| 687 |
+
value="Loading models, please wait...",
|
| 688 |
+
interactive=False,
|
| 689 |
+
visible=True
|
| 690 |
+
)
|
| 691 |
+
|
| 692 |
gr.Markdown(f"""
|
| 693 |
+
# π₯ ReCamMaster Demo
|
| 694 |
|
| 695 |
ReCamMaster allows you to re-capture videos with novel camera trajectories.
|
| 696 |
Upload a video and select a camera transformation to see the magic!
|
| 697 |
+
|
| 698 |
+
**Note:** All required models will be automatically downloaded to {MODELS_ROOT_DIR} when you start the app.
|
| 699 |
+
You can customize this location by setting the RECAMMASTER_MODELS_DIR environment variable.
|
| 700 |
""")
|
| 701 |
|
| 702 |
with gr.Row():
|
|
|
|
| 727 |
# Output section
|
| 728 |
output_video = gr.Video(label="Output Video")
|
| 729 |
status_output = gr.Textbox(label="Generation Status", interactive=False)
|
| 730 |
+
|
| 731 |
+
# Example videos
|
| 732 |
+
gr.Markdown("### Example Videos")
|
| 733 |
+
gr.Examples(
|
| 734 |
+
examples=[
|
| 735 |
+
[f"{TEST_DATA_DIR}/videos/case0.mp4", "A person dancing", "1"],
|
| 736 |
+
[f"{TEST_DATA_DIR}/videos/case1.mp4", "A scenic view", "5"],
|
| 737 |
+
],
|
| 738 |
+
inputs=[video_input, text_prompt, camera_type],
|
| 739 |
+
)
|
| 740 |
+
|
| 741 |
+
# Load models automatically when the interface loads
|
| 742 |
+
def on_load():
|
| 743 |
+
status = load_models()
|
| 744 |
+
return gr.update(value=status, visible=True if "Error" in status else False)
|
| 745 |
+
|
| 746 |
+
demo.load(on_load, outputs=[loading_status])
|
| 747 |
+
|
| 748 |
# Event handlers
|
| 749 |
generate_btn.click(
|
| 750 |
fn=generate_recammaster_video,
|
|
|
|
| 753 |
)
|
| 754 |
|
| 755 |
if __name__ == "__main__":
|
|
|
|
| 756 |
demo.launch(share=True)
|