Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,5 @@
|
|
1 |
-
# ==============================================================================
|
2 |
-
# 统一入口和依赖项
|
3 |
-
# ==============================================================================
|
4 |
import torch
|
|
|
5 |
import numpy as np
|
6 |
import random
|
7 |
import os
|
@@ -10,279 +8,48 @@ import argparse
|
|
10 |
from pathlib import Path
|
11 |
import imageio
|
12 |
import tempfile
|
13 |
-
|
14 |
-
|
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 |
-
|
68 |
-
|
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 |
-
|
87 |
"""
|
88 |
-
|
89 |
"""
|
90 |
-
|
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 |
-
|
99 |
-
await asyncio.sleep(5) # 每5秒检查一次
|
100 |
-
try:
|
101 |
-
if not os.path.isdir(dir_to_watch):
|
102 |
-
continue
|
103 |
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
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 |
-
|
140 |
-
|
|
|
|
|
|
|
|
|
|
|
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 |
-
|
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 |
-
|
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,
|
@@ -290,10 +57,18 @@ def generate(prompt, negative_prompt="worst quality, inconsistent motion, blurry
|
|
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 |
-
|
295 |
-
|
296 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
297 |
|
298 |
if randomize_seed:
|
299 |
seed_ui = random.randint(0, 2**32 - 1)
|
@@ -305,190 +80,281 @@ def generate(prompt, negative_prompt="worst quality, inconsistent motion, blurry
|
|
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 |
-
|
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 |
-
|
364 |
raise RuntimeError(f"Could not load video: {e}")
|
365 |
|
366 |
-
|
|
|
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 |
-
|
378 |
-
|
379 |
-
|
380 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
381 |
|
382 |
-
|
383 |
-
|
384 |
-
|
385 |
-
single_pass_call_kwargs
|
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
|
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 |
-
|
395 |
-
|
396 |
-
|
|
|
|
|
|
|
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 =
|
|
|
403 |
|
404 |
try:
|
405 |
-
with imageio.get_writer(output_video_path, fps=call_kwargs["frame_rate"], macro_block_size=1) as
|
406 |
-
for
|
407 |
-
|
408 |
-
|
409 |
-
|
410 |
-
|
411 |
-
|
412 |
-
|
413 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
414 |
return output_video_path, seed_ui
|
415 |
|
416 |
-
def
|
417 |
-
""
|
418 |
-
|
419 |
-
|
420 |
-
|
421 |
-
|
422 |
-
|
423 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
424 |
|
425 |
try:
|
426 |
output_path, used_seed = generate(
|
427 |
-
prompt=args.prompt,
|
428 |
-
|
429 |
-
|
430 |
-
|
431 |
-
|
432 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
433 |
)
|
434 |
-
|
435 |
-
|
436 |
-
|
|
|
437 |
|
438 |
except Exception as e:
|
439 |
-
|
440 |
-
|
441 |
-
|
442 |
|
443 |
-
# ==============================================================================
|
444 |
-
# 主入口和参数解析
|
445 |
-
# ==============================================================================
|
446 |
if __name__ == "__main__":
|
447 |
-
|
|
|
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)
|
|
|
|
|
|
|
|
|
1 |
import torch
|
2 |
+
|
3 |
import numpy as np
|
4 |
import random
|
5 |
import os
|
|
|
8 |
from pathlib import Path
|
9 |
import imageio
|
10 |
import tempfile
|
11 |
+
if latent_upsampler_instance:
|
12 |
+
latent_upsampler_instance.to(target_inference_device)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
|
|
|
|
|
|
14 |
|
15 |
+
# --- Helper function for dimension calculation ---
|
16 |
+
MIN_DIM_SLIDER = 256
|
17 |
+
TARGET_FIXED_SIDE = 768
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
+
def calculate_new_dimensions(orig_w, orig_h):
|
20 |
"""
|
21 |
+
both are multiples of 32, and within [MIN_DIM_SLIDER, MAX_IMAGE_SIZE].
|
22 |
"""
|
23 |
+
if orig_w == 0 or orig_h == 0:
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
+
return int(TARGET_FIXED_SIDE), int(TARGET_FIXED_SIDE)
|
|
|
|
|
|
|
|
|
26 |
|
27 |
+
if orig_w >= orig_h:
|
28 |
+
new_h = TARGET_FIXED_SIDE
|
29 |
+
aspect_ratio = orig_w / orig_h
|
30 |
+
new_w_ideal = new_h * aspect_ratio
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
|
|
|
|
32 |
|
33 |
+
new_w = round(new_w_ideal / 32) * 32
|
34 |
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
+
new_w = max(MIN_DIM_SLIDER, min(new_w, MAX_IMAGE_SIZE))
|
37 |
+
|
38 |
+
new_h = max(MIN_DIM_SLIDER, min(new_h, MAX_IMAGE_SIZE))
|
39 |
+
else:
|
40 |
+
new_w = TARGET_FIXED_SIDE
|
41 |
+
aspect_ratio = orig_h / orig_w
|
42 |
+
new_h_ideal = new_w * aspect_ratio
|
43 |
|
|
|
|
|
|
|
|
|
44 |
|
45 |
+
new_h = round(new_h_ideal / 32) * 32
|
|
|
|
|
46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
+
new_h = max(MIN_DIM_SLIDER, min(new_h, MAX_IMAGE_SIZE))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
+
new_w = max(MIN_DIM_SLIDER, min(new_w, MAX_IMAGE_SIZE))
|
|
|
|
|
|
|
|
|
|
|
51 |
|
52 |
+
return int(new_h), int(new_w)
|
53 |
|
54 |
def generate(prompt, negative_prompt="worst quality, inconsistent motion, blurry, jittery, distorted",
|
55 |
input_image_filepath=None, input_video_filepath=None,
|
|
|
57 |
duration_ui=2.0, ui_frames_to_use=9,
|
58 |
seed_ui=42, randomize_seed=True, ui_guidance_scale=None, improve_texture_flag=True):
|
59 |
|
60 |
+
|
61 |
+
|
62 |
+
|
63 |
+
|
64 |
+
|
65 |
+
|
66 |
+
|
67 |
+
|
68 |
+
|
69 |
+
|
70 |
+
|
71 |
+
|
72 |
|
73 |
if randomize_seed:
|
74 |
seed_ui = random.randint(0, 2**32 - 1)
|
|
|
80 |
target_frames_ideal = duration_ui * FPS
|
81 |
target_frames_rounded = round(target_frames_ideal)
|
82 |
if target_frames_rounded < 1:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
|
84 |
height_padded = ((actual_height - 1) // 32 + 1) * 32
|
85 |
width_padded = ((actual_width - 1) // 32 + 1) * 32
|
86 |
num_frames_padded = ((actual_num_frames - 2) // 8 + 1) * 8 + 1
|
87 |
+
|
88 |
+
|
89 |
|
90 |
padding_values = calculate_padding(actual_height, actual_width, height_padded, width_padded)
|
91 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
call_kwargs["conditioning_items"] = [ConditioningItem(media_tensor.to(target_inference_device), 0, 1.0)]
|
93 |
except Exception as e:
|
94 |
+
print(f"Error loading image {input_image_filepath}: {e}")
|
95 |
raise RuntimeError(f"Could not load image: {e}")
|
96 |
elif mode == "video-to-video" and input_video_filepath:
|
97 |
try:
|
98 |
call_kwargs["media_items"] = load_media_file(
|
|
|
|
|
99 |
).to(target_inference_device)
|
100 |
except Exception as e:
|
101 |
+
print(f"Error loading video {input_video_filepath}: {e}")
|
102 |
raise RuntimeError(f"Could not load video: {e}")
|
103 |
|
104 |
+
print(f"Moving models to {target_inference_device} for inference (if not already there)...")
|
105 |
+
|
106 |
result_images_tensor = None
|
|
|
107 |
if improve_texture_flag:
|
108 |
if not active_latent_upsampler:
|
109 |
+
raise RuntimeError("Spatial upscaler model not loaded or improve_texture not selected, cannot use multi-scale.")
|
110 |
|
111 |
multi_scale_pipeline_obj = LTXMultiScalePipeline(pipeline_instance, active_latent_upsampler)
|
|
|
|
|
112 |
|
113 |
+
first_pass_args = PIPELINE_CONFIG_YAML.get("first_pass", {}).copy()
|
114 |
+
first_pass_args["guidance_scale"] = float(ui_guidance_scale)
|
115 |
+
|
116 |
+
first_pass_args.pop("num_inference_steps", None)
|
117 |
+
|
118 |
+
|
119 |
+
second_pass_args = PIPELINE_CONFIG_YAML.get("second_pass", {}).copy()
|
120 |
+
second_pass_args["guidance_scale"] = float(ui_guidance_scale)
|
121 |
+
|
122 |
+
second_pass_args.pop("num_inference_steps", None)
|
123 |
+
|
124 |
+
multi_scale_call_kwargs = call_kwargs.copy()
|
125 |
+
first_pass_config_from_yaml = PIPELINE_CONFIG_YAML.get("first_pass", {})
|
126 |
+
|
127 |
+
single_pass_call_kwargs["timesteps"] = first_pass_config_from_yaml.get("timesteps")
|
128 |
+
single_pass_call_kwargs["guidance_scale"] = float(ui_guidance_scale)
|
129 |
+
single_pass_call_kwargs["stg_scale"] = first_pass_config_from_yaml.get("stg_scale")
|
130 |
+
single_pass_call_kwargs["rescaling_scale"] = first_pass_config_from_yaml.get("rescaling_scale")
|
131 |
+
single_pass_call_kwargs["skip_block_list"] = first_pass_config_from_yaml.get("skip_block_list")
|
132 |
|
133 |
+
|
134 |
+
single_pass_call_kwargs.pop("num_inference_steps", None)
|
135 |
+
single_pass_call_kwargs.pop("first_pass", None)
|
136 |
+
single_pass_call_kwargs.pop("second_pass", None)
|
|
|
137 |
result_images_tensor = pipeline_instance(**single_pass_call_kwargs).images
|
138 |
|
139 |
if result_images_tensor is None:
|
140 |
+
raise RuntimeError("Generation failed.")
|
141 |
|
142 |
pad_left, pad_right, pad_top, pad_bottom = padding_values
|
143 |
slice_h_end = -pad_bottom if pad_bottom > 0 else None
|
144 |
+
]
|
145 |
+
|
146 |
+
video_np = result_images_tensor[0].permute(1, 2, 3, 0).cpu().float().numpy()
|
147 |
+
|
148 |
+
video_np = np.clip(video_np, 0, 1)
|
149 |
+
video_np = (video_np * 255).astype(np.uint8)
|
150 |
|
|
|
|
|
|
|
151 |
timestamp = random.randint(10000, 99999)
|
152 |
+
output_video_path = f"output_{timestamp}.mp4"
|
153 |
+
|
154 |
|
155 |
try:
|
156 |
+
with imageio.get_writer(output_video_path, fps=call_kwargs["frame_rate"], macro_block_size=1) as video_writer:
|
157 |
+
for frame_idx in range(video_np.shape[0]):
|
158 |
+
|
159 |
+
video_writer.append_data(video_np[frame_idx])
|
160 |
+
if frame_idx % 10 == 0:
|
161 |
+
print(f"Saving frame {frame_idx + 1}/{video_np.shape[0]}")
|
162 |
+
except Exception as e:
|
163 |
+
print(f"Error saving video with macro_block_size=1: {e}")
|
164 |
+
try:
|
165 |
+
with imageio.get_writer(output_video_path, fps=call_kwargs["frame_rate"], format='FFMPEG', codec='libx264', quality=8) as video_writer:
|
166 |
+
for frame_idx in range(video_np.shape[0]):
|
167 |
+
|
168 |
+
video_writer.append_data(video_np[frame_idx])
|
169 |
+
if frame_idx % 10 == 0:
|
170 |
+
print(f"Saving frame {frame_idx + 1}/{video_np.shape[0]} (fallback)")
|
171 |
+
except Exception as e2:
|
172 |
+
print(f"Fallback video saving error: {e2}")
|
173 |
+
raise RuntimeError(f"Failed to save video: {e2}")
|
174 |
+
|
175 |
return output_video_path, seed_ui
|
176 |
|
177 |
+
def main():
|
178 |
+
parser = argparse.ArgumentParser(description="LTX Video Generation from Command Line")
|
179 |
+
parser.add_argument("--prompt", required=True, help="Text prompt for video generation")
|
180 |
+
parser.add_argument("--negative-prompt", default="worst quality, inconsistent motion, blurry, jittery, distorted",
|
181 |
+
help="Negative prompt")
|
182 |
+
parser.add_argument("--mode", choices=["text-to-video", "image-to-video", "video-to-video"],
|
183 |
+
default="text-to-video", help="Generation mode")
|
184 |
+
parser.add_argument("--input-image", help="Input image path for image-to-video mode")
|
185 |
+
parser.add_argument("--input-video", help="Input video path for video-to-video mode")
|
186 |
+
parser.add_argument("--duration", type=float, default=2.0, help="Video duration in seconds (0.3-8.5)")
|
187 |
+
parser.add_argument("--height", type=int, default=512, help="Video height (must be divisible by 32)")
|
188 |
+
parser.add_argument("--width", type=int, default=704, help="Video width (must be divisible by 32)")
|
189 |
+
parser.add_argument("--seed", type=int, default=42, help="Random seed")
|
190 |
+
parser.add_argument("--randomize-seed", action="store_true", help="Use random seed")
|
191 |
+
parser.add_argument("--guidance-scale", type=float, help="Guidance scale for generation")
|
192 |
+
parser.add_argument("--no-improve-texture", action="store_true", help="Disable texture improvement (faster)")
|
193 |
+
parser.add_argument("--frames-to-use", type=int, default=9, help="Frames to use from input video (for video-to-video)")
|
194 |
+
|
195 |
+
|
196 |
+
|
197 |
+
|
198 |
+
args = parser.parse_args()
|
199 |
+
|
200 |
+
|
201 |
+
|
202 |
+
|
203 |
+
|
204 |
+
|
205 |
+
|
206 |
+
|
207 |
+
|
208 |
+
|
209 |
+
|
210 |
+
|
211 |
+
|
212 |
+
|
213 |
+
|
214 |
+
|
215 |
+
|
216 |
+
|
217 |
+
|
218 |
+
|
219 |
+
|
220 |
+
|
221 |
+
|
222 |
+
|
223 |
+
|
224 |
+
|
225 |
+
|
226 |
+
|
227 |
+
|
228 |
+
|
229 |
+
|
230 |
+
|
231 |
+
|
232 |
+
|
233 |
+
|
234 |
+
|
235 |
+
|
236 |
+
|
237 |
+
|
238 |
+
|
239 |
+
|
240 |
+
|
241 |
+
|
242 |
+
|
243 |
+
|
244 |
+
|
245 |
+
|
246 |
+
|
247 |
+
|
248 |
+
|
249 |
+
|
250 |
+
|
251 |
+
|
252 |
+
|
253 |
+
|
254 |
+
|
255 |
+
|
256 |
+
|
257 |
+
|
258 |
+
|
259 |
+
|
260 |
+
|
261 |
+
|
262 |
+
|
263 |
+
|
264 |
+
|
265 |
+
|
266 |
+
|
267 |
+
|
268 |
+
|
269 |
+
|
270 |
+
|
271 |
+
|
272 |
+
|
273 |
+
|
274 |
+
|
275 |
+
|
276 |
+
|
277 |
+
|
278 |
+
|
279 |
+
|
280 |
+
|
281 |
+
|
282 |
+
|
283 |
+
|
284 |
+
|
285 |
+
|
286 |
+
|
287 |
+
|
288 |
+
|
289 |
+
|
290 |
+
|
291 |
+
|
292 |
+
|
293 |
+
|
294 |
+
|
295 |
+
|
296 |
+
# Validate parameters
|
297 |
+
if args.mode == "image-to-video" and not args.input_image:
|
298 |
+
print("Error: --input-image is required for image-to-video mode")
|
299 |
+
return
|
300 |
+
|
301 |
+
|
302 |
+
|
303 |
+
|
304 |
+
|
305 |
+
|
306 |
+
|
307 |
+
|
308 |
+
|
309 |
+
|
310 |
+
|
311 |
+
|
312 |
+
|
313 |
+
|
314 |
+
|
315 |
+
|
316 |
+
if args.mode == "video-to-video" and not args.input_video:
|
317 |
+
print("Error: --input-video is required for video-to-video mode")
|
318 |
+
return
|
319 |
+
|
320 |
+
|
321 |
+
# Ensure dimensions are divisible by 32
|
322 |
+
args.height = ((args.height - 1) // 32 + 1) * 32
|
323 |
+
args.width = ((args.width - 1) // 32 + 1) * 32
|
324 |
+
|
325 |
+
print(f"Starting video generation...")
|
326 |
+
print(f"Prompt: {args.prompt}")
|
327 |
+
print(f"Mode: {args.mode}")
|
328 |
+
print(f"Duration: {args.duration}s")
|
329 |
+
print(f"Resolution: {args.width}x{args.height}")
|
330 |
|
331 |
try:
|
332 |
output_path, used_seed = generate(
|
333 |
+
prompt=args.prompt,
|
334 |
+
negative_prompt=args.negative_prompt,
|
335 |
+
input_image_filepath=args.input_image,
|
336 |
+
input_video_filepath=args.input_video,
|
337 |
+
height_ui=args.height,
|
338 |
+
width_ui=args.width,
|
339 |
+
mode=args.mode,
|
340 |
+
duration_ui=args.duration,
|
341 |
+
ui_frames_to_use=args.frames_to_use,
|
342 |
+
seed_ui=args.seed,
|
343 |
+
randomize_seed=args.randomize_seed,
|
344 |
+
ui_guidance_scale=args.guidance_scale,
|
345 |
+
improve_texture_flag=not args.no_improve_texture
|
346 |
)
|
347 |
+
|
348 |
+
print(f"\nVideo generation completed!")
|
349 |
+
print(f"Output saved to: {output_path}")
|
350 |
+
print(f"Used seed: {used_seed}")
|
351 |
|
352 |
except Exception as e:
|
353 |
+
print(f"Error during generation: {e}")
|
354 |
+
raise
|
|
|
355 |
|
|
|
|
|
|
|
356 |
if __name__ == "__main__":
|
357 |
+
if os.path.exists(models_dir) and os.path.isdir(models_dir):
|
358 |
+
print(f"Model directory: {Path(models_dir).resolve()}")
|
359 |
|
360 |
+
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|