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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from diffusers_helper.hf_login import login
|
2 |
+
|
3 |
+
import os
|
4 |
+
import threading
|
5 |
+
import time
|
6 |
+
import requests
|
7 |
+
from requests.adapters import HTTPAdapter
|
8 |
+
from urllib3.util.retry import Retry
|
9 |
+
import json
|
10 |
+
|
11 |
+
os.environ['HF_HOME'] = os.path.abspath(os.path.realpath(os.path.join(os.path.dirname(__file__), './hf_download')))
|
12 |
+
|
13 |
+
# 添加中英双语翻译字典
|
14 |
+
translations = {
|
15 |
+
"en": {
|
16 |
+
"title": "FramePack - Image to Video Generation",
|
17 |
+
"upload_image": "Upload Image",
|
18 |
+
"prompt": "Prompt",
|
19 |
+
"quick_prompts": "Quick Prompts",
|
20 |
+
"start_generation": "Generate",
|
21 |
+
"stop_generation": "Stop",
|
22 |
+
"use_teacache": "Use TeaCache",
|
23 |
+
"teacache_info": "Faster speed, but may result in slightly worse finger and hand generation.",
|
24 |
+
"negative_prompt": "Negative Prompt",
|
25 |
+
"seed": "Seed",
|
26 |
+
"video_length": "Video Length (max 5 seconds)",
|
27 |
+
"latent_window": "Latent Window Size",
|
28 |
+
"steps": "Inference Steps",
|
29 |
+
"steps_info": "Changing this value is not recommended.",
|
30 |
+
"cfg_scale": "CFG Scale",
|
31 |
+
"distilled_cfg": "Distilled CFG Scale",
|
32 |
+
"distilled_cfg_info": "Changing this value is not recommended.",
|
33 |
+
"cfg_rescale": "CFG Rescale",
|
34 |
+
"gpu_memory": "GPU Memory Preservation (GB) (larger means slower)",
|
35 |
+
"gpu_memory_info": "Set this to a larger value if you encounter OOM errors. Larger values cause slower speed.",
|
36 |
+
"next_latents": "Next Latents",
|
37 |
+
"generated_video": "Generated Video",
|
38 |
+
"sampling_note": "Note: Due to reversed sampling, ending actions will be generated before starting actions. If the starting action is not in the video, please wait, it will be generated later.",
|
39 |
+
"error_message": "Error",
|
40 |
+
"processing_error": "Processing error",
|
41 |
+
"network_error": "Network connection is unstable, model download timed out. Please try again later.",
|
42 |
+
"memory_error": "GPU memory insufficient, please try increasing GPU memory preservation value or reduce video length.",
|
43 |
+
"model_error": "Failed to load model, possibly due to network issues or high server load. Please try again later.",
|
44 |
+
"partial_video": "Processing error, but partial video has been generated",
|
45 |
+
"processing_interrupt": "Processing was interrupted, but partial video has been generated"
|
46 |
+
},
|
47 |
+
"zh": {
|
48 |
+
"title": "FramePack - 图像到视频生成",
|
49 |
+
"upload_image": "上传图像",
|
50 |
+
"prompt": "提示词",
|
51 |
+
"quick_prompts": "快速提示词列表",
|
52 |
+
"start_generation": "开始生成",
|
53 |
+
"stop_generation": "结束生成",
|
54 |
+
"use_teacache": "使用TeaCache",
|
55 |
+
"teacache_info": "速度更快,但可能会使手指和手的生成效果稍差。",
|
56 |
+
"negative_prompt": "负面提示词",
|
57 |
+
"seed": "随机种子",
|
58 |
+
"video_length": "视频长度(最大5秒)",
|
59 |
+
"latent_window": "潜在窗口大小",
|
60 |
+
"steps": "推理步数",
|
61 |
+
"steps_info": "不建议修改此值。",
|
62 |
+
"cfg_scale": "CFG Scale",
|
63 |
+
"distilled_cfg": "蒸馏CFG比例",
|
64 |
+
"distilled_cfg_info": "不建议修改此值。",
|
65 |
+
"cfg_rescale": "CFG重缩放",
|
66 |
+
"gpu_memory": "GPU推理保留内存(GB)(值越大速度越慢)",
|
67 |
+
"gpu_memory_info": "如果出现OOM错误,请将此值设置得更大。值越大,速度越慢。",
|
68 |
+
"next_latents": "下一批潜变量",
|
69 |
+
"generated_video": "生成的视频",
|
70 |
+
"sampling_note": "注意:由于采样是倒序的,结束动作将在开始动作之前生成。如果视频中没有出现起始动作,请继续等待,它将在稍后生成。",
|
71 |
+
"error_message": "错误信息",
|
72 |
+
"processing_error": "处理过程出错",
|
73 |
+
"network_error": "网络连接不稳定,模型下载超时。请稍后再试。",
|
74 |
+
"memory_error": "GPU内存不足,请尝试增加GPU推理保留内存值或降低视频长度。",
|
75 |
+
"model_error": "模型加载失败,可能是网络问题或服务器负载过高。请稍后再试。",
|
76 |
+
"partial_video": "处理过程中出现错误,但已生成部分视频",
|
77 |
+
"processing_interrupt": "处理过程中断,但已生成部分视频"
|
78 |
+
}
|
79 |
+
}
|
80 |
+
|
81 |
+
# 语言切换功能
|
82 |
+
def get_translation(key, lang="en"):
|
83 |
+
if lang in translations and key in translations[lang]:
|
84 |
+
return translations[lang][key]
|
85 |
+
# 默认返回英文
|
86 |
+
return translations["en"].get(key, key)
|
87 |
+
|
88 |
+
# 默认语言设置
|
89 |
+
current_language = "en"
|
90 |
+
|
91 |
+
# 切换语言函数
|
92 |
+
def switch_language():
|
93 |
+
global current_language
|
94 |
+
current_language = "zh" if current_language == "en" else "en"
|
95 |
+
return current_language
|
96 |
+
|
97 |
+
import gradio as gr
|
98 |
+
import torch
|
99 |
+
import traceback
|
100 |
+
import einops
|
101 |
+
import safetensors.torch as sf
|
102 |
+
import numpy as np
|
103 |
+
import math
|
104 |
+
|
105 |
+
# 检查是否在Hugging Face Space环境中
|
106 |
+
IN_HF_SPACE = os.environ.get('SPACE_ID') is not None
|
107 |
+
|
108 |
+
# 添加变量跟踪GPU可用性
|
109 |
+
GPU_AVAILABLE = False
|
110 |
+
GPU_INITIALIZED = False
|
111 |
+
last_update_time = time.time()
|
112 |
+
|
113 |
+
# 如果在Hugging Face Space中,导入spaces模块
|
114 |
+
if IN_HF_SPACE:
|
115 |
+
try:
|
116 |
+
import spaces
|
117 |
+
print("在Hugging Face Space环境中运行,已导入spaces模块")
|
118 |
+
|
119 |
+
# 检查GPU可用性
|
120 |
+
try:
|
121 |
+
GPU_AVAILABLE = torch.cuda.is_available()
|
122 |
+
print(f"GPU available: {GPU_AVAILABLE}")
|
123 |
+
if GPU_AVAILABLE:
|
124 |
+
print(f"GPU device name: {torch.cuda.get_device_name(0)}")
|
125 |
+
print(f"GPU memory: {torch.cuda.get_device_properties(0).total_memory / 1e9} GB")
|
126 |
+
|
127 |
+
# 尝试进行小型GPU操作,确认GPU实际可用
|
128 |
+
test_tensor = torch.zeros(1, device='cuda')
|
129 |
+
test_tensor = test_tensor + 1
|
130 |
+
del test_tensor
|
131 |
+
print("成功进行GPU测试操作")
|
132 |
+
else:
|
133 |
+
print("警告: CUDA报告可用,但未检测到GPU设备")
|
134 |
+
except Exception as e:
|
135 |
+
GPU_AVAILABLE = False
|
136 |
+
print(f"检查GPU时出错: {e}")
|
137 |
+
print("将使用CPU模式运行")
|
138 |
+
except ImportError:
|
139 |
+
print("未能导入spaces模块,可能不在Hugging Face Space环境中")
|
140 |
+
GPU_AVAILABLE = torch.cuda.is_available()
|
141 |
+
|
142 |
+
from PIL import Image
|
143 |
+
from diffusers import AutoencoderKLHunyuanVideo
|
144 |
+
from transformers import LlamaModel, CLIPTextModel, LlamaTokenizerFast, CLIPTokenizer
|
145 |
+
from diffusers_helper.hunyuan import encode_prompt_conds, vae_decode, vae_encode, vae_decode_fake
|
146 |
+
from diffusers_helper.utils import save_bcthw_as_mp4, crop_or_pad_yield_mask, soft_append_bcthw, resize_and_center_crop, state_dict_weighted_merge, state_dict_offset_merge, generate_timestamp
|
147 |
+
from diffusers_helper.models.hunyuan_video_packed import HunyuanVideoTransformer3DModelPacked
|
148 |
+
from diffusers_helper.pipelines.k_diffusion_hunyuan import sample_hunyuan
|
149 |
+
from diffusers_helper.memory import cpu, gpu, get_cuda_free_memory_gb, move_model_to_device_with_memory_preservation, offload_model_from_device_for_memory_preservation, fake_diffusers_current_device, DynamicSwapInstaller, unload_complete_models, load_model_as_complete, IN_HF_SPACE as MEMORY_IN_HF_SPACE
|
150 |
+
from diffusers_helper.thread_utils import AsyncStream, async_run
|
151 |
+
from diffusers_helper.gradio.progress_bar import make_progress_bar_css, make_progress_bar_html
|
152 |
+
from transformers import SiglipImageProcessor, SiglipVisionModel
|
153 |
+
from diffusers_helper.clip_vision import hf_clip_vision_encode
|
154 |
+
from diffusers_helper.bucket_tools import find_nearest_bucket
|
155 |
+
|
156 |
+
outputs_folder = './outputs/'
|
157 |
+
os.makedirs(outputs_folder, exist_ok=True)
|
158 |
+
|
159 |
+
# 在Spaces环境中,我们延迟所有CUDA操作
|
160 |
+
if not IN_HF_SPACE:
|
161 |
+
# 仅在非Spaces环境中获取CUDA内存
|
162 |
+
try:
|
163 |
+
if torch.cuda.is_available():
|
164 |
+
free_mem_gb = get_cuda_free_memory_gb(gpu)
|
165 |
+
print(f'Free VRAM {free_mem_gb} GB')
|
166 |
+
else:
|
167 |
+
free_mem_gb = 6.0 # 默认值
|
168 |
+
print("CUDA不可用,使用默认的内存设置")
|
169 |
+
except Exception as e:
|
170 |
+
free_mem_gb = 6.0 # 默认值
|
171 |
+
print(f"获取CUDA内存时出错: {e},使用默认的内存设置")
|
172 |
+
|
173 |
+
high_vram = free_mem_gb > 60
|
174 |
+
print(f'High-VRAM Mode: {high_vram}')
|
175 |
+
else:
|
176 |
+
# 在Spaces环境中使用默认值
|
177 |
+
print("在Spaces环境中使用默认内存设置")
|
178 |
+
try:
|
179 |
+
if GPU_AVAILABLE:
|
180 |
+
free_mem_gb = torch.cuda.get_device_properties(0).total_memory / 1e9 * 0.9 # 使用90%的GPU内存
|
181 |
+
high_vram = free_mem_gb > 10 # 更保守的条件
|
182 |
+
else:
|
183 |
+
free_mem_gb = 6.0 # 默认值
|
184 |
+
high_vram = False
|
185 |
+
except Exception as e:
|
186 |
+
print(f"获取GPU内存时出错: {e}")
|
187 |
+
free_mem_gb = 6.0 # 默认值
|
188 |
+
high_vram = False
|
189 |
+
|
190 |
+
print(f'GPU内存: {free_mem_gb:.2f} GB, High-VRAM Mode: {high_vram}')
|
191 |
+
|
192 |
+
# 使用models变量存储全局模型引用
|
193 |
+
models = {}
|
194 |
+
cpu_fallback_mode = not GPU_AVAILABLE # 如果GPU不可用,使用CPU回退模式
|
195 |
+
|
196 |
+
# 使用加载模型的函数
|
197 |
+
def load_models():
|
198 |
+
global models, cpu_fallback_mode, GPU_INITIALIZED
|
199 |
+
|
200 |
+
if GPU_INITIALIZED:
|
201 |
+
print("模型已加载,跳过重复加载")
|
202 |
+
return models
|
203 |
+
|
204 |
+
print("开始加载模型...")
|
205 |
+
|
206 |
+
try:
|
207 |
+
# 设置设备,根据GPU可用性确定
|
208 |
+
device = 'cuda' if GPU_AVAILABLE and not cpu_fallback_mode else 'cpu'
|
209 |
+
model_device = 'cpu' # 初始加载到CPU
|
210 |
+
|
211 |
+
# 降低精度以节省内存
|
212 |
+
dtype = torch.float16 if GPU_AVAILABLE else torch.float32
|
213 |
+
transformer_dtype = torch.bfloat16 if GPU_AVAILABLE else torch.float32
|
214 |
+
|
215 |
+
print(f"使用设备: {device}, 模型精度: {dtype}, Transformer精度: {transformer_dtype}")
|
216 |
+
|
217 |
+
# 加载模型
|
218 |
+
try:
|
219 |
+
text_encoder = LlamaModel.from_pretrained("hunyuanvideo-community/HunyuanVideo", subfolder='text_encoder', torch_dtype=dtype).to(model_device)
|
220 |
+
text_encoder_2 = CLIPTextModel.from_pretrained("hunyuanvideo-community/HunyuanVideo", subfolder='text_encoder_2', torch_dtype=dtype).to(model_device)
|
221 |
+
tokenizer = LlamaTokenizerFast.from_pretrained("hunyuanvideo-community/HunyuanVideo", subfolder='tokenizer')
|
222 |
+
tokenizer_2 = CLIPTokenizer.from_pretrained("hunyuanvideo-community/HunyuanVideo", subfolder='tokenizer_2')
|
223 |
+
vae = AutoencoderKLHunyuanVideo.from_pretrained("hunyuanvideo-community/HunyuanVideo", subfolder='vae', torch_dtype=dtype).to(model_device)
|
224 |
+
|
225 |
+
feature_extractor = SiglipImageProcessor.from_pretrained("lllyasviel/flux_redux_bfl", subfolder='feature_extractor')
|
226 |
+
image_encoder = SiglipVisionModel.from_pretrained("lllyasviel/flux_redux_bfl", subfolder='image_encoder', torch_dtype=dtype).to(model_device)
|
227 |
+
|
228 |
+
transformer = HunyuanVideoTransformer3DModelPacked.from_pretrained('lllyasviel/FramePackI2V_HY', torch_dtype=transformer_dtype).to(model_device)
|
229 |
+
|
230 |
+
print("成功加载所有模型")
|
231 |
+
except Exception as e:
|
232 |
+
print(f"加载模型时出错: {e}")
|
233 |
+
print("尝试降低精度重新加载...")
|
234 |
+
|
235 |
+
# 降低精度重试
|
236 |
+
dtype = torch.float32
|
237 |
+
transformer_dtype = torch.float32
|
238 |
+
cpu_fallback_mode = True
|
239 |
+
|
240 |
+
text_encoder = LlamaModel.from_pretrained("hunyuanvideo-community/HunyuanVideo", subfolder='text_encoder', torch_dtype=dtype).to('cpu')
|
241 |
+
text_encoder_2 = CLIPTextModel.from_pretrained("hunyuanvideo-community/HunyuanVideo", subfolder='text_encoder_2', torch_dtype=dtype).to('cpu')
|
242 |
+
tokenizer = LlamaTokenizerFast.from_pretrained("hunyuanvideo-community/HunyuanVideo", subfolder='tokenizer')
|
243 |
+
tokenizer_2 = CLIPTokenizer.from_pretrained("hunyuanvideo-community/HunyuanVideo", subfolder='tokenizer_2')
|
244 |
+
vae = AutoencoderKLHunyuanVideo.from_pretrained("hunyuanvideo-community/HunyuanVideo", subfolder='vae', torch_dtype=dtype).to('cpu')
|
245 |
+
|
246 |
+
feature_extractor = SiglipImageProcessor.from_pretrained("lllyasviel/flux_redux_bfl", subfolder='feature_extractor')
|
247 |
+
image_encoder = SiglipVisionModel.from_pretrained("lllyasviel/flux_redux_bfl", subfolder='image_encoder', torch_dtype=dtype).to('cpu')
|
248 |
+
|
249 |
+
transformer = HunyuanVideoTransformer3DModelPacked.from_pretrained('lllyasviel/FramePackI2V_HY', torch_dtype=transformer_dtype).to('cpu')
|
250 |
+
|
251 |
+
print("使用CPU模式成功加载所有模型")
|
252 |
+
|
253 |
+
vae.eval()
|
254 |
+
text_encoder.eval()
|
255 |
+
text_encoder_2.eval()
|
256 |
+
image_encoder.eval()
|
257 |
+
transformer.eval()
|
258 |
+
|
259 |
+
if not high_vram or cpu_fallback_mode:
|
260 |
+
vae.enable_slicing()
|
261 |
+
vae.enable_tiling()
|
262 |
+
|
263 |
+
transformer.high_quality_fp32_output_for_inference = True
|
264 |
+
print('transformer.high_quality_fp32_output_for_inference = True')
|
265 |
+
|
266 |
+
# 设置模型精度
|
267 |
+
if not cpu_fallback_mode:
|
268 |
+
transformer.to(dtype=transformer_dtype)
|
269 |
+
vae.to(dtype=dtype)
|
270 |
+
image_encoder.to(dtype=dtype)
|
271 |
+
text_encoder.to(dtype=dtype)
|
272 |
+
text_encoder_2.to(dtype=dtype)
|
273 |
+
|
274 |
+
vae.requires_grad_(False)
|
275 |
+
text_encoder.requires_grad_(False)
|
276 |
+
text_encoder_2.requires_grad_(False)
|
277 |
+
image_encoder.requires_grad_(False)
|
278 |
+
transformer.requires_grad_(False)
|
279 |
+
|
280 |
+
if torch.cuda.is_available() and not cpu_fallback_mode:
|
281 |
+
try:
|
282 |
+
if not high_vram:
|
283 |
+
# DynamicSwapInstaller is same as huggingface's enable_sequential_offload but 3x faster
|
284 |
+
DynamicSwapInstaller.install_model(transformer, device=device)
|
285 |
+
DynamicSwapInstaller.install_model(text_encoder, device=device)
|
286 |
+
else:
|
287 |
+
text_encoder.to(device)
|
288 |
+
text_encoder_2.to(device)
|
289 |
+
image_encoder.to(device)
|
290 |
+
vae.to(device)
|
291 |
+
transformer.to(device)
|
292 |
+
print(f"成功将模型移动到{device}设备")
|
293 |
+
except Exception as e:
|
294 |
+
print(f"移动模型到{device}时出错: {e}")
|
295 |
+
print("回退到CPU模式")
|
296 |
+
cpu_fallback_mode = True
|
297 |
+
|
298 |
+
# 保存到全局变量
|
299 |
+
models = {
|
300 |
+
'text_encoder': text_encoder,
|
301 |
+
'text_encoder_2': text_encoder_2,
|
302 |
+
'tokenizer': tokenizer,
|
303 |
+
'tokenizer_2': tokenizer_2,
|
304 |
+
'vae': vae,
|
305 |
+
'feature_extractor': feature_extractor,
|
306 |
+
'image_encoder': image_encoder,
|
307 |
+
'transformer': transformer
|
308 |
+
}
|
309 |
+
|
310 |
+
GPU_INITIALIZED = True
|
311 |
+
print(f"模型加载完成,运行模式: {'CPU' if cpu_fallback_mode else 'GPU'}")
|
312 |
+
return models
|
313 |
+
except Exception as e:
|
314 |
+
print(f"加载模型过程中发生错误: {e}")
|
315 |
+
traceback.print_exc()
|
316 |
+
|
317 |
+
# 记录更详细的错误信息
|
318 |
+
error_info = {
|
319 |
+
"error": str(e),
|
320 |
+
"traceback": traceback.format_exc(),
|
321 |
+
"cuda_available": torch.cuda.is_available(),
|
322 |
+
"device": "cpu" if cpu_fallback_mode else "cuda",
|
323 |
+
}
|
324 |
+
|
325 |
+
# 保存错误信息到文件,方便排查
|
326 |
+
try:
|
327 |
+
with open(os.path.join(outputs_folder, "error_log.txt"), "w") as f:
|
328 |
+
f.write(str(error_info))
|
329 |
+
except:
|
330 |
+
pass
|
331 |
+
|
332 |
+
# 返回空字典,允许应用继续尝试运行
|
333 |
+
cpu_fallback_mode = True
|
334 |
+
return {}
|
335 |
+
|
336 |
+
|
337 |
+
# 使用Hugging Face Spaces GPU装饰器
|
338 |
+
if IN_HF_SPACE and 'spaces' in globals() and GPU_AVAILABLE:
|
339 |
+
try:
|
340 |
+
@spaces.GPU
|
341 |
+
def initialize_models():
|
342 |
+
"""在@spaces.GPU装饰器内初始化模型"""
|
343 |
+
global GPU_INITIALIZED
|
344 |
+
try:
|
345 |
+
result = load_models()
|
346 |
+
GPU_INITIALIZED = True
|
347 |
+
return result
|
348 |
+
except Exception as e:
|
349 |
+
print(f"使用spaces.GPU初始化模型时出错: {e}")
|
350 |
+
traceback.print_exc()
|
351 |
+
global cpu_fallback_mode
|
352 |
+
cpu_fallback_mode = True
|
353 |
+
# 不使用装饰器再次尝试
|
354 |
+
return load_models()
|
355 |
+
except Exception as e:
|
356 |
+
print(f"创建spaces.GPU装饰器时出错: {e}")
|
357 |
+
# 如果装饰器出错,直接使用非装饰器版本
|
358 |
+
def initialize_models():
|
359 |
+
return load_models()
|
360 |
+
|
361 |
+
|
362 |
+
# 以下函数内部会延迟获取模型
|
363 |
+
def get_models():
|
364 |
+
"""获取模型,如果尚未加载则加载模型"""
|
365 |
+
global models, GPU_INITIALIZED
|
366 |
+
|
367 |
+
# 添加模型加载锁,防止并发加载
|
368 |
+
model_loading_key = "__model_loading__"
|
369 |
+
|
370 |
+
if not models:
|
371 |
+
# 检查是否正在加载模型
|
372 |
+
if model_loading_key in globals():
|
373 |
+
print("模型正在加载中,等待...")
|
374 |
+
# 等待模型加载完成
|
375 |
+
import time
|
376 |
+
start_wait = time.time()
|
377 |
+
while not models and model_loading_key in globals():
|
378 |
+
time.sleep(0.5)
|
379 |
+
# 超过60秒认为加载失败
|
380 |
+
if time.time() - start_wait > 60:
|
381 |
+
print("等待模型加载超时")
|
382 |
+
break
|
383 |
+
|
384 |
+
if models:
|
385 |
+
return models
|
386 |
+
|
387 |
+
try:
|
388 |
+
# 设置加载标记
|
389 |
+
globals()[model_loading_key] = True
|
390 |
+
|
391 |
+
if IN_HF_SPACE and 'spaces' in globals() and GPU_AVAILABLE and not cpu_fallback_mode:
|
392 |
+
try:
|
393 |
+
print("使用@spaces.GPU装饰器加载模型")
|
394 |
+
models = initialize_models()
|
395 |
+
except Exception as e:
|
396 |
+
print(f"使用GPU装饰器加载模型失败: {e}")
|
397 |
+
print("尝试直接加载模型")
|
398 |
+
models = load_models()
|
399 |
+
else:
|
400 |
+
print("直接加载模型")
|
401 |
+
models = load_models()
|
402 |
+
except Exception as e:
|
403 |
+
print(f"加载模型时发生未预期的错误: {e}")
|
404 |
+
traceback.print_exc()
|
405 |
+
# 确保有一个空字典
|
406 |
+
models = {}
|
407 |
+
finally:
|
408 |
+
# 无论成功与否,都移除加载标记
|
409 |
+
if model_loading_key in globals():
|
410 |
+
del globals()[model_loading_key]
|
411 |
+
|
412 |
+
return models
|
413 |
+
|
414 |
+
|
415 |
+
stream = AsyncStream()
|
416 |
+
|
417 |
+
|
418 |
+
@torch.no_grad()
|
419 |
+
def worker(input_image, prompt, n_prompt, seed, total_second_length, latent_window_size, steps, cfg, gs, rs, gpu_memory_preservation, use_teacache):
|
420 |
+
global last_update_time
|
421 |
+
last_update_time = time.time()
|
422 |
+
|
423 |
+
# 限制视频长度不超过5秒
|
424 |
+
total_second_length = min(total_second_length, 5.0)
|
425 |
+
|
426 |
+
# 获取模型
|
427 |
+
try:
|
428 |
+
models = get_models()
|
429 |
+
if not models:
|
430 |
+
error_msg = "模型加载失败,请检查日志获取详细信息"
|
431 |
+
print(error_msg)
|
432 |
+
stream.output_queue.push(('error', error_msg))
|
433 |
+
stream.output_queue.push(('end', None))
|
434 |
+
return
|
435 |
+
|
436 |
+
text_encoder = models['text_encoder']
|
437 |
+
text_encoder_2 = models['text_encoder_2']
|
438 |
+
tokenizer = models['tokenizer']
|
439 |
+
tokenizer_2 = models['tokenizer_2']
|
440 |
+
vae = models['vae']
|
441 |
+
feature_extractor = models['feature_extractor']
|
442 |
+
image_encoder = models['image_encoder']
|
443 |
+
transformer = models['transformer']
|
444 |
+
except Exception as e:
|
445 |
+
error_msg = f"获取模型时出错: {e}"
|
446 |
+
print(error_msg)
|
447 |
+
traceback.print_exc()
|
448 |
+
stream.output_queue.push(('error', error_msg))
|
449 |
+
stream.output_queue.push(('end', None))
|
450 |
+
return
|
451 |
+
|
452 |
+
# 确定设备
|
453 |
+
device = 'cuda' if GPU_AVAILABLE and not cpu_fallback_mode else 'cpu'
|
454 |
+
print(f"使用设备: {device} 进行推理")
|
455 |
+
|
456 |
+
# 调整参数以适应CPU模式
|
457 |
+
if cpu_fallback_mode:
|
458 |
+
print("CPU模式下使用更精简的参数")
|
459 |
+
# 减小处理大小以加快CPU处理
|
460 |
+
latent_window_size = min(latent_window_size, 5)
|
461 |
+
steps = min(steps, 15) # 减少步数
|
462 |
+
total_second_length = min(total_second_length, 2.0) # CPU模式下进一步限制视频长度
|
463 |
+
|
464 |
+
total_latent_sections = (total_second_length * 30) / (latent_window_size * 4)
|
465 |
+
total_latent_sections = int(max(round(total_latent_sections), 1))
|
466 |
+
|
467 |
+
job_id = generate_timestamp()
|
468 |
+
last_output_filename = None
|
469 |
+
history_pixels = None
|
470 |
+
history_latents = None
|
471 |
+
total_generated_latent_frames = 0
|
472 |
+
|
473 |
+
stream.output_queue.push(('progress', (None, '', make_progress_bar_html(0, 'Starting ...'))))
|
474 |
+
|
475 |
+
try:
|
476 |
+
# Clean GPU
|
477 |
+
if not high_vram and not cpu_fallback_mode:
|
478 |
+
try:
|
479 |
+
unload_complete_models(
|
480 |
+
text_encoder, text_encoder_2, image_encoder, vae, transformer
|
481 |
+
)
|
482 |
+
except Exception as e:
|
483 |
+
print(f"卸载模型时出错: {e}")
|
484 |
+
# 继续执行,不中断流程
|
485 |
+
|
486 |
+
# Text encoding
|
487 |
+
last_update_time = time.time()
|
488 |
+
stream.output_queue.push(('progress', (None, '', make_progress_bar_html(0, 'Text encoding ...'))))
|
489 |
+
|
490 |
+
try:
|
491 |
+
if not high_vram and not cpu_fallback_mode:
|
492 |
+
fake_diffusers_current_device(text_encoder, device)
|
493 |
+
load_model_as_complete(text_encoder_2, target_device=device)
|
494 |
+
|
495 |
+
llama_vec, clip_l_pooler = encode_prompt_conds(prompt, text_encoder, text_encoder_2, tokenizer, tokenizer_2)
|
496 |
+
|
497 |
+
if cfg == 1:
|
498 |
+
llama_vec_n, clip_l_pooler_n = torch.zeros_like(llama_vec), torch.zeros_like(clip_l_pooler)
|
499 |
+
else:
|
500 |
+
llama_vec_n, clip_l_pooler_n = encode_prompt_conds(n_prompt, text_encoder, text_encoder_2, tokenizer, tokenizer_2)
|
501 |
+
|
502 |
+
llama_vec, llama_attention_mask = crop_or_pad_yield_mask(llama_vec, length=512)
|
503 |
+
llama_vec_n, llama_attention_mask_n = crop_or_pad_yield_mask(llama_vec_n, length=512)
|
504 |
+
except Exception as e:
|
505 |
+
error_msg = f"文本编码过程出错: {e}"
|
506 |
+
print(error_msg)
|
507 |
+
traceback.print_exc()
|
508 |
+
stream.output_queue.push(('error', error_msg))
|
509 |
+
stream.output_queue.push(('end', None))
|
510 |
+
return
|
511 |
+
|
512 |
+
# Processing input image
|
513 |
+
last_update_time = time.time()
|
514 |
+
stream.output_queue.push(('progress', (None, '', make_progress_bar_html(0, 'Image processing ...'))))
|
515 |
+
|
516 |
+
try:
|
517 |
+
H, W, C = input_image.shape
|
518 |
+
height, width = find_nearest_bucket(H, W, resolution=640)
|
519 |
+
|
520 |
+
# 如果是CPU模式,缩小处理尺寸
|
521 |
+
if cpu_fallback_mode:
|
522 |
+
height = min(height, 320)
|
523 |
+
width = min(width, 320)
|
524 |
+
|
525 |
+
input_image_np = resize_and_center_crop(input_image, target_width=width, target_height=height)
|
526 |
+
|
527 |
+
Image.fromarray(input_image_np).save(os.path.join(outputs_folder, f'{job_id}.png'))
|
528 |
+
|
529 |
+
input_image_pt = torch.from_numpy(input_image_np).float() / 127.5 - 1
|
530 |
+
input_image_pt = input_image_pt.permute(2, 0, 1)[None, :, None]
|
531 |
+
except Exception as e:
|
532 |
+
error_msg = f"图像处理过程出错: {e}"
|
533 |
+
print(error_msg)
|
534 |
+
traceback.print_exc()
|
535 |
+
stream.output_queue.push(('error', error_msg))
|
536 |
+
stream.output_queue.push(('end', None))
|
537 |
+
return
|
538 |
+
|
539 |
+
# VAE encoding
|
540 |
+
last_update_time = time.time()
|
541 |
+
stream.output_queue.push(('progress', (None, '', make_progress_bar_html(0, 'VAE encoding ...'))))
|
542 |
+
|
543 |
+
try:
|
544 |
+
if not high_vram and not cpu_fallback_mode:
|
545 |
+
load_model_as_complete(vae, target_device=device)
|
546 |
+
|
547 |
+
start_latent = vae_encode(input_image_pt, vae)
|
548 |
+
except Exception as e:
|
549 |
+
error_msg = f"VAE编码过程出错: {e}"
|
550 |
+
print(error_msg)
|
551 |
+
traceback.print_exc()
|
552 |
+
stream.output_queue.push(('error', error_msg))
|
553 |
+
stream.output_queue.push(('end', None))
|
554 |
+
return
|
555 |
+
|
556 |
+
# CLIP Vision
|
557 |
+
last_update_time = time.time()
|
558 |
+
stream.output_queue.push(('progress', (None, '', make_progress_bar_html(0, 'CLIP Vision encoding ...'))))
|
559 |
+
|
560 |
+
try:
|
561 |
+
if not high_vram and not cpu_fallback_mode:
|
562 |
+
load_model_as_complete(image_encoder, target_device=device)
|
563 |
+
|
564 |
+
image_encoder_output = hf_clip_vision_encode(input_image_np, feature_extractor, image_encoder)
|
565 |
+
image_encoder_last_hidden_state = image_encoder_output.last_hidden_state
|
566 |
+
except Exception as e:
|
567 |
+
error_msg = f"CLIP Vision编码过程出错: {e}"
|
568 |
+
print(error_msg)
|
569 |
+
traceback.print_exc()
|
570 |
+
stream.output_queue.push(('error', error_msg))
|
571 |
+
stream.output_queue.push(('end', None))
|
572 |
+
return
|
573 |
+
|
574 |
+
# Dtype
|
575 |
+
try:
|
576 |
+
llama_vec = llama_vec.to(transformer.dtype)
|
577 |
+
llama_vec_n = llama_vec_n.to(transformer.dtype)
|
578 |
+
clip_l_pooler = clip_l_pooler.to(transformer.dtype)
|
579 |
+
clip_l_pooler_n = clip_l_pooler_n.to(transformer.dtype)
|
580 |
+
image_encoder_last_hidden_state = image_encoder_last_hidden_state.to(transformer.dtype)
|
581 |
+
except Exception as e:
|
582 |
+
error_msg = f"数据类型转换出错: {e}"
|
583 |
+
print(error_msg)
|
584 |
+
traceback.print_exc()
|
585 |
+
stream.output_queue.push(('error', error_msg))
|
586 |
+
stream.output_queue.push(('end', None))
|
587 |
+
return
|
588 |
+
|
589 |
+
# Sampling
|
590 |
+
last_update_time = time.time()
|
591 |
+
stream.output_queue.push(('progress', (None, '', make_progress_bar_html(0, 'Start sampling ...'))))
|
592 |
+
|
593 |
+
rnd = torch.Generator("cpu").manual_seed(seed)
|
594 |
+
num_frames = latent_window_size * 4 - 3
|
595 |
+
|
596 |
+
try:
|
597 |
+
history_latents = torch.zeros(size=(1, 16, 1 + 2 + 16, height // 8, width // 8), dtype=torch.float32).cpu()
|
598 |
+
history_pixels = None
|
599 |
+
total_generated_latent_frames = 0
|
600 |
+
except Exception as e:
|
601 |
+
error_msg = f"初始化历史状态出错: {e}"
|
602 |
+
print(error_msg)
|
603 |
+
traceback.print_exc()
|
604 |
+
stream.output_queue.push(('error', error_msg))
|
605 |
+
stream.output_queue.push(('end', None))
|
606 |
+
return
|
607 |
+
|
608 |
+
latent_paddings = reversed(range(total_latent_sections))
|
609 |
+
|
610 |
+
if total_latent_sections > 4:
|
611 |
+
# In theory the latent_paddings should follow the above sequence, but it seems that duplicating some
|
612 |
+
# items looks better than expanding it when total_latent_sections > 4
|
613 |
+
# One can try to remove below trick and just
|
614 |
+
# use `latent_paddings = list(reversed(range(total_latent_sections)))` to compare
|
615 |
+
latent_paddings = [3] + [2] * (total_latent_sections - 3) + [1, 0]
|
616 |
+
|
617 |
+
for latent_padding in latent_paddings:
|
618 |
+
last_update_time = time.time()
|
619 |
+
is_last_section = latent_padding == 0
|
620 |
+
latent_padding_size = latent_padding * latent_window_size
|
621 |
+
|
622 |
+
if stream.input_queue.top() == 'end':
|
623 |
+
# 确保在结束时保存当前的视频
|
624 |
+
if history_pixels is not None and total_generated_latent_frames > 0:
|
625 |
+
try:
|
626 |
+
output_filename = os.path.join(outputs_folder, f'{job_id}_final_{total_generated_latent_frames}.mp4')
|
627 |
+
save_bcthw_as_mp4(history_pixels, output_filename, fps=30)
|
628 |
+
stream.output_queue.push(('file', output_filename))
|
629 |
+
except Exception as e:
|
630 |
+
print(f"保存最终视频时出错: {e}")
|
631 |
+
|
632 |
+
stream.output_queue.push(('end', None))
|
633 |
+
return
|
634 |
+
|
635 |
+
print(f'latent_padding_size = {latent_padding_size}, is_last_section = {is_last_section}')
|
636 |
+
|
637 |
+
try:
|
638 |
+
indices = torch.arange(0, sum([1, latent_padding_size, latent_window_size, 1, 2, 16])).unsqueeze(0)
|
639 |
+
clean_latent_indices_pre, blank_indices, latent_indices, clean_latent_indices_post, clean_latent_2x_indices, clean_latent_4x_indices = indices.split([1, latent_padding_size, latent_window_size, 1, 2, 16], dim=1)
|
640 |
+
clean_latent_indices = torch.cat([clean_latent_indices_pre, clean_latent_indices_post], dim=1)
|
641 |
+
|
642 |
+
clean_latents_pre = start_latent.to(history_latents)
|
643 |
+
clean_latents_post, clean_latents_2x, clean_latents_4x = history_latents[:, :, :1 + 2 + 16, :, :].split([1, 2, 16], dim=2)
|
644 |
+
clean_latents = torch.cat([clean_latents_pre, clean_latents_post], dim=2)
|
645 |
+
except Exception as e:
|
646 |
+
error_msg = f"准备采样数据时出错: {e}"
|
647 |
+
print(error_msg)
|
648 |
+
traceback.print_exc()
|
649 |
+
# 尝试继续下一轮迭代而不是完全终止
|
650 |
+
if last_output_filename:
|
651 |
+
stream.output_queue.push(('file', last_output_filename))
|
652 |
+
continue
|
653 |
+
|
654 |
+
if not high_vram and not cpu_fallback_mode:
|
655 |
+
try:
|
656 |
+
unload_complete_models()
|
657 |
+
move_model_to_device_with_memory_preservation(transformer, target_device=device, preserved_memory_gb=gpu_memory_preservation)
|
658 |
+
except Exception as e:
|
659 |
+
print(f"移动transformer到GPU时出错: {e}")
|
660 |
+
# 继续执行,可能影响性能但不必终止
|
661 |
+
|
662 |
+
if use_teacache and not cpu_fallback_mode:
|
663 |
+
try:
|
664 |
+
transformer.initialize_teacache(enable_teacache=True, num_steps=steps)
|
665 |
+
except Exception as e:
|
666 |
+
print(f"初始化teacache时出错: {e}")
|
667 |
+
# 禁用teacache并继续
|
668 |
+
transformer.initialize_teacache(enable_teacache=False)
|
669 |
+
else:
|
670 |
+
transformer.initialize_teacache(enable_teacache=False)
|
671 |
+
|
672 |
+
def callback(d):
|
673 |
+
global last_update_time
|
674 |
+
last_update_time = time.time()
|
675 |
+
|
676 |
+
try:
|
677 |
+
# 首先检查是否有停止信号
|
678 |
+
print(f"【调试】回调函数: 步骤 {d['i']}, 检查是否有停止信号")
|
679 |
+
try:
|
680 |
+
queue_top = stream.input_queue.top()
|
681 |
+
print(f"【调试】回调函数: 队列顶部信号 = {queue_top}")
|
682 |
+
|
683 |
+
if queue_top == 'end':
|
684 |
+
print("【调试】回调函数: 检测到停止信号,准备中断...")
|
685 |
+
try:
|
686 |
+
stream.output_queue.push(('end', None))
|
687 |
+
print("【调试】回调函数: 成功向输出队列推送end信号")
|
688 |
+
except Exception as e:
|
689 |
+
print(f"【调试】回调函数: 向输出队列推送end信号失败: {e}")
|
690 |
+
|
691 |
+
print("【调试】回调函数: 即将抛出KeyboardInterrupt异常")
|
692 |
+
raise KeyboardInterrupt('用户主动结束任务')
|
693 |
+
except Exception as e:
|
694 |
+
print(f"【调试】回调函数: 检查队列顶部信号出错: {e}")
|
695 |
+
|
696 |
+
preview = d['denoised']
|
697 |
+
preview = vae_decode_fake(preview)
|
698 |
+
|
699 |
+
preview = (preview * 255.0).detach().cpu().numpy().clip(0, 255).astype(np.uint8)
|
700 |
+
preview = einops.rearrange(preview, 'b c t h w -> (b h) (t w) c')
|
701 |
+
|
702 |
+
current_step = d['i'] + 1
|
703 |
+
percentage = int(100.0 * current_step / steps)
|
704 |
+
hint = f'Sampling {current_step}/{steps}'
|
705 |
+
desc = f'Total generated frames: {int(max(0, total_generated_latent_frames * 4 - 3))}, Video length: {max(0, (total_generated_latent_frames * 4 - 3) / 30) :.2f} seconds (FPS-30). The video is being extended now ...'
|
706 |
+
stream.output_queue.push(('progress', (preview, desc, make_progress_bar_html(percentage, hint))))
|
707 |
+
except KeyboardInterrupt as e:
|
708 |
+
# 捕获并重新抛出中断异常,确保它能传播到采样函数
|
709 |
+
print(f"【调试】回调函数: 捕获到KeyboardInterrupt: {e}")
|
710 |
+
print("【调试】回调函数: 重新抛出中断异常,确保传播到采样函数")
|
711 |
+
raise
|
712 |
+
except Exception as e:
|
713 |
+
print(f"【调试】回调函数中出错: {e}")
|
714 |
+
# 不中断采样过程
|
715 |
+
print(f"【调试】回调函数: 步骤 {d['i']} 完成")
|
716 |
+
return
|
717 |
+
|
718 |
+
try:
|
719 |
+
sampling_start_time = time.time()
|
720 |
+
print(f"开始采样,设备: {device}, 数据类型: {transformer.dtype}, 使用TeaCache: {use_teacache and not cpu_fallback_mode}")
|
721 |
+
|
722 |
+
try:
|
723 |
+
print("【调试】开始sample_hunyuan采样流程")
|
724 |
+
generated_latents = sample_hunyuan(
|
725 |
+
transformer=transformer,
|
726 |
+
sampler='unipc',
|
727 |
+
width=width,
|
728 |
+
height=height,
|
729 |
+
frames=num_frames,
|
730 |
+
real_guidance_scale=cfg,
|
731 |
+
distilled_guidance_scale=gs,
|
732 |
+
guidance_rescale=rs,
|
733 |
+
# shift=3.0,
|
734 |
+
num_inference_steps=steps,
|
735 |
+
generator=rnd,
|
736 |
+
prompt_embeds=llama_vec,
|
737 |
+
prompt_embeds_mask=llama_attention_mask,
|
738 |
+
prompt_poolers=clip_l_pooler,
|
739 |
+
negative_prompt_embeds=llama_vec_n,
|
740 |
+
negative_prompt_embeds_mask=llama_attention_mask_n,
|
741 |
+
negative_prompt_poolers=clip_l_pooler_n,
|
742 |
+
device=device,
|
743 |
+
dtype=transformer.dtype,
|
744 |
+
image_embeddings=image_encoder_last_hidden_state,
|
745 |
+
latent_indices=latent_indices,
|
746 |
+
clean_latents=clean_latents,
|
747 |
+
clean_latent_indices=clean_latent_indices,
|
748 |
+
clean_latents_2x=clean_latents_2x,
|
749 |
+
clean_latent_2x_indices=clean_latent_2x_indices,
|
750 |
+
clean_latents_4x=clean_latents_4x,
|
751 |
+
clean_latent_4x_indices=clean_latent_4x_indices,
|
752 |
+
callback=callback,
|
753 |
+
)
|
754 |
+
|
755 |
+
print(f"【调试】采样完成,用时: {time.time() - sampling_start_time:.2f}秒")
|
756 |
+
except KeyboardInterrupt as e:
|
757 |
+
# 用户主动中断
|
758 |
+
print(f"【调试】捕获到KeyboardInterrupt: {e}")
|
759 |
+
print("【调试】用户主动中断采样过程,处理中断逻辑")
|
760 |
+
|
761 |
+
# 如果已经有生���的视频,返回最后生成的视频
|
762 |
+
if last_output_filename:
|
763 |
+
print(f"【调试】已有部分生成视频: {last_output_filename},返回此视频")
|
764 |
+
stream.output_queue.push(('file', last_output_filename))
|
765 |
+
error_msg = "用户中断生成过程,但已生成部分视频"
|
766 |
+
else:
|
767 |
+
print("【调试】没有部分生成视频,返回中断消息")
|
768 |
+
error_msg = "用户中断生成过程,未生成视频"
|
769 |
+
|
770 |
+
print(f"【调试】推送错误消息: {error_msg}")
|
771 |
+
stream.output_queue.push(('error', error_msg))
|
772 |
+
print("【调试】推送end信号")
|
773 |
+
stream.output_queue.push(('end', None))
|
774 |
+
print("【调试】中断处理完成,返回")
|
775 |
+
return
|
776 |
+
except Exception as e:
|
777 |
+
print(f"采样过程中出错: {e}")
|
778 |
+
traceback.print_exc()
|
779 |
+
|
780 |
+
# 如果已经有生成的视频,返回最后生成的视频
|
781 |
+
if last_output_filename:
|
782 |
+
stream.output_queue.push(('file', last_output_filename))
|
783 |
+
|
784 |
+
# 创建错误信息
|
785 |
+
error_msg = f"采样过程中出错,但已返回部分生成的视频: {e}"
|
786 |
+
stream.output_queue.push(('error', error_msg))
|
787 |
+
else:
|
788 |
+
# 如果没有生成的视频,返回错误信息
|
789 |
+
error_msg = f"采样过程中出错,无法生成视频: {e}"
|
790 |
+
stream.output_queue.push(('error', error_msg))
|
791 |
+
|
792 |
+
stream.output_queue.push(('end', None))
|
793 |
+
return
|
794 |
+
|
795 |
+
try:
|
796 |
+
if is_last_section:
|
797 |
+
generated_latents = torch.cat([start_latent.to(generated_latents), generated_latents], dim=2)
|
798 |
+
|
799 |
+
total_generated_latent_frames += int(generated_latents.shape[2])
|
800 |
+
history_latents = torch.cat([generated_latents.to(history_latents), history_latents], dim=2)
|
801 |
+
except Exception as e:
|
802 |
+
error_msg = f"处理生成的潜变量时出错: {e}"
|
803 |
+
print(error_msg)
|
804 |
+
traceback.print_exc()
|
805 |
+
|
806 |
+
if last_output_filename:
|
807 |
+
stream.output_queue.push(('file', last_output_filename))
|
808 |
+
stream.output_queue.push(('error', error_msg))
|
809 |
+
stream.output_queue.push(('end', None))
|
810 |
+
return
|
811 |
+
|
812 |
+
if not high_vram and not cpu_fallback_mode:
|
813 |
+
try:
|
814 |
+
offload_model_from_device_for_memory_preservation(transformer, target_device=device, preserved_memory_gb=8)
|
815 |
+
load_model_as_complete(vae, target_device=device)
|
816 |
+
except Exception as e:
|
817 |
+
print(f"管理模型内存时出错: {e}")
|
818 |
+
# 继续执行
|
819 |
+
|
820 |
+
try:
|
821 |
+
real_history_latents = history_latents[:, :, :total_generated_latent_frames, :, :]
|
822 |
+
except Exception as e:
|
823 |
+
error_msg = f"处理历史潜变量时出错: {e}"
|
824 |
+
print(error_msg)
|
825 |
+
|
826 |
+
if last_output_filename:
|
827 |
+
stream.output_queue.push(('file', last_output_filename))
|
828 |
+
continue
|
829 |
+
|
830 |
+
try:
|
831 |
+
vae_start_time = time.time()
|
832 |
+
print(f"开始VAE解码,潜变量形状: {real_history_latents.shape}")
|
833 |
+
|
834 |
+
if history_pixels is None:
|
835 |
+
history_pixels = vae_decode(real_history_latents, vae).cpu()
|
836 |
+
else:
|
837 |
+
section_latent_frames = (latent_window_size * 2 + 1) if is_last_section else (latent_window_size * 2)
|
838 |
+
overlapped_frames = latent_window_size * 4 - 3
|
839 |
+
|
840 |
+
current_pixels = vae_decode(real_history_latents[:, :, :section_latent_frames], vae).cpu()
|
841 |
+
history_pixels = soft_append_bcthw(current_pixels, history_pixels, overlapped_frames)
|
842 |
+
|
843 |
+
print(f"VAE解码完成,用时: {time.time() - vae_start_time:.2f}秒")
|
844 |
+
|
845 |
+
if not high_vram and not cpu_fallback_mode:
|
846 |
+
try:
|
847 |
+
unload_complete_models()
|
848 |
+
except Exception as e:
|
849 |
+
print(f"卸载模型时出错: {e}")
|
850 |
+
|
851 |
+
output_filename = os.path.join(outputs_folder, f'{job_id}_{total_generated_latent_frames}.mp4')
|
852 |
+
|
853 |
+
save_start_time = time.time()
|
854 |
+
save_bcthw_as_mp4(history_pixels, output_filename, fps=30)
|
855 |
+
print(f"保存视频完成,用时: {time.time() - save_start_time:.2f}秒")
|
856 |
+
|
857 |
+
print(f'Decoded. Current latent shape {real_history_latents.shape}; pixel shape {history_pixels.shape}')
|
858 |
+
|
859 |
+
last_output_filename = output_filename
|
860 |
+
stream.output_queue.push(('file', output_filename))
|
861 |
+
except Exception as e:
|
862 |
+
print(f"视频解码或保存过程中出错: {e}")
|
863 |
+
traceback.print_exc()
|
864 |
+
|
865 |
+
# 如果已经有生成的视频,返回最后生成的视频
|
866 |
+
if last_output_filename:
|
867 |
+
stream.output_queue.push(('file', last_output_filename))
|
868 |
+
|
869 |
+
# 记录错误信息
|
870 |
+
error_msg = f"视频解码或保存过程中出错: {e}"
|
871 |
+
stream.output_queue.push(('error', error_msg))
|
872 |
+
|
873 |
+
# 尝试继续下一次迭代
|
874 |
+
continue
|
875 |
+
|
876 |
+
if is_last_section:
|
877 |
+
break
|
878 |
+
except Exception as e:
|
879 |
+
print(f"【调试】处理过程中出现错误: {e}, 类型: {type(e)}")
|
880 |
+
print(f"【调试】错误详情:")
|
881 |
+
traceback.print_exc()
|
882 |
+
|
883 |
+
# 检查是否是中断类型异常
|
884 |
+
if isinstance(e, KeyboardInterrupt):
|
885 |
+
print("【调试】捕获到外层KeyboardInterrupt异常")
|
886 |
+
|
887 |
+
if not high_vram and not cpu_fallback_mode:
|
888 |
+
try:
|
889 |
+
print("【调试】尝试卸载模型以释放资源")
|
890 |
+
unload_complete_models(
|
891 |
+
text_encoder, text_encoder_2, image_encoder, vae, transformer
|
892 |
+
)
|
893 |
+
print("【调试】模型卸载成功")
|
894 |
+
except Exception as unload_error:
|
895 |
+
print(f"【调试】卸载模型时出错: {unload_error}")
|
896 |
+
pass
|
897 |
+
|
898 |
+
# 如果已经有生成的视频,返回最后生成的视频
|
899 |
+
if last_output_filename:
|
900 |
+
print(f"【调试】外层异常处理: 返回已生成的部分视频 {last_output_filename}")
|
901 |
+
stream.output_queue.push(('file', last_output_filename))
|
902 |
+
else:
|
903 |
+
print("【调试】外层异常处理: 未找到已生成的视频")
|
904 |
+
|
905 |
+
# 返回错误信息
|
906 |
+
error_msg = f"处理过程中出现错误: {e}"
|
907 |
+
print(f"【调试】外层异常处理: 推送错误信息: {error_msg}")
|
908 |
+
stream.output_queue.push(('error', error_msg))
|
909 |
+
|
910 |
+
# 确保总是返回end信号
|
911 |
+
print("【调试】工作函数结束,推送end信号")
|
912 |
+
stream.output_queue.push(('end', None))
|
913 |
+
return
|
914 |
+
|
915 |
+
|
916 |
+
# 使用Hugging Face Spaces GPU装饰器处理进程函数
|
917 |
+
if IN_HF_SPACE and 'spaces' in globals():
|
918 |
+
@spaces.GPU
|
919 |
+
def process_with_gpu(input_image, prompt, n_prompt, seed, total_second_length, latent_window_size, steps, cfg, gs, rs, gpu_memory_preservation, use_teacache):
|
920 |
+
global stream
|
921 |
+
assert input_image is not None, 'No input image!'
|
922 |
+
|
923 |
+
# 初始化UI状态
|
924 |
+
yield None, None, '', '', gr.update(interactive=False), gr.update(interactive=True)
|
925 |
+
|
926 |
+
try:
|
927 |
+
stream = AsyncStream()
|
928 |
+
|
929 |
+
# 异步启动worker
|
930 |
+
async_run(worker, input_image, prompt, n_prompt, seed, total_second_length, latent_window_size, steps, cfg, gs, rs, gpu_memory_preservation, use_teacache)
|
931 |
+
|
932 |
+
output_filename = None
|
933 |
+
prev_output_filename = None
|
934 |
+
error_message = None
|
935 |
+
|
936 |
+
# 持续检查worker的输出
|
937 |
+
while True:
|
938 |
+
try:
|
939 |
+
flag, data = stream.output_queue.next()
|
940 |
+
|
941 |
+
if flag == 'file':
|
942 |
+
output_filename = data
|
943 |
+
prev_output_filename = output_filename
|
944 |
+
# 清除错误显示,确保文件成功时不显示错误
|
945 |
+
yield output_filename, gr.update(), gr.update(), '', gr.update(interactive=False), gr.update(interactive=True)
|
946 |
+
|
947 |
+
if flag == 'progress':
|
948 |
+
preview, desc, html = data
|
949 |
+
# 更新进度时不改变错误信息,并确保停止按钮可交互
|
950 |
+
yield gr.update(), gr.update(visible=True, value=preview), desc, html, gr.update(interactive=False), gr.update(interactive=True)
|
951 |
+
|
952 |
+
if flag == 'error':
|
953 |
+
error_message = data
|
954 |
+
print(f"收到错误消息: {error_message}")
|
955 |
+
# 不立即显示,等待end信号
|
956 |
+
|
957 |
+
if flag == 'end':
|
958 |
+
# 如果有最后的视频文件,确保返回
|
959 |
+
if output_filename is None and prev_output_filename is not None:
|
960 |
+
output_filename = prev_output_filename
|
961 |
+
|
962 |
+
# 如果有错误消息,创建友好的错误显示
|
963 |
+
if error_message:
|
964 |
+
error_html = create_error_html(error_message)
|
965 |
+
yield output_filename, gr.update(visible=False), gr.update(), error_html, gr.update(interactive=True), gr.update(interactive=False)
|
966 |
+
else:
|
967 |
+
# 确保成功完成时不显示任何错误
|
968 |
+
yield output_filename, gr.update(visible=False), gr.update(), '', gr.update(interactive=True), gr.update(interactive=False)
|
969 |
+
break
|
970 |
+
except Exception as e:
|
971 |
+
print(f"处理输出时出错: {e}")
|
972 |
+
# 检查是否长时间没有更新
|
973 |
+
current_time = time.time()
|
974 |
+
if current_time - last_update_time > 60: # 60秒没有更新,可能卡住了
|
975 |
+
print(f"处理似乎卡住了,已经 {current_time - last_update_time:.1f} 秒没有更新")
|
976 |
+
|
977 |
+
# 如果有部分生成的视频,返回
|
978 |
+
if prev_output_filename:
|
979 |
+
error_html = create_error_html("处理超时,但已生成部分视频", is_timeout=True)
|
980 |
+
yield prev_output_filename, gr.update(visible=False), gr.update(), error_html, gr.update(interactive=True), gr.update(interactive=False)
|
981 |
+
else:
|
982 |
+
error_html = create_error_html(f"处理超时: {e}", is_timeout=True)
|
983 |
+
yield None, gr.update(visible=False), gr.update(), error_html, gr.update(interactive=True), gr.update(interactive=False)
|
984 |
+
break
|
985 |
+
|
986 |
+
except Exception as e:
|
987 |
+
print(f"启动处理时出错: {e}")
|
988 |
+
traceback.print_exc()
|
989 |
+
error_msg = str(e)
|
990 |
+
|
991 |
+
error_html = create_error_html(error_msg)
|
992 |
+
yield None, gr.update(visible=False), gr.update(), error_html, gr.update(interactive=True), gr.update(interactive=False)
|
993 |
+
|
994 |
+
process = process_with_gpu
|
995 |
+
else:
|
996 |
+
def process(input_image, prompt, n_prompt, seed, total_second_length, latent_window_size, steps, cfg, gs, rs, gpu_memory_preservation, use_teacache):
|
997 |
+
global stream
|
998 |
+
assert input_image is not None, 'No input image!'
|
999 |
+
|
1000 |
+
# 初始化UI状态
|
1001 |
+
yield None, None, '', '', gr.update(interactive=False), gr.update(interactive=True)
|
1002 |
+
|
1003 |
+
try:
|
1004 |
+
stream = AsyncStream()
|
1005 |
+
|
1006 |
+
# 异步启动worker
|
1007 |
+
async_run(worker, input_image, prompt, n_prompt, seed, total_second_length, latent_window_size, steps, cfg, gs, rs, gpu_memory_preservation, use_teacache)
|
1008 |
+
|
1009 |
+
output_filename = None
|
1010 |
+
prev_output_filename = None
|
1011 |
+
error_message = None
|
1012 |
+
|
1013 |
+
# 持续检查worker的输出
|
1014 |
+
while True:
|
1015 |
+
try:
|
1016 |
+
flag, data = stream.output_queue.next()
|
1017 |
+
|
1018 |
+
if flag == 'file':
|
1019 |
+
output_filename = data
|
1020 |
+
prev_output_filename = output_filename
|
1021 |
+
# 清除错误显示,确保文件成功时不显示错误
|
1022 |
+
yield output_filename, gr.update(), gr.update(), '', gr.update(interactive=False), gr.update(interactive=True)
|
1023 |
+
|
1024 |
+
if flag == 'progress':
|
1025 |
+
preview, desc, html = data
|
1026 |
+
# 更新进度时不改变错误信息,并确保停止按钮可交互
|
1027 |
+
yield gr.update(), gr.update(visible=True, value=preview), desc, html, gr.update(interactive=False), gr.update(interactive=True)
|
1028 |
+
|
1029 |
+
if flag == 'error':
|
1030 |
+
error_message = data
|
1031 |
+
print(f"收到错误消息: {error_message}")
|
1032 |
+
# 不立即显示,等待end信号
|
1033 |
+
|
1034 |
+
if flag == 'end':
|
1035 |
+
# 如果有最后的视频文件,确保返回
|
1036 |
+
if output_filename is None and prev_output_filename is not None:
|
1037 |
+
output_filename = prev_output_filename
|
1038 |
+
|
1039 |
+
# 如果有错误消息,创建友好的错误显示
|
1040 |
+
if error_message:
|
1041 |
+
error_html = create_error_html(error_message)
|
1042 |
+
yield output_filename, gr.update(visible=False), gr.update(), error_html, gr.update(interactive=True), gr.update(interactive=False)
|
1043 |
+
else:
|
1044 |
+
# 确保成功完成时不显示任何错误
|
1045 |
+
yield output_filename, gr.update(visible=False), gr.update(), '', gr.update(interactive=True), gr.update(interactive=False)
|
1046 |
+
break
|
1047 |
+
except Exception as e:
|
1048 |
+
print(f"处理输出时出错: {e}")
|
1049 |
+
# 检查是否长时间没有更新
|
1050 |
+
current_time = time.time()
|
1051 |
+
if current_time - last_update_time > 60: # 60秒没有更新,可能卡住了
|
1052 |
+
print(f"处理似乎卡住了,已经 {current_time - last_update_time:.1f} 秒没有更新")
|
1053 |
+
|
1054 |
+
# 如果有部分生成的视频,返回
|
1055 |
+
if prev_output_filename:
|
1056 |
+
error_html = create_error_html("处理超时,但已生成部分视频", is_timeout=True)
|
1057 |
+
yield prev_output_filename, gr.update(visible=False), gr.update(), error_html, gr.update(interactive=True), gr.update(interactive=False)
|
1058 |
+
else:
|
1059 |
+
error_html = create_error_html(f"处理超时: {e}", is_timeout=True)
|
1060 |
+
yield None, gr.update(visible=False), gr.update(), error_html, gr.update(interactive=True), gr.update(interactive=False)
|
1061 |
+
break
|
1062 |
+
|
1063 |
+
except Exception as e:
|
1064 |
+
print(f"启动处理时出错: {e}")
|
1065 |
+
traceback.print_exc()
|
1066 |
+
error_msg = str(e)
|
1067 |
+
|
1068 |
+
error_html = create_error_html(error_msg)
|
1069 |
+
yield None, gr.update(visible=False), gr.update(), error_html, gr.update(interactive=True), gr.update(interactive=False)
|
1070 |
+
|
1071 |
+
|
1072 |
+
def end_process():
|
1073 |
+
"""停止生成过程函数 - 通过在队列中推送'end'信号来中断生成"""
|
1074 |
+
print("【调试】用户点击了停止按钮,发送停止信号...")
|
1075 |
+
# 确保stream已初始化
|
1076 |
+
if 'stream' in globals() and stream is not None:
|
1077 |
+
# 在推送前检查队列状态
|
1078 |
+
try:
|
1079 |
+
current_top = stream.input_queue.top()
|
1080 |
+
print(f"【调试】当前队列顶部信号: {current_top}")
|
1081 |
+
except Exception as e:
|
1082 |
+
print(f"【调试】检查队列状态出错: {e}")
|
1083 |
+
|
1084 |
+
# 推送end信号
|
1085 |
+
try:
|
1086 |
+
stream.input_queue.push('end')
|
1087 |
+
print("【调试】成功推送end信号到队列")
|
1088 |
+
|
1089 |
+
# 验证信号是否成功推送
|
1090 |
+
try:
|
1091 |
+
current_top_after = stream.input_queue.top()
|
1092 |
+
print(f"【调试】推送后队列顶部信号: {current_top_after}")
|
1093 |
+
except Exception as e:
|
1094 |
+
print(f"【调试】验证推送后队列状态出错: {e}")
|
1095 |
+
|
1096 |
+
except Exception as e:
|
1097 |
+
print(f"【调试】推送end信号到队列失败: {e}")
|
1098 |
+
else:
|
1099 |
+
print("【调试】警告: stream未初始化,无法发送停止信号")
|
1100 |
+
return None
|
1101 |
+
|
1102 |
+
|
1103 |
+
quick_prompts = [
|
1104 |
+
'The girl dances gracefully, with clear movements, full of charm.',
|
1105 |
+
'A character doing some simple body movements.',
|
1106 |
+
]
|
1107 |
+
quick_prompts = [[x] for x in quick_prompts]
|
1108 |
+
|
1109 |
+
|
1110 |
+
# 创建一个自定义CSS,增加响应式布局支持
|
1111 |
+
def make_custom_css():
|
1112 |
+
progress_bar_css = make_progress_bar_css()
|
1113 |
+
|
1114 |
+
responsive_css = """
|
1115 |
+
/* 基础响应式设置 */
|
1116 |
+
#app-container {
|
1117 |
+
max-width: 100%;
|
1118 |
+
margin: 0 auto;
|
1119 |
+
}
|
1120 |
+
|
1121 |
+
/* 语言切换按钮样式 */
|
1122 |
+
#language-toggle {
|
1123 |
+
position: fixed;
|
1124 |
+
top: 10px;
|
1125 |
+
right: 10px;
|
1126 |
+
z-index: 1000;
|
1127 |
+
background-color: rgba(0, 0, 0, 0.7);
|
1128 |
+
color: white;
|
1129 |
+
border: none;
|
1130 |
+
border-radius: 4px;
|
1131 |
+
padding: 5px 10px;
|
1132 |
+
cursor: pointer;
|
1133 |
+
font-size: 14px;
|
1134 |
+
}
|
1135 |
+
|
1136 |
+
/* 页面标题样式 */
|
1137 |
+
h1 {
|
1138 |
+
font-size: 2rem;
|
1139 |
+
text-align: center;
|
1140 |
+
margin-bottom: 1rem;
|
1141 |
+
}
|
1142 |
+
|
1143 |
+
/* 按钮样式 */
|
1144 |
+
.start-btn, .stop-btn {
|
1145 |
+
min-height: 45px;
|
1146 |
+
font-size: 1rem;
|
1147 |
+
}
|
1148 |
+
|
1149 |
+
/* 移动设备样式 - 小屏幕 */
|
1150 |
+
@media (max-width: 768px) {
|
1151 |
+
h1 {
|
1152 |
+
font-size: 1.5rem;
|
1153 |
+
margin-bottom: 0.5rem;
|
1154 |
+
}
|
1155 |
+
|
1156 |
+
/* 单列布局 */
|
1157 |
+
.mobile-full-width {
|
1158 |
+
flex-direction: column !important;
|
1159 |
+
}
|
1160 |
+
|
1161 |
+
.mobile-full-width > .gr-block {
|
1162 |
+
min-width: 100% !important;
|
1163 |
+
flex-grow: 1;
|
1164 |
+
}
|
1165 |
+
|
1166 |
+
/* 调整视频大小 */
|
1167 |
+
.video-container {
|
1168 |
+
height: auto !important;
|
1169 |
+
}
|
1170 |
+
|
1171 |
+
/* 调整按钮大小 */
|
1172 |
+
.button-container button {
|
1173 |
+
min-height: 50px;
|
1174 |
+
font-size: 1rem;
|
1175 |
+
touch-action: manipulation;
|
1176 |
+
}
|
1177 |
+
|
1178 |
+
/* 调整滑块 */
|
1179 |
+
.slider-container input[type="range"] {
|
1180 |
+
height: 30px;
|
1181 |
+
}
|
1182 |
+
}
|
1183 |
+
|
1184 |
+
/* 平板设备样式 */
|
1185 |
+
@media (min-width: 769px) and (max-width: 1024px) {
|
1186 |
+
.tablet-adjust {
|
1187 |
+
width: 48% !important;
|
1188 |
+
}
|
1189 |
+
}
|
1190 |
+
|
1191 |
+
/* 黑暗模式支持 */
|
1192 |
+
@media (prefers-color-scheme: dark) {
|
1193 |
+
.dark-mode-text {
|
1194 |
+
color: #f0f0f0;
|
1195 |
+
}
|
1196 |
+
|
1197 |
+
.dark-mode-bg {
|
1198 |
+
background-color: #2a2a2a;
|
1199 |
+
}
|
1200 |
+
}
|
1201 |
+
|
1202 |
+
/* 增强可访问性 */
|
1203 |
+
button, input, select, textarea {
|
1204 |
+
font-size: 16px; /* 防止iOS缩放 */
|
1205 |
+
}
|
1206 |
+
|
1207 |
+
/* 触摸优化 */
|
1208 |
+
button, .interactive-element {
|
1209 |
+
min-height: 44px;
|
1210 |
+
min-width: 44px;
|
1211 |
+
}
|
1212 |
+
|
1213 |
+
/* 提高对比度 */
|
1214 |
+
.high-contrast {
|
1215 |
+
color: #fff;
|
1216 |
+
background-color: #000;
|
1217 |
+
}
|
1218 |
+
|
1219 |
+
/* 进度条样式增强 */
|
1220 |
+
.progress-container {
|
1221 |
+
margin-top: 10px;
|
1222 |
+
margin-bottom: 10px;
|
1223 |
+
}
|
1224 |
+
|
1225 |
+
/* 错误消息样式 */
|
1226 |
+
#error-message {
|
1227 |
+
color: #ff4444;
|
1228 |
+
font-weight: bold;
|
1229 |
+
padding: 10px;
|
1230 |
+
border-radius: 4px;
|
1231 |
+
margin-top: 10px;
|
1232 |
+
}
|
1233 |
+
|
1234 |
+
/* 确保错误容器正确显示 */
|
1235 |
+
.error-message {
|
1236 |
+
background-color: rgba(255, 0, 0, 0.1);
|
1237 |
+
padding: 10px;
|
1238 |
+
border-radius: 4px;
|
1239 |
+
margin-top: 10px;
|
1240 |
+
border: 1px solid #ffcccc;
|
1241 |
+
}
|
1242 |
+
|
1243 |
+
/* 处理多语言错误消息 */
|
1244 |
+
.error-msg-en, .error-msg-zh {
|
1245 |
+
font-weight: bold;
|
1246 |
+
}
|
1247 |
+
|
1248 |
+
/* 错误图标 */
|
1249 |
+
.error-icon {
|
1250 |
+
color: #ff4444;
|
1251 |
+
font-size: 18px;
|
1252 |
+
margin-right: 8px;
|
1253 |
+
}
|
1254 |
+
|
1255 |
+
/* 确保空错误消息不显示背景和边框 */
|
1256 |
+
#error-message:empty {
|
1257 |
+
background-color: transparent;
|
1258 |
+
border: none;
|
1259 |
+
padding: 0;
|
1260 |
+
margin: 0;
|
1261 |
+
}
|
1262 |
+
|
1263 |
+
/* 修复Gradio默认错误显示 */
|
1264 |
+
.error {
|
1265 |
+
display: none !important;
|
1266 |
+
}
|
1267 |
+
"""
|
1268 |
+
|
1269 |
+
# 合并CSS
|
1270 |
+
combined_css = progress_bar_css + responsive_css
|
1271 |
+
return combined_css
|
1272 |
+
|
1273 |
+
|
1274 |
+
css = make_custom_css()
|
1275 |
+
block = gr.Blocks(css=css).queue()
|
1276 |
+
with block:
|
1277 |
+
# 添加语言切换功能
|
1278 |
+
gr.HTML("""
|
1279 |
+
<div id="app-container">
|
1280 |
+
<button id="language-toggle" onclick="toggleLanguage()">中文/English</button>
|
1281 |
+
</div>
|
1282 |
+
<script>
|
1283 |
+
// 全局变量,存储当前语言
|
1284 |
+
window.currentLang = "en";
|
1285 |
+
|
1286 |
+
// 语言切换函数
|
1287 |
+
function toggleLanguage() {
|
1288 |
+
window.currentLang = window.currentLang === "en" ? "zh" : "en";
|
1289 |
+
|
1290 |
+
// 获取所有带有data-i18n属性的元素
|
1291 |
+
const elements = document.querySelectorAll('[data-i18n]');
|
1292 |
+
|
1293 |
+
// 遍历并切换语言
|
1294 |
+
elements.forEach(el => {
|
1295 |
+
const key = el.getAttribute('data-i18n');
|
1296 |
+
const translations = {
|
1297 |
+
"en": {
|
1298 |
+
"title": "FramePack - Image to Video Generation",
|
1299 |
+
"upload_image": "Upload Image",
|
1300 |
+
"prompt": "Prompt",
|
1301 |
+
"quick_prompts": "Quick Prompts",
|
1302 |
+
"start_generation": "Generate",
|
1303 |
+
"stop_generation": "Stop",
|
1304 |
+
"use_teacache": "Use TeaCache",
|
1305 |
+
"teacache_info": "Faster speed, but may result in slightly worse finger and hand generation.",
|
1306 |
+
"negative_prompt": "Negative Prompt",
|
1307 |
+
"seed": "Seed",
|
1308 |
+
"video_length": "Video Length (max 5 seconds)",
|
1309 |
+
"latent_window": "Latent Window Size",
|
1310 |
+
"steps": "Inference Steps",
|
1311 |
+
"steps_info": "Changing this value is not recommended.",
|
1312 |
+
"cfg_scale": "CFG Scale",
|
1313 |
+
"distilled_cfg": "Distilled CFG Scale",
|
1314 |
+
"distilled_cfg_info": "Changing this value is not recommended.",
|
1315 |
+
"cfg_rescale": "CFG Rescale",
|
1316 |
+
"gpu_memory": "GPU Memory Preservation (GB) (larger means slower)",
|
1317 |
+
"gpu_memory_info": "Set this to a larger value if you encounter OOM errors. Larger values cause slower speed.",
|
1318 |
+
"next_latents": "Next Latents",
|
1319 |
+
"generated_video": "Generated Video",
|
1320 |
+
"sampling_note": "Note: Due to reversed sampling, ending actions will be generated before starting actions. If the starting action is not in the video, please wait, it will be generated later.",
|
1321 |
+
"error_message": "Error",
|
1322 |
+
"processing_error": "Processing error",
|
1323 |
+
"network_error": "Network connection is unstable, model download timed out. Please try again later.",
|
1324 |
+
"memory_error": "GPU memory insufficient, please try increasing GPU memory preservation value or reduce video length.",
|
1325 |
+
"model_error": "Failed to load model, possibly due to network issues or high server load. Please try again later.",
|
1326 |
+
"partial_video": "Processing error, but partial video has been generated",
|
1327 |
+
"processing_interrupt": "Processing was interrupted, but partial video has been generated"
|
1328 |
+
},
|
1329 |
+
"zh": {
|
1330 |
+
"title": "FramePack - 图像到视频生成",
|
1331 |
+
"upload_image": "上传图像",
|
1332 |
+
"prompt": "提示词",
|
1333 |
+
"quick_prompts": "快速提示词列表",
|
1334 |
+
"start_generation": "开始生成",
|
1335 |
+
"stop_generation": "结束生成",
|
1336 |
+
"use_teacache": "使用TeaCache",
|
1337 |
+
"teacache_info": "速度更快,但可能会使手指和手的生成效果稍差。",
|
1338 |
+
"negative_prompt": "负面提示词",
|
1339 |
+
"seed": "随机种子",
|
1340 |
+
"video_length": "视频长度(最大5秒)",
|
1341 |
+
"latent_window": "潜在窗口大小",
|
1342 |
+
"steps": "推理步数",
|
1343 |
+
"steps_info": "不建议修改此值。",
|
1344 |
+
"cfg_scale": "CFG Scale",
|
1345 |
+
"distilled_cfg": "蒸馏CFG比例",
|
1346 |
+
"distilled_cfg_info": "不建议修改此值。",
|
1347 |
+
"cfg_rescale": "CFG重缩放",
|
1348 |
+
"gpu_memory": "GPU推理保留内存(GB)(值越大速度越慢)",
|
1349 |
+
"gpu_memory_info": "如果出现OOM错误,请将此值设置得更大。值越大,速度越慢。",
|
1350 |
+
"next_latents": "下一批潜变量",
|
1351 |
+
"generated_video": "生成的视频",
|
1352 |
+
"sampling_note": "注意:由于采样是倒序的,结束动作将在开始动作之前生成。如果视频中没有出现起始动作,请继续等待,它将在稍后生成。",
|
1353 |
+
"error_message": "错误信息",
|
1354 |
+
"processing_error": "处理过程出错",
|
1355 |
+
"network_error": "网络连接不稳定,模型下载超时。请稍后再试。",
|
1356 |
+
"memory_error": "GPU内存不足,请尝试增加GPU推理保留内存值或降低视频长度。",
|
1357 |
+
"model_error": "模型加载失败,可能是网络问题或服务器负载过高。请稍后再试。",
|
1358 |
+
"partial_video": "处理过程中出现错误,但已生成部分视频",
|
1359 |
+
"processing_interrupt": "处理过程中断,但已生成部分视频"
|
1360 |
+
}
|
1361 |
+
};
|
1362 |
+
|
1363 |
+
if (translations[window.currentLang] && translations[window.currentLang][key]) {
|
1364 |
+
// 根据元素类型设置文本
|
1365 |
+
if (el.tagName === 'BUTTON') {
|
1366 |
+
el.textContent = translations[window.currentLang][key];
|
1367 |
+
} else if (el.tagName === 'LABEL') {
|
1368 |
+
el.textContent = translations[window.currentLang][key];
|
1369 |
+
} else {
|
1370 |
+
el.innerHTML = translations[window.currentLang][key];
|
1371 |
+
}
|
1372 |
+
}
|
1373 |
+
});
|
1374 |
+
|
1375 |
+
// 更新页面上其他元素
|
1376 |
+
document.querySelectorAll('.bilingual-label').forEach(el => {
|
1377 |
+
const enText = el.getAttribute('data-en');
|
1378 |
+
const zhText = el.getAttribute('data-zh');
|
1379 |
+
el.textContent = window.currentLang === 'en' ? enText : zhText;
|
1380 |
+
});
|
1381 |
+
|
1382 |
+
// 处理错误消息容器
|
1383 |
+
document.querySelectorAll('[data-lang]').forEach(el => {
|
1384 |
+
el.style.display = el.getAttribute('data-lang') === window.currentLang ? 'block' : 'none';
|
1385 |
+
});
|
1386 |
+
}
|
1387 |
+
|
1388 |
+
// 页面加载后初始化
|
1389 |
+
document.addEventListener('DOMContentLoaded', function() {
|
1390 |
+
// 添加data-i18n属性到需要国际化的元素
|
1391 |
+
setTimeout(() => {
|
1392 |
+
// 给所有标签添加i18n属性
|
1393 |
+
const labelMap = {
|
1394 |
+
"Upload Image": "upload_image",
|
1395 |
+
"上传图像": "upload_image",
|
1396 |
+
"Prompt": "prompt",
|
1397 |
+
"提示词": "prompt",
|
1398 |
+
"Quick Prompts": "quick_prompts",
|
1399 |
+
"快速提示词列表": "quick_prompts",
|
1400 |
+
"Generate": "start_generation",
|
1401 |
+
"开始生成": "start_generation",
|
1402 |
+
"Stop": "stop_generation",
|
1403 |
+
"结束生成": "stop_generation",
|
1404 |
+
// 添加其他标签映射...
|
1405 |
+
};
|
1406 |
+
|
1407 |
+
// 处理标签
|
1408 |
+
document.querySelectorAll('label, span, button').forEach(el => {
|
1409 |
+
const text = el.textContent.trim();
|
1410 |
+
if (labelMap[text]) {
|
1411 |
+
el.setAttribute('data-i18n', labelMap[text]);
|
1412 |
+
}
|
1413 |
+
});
|
1414 |
+
|
1415 |
+
// 添加特定元素的i18n属性
|
1416 |
+
const titleEl = document.querySelector('h1');
|
1417 |
+
if (titleEl) titleEl.setAttribute('data-i18n', 'title');
|
1418 |
+
|
1419 |
+
// 初始化标签语言
|
1420 |
+
toggleLanguage();
|
1421 |
+
}, 1000);
|
1422 |
+
});
|
1423 |
+
</script>
|
1424 |
+
""")
|
1425 |
+
|
1426 |
+
# 标题使用data-i18n属性以便JavaScript切换
|
1427 |
+
gr.HTML("<h1 data-i18n='title'>FramePack - Image to Video Generation / 图像到视频生成</h1>")
|
1428 |
+
|
1429 |
+
# 使用带有mobile-full-width类的响应式行
|
1430 |
+
with gr.Row(elem_classes="mobile-full-width"):
|
1431 |
+
with gr.Column(scale=1, elem_classes="mobile-full-width"):
|
1432 |
+
# 添加双语标签 - 上传图像
|
1433 |
+
input_image = gr.Image(
|
1434 |
+
sources='upload',
|
1435 |
+
type="numpy",
|
1436 |
+
label="Upload Image / 上传图像",
|
1437 |
+
elem_id="input-image",
|
1438 |
+
height=320
|
1439 |
+
)
|
1440 |
+
|
1441 |
+
# 添加双语标签 - 提示词
|
1442 |
+
prompt = gr.Textbox(
|
1443 |
+
label="Prompt / 提示词",
|
1444 |
+
value='',
|
1445 |
+
elem_id="prompt-input"
|
1446 |
+
)
|
1447 |
+
|
1448 |
+
# 添加双语标签 - 快速提示词
|
1449 |
+
example_quick_prompts = gr.Dataset(
|
1450 |
+
samples=quick_prompts,
|
1451 |
+
label='Quick Prompts / 快速提示词列表',
|
1452 |
+
samples_per_page=1000,
|
1453 |
+
components=[prompt]
|
1454 |
+
)
|
1455 |
+
example_quick_prompts.click(lambda x: x[0], inputs=[example_quick_prompts], outputs=prompt, show_progress=False, queue=False)
|
1456 |
+
|
1457 |
+
# 按钮添加样式和双语标签
|
1458 |
+
with gr.Row(elem_classes="button-container"):
|
1459 |
+
start_button = gr.Button(
|
1460 |
+
value="Generate / 开始生成",
|
1461 |
+
elem_classes="start-btn",
|
1462 |
+
elem_id="start-button",
|
1463 |
+
variant="primary"
|
1464 |
+
)
|
1465 |
+
|
1466 |
+
end_button = gr.Button(
|
1467 |
+
value="Stop / 结束生成",
|
1468 |
+
elem_classes="stop-btn",
|
1469 |
+
elem_id="stop-button",
|
1470 |
+
interactive=False
|
1471 |
+
)
|
1472 |
+
|
1473 |
+
# 参数设置区域
|
1474 |
+
with gr.Group():
|
1475 |
+
use_teacache = gr.Checkbox(
|
1476 |
+
label='Use TeaCache / 使用TeaCache',
|
1477 |
+
value=True,
|
1478 |
+
info='Faster speed, but may result in slightly worse finger and hand generation. / 速度更快,但可能会使手指和手的生成效果稍差。'
|
1479 |
+
)
|
1480 |
+
|
1481 |
+
n_prompt = gr.Textbox(label="Negative Prompt / 负面提示词", value="", visible=False) # Not used
|
1482 |
+
|
1483 |
+
seed = gr.Number(
|
1484 |
+
label="Seed / 随机种子",
|
1485 |
+
value=31337,
|
1486 |
+
precision=0
|
1487 |
+
)
|
1488 |
+
|
1489 |
+
# 添加slider-container类以便CSS触摸优化
|
1490 |
+
with gr.Group(elem_classes="slider-container"):
|
1491 |
+
total_second_length = gr.Slider(
|
1492 |
+
label="Video Length (max 5 seconds) / 视频长度(最大5秒)",
|
1493 |
+
minimum=1,
|
1494 |
+
maximum=5,
|
1495 |
+
value=5,
|
1496 |
+
step=0.1
|
1497 |
+
)
|
1498 |
+
|
1499 |
+
latent_window_size = gr.Slider(
|
1500 |
+
label="Latent Window Size / 潜在窗口大小",
|
1501 |
+
minimum=1,
|
1502 |
+
maximum=33,
|
1503 |
+
value=9,
|
1504 |
+
step=1,
|
1505 |
+
visible=False
|
1506 |
+
)
|
1507 |
+
|
1508 |
+
steps = gr.Slider(
|
1509 |
+
label="Inference Steps / 推理步数",
|
1510 |
+
minimum=1,
|
1511 |
+
maximum=100,
|
1512 |
+
value=25,
|
1513 |
+
step=1,
|
1514 |
+
info='Changing this value is not recommended. / 不建议修改此值。'
|
1515 |
+
)
|
1516 |
+
|
1517 |
+
cfg = gr.Slider(
|
1518 |
+
label="CFG Scale",
|
1519 |
+
minimum=1.0,
|
1520 |
+
maximum=32.0,
|
1521 |
+
value=1.0,
|
1522 |
+
step=0.01,
|
1523 |
+
visible=False
|
1524 |
+
)
|
1525 |
+
|
1526 |
+
gs = gr.Slider(
|
1527 |
+
label="Distilled CFG Scale / 蒸馏CFG比例",
|
1528 |
+
minimum=1.0,
|
1529 |
+
maximum=32.0,
|
1530 |
+
value=10.0,
|
1531 |
+
step=0.01,
|
1532 |
+
info='Changing this value is not recommended. / 不建议修改此值。'
|
1533 |
+
)
|
1534 |
+
|
1535 |
+
rs = gr.Slider(
|
1536 |
+
label="CFG Rescale / CFG重缩放",
|
1537 |
+
minimum=0.0,
|
1538 |
+
maximum=1.0,
|
1539 |
+
value=0.0,
|
1540 |
+
step=0.01,
|
1541 |
+
visible=False
|
1542 |
+
)
|
1543 |
+
|
1544 |
+
gpu_memory_preservation = gr.Slider(
|
1545 |
+
label="GPU Memory (GB) / GPU推理保留内存(GB)",
|
1546 |
+
minimum=6,
|
1547 |
+
maximum=128,
|
1548 |
+
value=6,
|
1549 |
+
step=0.1,
|
1550 |
+
info="Set this to a larger value if you encounter OOM errors. Larger values cause slower speed. / 如果出现OOM错误,请将此值设置得更大。值越大,速度越慢。"
|
1551 |
+
)
|
1552 |
+
|
1553 |
+
# 右侧预览和结果列
|
1554 |
+
with gr.Column(scale=1, elem_classes="mobile-full-width"):
|
1555 |
+
# 预览图像
|
1556 |
+
preview_image = gr.Image(
|
1557 |
+
label="Preview / 预览",
|
1558 |
+
height=200,
|
1559 |
+
visible=False,
|
1560 |
+
elem_classes="preview-container"
|
1561 |
+
)
|
1562 |
+
|
1563 |
+
# 视频结果容器
|
1564 |
+
result_video = gr.Video(
|
1565 |
+
label="Generated Video / 生成的视频",
|
1566 |
+
autoplay=True,
|
1567 |
+
show_share_button=True, # 添加分享按钮
|
1568 |
+
height=512,
|
1569 |
+
loop=True,
|
1570 |
+
elem_classes="video-container",
|
1571 |
+
elem_id="result-video"
|
1572 |
+
)
|
1573 |
+
|
1574 |
+
# 双语说明
|
1575 |
+
gr.HTML("<div data-i18n='sampling_note' class='note'>Note: Due to reversed sampling, ending actions will be generated before starting actions. If the starting action is not in the video, please wait, it will be generated later.</div>")
|
1576 |
+
|
1577 |
+
# 进度指示器
|
1578 |
+
with gr.Group(elem_classes="progress-container"):
|
1579 |
+
progress_desc = gr.Markdown('', elem_classes='no-generating-animation')
|
1580 |
+
progress_bar = gr.HTML('', elem_classes='no-generating-animation')
|
1581 |
+
|
1582 |
+
# 错误信息区域 - 确保使用HTML组件以支持我们的自定义错误消息格式
|
1583 |
+
error_message = gr.HTML('', elem_id='error-message', visible=True)
|
1584 |
+
|
1585 |
+
# 处理函数
|
1586 |
+
ips = [input_image, prompt, n_prompt, seed, total_second_length, latent_window_size, steps, cfg, gs, rs, gpu_memory_preservation, use_teacache]
|
1587 |
+
|
1588 |
+
# 开始和结束按钮事件
|
1589 |
+
start_button.click(fn=process, inputs=ips, outputs=[result_video, preview_image, progress_desc, progress_bar, start_button, end_button])
|
1590 |
+
end_button.click(fn=end_process)
|
1591 |
+
|
1592 |
+
|
1593 |
+
block.launch()
|
1594 |
+
|
1595 |
+
# 创建友好的错误显示HTML
|
1596 |
+
def create_error_html(error_msg, is_timeout=False):
|
1597 |
+
"""创建双语错误消息HTML"""
|
1598 |
+
# 提供更友好的中英文双语错误信息
|
1599 |
+
en_msg = ""
|
1600 |
+
zh_msg = ""
|
1601 |
+
|
1602 |
+
if is_timeout:
|
1603 |
+
en_msg = "Processing timed out, but partial video may have been generated" if "部分视频" in error_msg else f"Processing timed out: {error_msg}"
|
1604 |
+
zh_msg = "处理超时,但已生成部分视频" if "部分视频" in error_msg else f"处理超时: {error_msg}"
|
1605 |
+
elif "模型加载失败" in error_msg:
|
1606 |
+
en_msg = "Failed to load models. The Space may be experiencing high traffic or GPU issues."
|
1607 |
+
zh_msg = "模型加载失败,可能是Space流量过高或GPU资源不足。"
|
1608 |
+
elif "GPU" in error_msg or "CUDA" in error_msg or "内存" in error_msg or "memory" in error_msg:
|
1609 |
+
en_msg = "GPU memory insufficient or GPU error. Try increasing GPU memory preservation value or reduce video length."
|
1610 |
+
zh_msg = "GPU内存不足或GPU错误,请尝试增加GPU推理保留内存值或降低视频长度。"
|
1611 |
+
elif "采样过程中出错" in error_msg:
|
1612 |
+
if "部分" in error_msg:
|
1613 |
+
en_msg = "Error during sampling process, but partial video has been generated."
|
1614 |
+
zh_msg = "采样过程中出错,但已生成部分视频。"
|
1615 |
+
else:
|
1616 |
+
en_msg = "Error during sampling process. Unable to generate video."
|
1617 |
+
zh_msg = "采样过程中出错,无法生成视频。"
|
1618 |
+
elif "模型下载超时" in error_msg or "网络连接不稳定" in error_msg or "ReadTimeoutError" in error_msg or "ConnectionError" in error_msg:
|
1619 |
+
en_msg = "Network connection is unstable, model download timed out. Please try again later."
|
1620 |
+
zh_msg = "网络连接不稳定,模型下载超时。请稍后再试。"
|
1621 |
+
elif "VAE" in error_msg or "解码" in error_msg or "decode" in error_msg:
|
1622 |
+
en_msg = "Error during video decoding or saving process. Try again with a different seed."
|
1623 |
+
zh_msg = "视频解码或保存过程中出错,请尝试使用不同的随机种子。"
|
1624 |
+
else:
|
1625 |
+
en_msg = f"Processing error: {error_msg}"
|
1626 |
+
zh_msg = f"处理过程出错: {error_msg}"
|
1627 |
+
|
1628 |
+
# 创建双语错误消息HTML - 添加有用的图标并确保CSS样式适用
|
1629 |
+
return f"""
|
1630 |
+
<div class="error-message" id="custom-error-container">
|
1631 |
+
<div class="error-msg-en" data-lang="en">
|
1632 |
+
<span class="error-icon">⚠️</span> {en_msg}
|
1633 |
+
</div>
|
1634 |
+
<div class="error-msg-zh" data-lang="zh">
|
1635 |
+
<span class="error-icon">⚠️</span> {zh_msg}
|
1636 |
+
</div>
|
1637 |
+
</div>
|
1638 |
+
<script>
|
1639 |
+
// 根据当前语言显示相应的错误消息
|
1640 |
+
(function() {{
|
1641 |
+
const errorContainer = document.getElementById('custom-error-container');
|
1642 |
+
if (errorContainer) {{
|
1643 |
+
const currentLang = window.currentLang || 'en'; // 默认英语
|
1644 |
+
const errMsgs = errorContainer.querySelectorAll('[data-lang]');
|
1645 |
+
errMsgs.forEach(msg => {{
|
1646 |
+
msg.style.display = msg.getAttribute('data-lang') === currentLang ? 'block' : 'none';
|
1647 |
+
}});
|
1648 |
+
|
1649 |
+
// 确保Gradio默认错误UI不显示
|
1650 |
+
const defaultErrorElements = document.querySelectorAll('.error');
|
1651 |
+
defaultErrorElements.forEach(el => {{
|
1652 |
+
el.style.display = 'none';
|
1653 |
+
}});
|
1654 |
+
}}
|
1655 |
+
}})();
|
1656 |
+
</script>
|
1657 |
+
"""
|