-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinference.py
More file actions
126 lines (104 loc) · 4.18 KB
/
inference.py
File metadata and controls
126 lines (104 loc) · 4.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import os
import csv
import shutil
import itertools
from PIL import Image
import torch
from vans import save_video, VideoData
from vans.pipelines.wan_video_new import WanVideoPipeline, ModelConfig
from vans.trainers.utils import VideoDataset
from config import (
VDM_PRETRAINED_PATH, BASE_SAVE_PATH, CSV_FILE_PATH,
MLLM_PRETRAINED_PATH, TRAINED_CKPT_PATH, MLLM_TRAINED_CKPT_PATH
)
def setup_environment():
"""Setup CUDA environment and create directories"""
# os.environ['CUDA_VISIBLE_DEVICES'] = '0'
os.makedirs(BASE_SAVE_PATH, exist_ok=True)
def generate_prefixes():
"""Generate 5-character prefixes for file naming"""
letters = 'abcdefghijklmnopqrstuvwxyz'
for chars in itertools.product(letters, repeat=5):
yield ''.join(chars)
def initialize_pipeline():
"""Initialize the video generation pipeline"""
model_configs = [
ModelConfig(
model_id="Wan-AI/Wan2.1-T2V-1.3B",
origin_file_pattern=f"{VDM_PRETRAINED_PATH}/diffusion_pytorch_model*.safetensors",
offload_device="cpu"
),
ModelConfig(
model_id="Wan-AI/Wan2.1-T2V-1.3B",
origin_file_pattern=f"{VDM_PRETRAINED_PATH}/models_t5_umt5-xxl-enc-bf16.pth",
offload_device="cpu"
),
ModelConfig(
model_id="Wan-AI/Wan2.1-T2V-1.3B",
origin_file_pattern=f"{VDM_PRETRAINED_PATH}/Wan2.1_VAE.pth",
offload_device="cpu"
),
]
return WanVideoPipeline.from_pretrained(
torch_dtype=torch.bfloat16,
device="cuda",
model_configs=model_configs,
mllm_pretrained_path=MLLM_PRETRAINED_PATH,
use_mllm=True,
trained_ckpt_path=TRAINED_CKPT_PATH,
mllm_trained_ckpt_path=MLLM_TRAINED_CKPT_PATH
)
def process_video_row(pipe, dataset, row, prefix):
"""Process a single row from CSV and generate video"""
input_video_path = row.get('input_video_path', '')
output_video_path = row.get('\ufeffoutput_video_path', '')
eng_gt_caption = row.get('ENG_GT_Caption', '')
instructions = row.get('ENG_Instruction', '')
# Load reference video
ref_VAE_video = dataset.load_video_imageio(input_video_path)
# Generate video
text, video = pipe(
prompt=eng_gt_caption,
instructions=instructions,
input_video_path=input_video_path,
negative_prompt="vivid colors, overexposed, static, blurry details, subtitles, style, artwork, painting, frame, stationary, overall grayish, worst quality, low quality, JPEG compression artifacts, ugly, incomplete, extra fingers, poorly drawn hands, poorly drawn face, deformed, disfigured, malformed limbs, fused fingers, stationary frame, cluttered background, three legs, crowded background people, walking backwards",
seed=0,
tiled=True,
height=352,
width=640,
num_frames=33,
ref_VAE_video=ref_VAE_video,
)
# Save generated video
save_path = os.path.join(BASE_SAVE_PATH, f"{prefix}_gen.mp4")
save_video(video, save_path, fps=11, quality=5)
# Optionally copy input and output videos
# in_save_path = os.path.join(BASE_SAVE_PATH, f"{prefix}_in.mp4")
# out_save_path = os.path.join(BASE_SAVE_PATH, f"{prefix}_out.mp4")
# shutil.copy(input_video_path, in_save_path)
# shutil.copy(output_video_path, out_save_path)
def main():
"""Main execution function"""
setup_environment()
# Initialize components
dataset = VideoDataset(
height=352,
width=640,
num_frames=33,
)
pipe = initialize_pipeline()
prefix_gen = generate_prefixes()
# Process CSV file
with open(CSV_FILE_PATH, 'r') as file:
csv_reader = csv.DictReader(file)
for row_number, row in enumerate(csv_reader, 1):
prefix = next(prefix_gen)
print(f"Processing row {row_number} with prefix: {prefix}")
try:
process_video_row(pipe, dataset, row, prefix)
print(f"Successfully processed row {row_number}")
except Exception as e:
print(f"Error processing row {row_number}: {e}")
continue
if __name__ == "__main__":
main()