dangthr commited on
Commit
cc137de
·
verified ·
1 Parent(s): 515245d

Update downloader.py

Browse files
Files changed (1) hide show
  1. downloader.py +469 -107
downloader.py CHANGED
@@ -1,132 +1,494 @@
1
- import yaml
 
 
 
 
 
2
  import os
 
 
3
  from pathlib import Path
 
 
 
4
  from huggingface_hub import hf_hub_download
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
- def download_ltx_models():
 
7
  """
8
- 独立下载LTX-Video模型的脚本
9
- 保持与主程序相同的路径和配置
10
  """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
- # 读取配置文件
13
- config_file_path = "configs/ltxv-13b-0.9.7-distilled.yaml"
 
 
 
14
 
15
- if not os.path.exists(config_file_path):
16
- print(f"错误: 配置文件 {config_file_path} 不存在")
17
- print("请确保配置文件在正确的位置")
18
- return False
19
 
20
- with open(config_file_path, "r") as file:
21
- PIPELINE_CONFIG_YAML = yaml.safe_load(file)
 
 
22
 
23
- # 设置常量
24
- LTX_REPO = "Lightricks/LTX-Video"
25
- models_dir = "downloaded_models_gradio_cpu_init"
 
 
26
 
27
- # 创建模型目录
28
- Path(models_dir).mkdir(parents=True, exist_ok=True)
29
- print(f"模型下载目录: {Path(models_dir).resolve()}")
 
 
 
30
 
31
- try:
32
- # 下载主模型
33
- print("\n开始下载主模型...")
34
- print(f"模型文件: {PIPELINE_CONFIG_YAML['checkpoint_path']}")
35
-
36
- distilled_model_actual_path = hf_hub_download(
37
- repo_id=LTX_REPO,
38
- filename=PIPELINE_CONFIG_YAML["checkpoint_path"],
39
- local_dir=models_dir,
40
- local_dir_use_symlinks=False
41
- )
42
- print(f"✅ 主模型下载完成: {distilled_model_actual_path}")
43
-
44
- # 下载空间上采样器模型
45
- print("\n开始下载空间上采样器模型...")
46
- SPATIAL_UPSCALER_FILENAME = PIPELINE_CONFIG_YAML["spatial_upscaler_model_path"]
47
- print(f"模型文件: {SPATIAL_UPSCALER_FILENAME}")
48
-
49
- spatial_upscaler_actual_path = hf_hub_download(
50
- repo_id=LTX_REPO,
51
- filename=SPATIAL_UPSCALER_FILENAME,
52
- local_dir=models_dir,
53
- local_dir_use_symlinks=False
54
- )
55
- print(f"✅ 空间上采样器模型下载完成: {spatial_upscaler_actual_path}")
56
-
57
- # 显示下载摘要
58
- print("\n" + "="*60)
59
- print("模型下载完成摘要:")
60
- print("="*60)
61
- print(f"下载目录: {models_dir}")
62
- print(f"主模型: {os.path.basename(distilled_model_actual_path)}")
63
- print(f"上采样器: {os.path.basename(spatial_upscaler_actual_path)}")
64
-
65
- # 检查文件大小
66
- main_size = os.path.getsize(distilled_model_actual_path) / (1024**3) # GB
67
- upscaler_size = os.path.getsize(spatial_upscaler_actual_path) / (1024**3) # GB
68
- total_size = main_size + upscaler_size
 
 
 
 
 
 
 
 
 
 
 
69
 
70
- print(f"\n文件大小:")
71
- print(f"主模型: {main_size:.2f} GB")
72
- print(f"上采样器: {upscaler_size:.2f} GB")
73
- print(f"总计: {total_size:.2f} GB")
74
 
75
- return True
 
 
 
76
 
77
- except Exception as e:
78
- print(f"\n❌ 下载过程中出现错误: {e}")
79
- print("可能的解决方案:")
80
- print("1. 检查网络连接")
81
- print("2. 确认Hugging Face访问权限")
82
- print("3. 检查磁盘空间是否足够")
83
- return False
84
 
