Wan2.1-Text-Vdo-Demp / generate.py
rahul7star's picture
Update generate.py
e20e208 verified
import argparse
import os
import subprocess # Ensure subprocess is imported
import torch
from huggingface_hub import snapshot_download
# Arguments
parser = argparse.ArgumentParser()
parser.add_argument("--task", type=str, default="t2v-1.3B")
parser.add_argument("--size", type=str, default="200*200")
parser.add_argument("--frame_num", type=int, default=60)
parser.add_argument("--sample_steps", type=int, default=20)
parser.add_argument("--ckpt_dir", type=str, default="./Wan2.1-T2V-1.3B")
parser.add_argument("--offload_model", type=bool, default=True, help="Whether to offload the model (True/False)")
parser.add_argument("--t5_cpu", action="store_true", help="Use CPU for T5 model (optional)")
parser.add_argument("--sample_shift", type=int, default=8, help="Sampling shift for generation")
parser.add_argument("--sample_guide_scale", type=int, default=6, help="Sampling guide scale for generation")
parser.add_argument("--prompt", type=str, required=True)
args = parser.parse_args()
# Log input parameters
print(f"Generating video with the following settings:\n"
f"Task: {args.task}\n"
f"Resolution: {args.size}\n"
f"Frames: {args.frame_num}\n"
f"Sample Steps: {args.sample_steps}\n"
f"Prompt: {args.prompt}\n"
f"Sample Shift: {args.sample_shift}\n"
f"Sample Guide Scale: {args.sample_guide_scale}\n"
f"Using T5 on CPU: {args.t5_cpu}\n"
f"Offload Model: {args.offload_model}")
# Ensure the model is downloaded
if not os.path.exists(args.ckpt_dir):
print("πŸ”„ Downloading WAN 2.1 - 1.3B model from Hugging Face...")
snapshot_download(repo_id="Wan-AI/Wan2.1-T2V-1.3B", local_dir=args.ckpt_dir)
# Free up GPU memory
if torch.cuda.is_available():
torch.cuda.empty_cache()
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
# Command to run the model generation
offload_model_value = "True" if args.offload_model else "False"
command = f"python generate.py --task t2v-1.3B --size 480832 --frame_num 1 --ckpt_dir ./Wan2.1-T2V-1.3B --offload_model True --t5_cpu --sample_shift 8 --sample_guide_scale 6 --prompt \"{args.prompt}\""
# Run the model
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = process.communicate()
# Print logs for debugging
print("πŸ”Ή Output:", stdout.decode())
print("πŸ”Ί Error:", stderr.decode())
# Verify if video was created
if os.path.exists("output.mp4"):
print("βœ… Video generated successfully: output.mp4")
else:
print("❌ Error: Video file not found!")