Views
No views yet
1sudo apt-get update && sudo apt-get install ffmpeg git-lfs
2pip install torch torchvision diffusers transformers moviepy==1.0.3 peft safetensors
3git clone https://huggingface.co/svjack/hunyuan_video_pixel_early_lora1import torch
2from diffusers import HunyuanVideoPipeline, HunyuanVideoTransformer3DModel
3from diffusers.utils import export_to_video
4from safetensors.torch import load_file
5import os
6
7def infer_video(
8 pretrained_model,
9 prompt,
10 height,
11 width,
12 num_frames,
13 num_inference_steps,
14 seed,
15 output_dir,
16 use_lora=False,
17 lora_path=None,
18 alpha=None,
19):
20 """
21 合并使用和不使用 LoRA 的视频生成函数。
22
23 参数:
24 pretrained_model (str): 预训练模型的路径。
25 prompt (str): 生成视频的提示词。
26 height (int): 生成视频的高度。
27 width (int): 生成视频的宽度。
28 num_frames (int): 生成视频的帧数。
29 num_inference_steps (int): 推断步数。
30 seed (int): 随机种子。
31 output_dir (str): 输出视频的目录。
32 use_lora (bool): 是否使用 LoRA,默认为 False。
33 lora_path (str): LoRA 文件的路径,仅在 use_lora=True 时有效。
34 alpha (int): LoRA 的 alpha 参数,仅在 use_lora=True 时有效。
35 """
36 # 加载模型
37 transformer = HunyuanVideoTransformer3DModel.from_pretrained(
38 pretrained_model,
39 subfolder="transformer",
40 torch_dtype=torch.bfloat16,
41 )
42 # 如果使用 LoRA
43 if use_lora:
44 if lora_path is None:
45 raise ValueError("lora_path must be provided when use_lora is True")
46
47 # 加载 LoRA 权重
48 lora_sd = load_file(lora_path)
49 rank = 0
50 for key in lora_sd.keys():
51 if ".lora_A.weight" in key:
52 rank = lora_sd[key].shape[0]
53
54 alpha = 1 if alpha is None else alpha
55 lora_weight = alpha / rank
56
57 print(f"lora rank = {rank}")
58 print(f"alpha = {alpha}")
59 print(f"lora weight = {lora_weight}")
60
61 # 应用 LoRA
62 transformer.load_lora_adapter(lora_sd, adapter_name="default_lora")
63 transformer.set_adapters(adapter_names="default_lora", weights=lora_weight)
64
65 pipe = HunyuanVideoPipeline.from_pretrained(pretrained_model, transformer=transformer, torch_dtype=torch.float16)
66 pipe.transformer = transformer
67
68 pipe.vae.enable_tiling(
69 tile_sample_min_height=256,
70 tile_sample_min_width=256,
71 tile_sample_min_num_frames=64,
72 tile_sample_stride_height=192,
73 tile_sample_stride_width=192,
74 tile_sample_stride_num_frames=16,
75 )
76 pipe.enable_sequential_cpu_offload()
77
78 # 进行推断
79 output = pipe(
80 prompt=prompt,
81 height=height,
82 width=width,
83 num_frames=num_frames,
84 num_inference_steps=num_inference_steps,
85 generator=torch.Generator(device="cpu").manual_seed(seed),
86 ).frames[0]
87
88 # 导出视频
89 output_filename = "output_lora.mp4" if use_lora else "output_base.mp4"
90 export_to_video(
91 output,
92 os.path.join(output_dir, output_filename),
93 fps=15,
94 )
95
96### Base
97infer_video(
98 pretrained_model="hunyuanvideo-community/HunyuanVideo",
99 prompt="The video showcases a pixel art animation featuring charming anime-style scene featuring a pink-haired girl with angel wings. She's seated at a desk, enjoying a donut while working on a laptop. The setting is a cozy, pastel-colored room with a pink chair, a milk carton, and a coffee cup. The girl's expression is one of delight as she savors her treat",
100 height=512,
101 width=512,
102 num_frames=33,
103 num_inference_steps=20,
104 seed=42,
105 output_dir="./",
106 use_lora=False,
107)
108
109### With Lora
110infer_video(
111 pretrained_model="hunyuanvideo-community/HunyuanVideo",
112 prompt="The video showcases a pixel art animation featuring a serene and majestic snowy mountain landscape. The scene is dominated by towering peaks covered in pristine white snow, with a soft gradient of blue and purple hues in the sky. A small cabin with a smoking chimney sits at the base of the mountain, surrounded by pine trees dusted with snow. A winding path leads up the mountain, with footprints visible in the snow. The atmosphere is calm and peaceful, evoking a sense of solitude and wonder.",
113 height=512,
114 width=512,
115 num_frames=33,
116 num_inference_steps=20,
117 seed=42,
118 output_dir="./",
119 use_lora=True,
120 lora_path="hunyuan_video_pixel_early_lora/hyv-lora-00000700.safetensors",
121 alpha=16,
122)