85
- def check_models_exist():
86
- """
87
- 检查模型是否已经存在
88
- """
89
- config_file_path = "configs/ltxv-13b-0.9.7-distilled.yaml"
90
-
91
- if not os.path.exists(config_file_path):
92
- return False
93
-
94
- with open(config_file_path, "r") as file:
95
- config = yaml.safe_load(file)
96
 
97
- models_dir = "downloaded_models_gradio_cpu_init"
98
- main_model = os.path.join(models_dir, config["checkpoint_path"])
99
- upscaler_model = os.path.join(models_dir, config["spatial_upscaler_model_path"])
100
 
101
- main_exists = os.path.exists(main_model)
102
- upscaler_exists = os.path.exists(upscaler_model)
 
103
 
104
- print("模型存在性检查:")
105
- print(f"主模型: {'✅ 存在' if main_exists else '❌ 不存在'}")
106
- print(f"上采样器: {'✅ 存在' if upscaler_exists else '❌ 不存在'}")
 
 
 
 
 
107
 
108
- return main_exists and upscaler_exists
 
109
 
110
- def main():
111
- print("LTX-Video 模型下载器")
112
- print("="*40)
 
 
 
 
 
113
 
114
- # 检查模型是否已存在
115
- if check_models_exist():
116
- print("\n所有模型已存在,无需重新下载。")
117
- choice = input("是否要重新下载?(y/N): ").lower().strip()
118
- if choice != 'y':
119
- print("取消下载。")
120
- return
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
121
 
122
- print("\n开始下载模型...")
123
- success = download_ltx_models()
 
124
 
125
- if success:
126
- print("\n🎉 所有模型下载成功!")
127
- print("现在可以运行主程序了。")
128
- else:
129
- print("\n💥 模型下载失败,请检查错误信息并重试。")
130
 
131
- if __name__ == "__main__":
132
- main()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # ==============================================================================
2
+ # 统一入口和依赖项
3
+ # ==============================================================================
4
+ import torch
5
+ import numpy as np
6
+ import random
7
  import os
8
+ import yaml
9
+ import argparse
10
  from pathlib import Path
11
+ import imageio
12
+ import tempfile
13
+ from PIL import Image
14
  from huggingface_hub import hf_hub_download
15
+ import shutil
16
+
17
+ # 监听模式所需的依赖项
18
+ import asyncio
19
+ import websockets
20
+ import subprocess
21
+ import json
22
+ import logging
23
+ import sys
24
+ import urllib.parse
25
+ import requests
26
+
27
+ from inference import (
28
+ create_ltx_video_pipeline,
29
+ create_latent_upsampler,
30
+ load_image_to_tensor_with_resize_and_crop,
31
+ seed_everething,
32
+ get_device,
33
+ calculate_padding,
34
+ load_media_file
35
+ )
36
+ from ltx_video.pipelines.pipeline_ltx_video import ConditioningItem, LTXMultiScalePipeline, LTXVideoPipeline
37
+ from ltx_video.utils.skip_layer_strategy import SkipLayerStrategy
38
+
39
+ # ==============================================================================
40
+ # 日志配置
41
+ # ==============================================================================
42
+ logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
43
+ logger = logging.getLogger(__name__)
44
+
45
+ # ==============================================================================
46
+ # 监听模式的函数 (原 remote_client.py)
47
+ # ==============================================================================
48
+
49
+ # 全局变量,用于在监听模式下共享状态
50
+ global_websocket = None
51
+ global_machine_id = None
52
+ global_card_id = None
53
+ global_machine_secret = None
54
+ global_server_url = None
55
+
56
+ async def upload_file_to_server(file_path, card_id, machine_secret, machine_id):
57
+ """将文件上传到服务器的指定端点"""
58
+ try:
59
+ if not os.path.exists(file_path):
60
+ logger.error(f"[Uploader] File not found: {file_path}")
61
+ return False
62
+
63
+ upload_url = f"{global_server_url}/terminal/{card_id}/machine-upload?secret={urllib.parse.quote(machine_secret)}"
64
+ files = {'file': (os.path.basename(file_path), open(file_path, 'rb'), 'application/octet-stream')}
65
+ data = {'machine_id': machine_id}
66
+
67
+ logger.info(f"[Uploader] Uploading {os.path.basename(file_path)} to {upload_url}...")
68
+ response = requests.post(upload_url, files=files, data=data, timeout=120)
69
+
70
+ if response.status_code == 200:
71
+ result = response.json()
72
+ if result and result.get("success"):
73
+ logger.info(f"[Uploader] Upload successful: {file_path}")
74
+ return True
75
+ else:
76
+ logger.error(f"[Uploader] Upload failed: {result.get('error', 'Unknown error')}")
77
+ return False
78
+ else:
79
+ logger.error(f"[Uploader] Upload failed with status code {response.status_code}: {response.text}")
80
+ return False
81
+
82
+ except Exception as e:
83
+ logger.error(f"[Uploader] An exception occurred during upload: {e}")
84
+ return False
85
+
86
+ async def watch_directory_for_uploads(dir_to_watch, card_id, secret, get_machine_id_func):
87
+ """
88
+ 监视指定目录中的新文件,并自动上传。
89
+ """
90
+ processed_files = set()
91
+ logger.info(f"[Watcher] Starting to watch directory: {dir_to_watch}")
92
+
93
+ # 初始扫描,将已存在的文件视为已处理
94
+ if os.path.isdir(dir_to_watch):
95
+ processed_files.update(os.listdir(dir_to_watch))
96
+ logger.info(f"[Watcher] Initial scan: {len(processed_files)} existing files ignored.")
97
+
98
+ while True:
99
+ await asyncio.sleep(5) # 每5秒检查一次
100
+ try:
101
+ if not os.path.isdir(dir_to_watch):
102
+ continue
103
+
104
+ current_files = set(os.listdir(dir_to_watch))
105
+ new_files = current_files - processed_files
106
+
107
+ if new_files:
108
+ machine_id = get_machine_id_func()
109
+ if not machine_id:
110
+ logger.warning("[Watcher] Machine ID not available, skipping upload cycle.")
111
+ continue
112
+
113
+ logger.info(f"[Watcher] Detected {len(new_files)} new file(s): {', '.join(new_files)}")
114
+ for filename in new_files:
115
+ file_path = os.path.join(dir_to_watch, filename)
116
+ # 等待文件写入完成 (简单检查)
117
+ await asyncio.sleep(2)
118
+
119
+ success = await upload_file_to_server(file_path, card_id, secret, machine_id)
120
+ if success:
121
+ logger.info(f"[Watcher] Successfully uploaded {filename}. Marking as processed.")
122
+ processed_files.add(filename)
123
+ else:
124
+ logger.warning(f"[Watcher] Failed to upload {filename}. Will retry on next cycle.")
125
+
126
+ # 同步已处理列表,移除已删除的文件
127
+ processed_files.intersection_update(current_files)
128
+
129
+ except Exception as e:
130
+ logger.error(f"[Watcher] Error in file watching loop: {e}")
131
 
132
+
133
+ async def start_listener_mode(card_id, machine_secret, watch_dir):
134
  """
135
+ 启动监听模式的主函数。
 
136
  """
137
+ global global_websocket, global_machine_id, global_card_id, global_machine_secret, global_server_url
138
+
139
+ global_card_id = card_id
140
+ global_machine_secret = machine_secret
141
+
142
+ server_hostname = "remote-terminal-worker.nianxi4563.workers.dev" # 或者您的服务器域名
143
+ global_server_url = f"https://{server_hostname}"
144
+ encoded_secret = urllib.parse.quote(machine_secret)
145
+ uri = f"wss://{server_hostname}/terminal/{card_id}?secret={encoded_secret}"
146
+
147
+ # 启动文件监视器
148
+ def get_machine_id(): return global_machine_id
149
+ watcher_task = asyncio.create_task(watch_directory_for_uploads(watch_dir, card_id, machine_secret, get_machine_id))
150
+
151
+ while True: # 自动重连循环
152
+ try:
153
+ logger.info(f"[Listener] Attempting to connect to {uri}")
154
+ async with websockets.connect(uri, ping_interval=20, ping_timeout=60) as websocket:
155
+ global_websocket = websocket
156
+ logger.info("[Listener] Connected to WebSocket server.")
157
+
158
+ # 循环以获取 machine_id
159
+ while global_machine_id is None:
160
+ try:
161
+ response = await asyncio.wait_for(websocket.recv(), timeout=10.0)
162
+ data = json.loads(response)
163
+ if data.get("type") == "connected" and "machine_id" in data:
164
+ global_machine_id = data["machine_id"]
165
+ logger.info(f"[Listener] Assigned machine ID: {global_machine_id}")
166
+ break
167
+ except asyncio.TimeoutError:
168
+ logger.debug("[Listener] Waiting for machine ID...")
169
+ except Exception as e:
170
+ logger.error(f"[Listener] Error receiving machine ID: {e}")
171
+ await asyncio.sleep(5) # 等待后重试
172
+ break # break inner loop to reconnect
173
+
174
+ if not global_machine_id:
175
+ continue # continue outer loop to reconnect
176
+
177
+ # 主消息处理循环
178
+ while True:
179
+ message = await websocket.recv()
180
+ data = json.loads(message)
181
+ logger.debug(f"[Listener] Received message: {data}")
182
+
183
+ if data.get("type") == "command":
184
+ command = data["command"]
185
+ logger.info(f"[Listener] Received command: {command}")
186
+
187
+ # 使用 subprocess 在新进程中执行命令
188
+ # 这使得监听器可以继续工作,而推理在后台运行
189
+ try:
190
+ # 将命令包装在 `python app.py ...` 中
191
+ full_command = f"python app.py {command}"
192
+ logger.info(f"Executing subprocess: {full_command}")
193
+ subprocess.run(full_command, shell=True, check=True)
194
+ logger.info("Subprocess finished successfully.")
195
+ # 结果文件将由 watcher 自动上传
196
+ except subprocess.CalledProcessError as e:
197
+ logger.error(f"Command execution failed with return code {e.returncode}")
198
+ error_output = e.stderr if e.stderr else e.stdout
199
+ if global_websocket:
200
+ await global_websocket.send(json.dumps({
201
+ "type": "error", "data": f"Command failed: {error_output}", "machine_id": global_machine_id
202
+ }))
203
+ except Exception as e:
204
+ logger.error(f"Failed to run command: {e}")
205
+
206
+ except websockets.exceptions.ConnectionClosed as e:
207
+ logger.warning(f"[Listener] WebSocket closed: code={e.code}, reason={e.reason}. Reconnecting in 10 seconds...")
208
+ except Exception as e:
209
+ logger.error(f"[Listener] Connection failed: {e}. Reconnecting in 10 seconds...")
210
+
211
+ global_websocket = None
212
+ global_machine_id = None
213
+ await asyncio.sleep(10)
214
+
215
+
216
+ # ==============================================================================
217
+ # 推理模式的函数 (原 app.py)
218
+ # ==============================================================================
219
+ config_file_path = "configs/ltxv-13b-0.9.7-distilled.yaml"
220
+ with open(config_file_path, "r") as file:
221
+ PIPELINE_CONFIG_YAML = yaml.safe_load(file)
222
+
223
+ LTX_REPO = "Lightricks/LTX-Video"
224
+ MAX_IMAGE_SIZE = PIPELINE_CONFIG_YAML.get("max_resolution", 1280)
225
+ MAX_NUM_FRAMES = 257
226
+ FPS = 30.0
227
+
228
+ # 全局变量以缓存加载的模型
229
+ pipeline_instance = None
230
+ latent_upsampler_instance = None
231
+ models_dir = "downloaded_models_gradio_cpu_init"
232
+ Path(models_dir).mkdir(parents=True, exist_ok=True)
233
+ output_dir = "output" # 所有模式共用的输出目录
234
+ Path(output_dir).mkdir(parents=True, exist_ok=True)
235
+
236
+ def initialize_models():
237
+ """加载并初始化所有AI模型(如果尚未加载)。"""
238
+ global pipeline_instance, latent_upsampler_instance
239
+
240
+ if pipeline_instance is not None:
241
+ logger.info("Models already initialized.")
242
+ return
243
+
244
+ logger.info("Initializing models for the first time...")
245
+ logger.info("Downloading models (if not present)...")
246
+ distilled_model_actual_path = hf_hub_download(
247
+ repo_id=LTX_REPO, filename=PIPELINE_CONFIG_YAML["checkpoint_path"], local_dir=models_dir, local_dir_use_symlinks=False
248
+ )
249
+ PIPELINE_CONFIG_YAML["checkpoint_path"] = distilled_model_actual_path
250
+ logger.info(f"Distilled model path: {distilled_model_actual_path}")
251
+
252
+ SPATIAL_UPSCALER_FILENAME = PIPELINE_CONFIG_YAML["spatial_upscaler_model_path"]
253
+ spatial_upscaler_actual_path = hf_hub_download(
254
+ repo_id=LTX_REPO, filename=SPATIAL_UPSCALER_FILENAME, local_dir=models_dir, local_dir_use_symlinks=False
255
+ )
256
+ PIPELINE_CONFIG_YAML["spatial_upscaler_model_path"] = spatial_upscaler_actual_path
257
+ logger.info(f"Spatial upscaler model path: {spatial_upscaler_actual_path}")
258
+
259
+ logger.info("Creating LTX Video pipeline on CPU...")
260
+ pipeline_instance = create_ltx_video_pipeline(
261
+ ckpt_path=PIPELINE_CONFIG_YAML["checkpoint_path"],
262
+ precision=PIPELINE_CONFIG_YAML["precision"],
263
+ text_encoder_model_name_or_path=PIPELINE_CONFIG_YAML["text_encoder_model_name_or_path"],
264
+ sampler=PIPELINE_CONFIG_YAML["sampler"],
265
+ device="cpu",
266
+ enhance_prompt=False,
267
+ prompt_enhancer_image_caption_model_name_or_path=PIPELINE_CONFIG_YAML["prompt_enhancer_image_caption_model_name_or_path"],
268
+ prompt_enhancer_llm_model_name_or_path=PIPELINE_CONFIG_YAML["prompt_enhancer_llm_model_name_or_path"],
269
+ )
270
+ logger.info("LTX Video pipeline created on CPU.")
271
+
272
+ if PIPELINE_CONFIG_YAML.get("spatial_upscaler_model_path"):
273
+ logger.info("Creating latent upsampler on CPU...")
274
+ latent_upsampler_instance = create_latent_upsampler(
275
+ PIPELINE_CONFIG_YAML["spatial_upscaler_model_path"], device="cpu"
276
+ )
277
+ logger.info("Latent upsampler created on CPU.")
278
+
279
+ target_inference_device = "cuda" if torch.cuda.is_available() else "cpu"
280
+ logger.info(f"Moving models to target inference device: {target_inference_device}")
281
+ pipeline_instance.to(target_inference_device)
282
+ if latent_upsampler_instance:
283
+ latent_upsampler_instance.to(target_inference_device)
284
+ logger.info("Model initialization complete.")
285
+
286
+
287
+ def generate(prompt, negative_prompt="worst quality, inconsistent motion, blurry, jittery, distorted",
288
+ input_image_filepath=None, input_video_filepath=None,
289
+ height_ui=512, width_ui=704, mode="text-to-video",
290
+ duration_ui=2.0, ui_frames_to_use=9,
291
+ seed_ui=42, randomize_seed=True, ui_guidance_scale=None, improve_texture_flag=True):
292
+
293
+ # 确保模型已加载
294
+ initialize_models()
295
 
296
+ target_inference_device = "cuda" if torch.cuda.is_available() else "cpu"
297
+
298
+ if randomize_seed:
299
+ seed_ui = random.randint(0, 2**32 - 1)
300
+ seed_everething(int(seed_ui))
301
 
302
+ if ui_guidance_scale is None:
303
+ ui_guidance_scale = PIPELINE_CONFIG_YAML.get("first_pass", {}).get("guidance_scale", 1.0)
 
 
304
 
305
+ target_frames_ideal = duration_ui * FPS
306
+ target_frames_rounded = round(target_frames_ideal)
307
+ if target_frames_rounded < 1:
308
+ target_frames_rounded = 1
309
 
310
+ n_val = round((float(target_frames_rounded) - 1.0) / 8.0)
311
+ actual_num_frames = int(n_val * 8 + 1)
312
+
313
+ actual_num_frames = max(9, actual_num_frames)
314
+ actual_num_frames = min(MAX_NUM_FRAMES, actual_num_frames)
315
 
316
+ actual_height = int(height_ui)
317
+ actual_width = int(width_ui)
318
+
319
+ height_padded = ((actual_height - 1) // 32 + 1) * 32
320
+ width_padded = ((actual_width - 1) // 32 + 1) * 32
321
+ num_frames_padded = ((actual_num_frames - 2) // 8 + 1) * 8 + 1
322
 
323
+ padding_values = calculate_padding(actual_height, actual_width, height_padded, width_padded)
324
+
325
+ call_kwargs = {
326
+ "prompt": prompt, "negative_prompt": negative_prompt, "height": height_padded, "width": width_padded,
327
+ "num_frames": num_frames_padded, "frame_rate": int(FPS),
328
+ "generator": torch.Generator(device=target_inference_device).manual_seed(int(seed_ui)),
329
+ "output_type": "pt", "conditioning_items": None, "media_items": None,
330
+ "decode_timestep": PIPELINE_CONFIG_YAML["decode_timestep"], "decode_noise_scale": PIPELINE_CONFIG_YAML["decode_noise_scale"],
331
+ "stochastic_sampling": PIPELINE_CONFIG_YAML["stochastic_sampling"], "image_cond_noise_scale": 0.15,
332
+ "is_video": True, "vae_per_channel_normalize": True, "mixed_precision": (PIPELINE_CONFIG_YAML["precision"] == "mixed_precision"),
333
+ "offload_to_cpu": False, "enhance_prompt": False,
334
+ }
335
+
336
+ stg_mode_str = PIPELINE_CONFIG_YAML.get("stg_mode", "attention_values")
337
+ if stg_mode_str.lower() in ["stg_av", "attention_values"]:
338
+ call_kwargs["skip_layer_strategy"] = SkipLayerStrategy.AttentionValues
339
+ elif stg_mode_str.lower() in ["stg_as", "attention_skip"]:
340
+ call_kwargs["skip_layer_strategy"] = SkipLayerStrategy.AttentionSkip
341
+ elif stg_mode_str.lower() in ["stg_r", "residual"]:
342
+ call_kwargs["skip_layer_strategy"] = SkipLayerStrategy.Residual
343
+ elif stg_mode_str.lower() in ["stg_t", "transformer_block"]:
344
+ call_kwargs["skip_layer_strategy"] = SkipLayerStrategy.TransformerBlock
345
+ else:
346
+ raise ValueError(f"Invalid stg_mode: {stg_mode_str}")
347
+
348
+ if mode == "image-to-video" and input_image_filepath:
349
+ try:
350
+ media_tensor = load_image_to_tensor_with_resize_and_crop(input_image_filepath, actual_height, actual_width)
351
+ media_tensor = torch.nn.functional.pad(media_tensor, padding_values)
352
+ call_kwargs["conditioning_items"] = [ConditioningItem(media_tensor.to(target_inference_device), 0, 1.0)]
353
+ except Exception as e:
354
+ logger.error(f"Error loading image {input_image_filepath}: {e}")
355
+ raise RuntimeError(f"Could not load image: {e}")
356
+ elif mode == "video-to-video" and input_video_filepath:
357
+ try:
358
+ call_kwargs["media_items"] = load_media_file(
359
+ media_path=input_video_filepath, height=actual_height, width=actual_width,
360
+ max_frames=int(ui_frames_to_use), padding=padding_values
361
+ ).to(target_inference_device)
362
+ except Exception as e:
363
+ logger.error(f"Error loading video {input_video_filepath}: {e}")
364
+ raise RuntimeError(f"Could not load video: {e}")
365
+
366
+ active_latent_upsampler = latent_upsampler_instance if improve_texture_flag and latent_upsampler_instance else None
367
+ result_images_tensor = None
368
+
369
+ if improve_texture_flag:
370
+ if not active_latent_upsampler:
371
+ raise RuntimeError("Spatial upscaler model not loaded or improve_texture not selected.")
372
 
373
+ multi_scale_pipeline_obj = LTXMultiScalePipeline(pipeline_instance, active_latent_upsampler)
374
+ first_pass_args = {**PIPELINE_CONFIG_YAML.get("first_pass", {}), "guidance_scale": float(ui_guidance_scale)}
375
+ second_pass_args = {**PIPELINE_CONFIG_YAML.get("second_pass", {}), "guidance_scale": float(ui_guidance_scale)}
 
376
 
377
+ multi_scale_call_kwargs = {
378
+ **call_kwargs, "downscale_factor": PIPELINE_CONFIG_YAML["downscale_factor"],
379
+ "first_pass": first_pass_args, "second_pass": second_pass_args
380
+ }
381
 
382
+ logger.info(f"Calling multi-scale pipeline on {target_inference_device}")
383
+ result_images_tensor = multi_scale_pipeline_obj(**multi_scale_call_kwargs).images
384
+ else:
385
+ single_pass_call_kwargs = {**call_kwargs, **PIPELINE_CONFIG_YAML.get("first_pass", {}), "guidance_scale": float(ui_guidance_scale)}
386
+ logger.info(f"Calling base pipeline on {target_inference_device}")
387
+ result_images_tensor = pipeline_instance(**single_pass_call_kwargs).images
 
388
 
389
+ if result_images_tensor is None:
390
+ raise RuntimeError("Generation failed, result tensor is None.")
391
+
392
+ pad_left, pad_right, pad_top, pad_bottom = padding_values
393
+ slice_h_end = -pad_bottom if pad_bottom > 0 else None
394
+ slice_w_end = -pad_right if pad_right > 0 else None
 
 
 
 
 
395
 
396
+ result_images_tensor = result_images_tensor[:, :, :actual_num_frames, pad_top:slice_h_end, pad_left:slice_w_end]
397
+
398
+ video_np = (result_images_tensor[0].permute(1, 2, 3, 0).cpu().float().numpy() * 255).clip(0, 255).astype(np.uint8)
399
 
400
+ # 使用随机数确保文件名几乎不重复
401
+ timestamp = random.randint(10000, 99999)
402
+ output_video_path = os.path.join(output_dir, f"output_{timestamp}_{seed_ui}.mp4")
403
 
404
+ try:
405
+ with imageio.get_writer(output_video_path, fps=call_kwargs["frame_rate"], macro_block_size=1) as writer:
406
+ for frame in video_np:
407
+ writer.append_data(frame)
408
+ except Exception:
409
+ with imageio.get_writer(output_video_path, fps=call_kwargs["frame_rate"], format='FFMPEG', codec='libx264') as writer:
410
+ for frame in video_np:
411
+ writer.append_data(frame)
412
 
413
+ logger.info(f"Video saved successfully to: {output_video_path}")
414
+ return output_video_path, seed_ui
415
 
416
+ def run_inference(args):
417
+ """处理命令行参数并运行AI推理。"""
418
+ logger.info(f"Starting single-run inference...")
419
+ logger.info(f"Prompt: {args.prompt}")
420
+ logger.info(f"Mode: {args.mode}")
421
+ logger.info(f"Duration: {args.duration}s")
422
+ logger.info(f"Resolution: {args.width}x{args.height}")
423
+ logger.info(f"Output directory: {os.path.abspath(output_dir)}")
424
 
425
+ try:
426
+ output_path, used_seed = generate(
427
+ prompt=args.prompt, negative_prompt=args.negative_prompt,
428
+ input_image_filepath=args.input_image, input_video_filepath=args.input_video,
429
+ height_ui=args.height, width_ui=args.width, mode=args.mode,
430
+ duration_ui=args.duration, ui_frames_to_use=args.frames_to_use,
431
+ seed_ui=args.seed, randomize_seed=args.randomize_seed,
432
+ ui_guidance_scale=args.guidance_scale, improve_texture_flag=not args.no_improve_texture
433
+ )
434
+ logger.info(f"\n✅ Video generation completed!")
435
+ logger.info(f"📁 Output saved to: {output_path}")
436
+ logger.info(f"🎲 Used seed: {used_seed}")
437
+
438
+ except Exception as e:
439
+ logger.error(f"❌ Error during generation: {e}", exc_info=True)
440
+ sys.exit(1)
441
+
442
+
443
+ # ==============================================================================
444
+ # 主入口和参数解析
445
+ # ==============================================================================
446
+ if __name__ == "__main__":
447
+ parser = argparse.ArgumentParser(description="LTX Video Generation and Server Client")
448
 
449
+ # --- 模式选择 ---
450
+ group = parser.add_argument_group('运行模式')
451
+ group.add_argument("--listen", action="store_true", help="以监听模式运行,连接到服务器等待指令。")
452
 
453
+ # --- 监听模式参数 ---
454
+ listener_group = parser.add_argument_group('监听模式参数 (需配合 --listen)')
455
+ listener_group.add_argument("--card-id", help="用于向服务器认证的Card ID。")
456
+ listener_group.add_argument("--secret", help="用于向服务器认证的Machine Secret。")
457
+ listener_group.add_argument("--watch-dir", default=output_dir, help=f"监听新文件并自动上传的目录 (默认: {output_dir})")
458
 
459
+ # --- 推理模式参数 ---
460
+ inference_group = parser.add_argument_group('推理模式参数 (默认模式)')
461
+ inference_group.add_argument("--prompt", help="用于视频生成的文本提示。")
462
+ inference_group.add_argument("--negative-prompt", default="worst quality, inconsistent motion, blurry, jittery, distorted", help="负面提示。")
463
+ inference_group.add_argument("--mode", choices=["text-to-video", "image-to-video", "video-to-video"], default="text-to-video", help="生成模式。")
464
+ inference_group.add_argument("--input-image", help="输入图像路径 (用于 image-to-video 模式)。")
465
+ inference_group.add_argument("--input-video", help="输入视频路径 (用于 video-to-video 模式)。")
466
+ inference_group.add_argument("--duration", type=float, default=2.0, help="视频时长 (秒, 0.3-8.5)。")
467
+ inference_group.add_argument("--height", type=int, default=512, help="视频高度 (将被调整为32的倍数)。")
468
+ inference_group.add_argument("--width", type=int, default=704, help="视频宽度 (将被调整为32的倍数)。")
469
+ inference_group.add_argument("--seed", type=int, default=42, help="随机种子。")
470
+ inference_group.add_argument("--randomize-seed", action="store_true", help="使用一个随机的种子。")
471
+ inference_group.add_argument("--guidance-scale", type=float, help="引导比例。")
472
+ inference_group.add_argument("--no-improve-texture", action="store_true", help="禁用纹理增强 (更快,但质量可能较低)。")
473
+ inference_group.add_argument("--frames-to-use", type=int, default=9, help="从输入视频中使用多少帧 (用于 video-to-video)。")
474
+
475
+ args = parser.parse_args()
476
+
477
+ # 根据模式分发任务
478
+ if args.listen:
479
+ if not args.card_id or not args.secret:
480
+ parser.error("--card-id 和 --secret 是 --listen 模式的必需参数。")
481
+ logger.info(f"启动监听模式... Card ID: {args.card_id}, Watch Dir: {args.watch_dir}")
482
+ try:
483
+ asyncio.run(start_listener_mode(args.card_id, args.secret, args.watch_dir))
484
+ except KeyboardInterrupt:
485
+ logger.info("监听模式已停止。")
486
+ else:
487
+ if not args.prompt:
488
+ parser.error("--prompt 是推理模式的必需参数。")
489
+
490
+ # 确保尺寸是32的倍数
491
+ args.height = ((args.height - 1) // 32 + 1) * 32
492
+ args.width = ((args.width - 1) // 32 + 1) * 32
493
+
494
+ run_inference(args)