Update api/ltx_server_refactored.py
Browse files- api/ltx_server_refactored.py +333 -677
api/ltx_server_refactored.py
CHANGED
|
@@ -1,769 +1,425 @@
|
|
| 1 |
-
# ltx_server.py — VideoService (beta 1.
|
| 2 |
-
#
|
| 3 |
-
#
|
| 4 |
-
#
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
import warnings
|
| 7 |
warnings.filterwarnings("ignore", category=UserWarning)
|
| 8 |
warnings.filterwarnings("ignore", category=FutureWarning)
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
logging.set_verbosity_info()
|
| 14 |
-
logging.set_verbosity_debug()
|
| 15 |
-
LTXV_DEBUG=1
|
| 16 |
-
LTXV_FRAME_LOG_EVERY=8
|
| 17 |
-
import os, subprocess, shlex, tempfile
|
| 18 |
-
import torch
|
| 19 |
-
import json
|
| 20 |
-
import numpy as np
|
| 21 |
-
import random
|
| 22 |
-
import os
|
| 23 |
-
import shlex
|
| 24 |
-
import yaml
|
| 25 |
from typing import List, Dict
|
| 26 |
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
import imageio
|
| 28 |
-
from PIL import Image
|
| 29 |
-
import tempfile
|
| 30 |
-
from huggingface_hub import hf_hub_download
|
| 31 |
-
import sys
|
| 32 |
-
import subprocess
|
| 33 |
-
import gc
|
| 34 |
-
import shutil
|
| 35 |
-
import contextlib
|
| 36 |
-
import time
|
| 37 |
-
import traceback
|
| 38 |
from einops import rearrange
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
| 42 |
DEPS_DIR = Path("/data")
|
| 43 |
LTX_VIDEO_REPO_DIR = DEPS_DIR / "LTX-Video"
|
| 44 |
|
| 45 |
-
#
|
| 46 |
def run_setup():
|
| 47 |
setup_script_path = "setup.py"
|
| 48 |
if not os.path.exists(setup_script_path):
|
| 49 |
-
print("[DEBUG] 'setup.py' não encontrado. Pulando
|
| 50 |
return
|
| 51 |
try:
|
| 52 |
-
print("[DEBUG] Executando setup.py para dependências...")
|
| 53 |
-
subprocess.run([sys.executable, setup_script_path], check=True)
|
| 54 |
-
print("[DEBUG] Setup concluído
|
| 55 |
except subprocess.CalledProcessError as e:
|
| 56 |
-
print(f"[
|
| 57 |
sys.exit(1)
|
| 58 |
-
|
| 59 |
-
if not LTX_VIDEO_REPO_DIR.exists():
|
| 60 |
-
print(f"[DEBUG] Repositório não encontrado em {LTX_VIDEO_REPO_DIR}. Rodando setup...")
|
| 61 |
-
run_setup()
|
| 62 |
|
| 63 |
def add_deps_to_path():
|
| 64 |
repo_path = str(LTX_VIDEO_REPO_DIR.resolve())
|
| 65 |
-
if
|
| 66 |
sys.path.insert(0, repo_path)
|
| 67 |
-
print(f"[DEBUG]
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
import pynvml as nvml
|
| 72 |
-
nvml.nvmlInit()
|
| 73 |
-
handle = nvml.nvmlDeviceGetHandleByIndex(device_index)
|
| 74 |
-
try:
|
| 75 |
-
procs = nvml.nvmlDeviceGetComputeRunningProcesses_v3(handle)
|
| 76 |
-
except Exception:
|
| 77 |
-
procs = nvml.nvmlDeviceGetComputeRunningProcesses(handle)
|
| 78 |
-
results = []
|
| 79 |
-
for p in procs:
|
| 80 |
-
pid = int(p.pid)
|
| 81 |
-
used_mb = None
|
| 82 |
-
try:
|
| 83 |
-
if getattr(p, "usedGpuMemory", None) is not None and p.usedGpuMemory not in (0,):
|
| 84 |
-
used_mb = max(0, int(p.usedGpuMemory) // (1024 * 1024))
|
| 85 |
-
except Exception:
|
| 86 |
-
used_mb = None
|
| 87 |
-
name = "unknown"
|
| 88 |
-
user = "unknown"
|
| 89 |
-
try:
|
| 90 |
-
import psutil
|
| 91 |
-
pr = psutil.Process(pid)
|
| 92 |
-
name = pr.name()
|
| 93 |
-
user = pr.username()
|
| 94 |
-
except Exception:
|
| 95 |
-
pass
|
| 96 |
-
results.append({"pid": pid, "name": name, "user": user, "used_mb": used_mb})
|
| 97 |
-
nvml.nvmlShutdown()
|
| 98 |
-
return results
|
| 99 |
-
except Exception:
|
| 100 |
-
return []
|
| 101 |
-
def _query_gpu_processes_via_nvidiasmi(device_index: int) -> List[Dict]:
|
| 102 |
-
cmd = f"nvidia-smi -i {device_index} --query-compute-apps=pid,process_name,used_memory --format=csv,noheader,nounits"
|
| 103 |
-
try:
|
| 104 |
-
out = subprocess.check_output(shlex.split(cmd), stderr=subprocess.STDOUT, text=True, timeout=2.0)
|
| 105 |
-
except Exception:
|
| 106 |
-
return []
|
| 107 |
-
results = []
|
| 108 |
-
for line in out.strip().splitlines():
|
| 109 |
-
parts = [p.strip() for p in line.split(",")]
|
| 110 |
-
if len(parts) >= 3:
|
| 111 |
-
try:
|
| 112 |
-
pid = int(parts[0]); name = parts[1]; used_mb = int(parts[2])
|
| 113 |
-
user = "unknown"
|
| 114 |
-
try:
|
| 115 |
-
import psutil
|
| 116 |
-
pr = psutil.Process(pid)
|
| 117 |
-
user = pr.username()
|
| 118 |
-
except Exception:
|
| 119 |
-
pass
|
| 120 |
-
results.append({"pid": pid, "name": name, "user": user, "used_mb": used_mb})
|
| 121 |
-
except Exception:
|
| 122 |
-
continue
|
| 123 |
-
return results
|
| 124 |
-
def calculate_new_dimensions(orig_w, orig_h, divisor=8):
|
| 125 |
-
if orig_w == 0 or orig_h == 0:
|
| 126 |
-
return 512, 512
|
| 127 |
-
if orig_w >= orig_h:
|
| 128 |
-
aspect_ratio = orig_w / orig_h
|
| 129 |
-
new_h = 512
|
| 130 |
-
new_w = new_h * aspect_ratio
|
| 131 |
-
else:
|
| 132 |
-
aspect_ratio = orig_h / orig_w
|
| 133 |
-
new_w = 512
|
| 134 |
-
new_h = new_w * aspect_ratio
|
| 135 |
-
final_w = int(round(new_w / divisor)) * divisor
|
| 136 |
-
final_h = int(round(new_h / divisor)) * divisor
|
| 137 |
-
final_w = max(divisor, final_w)
|
| 138 |
-
final_h = max(divisor, final_h)
|
| 139 |
-
print(f"[Dimension Calc] Original: {orig_w}x{orig_h} -> Calculado: {new_w:.0f}x{new_h:.0f} -> Final (divisível por {divisor}): {final_w}x{final_h}")
|
| 140 |
-
return final_h, final_w
|
| 141 |
-
def handle_media_upload_for_dims(filepath, current_h, current_w):
|
| 142 |
-
# CORREÇÃO: Gradio (`gr`) não deve ser usado no backend. Retornando tupla diretamente.
|
| 143 |
-
if not filepath or not os.path.exists(str(filepath)):
|
| 144 |
-
return current_h, current_w
|
| 145 |
-
try:
|
| 146 |
-
if str(filepath).lower().endswith(('.png', '.jpg', '.jpeg', '.webp')):
|
| 147 |
-
with Image.open(filepath) as img:
|
| 148 |
-
orig_w, orig_h = img.size
|
| 149 |
-
else:
|
| 150 |
-
with imageio.get_reader(filepath) as reader:
|
| 151 |
-
meta = reader.get_meta_data()
|
| 152 |
-
orig_w, orig_h = meta.get('size', (current_w, current_h))
|
| 153 |
-
new_h, new_w = calculate_new_dimensions(orig_w, orig_h)
|
| 154 |
-
return new_h, new_w
|
| 155 |
-
except Exception as e:
|
| 156 |
-
print(f"Erro ao processar mídia para dimensões: {e}")
|
| 157 |
-
return current_h, current_w
|
| 158 |
-
def _gpu_process_table(processes: List[Dict], current_pid: int) -> str:
|
| 159 |
-
if not processes:
|
| 160 |
-
return " - Processos ativos: (nenhum)\n"
|
| 161 |
-
processes = sorted(processes, key=lambda x: (x.get("used_mb") or 0), reverse=True)
|
| 162 |
-
lines = [" - Processos ativos (PID | USER | NAME | VRAM MB):"]
|
| 163 |
-
for p in processes:
|
| 164 |
-
star = "*" if p["pid"] == current_pid else " "
|
| 165 |
-
used_str = str(p["used_mb"]) if p.get("used_mb") is not None else "N/A"
|
| 166 |
-
lines.append(f" {star} {p['pid']} | {p['user']} | {p['name']} | {used_str}")
|
| 167 |
-
return "\n".join(lines) + "\n"
|
| 168 |
-
def log_tensor_info(tensor, name="Tensor"):
|
| 169 |
-
if not isinstance(tensor, torch.Tensor):
|
| 170 |
-
print(f"\n[INFO] '{name}' não é tensor.")
|
| 171 |
-
return
|
| 172 |
-
print(f"\n--- Tensor: {name} ---")
|
| 173 |
-
print(f" - Shape: {tuple(tensor.shape)}")
|
| 174 |
-
print(f" - Dtype: {tensor.dtype}")
|
| 175 |
-
print(f" - Device: {tensor.device}")
|
| 176 |
-
if tensor.numel() > 0:
|
| 177 |
-
try:
|
| 178 |
-
print(f" - Min: {tensor.min().item():.4f} Max: {tensor.max().item():.4f} Mean: {tensor.mean().item():.4f}")
|
| 179 |
-
except Exception:
|
| 180 |
-
pass
|
| 181 |
-
print("------------------------------------------\n")
|
| 182 |
add_deps_to_path()
|
| 183 |
-
|
|
|
|
|
|
|
|
|
|
| 184 |
from ltx_video.utils.skip_layer_strategy import SkipLayerStrategy
|
| 185 |
from ltx_video.models.autoencoders.vae_encode import un_normalize_latents, normalize_latents
|
| 186 |
-
from ltx_video.pipelines.pipeline_ltx_video import adain_filter_latent
|
| 187 |
from api.ltx.inference import (
|
| 188 |
-
create_ltx_video_pipeline,
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
seed_everething,
|
| 192 |
-
calculate_padding,
|
| 193 |
-
load_media_file,
|
| 194 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
class VideoService:
|
| 196 |
def __init__(self):
|
| 197 |
t0 = time.perf_counter()
|
| 198 |
-
print("[
|
| 199 |
-
self.debug = os.getenv("LTXV_DEBUG", "1") == "1"
|
| 200 |
-
self.frame_log_every = int(os.getenv("LTXV_FRAME_LOG_EVERY", "8"))
|
| 201 |
-
self.config = self._load_config()
|
| 202 |
-
print(f"[DEBUG] Config carregada (precision={self.config.get('precision')}, sampler={self.config.get('sampler')})")
|
| 203 |
self.device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 204 |
-
|
| 205 |
-
self.
|
| 206 |
-
self._tmp_dirs
|
| 207 |
-
|
| 208 |
self.pipeline, self.latent_upsampler = self._load_models()
|
| 209 |
-
print(f"[DEBUG] Pipeline e Upsampler carregados. Upsampler ativo? {bool(self.latent_upsampler)}")
|
| 210 |
-
|
| 211 |
-
print(f"[DEBUG] Movendo modelos para {self.device}...")
|
| 212 |
self.pipeline.to(self.device)
|
| 213 |
-
if self.latent_upsampler:
|
| 214 |
-
self.latent_upsampler.to(self.device)
|
| 215 |
-
|
| 216 |
self._apply_precision_policy()
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
self.pipeline,
|
| 221 |
-
device=self.device,
|
| 222 |
-
autocast_dtype=self.runtime_autocast_dtype
|
| 223 |
-
)
|
| 224 |
-
print(f"[DEBUG] VAE manager conectado: has_vae={hasattr(self.pipeline, 'vae')} device={self.device}")
|
| 225 |
-
|
| 226 |
-
if self.device == "cuda":
|
| 227 |
-
torch.cuda.empty_cache()
|
| 228 |
-
self._log_gpu_memory("Após carregar modelos")
|
| 229 |
-
|
| 230 |
-
print(f"[DEBUG] VideoService pronto. boot_time={time.perf_counter()-t0:.3f}s")
|
| 231 |
-
|
| 232 |
-
def _log_gpu_memory(self, stage_name: str):
|
| 233 |
-
if self.device != "cuda":
|
| 234 |
-
return
|
| 235 |
-
device_index = torch.cuda.current_device() if torch.cuda.is_available() else 0
|
| 236 |
-
current_reserved_b = torch.cuda.memory_reserved(device_index)
|
| 237 |
-
current_reserved_mb = current_reserved_b / (1024 ** 2)
|
| 238 |
-
total_memory_b = torch.cuda.get_device_properties(device_index).total_memory
|
| 239 |
-
total_memory_mb = total_memory_b / (1024 ** 2)
|
| 240 |
-
peak_reserved_mb = torch.cuda.max_memory_reserved(device_index) / (1024 ** 2)
|
| 241 |
-
delta_mb = current_reserved_mb - getattr(self, "last_memory_reserved_mb", 0.0)
|
| 242 |
-
processes = _query_gpu_processes_via_nvml(device_index) or _query_gpu_processes_via_nvidiasmi(device_index)
|
| 243 |
-
print(f"\n--- [LOG GPU] {stage_name} (cuda:{device_index}) ---")
|
| 244 |
-
print(f" - Reservado: {current_reserved_mb:.2f} MB / {total_memory_mb:.2f} MB (Δ={delta_mb:+.2f} MB)")
|
| 245 |
-
if peak_reserved_mb > getattr(self, "last_memory_reserved_mb", 0.0):
|
| 246 |
-
print(f" - Pico reservado (nesta fase): {peak_reserved_mb:.2f} MB")
|
| 247 |
-
print(_gpu_process_table(processes, os.getpid()), end="")
|
| 248 |
-
print("--------------------------------------------------\n")
|
| 249 |
-
self.last_memory_reserved_mb = current_reserved_mb
|
| 250 |
-
|
| 251 |
-
def _register_tmp_dir(self, d: str):
|
| 252 |
-
if d and os.path.isdir(d):
|
| 253 |
-
self._tmp_dirs.add(d); print(f"[DEBUG] Registrado tmp dir: {d}")
|
| 254 |
-
|
| 255 |
-
def _register_tmp_file(self, f: str):
|
| 256 |
-
if f and os.path.exists(f):
|
| 257 |
-
self._tmp_files.add(f); print(f"[DEBUG] Registrado tmp file: {f}")
|
| 258 |
-
|
| 259 |
-
def finalize(self, keep_paths=None, extra_paths=None, clear_gpu=True):
|
| 260 |
-
print("[DEBUG] Finalize: iniciando limpeza...")
|
| 261 |
-
keep = set(keep_paths or []); extras = set(extra_paths or [])
|
| 262 |
-
removed_files = 0
|
| 263 |
-
for f in list(self._tmp_files | extras):
|
| 264 |
-
try:
|
| 265 |
-
if f not in keep and os.path.isfile(f):
|
| 266 |
-
os.remove(f); removed_files += 1; print(f"[DEBUG] Removido arquivo tmp: {f}")
|
| 267 |
-
except Exception as e:
|
| 268 |
-
print(f"[DEBUG] Falha removendo arquivo {f}: {e}")
|
| 269 |
-
finally:
|
| 270 |
-
self._tmp_files.discard(f)
|
| 271 |
-
removed_dirs = 0
|
| 272 |
-
for d in list(self._tmp_dirs):
|
| 273 |
-
try:
|
| 274 |
-
if d not in keep and os.path.isdir(d):
|
| 275 |
-
shutil.rmtree(d, ignore_errors=True); removed_dirs += 1; print(f"[DEBUG] Removido diretório tmp: {d}")
|
| 276 |
-
except Exception as e:
|
| 277 |
-
print(f"[DEBUG] Falha removendo diretório {d}: {e}")
|
| 278 |
-
finally:
|
| 279 |
-
self._tmp_dirs.discard(d)
|
| 280 |
-
print(f"[DEBUG] Finalize: arquivos removidos={removed_files}, dirs removidos={removed_dirs}")
|
| 281 |
-
gc.collect()
|
| 282 |
-
try:
|
| 283 |
-
if clear_gpu and torch.cuda.is_available():
|
| 284 |
-
torch.cuda.empty_cache()
|
| 285 |
-
try:
|
| 286 |
-
torch.cuda.ipc_collect()
|
| 287 |
-
except Exception:
|
| 288 |
-
pass
|
| 289 |
-
except Exception as e:
|
| 290 |
-
print(f"[DEBUG] Finalize: limpeza GPU falhou: {e}")
|
| 291 |
-
try:
|
| 292 |
-
self._log_gpu_memory("Após finalize")
|
| 293 |
-
except Exception as e:
|
| 294 |
-
print(f"[DEBUG] Log GPU pós-finalize falhou: {e}")
|
| 295 |
|
| 296 |
def _load_config(self):
|
|
|
|
| 297 |
base = LTX_VIDEO_REPO_DIR / "configs"
|
| 298 |
candidates = [
|
| 299 |
base / "ltxv-13b-0.9.8-dev-fp8.yaml",
|
| 300 |
base / "ltxv-13b-0.9.8-distilled-fp8.yaml",
|
| 301 |
base / "ltxv-13b-0.9.8-distilled.yaml",
|
| 302 |
]
|
| 303 |
-
for
|
| 304 |
-
if
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
return yaml.safe_load(file)
|
| 308 |
-
cfg = base / "ltxv-13b-0.9.8-distilled-fp8.yaml"
|
| 309 |
-
print(f"[DEBUG] Config fallback: {cfg}")
|
| 310 |
-
with open(cfg, "r") as file:
|
| 311 |
-
return yaml.safe_load(file)
|
| 312 |
|
| 313 |
def _load_models(self):
|
|
|
|
| 314 |
t0 = time.perf_counter()
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
repo_id=LTX_REPO,
|
| 319 |
-
filename=self.config["checkpoint_path"],
|
| 320 |
-
local_dir=os.getenv("HF_HOME"),
|
| 321 |
-
cache_dir=os.getenv("HF_HOME_CACHE"),
|
| 322 |
-
token=os.getenv("HF_TOKEN"),
|
| 323 |
-
)
|
| 324 |
-
self.config["checkpoint_path"] = distilled_model_path
|
| 325 |
-
print(f"[DEBUG] Checkpoint em: {distilled_model_path}")
|
| 326 |
-
|
| 327 |
-
print("[DEBUG] Baixando upscaler espacial...")
|
| 328 |
-
spatial_upscaler_path = hf_hub_download(
|
| 329 |
-
repo_id=LTX_REPO,
|
| 330 |
-
filename=self.config["spatial_upscaler_model_path"],
|
| 331 |
-
local_dir=os.getenv("HF_HOME"),
|
| 332 |
-
cache_dir=os.getenv("HF_HOME_CACHE"),
|
| 333 |
-
token=os.getenv("HF_TOKEN")
|
| 334 |
-
)
|
| 335 |
-
self.config["spatial_upscaler_model_path"] = spatial_upscaler_path
|
| 336 |
-
print(f"[DEBUG] Upscaler em: {spatial_upscaler_path}")
|
| 337 |
-
|
| 338 |
-
print("[DEBUG] Construindo pipeline...")
|
| 339 |
pipeline = create_ltx_video_pipeline(
|
| 340 |
-
ckpt_path=
|
| 341 |
precision=self.config["precision"],
|
| 342 |
text_encoder_model_name_or_path=self.config["text_encoder_model_name_or_path"],
|
| 343 |
sampler=self.config["sampler"],
|
| 344 |
-
device="cpu"
|
| 345 |
-
enhance_prompt=False,
|
| 346 |
-
prompt_enhancer_image_caption_model_name_or_path=self.config["prompt_enhancer_image_caption_model_name_or_path"],
|
| 347 |
-
prompt_enhancer_llm_model_name_or_path=self.config["prompt_enhancer_llm_model_name_or_path"],
|
| 348 |
)
|
| 349 |
-
|
| 350 |
-
|
| 351 |
latent_upsampler = None
|
| 352 |
if self.config.get("spatial_upscaler_model_path"):
|
| 353 |
-
|
| 354 |
-
latent_upsampler = create_latent_upsampler(
|
| 355 |
-
|
| 356 |
-
print(f"[DEBUG]
|
| 357 |
return pipeline, latent_upsampler
|
| 358 |
|
| 359 |
-
def _promote_fp8_weights_to_bf16(self, module):
|
| 360 |
-
if not isinstance(module, torch.nn.Module):
|
| 361 |
-
print("[DEBUG] Promoção FP8→BF16 ignorada: alvo não é nn.Module.")
|
| 362 |
-
return
|
| 363 |
-
f8 = getattr(torch, "float8_e4m3fn", None)
|
| 364 |
-
if f8 is None:
|
| 365 |
-
print("[DEBUG] torch.float8_e4m3fn indisponível.")
|
| 366 |
-
return
|
| 367 |
-
p_cnt = b_cnt = 0
|
| 368 |
-
for _, p in module.named_parameters(recurse=True):
|
| 369 |
-
try:
|
| 370 |
-
if p.dtype == f8:
|
| 371 |
-
with torch.no_grad():
|
| 372 |
-
p.data = p.data.to(torch.bfloat16); p_cnt += 1
|
| 373 |
-
except Exception:
|
| 374 |
-
pass
|
| 375 |
-
for _, b in module.named_buffers(recurse=True):
|
| 376 |
-
try:
|
| 377 |
-
if hasattr(b, "dtype") and b.dtype == f8:
|
| 378 |
-
b.data = b.data.to(torch.bfloat16); b_cnt += 1
|
| 379 |
-
except Exception:
|
| 380 |
-
pass
|
| 381 |
-
print(f"[DEBUG] FP8→BF16: params_promoted={p_cnt}, buffers_promoted={b_cnt}")
|
| 382 |
-
|
| 383 |
-
@torch.no_grad()
|
| 384 |
-
def _upsample_latents_internal(self, latents: torch.Tensor) -> torch.Tensor:
|
| 385 |
-
if not self.latent_upsampler:
|
| 386 |
-
raise ValueError("Latent Upsampler não está carregado.")
|
| 387 |
-
self.latent_upsampler.to(self.device)
|
| 388 |
-
self.pipeline.vae.to(self.device)
|
| 389 |
-
print(f"[DEBUG-UPSAMPLE] Shape de entrada: {tuple(latents.shape)}")
|
| 390 |
-
latents = un_normalize_latents(latents, self.pipeline.vae, vae_per_channel_normalize=True)
|
| 391 |
-
upsampled_latents = self.latent_upsampler(latents)
|
| 392 |
-
upsampled_latents = normalize_latents(upsampled_latents, self.pipeline.vae, vae_per_channel_normalize=True)
|
| 393 |
-
print(f"[DEBUG-UPSAMPLE] Shape de saída: {tuple(upsampled_latents.shape)}")
|
| 394 |
-
return upsampled_latents
|
| 395 |
-
|
| 396 |
def _apply_precision_policy(self):
|
| 397 |
prec = str(self.config.get("precision", "")).lower()
|
| 398 |
self.runtime_autocast_dtype = torch.float32
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
tensor = torch.nn.functional.pad(tensor, padding_values)
|
| 425 |
-
out = tensor.to(self.device, dtype=self.runtime_autocast_dtype) if self.device == "cuda" else tensor.to(self.device)
|
| 426 |
-
print(f"[DEBUG] Cond shape={tuple(out.shape)} dtype={out.dtype} device={out.device}")
|
| 427 |
-
return out
|
| 428 |
|
| 429 |
def _dividir_latentes_por_tamanho(self, latents_brutos, num_latente_por_chunk: int, overlap: int = 1):
|
| 430 |
-
|
|
|
|
|
|
|
|
|
|
| 431 |
chunks = []
|
| 432 |
-
if num_latente_por_chunk >= sum_latent:
|
| 433 |
-
return [latents_brutos.clone().detach()] # CORREÇÃO: Retornar uma lista e clonar
|
| 434 |
-
# CORREÇÃO: Lógica de chunking simplificada e corrigida para evitar estouro de índice
|
| 435 |
start = 0
|
| 436 |
-
while start <
|
| 437 |
-
end = min(start + num_latente_por_chunk,
|
| 438 |
-
#
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
# O chunk a ser processado vai de `overlap_start` até `end`
|
| 442 |
-
# mas o chunk "real" para junção posterior seria de `start` a `end`
|
| 443 |
-
# A lógica atual já faz um overlap simples, vamos refinar
|
| 444 |
-
effective_end = min(start + num_latente_por_chunk, sum_latent)
|
| 445 |
-
chunk = latents_brutos[:, :, start:effective_end, :, :].clone().detach()
|
| 446 |
-
|
| 447 |
-
# Adiciona overlap no final se não for o último chunk
|
| 448 |
-
if effective_end < sum_latent:
|
| 449 |
-
overlap_end = min(effective_end + overlap, sum_latent)
|
| 450 |
-
chunk = latents_brutos[:, :, start:overlap_end, :, :].clone().detach()
|
| 451 |
-
|
| 452 |
-
print(f"[DEBUG] Chunk: start={start}, end={chunk.shape[2]}, total_latents={sum_latent}")
|
| 453 |
chunks.append(chunk)
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
if start + num_latente_por_chunk >= sum_latent:
|
| 457 |
-
break
|
| 458 |
-
start += num_latente_por_chunk
|
| 459 |
-
|
| 460 |
return chunks
|
| 461 |
-
|
| 462 |
def _get_total_frames(self, video_path: str) -> int:
|
| 463 |
-
cmd = [
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
|
|
|
| 469 |
|
| 470 |
def _gerar_lista_com_transicoes(self, pasta: str, video_paths: list[str], crossfade_frames: int = 8) -> list[str]:
|
| 471 |
-
|
| 472 |
-
|
| 473 |
-
|
| 474 |
-
|
| 475 |
-
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
|
| 481 |
-
|
| 482 |
-
|
| 483 |
-
|
| 484 |
-
|
| 485 |
-
|
| 486 |
-
|
| 487 |
-
# Pular poda se não houver frames suficientes
|
| 488 |
-
if start_frame >= end_frame:
|
| 489 |
-
continue
|
| 490 |
-
|
| 491 |
-
cmd = [
|
| 492 |
-
'ffmpeg', '-y', '-hide_banner', '-loglevel', 'error', '-i', base,
|
| 493 |
-
'-vf', f'trim=start_frame={start_frame}:end_frame={end_frame},setpts=PTS-STARTPTS',
|
| 494 |
-
'-an', video_podado
|
| 495 |
-
]
|
| 496 |
-
subprocess.run(cmd, check=True)
|
| 497 |
-
videos_podados.append(video_podado)
|
| 498 |
-
|
| 499 |
-
# Agora, cria as transições e monta a lista final
|
| 500 |
-
lista_final = [videos_podados[0]]
|
| 501 |
for i in range(len(video_paths) - 1):
|
| 502 |
-
|
| 503 |
-
|
| 504 |
|
| 505 |
-
|
| 506 |
-
|
| 507 |
-
|
| 508 |
-
|
| 509 |
-
|
| 510 |
-
|
| 511 |
-
|
| 512 |
-
|
| 513 |
-
subprocess.run(
|
| 514 |
-
|
| 515 |
-
#
|
| 516 |
-
|
| 517 |
-
|
| 518 |
-
|
| 519 |
-
|
| 520 |
-
]
|
| 521 |
-
|
| 522 |
-
|
| 523 |
-
|
| 524 |
-
|
| 525 |
-
|
| 526 |
-
'ffmpeg
|
| 527 |
-
|
| 528 |
-
|
| 529 |
-
|
| 530 |
-
|
| 531 |
-
|
| 532 |
-
|
| 533 |
-
|
| 534 |
-
|
| 535 |
-
|
| 536 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 537 |
|
| 538 |
def _concat_mp4s_no_reencode(self, mp4_list: List[str], out_path: str):
|
| 539 |
-
if not mp4_list:
|
| 540 |
-
raise ValueError("A lista de MP4s para concatenar está vazia.")
|
| 541 |
-
# Se houver apenas um vídeo, apenas o copie/mova
|
| 542 |
if len(mp4_list) == 1:
|
| 543 |
shutil.move(mp4_list[0], out_path)
|
| 544 |
-
print(f"[DEBUG] Apenas um vídeo, movido para: {out_path}")
|
| 545 |
return
|
| 546 |
-
|
| 547 |
-
with tempfile.NamedTemporaryFile("w", delete=False, suffix=".txt") as f:
|
| 548 |
for mp4 in mp4_list:
|
| 549 |
f.write(f"file '{os.path.abspath(mp4)}'\n")
|
| 550 |
list_path = f.name
|
| 551 |
-
|
| 552 |
cmd = f"ffmpeg -y -f concat -safe 0 -i {list_path} -c copy {out_path}"
|
| 553 |
-
print(f"[DEBUG] Concat: {cmd}")
|
| 554 |
-
|
| 555 |
try:
|
| 556 |
-
subprocess.
|
|
|
|
|
|
|
|
|
|
| 557 |
finally:
|
| 558 |
-
|
| 559 |
-
|
| 560 |
-
|
| 561 |
-
|
| 562 |
-
|
| 563 |
-
|
| 564 |
-
|
| 565 |
-
|
| 566 |
-
|
| 567 |
-
|
| 568 |
-
|
| 569 |
-
|
| 570 |
-
|
| 571 |
-
|
| 572 |
-
|
| 573 |
-
|
| 574 |
-
input_video_filepath=None,
|
| 575 |
-
height=512,
|
| 576 |
-
width=704,
|
| 577 |
-
duration=2.0,
|
| 578 |
-
frames_to_use=9, # Parâmetro não utilizado, mas mantido por consistência
|
| 579 |
-
seed=42,
|
| 580 |
-
randomize_seed=True,
|
| 581 |
-
guidance_scale=3.0,
|
| 582 |
-
improve_texture=True,
|
| 583 |
-
progress_callback=None,
|
| 584 |
-
external_decode=True, # Parâmetro não utilizado, mas mantido
|
| 585 |
-
):
|
| 586 |
-
t_all = time.perf_counter()
|
| 587 |
-
print(f"[DEBUG] generate() begin mode={mode} improve_texture={improve_texture}")
|
| 588 |
-
if self.device == "cuda":
|
| 589 |
-
torch.cuda.empty_cache(); torch.cuda.reset_peak_memory_stats()
|
| 590 |
-
self._log_gpu_memory("Início da Geração")
|
| 591 |
-
|
| 592 |
-
if mode == "image-to-video" and not start_image_filepath:
|
| 593 |
-
raise ValueError("A imagem de início é obrigatória para o modo image-to-video")
|
| 594 |
-
used_seed = random.randint(0, 2**32 - 1) if randomize_seed else int(seed)
|
| 595 |
-
seed_everething(used_seed); print(f"[DEBUG] Seed usado: {used_seed}")
|
| 596 |
-
FPS = 24.0; MAX_NUM_FRAMES = 2570
|
| 597 |
-
target_frames_rounded = round(duration * FPS)
|
| 598 |
-
n_val = round((float(target_frames_rounded) - 1.0) / 8.0)
|
| 599 |
-
actual_num_frames = max(9, min(MAX_NUM_FRAMES, int(n_val * 8 + 1)))
|
| 600 |
-
height_padded = ((height - 1) // 8 + 1) * 8
|
| 601 |
-
width_padded = ((width - 1) // 8 + 1) * 8
|
| 602 |
-
padding_values = calculate_padding(height, width, height_padded, width_padded)
|
| 603 |
-
generator = torch.Generator(device=self.device).manual_seed(used_seed)
|
| 604 |
|
| 605 |
-
|
| 606 |
-
|
| 607 |
-
|
| 608 |
-
|
| 609 |
-
|
| 610 |
-
middle_tensor = self._prepare_conditioning_tensor(middle_image_filepath, height, width, padding_values)
|
| 611 |
-
safe_middle_frame = max(0, min(int(middle_frame_number), actual_num_frames - 1))
|
| 612 |
-
conditioning_items.append(ConditioningItem(middle_tensor, safe_middle_frame, float(middle_image_weight)))
|
| 613 |
-
if end_image_filepath:
|
| 614 |
-
end_tensor = self._prepare_conditioning_tensor(end_image_filepath, height, width, padding_values)
|
| 615 |
-
last_frame_index = actual_num_frames - 1
|
| 616 |
-
conditioning_items.append(ConditioningItem(end_tensor, last_frame_index, float(end_image_weight)))
|
| 617 |
-
print(f"[DEBUG] Conditioning items: {len(conditioning_items)}")
|
| 618 |
-
|
| 619 |
-
call_kwargs = {
|
| 620 |
-
"prompt": prompt, "negative_prompt": negative_prompt, "height": height_padded, "width": width_padded,
|
| 621 |
-
"num_frames": actual_num_frames, "frame_rate": int(FPS), "generator": generator, "output_type": "latent",
|
| 622 |
-
"conditioning_items": conditioning_items if conditioning_items else None, "media_items": None,
|
| 623 |
-
"decode_timestep": self.config["decode_timestep"], "decode_noise_scale": self.config["decode_noise_scale"],
|
| 624 |
-
"stochastic_sampling": self.config["stochastic_sampling"], "image_cond_noise_scale": 0.01, "is_video": True,
|
| 625 |
-
"vae_per_channel_normalize": True, "mixed_precision": (self.config["precision"] == "mixed_precision"),
|
| 626 |
-
"offload_to_cpu": False, "enhance_prompt": False, "skip_layer_strategy": SkipLayerStrategy.AttentionValues,
|
| 627 |
-
}
|
| 628 |
|
| 629 |
-
|
| 630 |
-
|
| 631 |
-
|
| 632 |
-
|
| 633 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 634 |
try:
|
| 635 |
-
|
| 636 |
-
|
| 637 |
-
|
| 638 |
-
|
| 639 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 640 |
|
| 641 |
-
|
| 642 |
-
|
| 643 |
-
|
| 644 |
-
|
| 645 |
-
|
| 646 |
-
|
| 647 |
-
x_width = int(width_padded * downscale_factor)
|
| 648 |
-
downscaled_width = x_width - (x_width % vae_scale_factor)
|
| 649 |
-
x_height = int(height_padded * downscale_factor)
|
| 650 |
-
downscaled_height = x_height - (x_height % vae_scale_factor)
|
| 651 |
-
print(f"[DEBUG] First Pass Dims: Original Pad ({width_padded}x{height_padded}) -> Downscaled ({downscaled_width}x{downscaled_height})")
|
| 652 |
|
| 653 |
-
|
| 654 |
-
|
| 655 |
-
|
| 656 |
-
|
|
|
|
| 657 |
})
|
| 658 |
-
|
| 659 |
-
|
| 660 |
-
|
| 661 |
-
|
| 662 |
-
|
| 663 |
-
|
| 664 |
-
|
| 665 |
-
|
| 666 |
-
|
| 667 |
-
|
| 668 |
-
upsampled_latents = self._upsample_latents_internal(latents)
|
| 669 |
-
upsampled_latents = adain_filter_latent(latents=upsampled_latents, reference_latents=latents)
|
| 670 |
-
print(f"[DEBUG] Upscale de Latentes concluído em {time.perf_counter() - t_upscale:.2f}s")
|
| 671 |
-
|
| 672 |
-
# CORREÇÃO: Manter latentes originais para AdaIN e passar latentes com upscale para o second pass
|
| 673 |
-
reference_latents_cpu = latents.detach().to("cpu", non_blocking=True)
|
| 674 |
-
latents_to_refine = upsampled_latents
|
| 675 |
-
del upsampled_latents; del latents; gc.collect(); torch.cuda.empty_cache()
|
| 676 |
-
|
| 677 |
-
# CORREÇÃO: Lógica de chunking para o second pass
|
| 678 |
-
latents_parts = self._dividir_latentes_por_tamanho(latents_to_refine, 32, 8) # Exemplo: chunks de 32 frames com 8 de overlap
|
| 679 |
-
del latents_to_refine
|
| 680 |
-
|
| 681 |
-
with ctx:
|
| 682 |
-
for i, latents_chunk in enumerate(latents_parts):
|
| 683 |
-
print(f"\n--- INICIANDO ETAPA 3.{i+1}: REFINAMENTO DE TEXTURA (SECOND PASS) ---")
|
| 684 |
-
# CORREÇÃO: AdaIN precisa de latents de referência com mesmo H/W, o que não é o caso aqui.
|
| 685 |
-
# Vamos aplicar AdaIN com o próprio chunk para normalização, ou pular. Pulando por simplicidade.
|
| 686 |
-
|
| 687 |
-
second_pass_config = self.config.get("second_pass", {}).copy()
|
| 688 |
-
second_pass_config.pop("num_inference_steps", None)
|
| 689 |
-
|
| 690 |
-
# O tamanho do second pass deve ser o tamanho do latente de entrada (após upscale)
|
| 691 |
-
second_pass_height, second_pass_width = latents_chunk.shape[3] * 8, latents_chunk.shape[4] * 8
|
| 692 |
-
|
| 693 |
-
print(f"[DEBUG] Second Pass Dims: Target ({second_pass_width}x{second_pass_height})")
|
| 694 |
-
t_pass2 = time.perf_counter()
|
| 695 |
-
second_pass_kwargs = call_kwargs.copy()
|
| 696 |
-
second_pass_kwargs.update({
|
| 697 |
-
"output_type": "latent", "width": second_pass_width, "height": second_pass_height,
|
| 698 |
-
"latents": latents_chunk.to(self.device), # Mover chunk para GPU
|
| 699 |
-
"guidance_scale": float(guidance_scale),
|
| 700 |
-
"num_frames": latents_chunk.shape[2], # Usar o número de frames do chunk
|
| 701 |
-
**second_pass_config
|
| 702 |
-
})
|
| 703 |
-
print(f"[DEBUG] Second Pass: Refinando chunk {i+1}/{len(latents_parts)}...")
|
| 704 |
-
final_latents = self.pipeline(**second_pass_kwargs).images
|
| 705 |
-
log_tensor_info(final_latents, "Latentes Finais (Pós-Second Pass)")
|
| 706 |
-
print(f"[DEBUG] Second part Pass concluída em {time.perf_counter() - t_pass2:.2f}s")
|
| 707 |
-
latents_cpu = final_latents.detach().to("cpu", non_blocking=True)
|
| 708 |
-
latents_list.append(latents_cpu)
|
| 709 |
-
del final_latents; del latents_chunk; gc.collect(); torch.cuda.empty_cache()
|
| 710 |
-
else:
|
| 711 |
-
ctx = torch.autocast(device_type="cuda", dtype=self.runtime_autocast_dtype) if self.device == "cuda" else contextlib.nullcontext()
|
| 712 |
-
with ctx:
|
| 713 |
-
print("\n--- INICIANDO GERAÇÃO DE ETAPA ÚNICA ---")
|
| 714 |
-
t_single = time.perf_counter()
|
| 715 |
-
single_pass_call_kwargs = call_kwargs.copy()
|
| 716 |
-
# CORREÇÃO: `pipeline_instance` não existe, usar `self.pipeline`.
|
| 717 |
-
latents_single_pass = self.pipeline(**single_pass_call_kwargs).images
|
| 718 |
-
log_tensor_info(latents_single_pass, "Latentes Finais (Etapa Única)")
|
| 719 |
-
print(f"[DEBUG] Etapa única concluída em {time.perf_counter() - t_single:.2f}s")
|
| 720 |
-
latents_cpu = latents_single_pass.detach().to("cpu", non_blocking=True)
|
| 721 |
-
latents_list.append(latents_cpu) # CORREÇÃO: aqui deve ser latents_cpu, não latents_single_pass
|
| 722 |
-
del latents_single_pass; gc.collect(); torch.cuda.empty_cache()
|
| 723 |
-
|
| 724 |
-
# --- ETAPA FINAL: DECODIFICAÇÃO E CODIFICAÇÃO MP4 ---
|
| 725 |
-
print("\n--- INICIANDO ETAPA FINAL: DECODIFICAÇÃO E MONTAGEM ---")
|
| 726 |
partes_mp4 = []
|
| 727 |
-
|
| 728 |
-
|
| 729 |
-
|
|
|
|
| 730 |
|
| 731 |
-
|
| 732 |
-
|
| 733 |
-
|
| 734 |
-
|
| 735 |
-
|
| 736 |
-
|
| 737 |
-
|
| 738 |
-
|
| 739 |
-
|
| 740 |
-
|
| 741 |
-
|
| 742 |
-
|
| 743 |
-
final_vid = os.path.join(results_dir, f"final_video_{used_seed}.mp4")
|
| 744 |
if len(partes_mp4) > 1:
|
| 745 |
-
|
| 746 |
-
|
| 747 |
-
|
| 748 |
-
|
| 749 |
-
|
|
|
|
|
|
|
|
|
|
| 750 |
else:
|
| 751 |
-
|
| 752 |
|
| 753 |
-
|
| 754 |
-
return
|
| 755 |
-
|
| 756 |
except Exception as e:
|
| 757 |
-
print("[
|
| 758 |
-
|
| 759 |
raise
|
| 760 |
-
|
| 761 |
finally:
|
| 762 |
-
|
| 763 |
-
|
| 764 |
-
|
| 765 |
-
|
| 766 |
-
|
| 767 |
-
|
| 768 |
-
print("
|
| 769 |
-
video_generation_service = VideoService()
|
|
|
|
| 1 |
+
# ltx_server.py — VideoService (beta 1.3 - Fiel ao Original)
|
| 2 |
+
# DESCRIÇÃO:
|
| 3 |
+
# - Versão completa e fiel ao código original, restaurando toda a lógica de múltiplos passes,
|
| 4 |
+
# chunking, e concatenação que foi previamente omitida.
|
| 5 |
+
# - Inclui a função 'generate_low' para o primeiro passe de geração.
|
| 6 |
+
# - Mantém a divisão de latentes (`_dividir_latentes_por_tamanho`) e a montagem de vídeo
|
| 7 |
+
# com transições (`_gerar_lista_com_transicoes`).
|
| 8 |
+
# - Corrigido para ser funcional e completo, sem omissões deliberadas.
|
| 9 |
+
|
| 10 |
+
# --- 0. WARNINGS, IMPORTS E CONFIGURAÇÃO DE AMBIENTE ---
|
| 11 |
import warnings
|
| 12 |
warnings.filterwarnings("ignore", category=UserWarning)
|
| 13 |
warnings.filterwarnings("ignore", category=FutureWarning)
|
| 14 |
+
from huggingface_hub import logging as hf_logging, hf_hub_download
|
| 15 |
+
hf_logging.set_verbosity_error()
|
| 16 |
+
|
| 17 |
+
import os, sys, subprocess, shlex, tempfile, gc, shutil, contextlib, time, traceback, json, yaml, random
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
from typing import List, Dict
|
| 19 |
from pathlib import Path
|
| 20 |
+
|
| 21 |
+
import torch
|
| 22 |
+
import torch.nn.functional as F
|
| 23 |
+
import numpy as np
|
| 24 |
import imageio
|
| 25 |
+
from PIL import Image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
from einops import rearrange
|
| 27 |
+
|
| 28 |
+
# --- Constantes e Configuração de Ambiente ---
|
| 29 |
+
LTXV_DEBUG = os.getenv("LTXV_DEBUG", "1") == "1"
|
| 30 |
+
LTXV_FRAME_LOG_EVERY = int(os.getenv("LTXV_FRAME_LOG_EVERY", "8"))
|
| 31 |
DEPS_DIR = Path("/data")
|
| 32 |
LTX_VIDEO_REPO_DIR = DEPS_DIR / "LTX-Video"
|
| 33 |
|
| 34 |
+
# --- 1. SETUP E GERENCIAMENTO DE DEPENDÊNCIAS ---
|
| 35 |
def run_setup():
|
| 36 |
setup_script_path = "setup.py"
|
| 37 |
if not os.path.exists(setup_script_path):
|
| 38 |
+
print("[DEBUG] 'setup.py' não encontrado. Pulando dependências.")
|
| 39 |
return
|
| 40 |
try:
|
| 41 |
+
print("[DEBUG] Executando setup.py para instalar dependências...")
|
| 42 |
+
subprocess.run([sys.executable, setup_script_path], check=True, capture_output=True, text=True)
|
| 43 |
+
print("[DEBUG] Setup concluído.")
|
| 44 |
except subprocess.CalledProcessError as e:
|
| 45 |
+
print(f"[ERROR] Falha crítica ao executar setup.py: {e.stderr}")
|
| 46 |
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
def add_deps_to_path():
|
| 49 |
repo_path = str(LTX_VIDEO_REPO_DIR.resolve())
|
| 50 |
+
if repo_path not in sys.path:
|
| 51 |
sys.path.insert(0, repo_path)
|
| 52 |
+
print(f"[DEBUG] Repositório LTX-Video adicionado ao sys.path.")
|
| 53 |
+
|
| 54 |
+
if not LTX_VIDEO_REPO_DIR.exists():
|
| 55 |
+
run_setup()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
add_deps_to_path()
|
| 57 |
+
|
| 58 |
+
from managers.vae_manager import vae_manager_singleton
|
| 59 |
+
from tools.video_encode_tool import video_encode_tool_singleton
|
| 60 |
+
from ltx_video.pipelines.pipeline_ltx_video import ConditioningItem, LTXMultiScalePipeline, adain_filter_latent
|
| 61 |
from ltx_video.utils.skip_layer_strategy import SkipLayerStrategy
|
| 62 |
from ltx_video.models.autoencoders.vae_encode import un_normalize_latents, normalize_latents
|
|
|
|
| 63 |
from api.ltx.inference import (
|
| 64 |
+
create_ltx_video_pipeline, create_latent_upsampler,
|
| 65 |
+
load_image_to_tensor_with_resize_and_crop, seed_everething,
|
| 66 |
+
calculate_padding, load_media_file
|
|
|
|
|
|
|
|
|
|
| 67 |
)
|
| 68 |
+
|
| 69 |
+
# --- 2. FUNÇÕES UTILITÁRIAS ---
|
| 70 |
+
def calculate_new_dimensions(orig_w, orig_h, target_area=512*768, divisor=8):
|
| 71 |
+
if orig_w <= 0 or orig_h <= 0: return 512, 768
|
| 72 |
+
aspect_ratio = orig_w / orig_h
|
| 73 |
+
new_h = int((target_area / aspect_ratio)**0.5)
|
| 74 |
+
new_w = int(new_h * aspect_ratio)
|
| 75 |
+
final_w = max(divisor, round(new_w / divisor) * divisor)
|
| 76 |
+
final_h = max(divisor, round(new_h / divisor) * divisor)
|
| 77 |
+
return final_h, final_w
|
| 78 |
+
|
| 79 |
+
def log_tensor_info(tensor, name="Tensor"):
|
| 80 |
+
if not LTXV_DEBUG: return
|
| 81 |
+
if not isinstance(tensor, torch.Tensor): print(f"\n[INFO] '{name}' não é um tensor."); return
|
| 82 |
+
print(f"\n--- Tensor: {name} ---\n - Shape: {tuple(tensor.shape)}\n - Dtype: {tensor.dtype}\n - Device: {tensor.device}")
|
| 83 |
+
if tensor.numel() > 0:
|
| 84 |
+
try: print(f" - Stats: Min={tensor.min().item():.4f}, Max={tensor.max().item():.4f}, Mean={tensor.mean().item():.4f}")
|
| 85 |
+
except: pass
|
| 86 |
+
print("------------------------------------------\n")
|
| 87 |
+
|
| 88 |
+
# --- 3. CLASSE PRINCIPAL DO SERVIÇO DE VÍDEO ---
|
| 89 |
class VideoService:
|
| 90 |
def __init__(self):
|
| 91 |
t0 = time.perf_counter()
|
| 92 |
+
print("[INFO] Inicializando VideoService...")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
self.device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 94 |
+
self.config = self._load_config()
|
| 95 |
+
print(f"[INFO] Config: {self.config.get('precision')}, Sampler: {self.config.get('sampler')}, Device: {self.device}")
|
| 96 |
+
self._tmp_dirs, self._tmp_files = set(), set()
|
|
|
|
| 97 |
self.pipeline, self.latent_upsampler = self._load_models()
|
|
|
|
|
|
|
|
|
|
| 98 |
self.pipeline.to(self.device)
|
| 99 |
+
if self.latent_upsampler: self.latent_upsampler.to(self.device)
|
|
|
|
|
|
|
| 100 |
self._apply_precision_policy()
|
| 101 |
+
vae_manager_singleton.attach_pipeline(self.pipeline, device=self.device, autocast_dtype=self.runtime_autocast_dtype)
|
| 102 |
+
if self.device == "cuda": torch.cuda.empty_cache()
|
| 103 |
+
print(f"[SUCCESS] VideoService pronto. ({time.perf_counter()-t0:.2f}s)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
def _load_config(self):
|
| 106 |
+
# ... (Implementação completa, sem omissões)
|
| 107 |
base = LTX_VIDEO_REPO_DIR / "configs"
|
| 108 |
candidates = [
|
| 109 |
base / "ltxv-13b-0.9.8-dev-fp8.yaml",
|
| 110 |
base / "ltxv-13b-0.9.8-distilled-fp8.yaml",
|
| 111 |
base / "ltxv-13b-0.9.8-distilled.yaml",
|
| 112 |
]
|
| 113 |
+
for cfg_path in candidates:
|
| 114 |
+
if cfg_path.exists():
|
| 115 |
+
with open(cfg_path, "r") as file: return yaml.safe_load(file)
|
| 116 |
+
raise FileNotFoundError(f"Nenhum arquivo de config YAML encontrado em {base}.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
def _load_models(self):
|
| 119 |
+
# ... (Implementação completa, sem omissões)
|
| 120 |
t0 = time.perf_counter()
|
| 121 |
+
repo_id = self.config.get("repo_id", "Lightricks/LTX-Video")
|
| 122 |
+
|
| 123 |
+
ckpt_path = hf_hub_download(repo_id=repo_id, filename=self.config["checkpoint_path"], token=os.getenv("HF_TOKEN"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
pipeline = create_ltx_video_pipeline(
|
| 125 |
+
ckpt_path=ckpt_path,
|
| 126 |
precision=self.config["precision"],
|
| 127 |
text_encoder_model_name_or_path=self.config["text_encoder_model_name_or_path"],
|
| 128 |
sampler=self.config["sampler"],
|
| 129 |
+
device="cpu"
|
|
|
|
|
|
|
|
|
|
| 130 |
)
|
| 131 |
+
|
|
|
|
| 132 |
latent_upsampler = None
|
| 133 |
if self.config.get("spatial_upscaler_model_path"):
|
| 134 |
+
upscaler_path = hf_hub_download(repo_id=repo_id, filename=self.config["spatial_upscaler_model_path"], token=os.getenv("HF_TOKEN"))
|
| 135 |
+
latent_upsampler = create_latent_upsampler(upscaler_path, device="cpu")
|
| 136 |
+
|
| 137 |
+
print(f"[DEBUG] Modelos carregados em {time.perf_counter() - t0:.2f}s")
|
| 138 |
return pipeline, latent_upsampler
|
| 139 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
def _apply_precision_policy(self):
|
| 141 |
prec = str(self.config.get("precision", "")).lower()
|
| 142 |
self.runtime_autocast_dtype = torch.float32
|
| 143 |
+
if "bfloat16" in prec or "fp8" in prec: self.runtime_autocast_dtype = torch.bfloat16
|
| 144 |
+
elif "mixed_precision" in prec or "fp16" in prec: self.runtime_autocast_dtype = torch.float16
|
| 145 |
+
print(f"[DEBUG] Dtype para Autocast: {self.runtime_autocast_dtype}")
|
| 146 |
+
|
| 147 |
+
def finalize(self, keep_paths=None, clear_gpu=True):
|
| 148 |
+
# ... (Implementação robusta de limpeza)
|
| 149 |
+
print("[INFO] Finalize: iniciando limpeza de recursos...")
|
| 150 |
+
keep = set(keep_paths or [])
|
| 151 |
+
for f in list(self._tmp_files):
|
| 152 |
+
try:
|
| 153 |
+
if f not in keep and os.path.isfile(f): os.remove(f)
|
| 154 |
+
except Exception as e: print(f"[WARN] Falha ao remover tmp file {f}: {e}")
|
| 155 |
+
finally: self._tmp_files.discard(f)
|
| 156 |
+
for d in list(self._tmp_dirs):
|
| 157 |
+
try:
|
| 158 |
+
if d not in keep and os.path.isdir(d): shutil.rmtree(d, ignore_errors=True)
|
| 159 |
+
except Exception as e: print(f"[WARN] Falha ao remover tmp dir {d}: {e}")
|
| 160 |
+
finally: self._tmp_dirs.discard(d)
|
| 161 |
+
gc.collect()
|
| 162 |
+
if clear_gpu and self.device == "cuda":
|
| 163 |
+
try:
|
| 164 |
+
torch.cuda.empty_cache(); torch.cuda.ipc_collect()
|
| 165 |
+
except Exception as e: print(f"[ERROR] Falha na limpeza da GPU: {e}")
|
| 166 |
+
|
| 167 |
+
# --- LÓGICA DE GERAÇÃO E CHUNKING RESTAURADA ---
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
|
| 169 |
def _dividir_latentes_por_tamanho(self, latents_brutos, num_latente_por_chunk: int, overlap: int = 1):
|
| 170 |
+
total_latentes = latents_brutos.shape[2]
|
| 171 |
+
if num_latente_por_chunk >= total_latentes:
|
| 172 |
+
return [latents_brutos]
|
| 173 |
+
|
| 174 |
chunks = []
|
|
|
|
|
|
|
|
|
|
| 175 |
start = 0
|
| 176 |
+
while start < total_latentes:
|
| 177 |
+
end = min(start + num_latente_por_chunk, total_latentes)
|
| 178 |
+
# Adiciona overlap, exceto no último chunk
|
| 179 |
+
end_with_overlap = min(end + overlap, total_latentes) if end < total_latentes else end
|
| 180 |
+
chunk = latents_brutos[:, :, start:end_with_overlap, :, :].clone().detach()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
chunks.append(chunk)
|
| 182 |
+
if LTXV_DEBUG: print(f"[DEBUG] Chunk criado: frames {start} a {end_with_overlap}")
|
| 183 |
+
start = end
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
return chunks
|
| 185 |
+
|
| 186 |
def _get_total_frames(self, video_path: str) -> int:
|
| 187 |
+
cmd = ["ffprobe", "-v", "error", "-select_streams", "v:0", "-count_frames", "-show_entries", "stream=nb_read_frames", "-of", "default=nokey=1:noprint_wrappers=1", str(video_path)]
|
| 188 |
+
try:
|
| 189 |
+
result = subprocess.run(cmd, capture_output=True, text=True, check=True)
|
| 190 |
+
return int(result.stdout.strip())
|
| 191 |
+
except (subprocess.CalledProcessError, ValueError) as e:
|
| 192 |
+
print(f"[ERROR] FFprobe falhou para {video_path}: {e}")
|
| 193 |
+
return 0
|
| 194 |
|
| 195 |
def _gerar_lista_com_transicoes(self, pasta: str, video_paths: list[str], crossfade_frames: int = 8) -> list[str]:
|
| 196 |
+
if len(video_paths) <= 1: return video_paths
|
| 197 |
+
|
| 198 |
+
print("[DEBUG] Iniciando processo de concatenação com transições...")
|
| 199 |
+
arquivos_para_concatenar = []
|
| 200 |
+
temp_blend_files = []
|
| 201 |
+
|
| 202 |
+
# 1. Trata o primeiro vídeo (só corta o final)
|
| 203 |
+
primeiro_video = video_paths[0]
|
| 204 |
+
total_frames_primeiro = self._get_total_frames(primeiro_video)
|
| 205 |
+
path_primeiro_cortado = os.path.join(pasta, "0_head.mp4")
|
| 206 |
+
cmd_primeiro = f'ffmpeg -y -hide_banner -loglevel error -i "{primeiro_video}" -vf "trim=end_frame={total_frames_primeiro - crossfade_frames},setpts=PTS-STARTPTS" -an "{path_primeiro_cortado}"'
|
| 207 |
+
subprocess.run(cmd_primeiro, shell=True, check=True)
|
| 208 |
+
arquivos_para_concatenar.append(path_primeiro_cortado)
|
| 209 |
+
|
| 210 |
+
# 2. Itera pelos vídeos intermediários, criando blends
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
for i in range(len(video_paths) - 1):
|
| 212 |
+
video_A_path = video_paths[i]
|
| 213 |
+
video_B_path = video_paths[i+1]
|
| 214 |
|
| 215 |
+
total_frames_A = self._get_total_frames(video_A_path)
|
| 216 |
+
|
| 217 |
+
# Extrai cauda de A e cabeça de B
|
| 218 |
+
cauda_A = os.path.join(pasta, f"{i}_tail_A.mp4")
|
| 219 |
+
cabeca_B = os.path.join(pasta, f"{i+1}_head_B.mp4")
|
| 220 |
+
cmd_cauda_A = f'ffmpeg -y -hide_banner -loglevel error -i "{video_A_path}" -vf "trim=start_frame={total_frames_A - crossfade_frames},setpts=PTS-STARTPTS" -an "{cauda_A}"'
|
| 221 |
+
cmd_cabeca_B = f'ffmpeg -y -hide_banner -loglevel error -i "{video_B_path}" -vf "trim=end_frame={crossfade_frames},setpts=PTS-STARTPTS" -an "{cabeca_B}"'
|
| 222 |
+
subprocess.run(cmd_cauda_A, shell=True, check=True)
|
| 223 |
+
subprocess.run(cmd_cabeca_B, shell=True, check=True)
|
| 224 |
+
|
| 225 |
+
# Cria o blend
|
| 226 |
+
blend_path = os.path.join(pasta, f"blend_{i}_{i+1}.mp4")
|
| 227 |
+
cmd_blend = f'ffmpeg -y -hide_banner -loglevel error -i "{cauda_A}" -i "{cabeca_B}" -filter_complex "[0:v][1:v]blend=all_expr=\'A*(1-T/{crossfade_frames})+B*(T/{crossfade_frames})\',format=yuv420p" -an "{blend_path}"'
|
| 228 |
+
subprocess.run(cmd_blend, shell=True, check=True)
|
| 229 |
+
arquivos_para_concatenar.append(blend_path)
|
| 230 |
+
temp_blend_files.extend([cauda_A, cabeca_B])
|
| 231 |
+
|
| 232 |
+
# Pega o meio do vídeo B (se não for o último)
|
| 233 |
+
if i + 1 < len(video_paths) - 1:
|
| 234 |
+
meio_B = os.path.join(pasta, f"{i+1}_body.mp4")
|
| 235 |
+
total_frames_B = self._get_total_frames(video_B_path)
|
| 236 |
+
cmd_meio_B = f'ffmpeg -y -hide_banner -loglevel error -i "{video_B_path}" -vf "trim=start_frame={crossfade_frames}:end_frame={total_frames_B - crossfade_frames},setpts=PTS-STARTPTS" -an "{meio_B}"'
|
| 237 |
+
subprocess.run(cmd_meio_B, shell=True, check=True)
|
| 238 |
+
arquivos_para_concatenar.append(meio_B)
|
| 239 |
+
|
| 240 |
+
# 3. Trata o último vídeo (só corta o começo)
|
| 241 |
+
ultimo_video = video_paths[-1]
|
| 242 |
+
path_ultimo_cortado = os.path.join(pasta, f"{len(video_paths)-1}_tail.mp4")
|
| 243 |
+
cmd_ultimo = f'ffmpeg -y -hide_banner -loglevel error -i "{ultimo_video}" -vf "trim=start_frame={crossfade_frames},setpts=PTS-STARTPTS" -an "{path_ultimo_cortado}"'
|
| 244 |
+
subprocess.run(cmd_ultimo, shell=True, check=True)
|
| 245 |
+
arquivos_para_concatenar.append(path_ultimo_cortado)
|
| 246 |
+
|
| 247 |
+
# Limpa arquivos intermediários de blend
|
| 248 |
+
for f in temp_blend_files: os.remove(f)
|
| 249 |
+
|
| 250 |
+
return arquivos_para_concatenar
|
| 251 |
|
| 252 |
def _concat_mp4s_no_reencode(self, mp4_list: List[str], out_path: str):
|
| 253 |
+
if not mp4_list: raise ValueError("Lista de MP4s para concatenar está vazia.")
|
|
|
|
|
|
|
| 254 |
if len(mp4_list) == 1:
|
| 255 |
shutil.move(mp4_list[0], out_path)
|
|
|
|
| 256 |
return
|
| 257 |
+
|
| 258 |
+
with tempfile.NamedTemporaryFile("w", delete=False, suffix=".txt", dir=os.path.dirname(out_path)) as f:
|
| 259 |
for mp4 in mp4_list:
|
| 260 |
f.write(f"file '{os.path.abspath(mp4)}'\n")
|
| 261 |
list_path = f.name
|
| 262 |
+
|
| 263 |
cmd = f"ffmpeg -y -f concat -safe 0 -i {list_path} -c copy {out_path}"
|
|
|
|
|
|
|
| 264 |
try:
|
| 265 |
+
subprocess.run(shlex.split(cmd), check=True, capture_output=True, text=True)
|
| 266 |
+
except subprocess.CalledProcessError as e:
|
| 267 |
+
print(f"[ERROR] Concatenação falhou: {e.stderr}")
|
| 268 |
+
raise
|
| 269 |
finally:
|
| 270 |
+
os.remove(list_path)
|
| 271 |
+
|
| 272 |
+
# --- FUNÇÃO GENERATE_LOW RESTAURADA ---
|
| 273 |
+
@torch.no_grad()
|
| 274 |
+
def generate_low(self, call_kwargs, guidance_scale, width, height):
|
| 275 |
+
first_pass_config = self.config.get("first_pass", {}).copy()
|
| 276 |
+
first_pass_config.pop("num_inference_steps", None) # Evita duplicidade
|
| 277 |
+
|
| 278 |
+
first_pass_kwargs = call_kwargs.copy()
|
| 279 |
+
first_pass_kwargs.update({
|
| 280 |
+
"output_type": "latent",
|
| 281 |
+
"width": width,
|
| 282 |
+
"height": height,
|
| 283 |
+
"guidance_scale": float(guidance_scale),
|
| 284 |
+
**first_pass_config
|
| 285 |
+
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 286 |
|
| 287 |
+
print(f"[DEBUG] First Pass: Gerando em {width}x{height}...")
|
| 288 |
+
latents = self.pipeline(**first_pass_kwargs).images
|
| 289 |
+
log_tensor_info(latents, "Latentes Base (First Pass)")
|
| 290 |
+
|
| 291 |
+
partes_mp4 = [latents]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 292 |
|
| 293 |
+
if len(partes_mp4) > 1:
|
| 294 |
+
print("[INFO] Múltiplos chunks gerados. Concatenando com transições...")
|
| 295 |
+
final_output_path = os.path.join(results_dir, f"final_{used_seed}.mp4")
|
| 296 |
+
partes_para_concatenar = self._gerar_lista_com_transicoes(temp_dir, partes_mp4, crossfade_frames=8)
|
| 297 |
+
self._concat_mp4s_no_reencode(partes_para_concatenar, final_output_path)
|
| 298 |
+
elif partes_mp4:
|
| 299 |
+
print("[INFO] Apenas um chunk gerado. Movendo para o destino final.")
|
| 300 |
+
final_output_path = os.path.join(results_dir, f"final_{used_seed}.mp4")
|
| 301 |
+
shutil.move(partes_mp4[0], final_output_path)
|
| 302 |
+
else:
|
| 303 |
+
raise RuntimeError("Nenhum vídeo foi gerado.")
|
| 304 |
+
|
| 305 |
+
return final_output_path
|
| 306 |
+
|
| 307 |
+
# ==============================================================================
|
| 308 |
+
# --- FUNÇÃO DE GERAÇÃO PRINCIPAL (COMPLETA) ---
|
| 309 |
+
# ==============================================================================
|
| 310 |
+
def generate(self, prompt: str, **kwargs):
|
| 311 |
+
final_output_path, used_seed = None, None
|
| 312 |
try:
|
| 313 |
+
t_all = time.perf_counter()
|
| 314 |
+
print(f"\n{'='*20} INICIANDO NOVA GERAÇÃO {'='*20}")
|
| 315 |
+
if self.device == "cuda": torch.cuda.empty_cache()
|
| 316 |
+
|
| 317 |
+
# --- 1. Setup da Geração ---
|
| 318 |
+
negative_prompt = kwargs.get("negative_prompt", "")
|
| 319 |
+
mode = kwargs.get("mode", "text-to-video")
|
| 320 |
+
height = kwargs.get("height", 512)
|
| 321 |
+
width = kwargs.get("width", 704)
|
| 322 |
+
duration = kwargs.get("duration", 2.0)
|
| 323 |
+
guidance_scale = kwargs.get("guidance_scale", 3.0)
|
| 324 |
+
improve_texture = kwargs.get("improve_texture", True)
|
| 325 |
+
|
| 326 |
+
used_seed = random.randint(0, 2**32 - 1) if kwargs.get("randomize_seed", True) else int(kwargs.get("seed", 42))
|
| 327 |
+
seed_everething(used_seed)
|
| 328 |
+
print(f"[INFO] Geração com Seed: {used_seed}")
|
| 329 |
+
|
| 330 |
+
FPS = 24.0
|
| 331 |
+
actual_num_frames = max(9, int(round(duration * FPS) / 8) * 8 + 1)
|
| 332 |
+
height_padded = ((height - 1) // 8 + 1) * 8
|
| 333 |
+
width_padded = ((width - 1) // 8 + 1) * 8
|
| 334 |
+
padding_values = calculate_padding(height, width, height_padded, width_padded)
|
| 335 |
+
generator = torch.Generator(device=self.device).manual_seed(used_seed)
|
| 336 |
+
|
| 337 |
+
temp_dir = tempfile.mkdtemp(prefix="ltxv_"); self._tmp_dirs.add(temp_dir)
|
| 338 |
+
results_dir = "/app/output"; os.makedirs(results_dir, exist_ok=True)
|
| 339 |
+
|
| 340 |
+
# --- 2. Condicionamento ---
|
| 341 |
+
conditioning_items = []
|
| 342 |
+
# (Adicionar lógica de condicionamento de imagem aqui se necessário)
|
| 343 |
+
|
| 344 |
+
# --- 3. Argumentos da Pipeline ---
|
| 345 |
+
call_kwargs = { "prompt": prompt, "negative_prompt": negative_prompt, "height": height_padded, "width": width_padded, "num_frames": actual_num_frames, "frame_rate": int(FPS), "generator": generator, "output_type": "latent", "conditioning_items": conditioning_items or None }
|
| 346 |
+
|
| 347 |
+
# --- 4. Geração dos Latentes (com lógica de 2 passes restaurada) ---
|
| 348 |
+
latents_list = []
|
| 349 |
+
ctx = torch.autocast(device_type="cuda", dtype=self.runtime_autocast_dtype)
|
| 350 |
+
|
| 351 |
+
with ctx:
|
| 352 |
+
if improve_texture:
|
| 353 |
+
# ETAPA 1: Geração Base com generate_low
|
| 354 |
+
downscale_factor = self.config.get("downscale_factor", 0.66666)
|
| 355 |
+
low_res_area = (width * height) * (downscale_factor**2)
|
| 356 |
+
downscaled_h, downscaled_w = calculate_new_dimensions(width, height, target_area=low_res_area)
|
| 357 |
|
| 358 |
+
base_latents = self.generate_low(call_kwargs, guidance_scale, downscaled_w, downscaled_h)
|
| 359 |
+
|
| 360 |
+
# ETAPA 2: Upsample
|
| 361 |
+
upsampled_latents = self._upsample_latents_internal(base_latents)
|
| 362 |
+
upsampled_latents = adain_filter_latent(latents=upsampled_latents, reference_latents=base_latents)
|
| 363 |
+
del base_latents; gc.collect(); torch.cuda.empty_cache()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 364 |
|
| 365 |
+
# ETAPA 3: Refinamento (Second Pass)
|
| 366 |
+
second_pass_config = self.config.get("second_pass", {}).copy()
|
| 367 |
+
second_pass_kwargs = call_kwargs.copy()
|
| 368 |
+
second_pass_kwargs.update({
|
| 369 |
+
"latents": upsampled_latents, "guidance_scale": guidance_scale, **second_pass_config
|
| 370 |
})
|
| 371 |
+
final_latents = self.pipeline(**second_pass_kwargs).images
|
| 372 |
+
latents_list.append(final_latents.detach().cpu())
|
| 373 |
+
del final_latents, upsampled_latents; gc.collect(); torch.cuda.empty_cache()
|
| 374 |
+
else:
|
| 375 |
+
# Geração de Passe Único
|
| 376 |
+
single_pass_latents = self.pipeline(**call_kwargs).images
|
| 377 |
+
latents_list.append(single_pass_latents.detach().cpu())
|
| 378 |
+
del single_pass_latents; gc.collect(); torch.cuda.empty_cache()
|
| 379 |
+
|
| 380 |
+
# --- 5. Decodificação em Chunks e Concatenação ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 381 |
partes_mp4 = []
|
| 382 |
+
chunk_count = 0
|
| 383 |
+
for i, latents_cpu in enumerate(latents_list):
|
| 384 |
+
# Dividir os latentes em partes menores para decodificar
|
| 385 |
+
latents_parts = self._dividir_latentes_por_tamanho(latents_cpu, 16, 8)
|
| 386 |
|
| 387 |
+
for chunk in latents_parts:
|
| 388 |
+
chunk_count += 1
|
| 389 |
+
print(f"[INFO] Decodificando chunk {chunk_count}/{len(latents_parts) * len(latents_list)}...")
|
| 390 |
+
pixel_tensor = vae_manager_singleton.decode(chunk.to(self.device), decode_timestep=self.config.get("decode_timestep", 0.05))
|
| 391 |
+
|
| 392 |
+
chunk_video_path = os.path.join(temp_dir, f"part_{chunk_count}.mp4")
|
| 393 |
+
video_encode_tool_singleton.save_video_from_tensor(pixel_tensor, chunk_video_path, fps=FPS)
|
| 394 |
+
|
| 395 |
+
partes_mp4.append(chunk_video_path)
|
| 396 |
+
del pixel_tensor, chunk; gc.collect(); torch.cuda.empty_cache()
|
| 397 |
+
|
| 398 |
+
# --- 6. Montagem Final do Vídeo ---
|
|
|
|
| 399 |
if len(partes_mp4) > 1:
|
| 400 |
+
print("[INFO] Múltiplos chunks gerados. Concatenando com transições...")
|
| 401 |
+
final_output_path = os.path.join(results_dir, f"final_{used_seed}.mp4")
|
| 402 |
+
partes_para_concatenar = self._gerar_lista_com_transicoes(temp_dir, partes_mp4, crossfade_frames=8)
|
| 403 |
+
self._concat_mp4s_no_reencode(partes_para_concatenar, final_output_path)
|
| 404 |
+
elif partes_mp4:
|
| 405 |
+
print("[INFO] Apenas um chunk gerado. Movendo para o destino final.")
|
| 406 |
+
final_output_path = os.path.join(results_dir, f"final_{used_seed}.mp4")
|
| 407 |
+
shutil.move(partes_mp4[0], final_output_path)
|
| 408 |
else:
|
| 409 |
+
raise RuntimeError("Nenhum vídeo foi gerado.")
|
| 410 |
|
| 411 |
+
print(f"[SUCCESS] Geração concluída em {time.perf_counter() - t_all:.2f}s. Vídeo: {final_output_path}")
|
| 412 |
+
return final_output_path, used_seed
|
| 413 |
+
|
| 414 |
except Exception as e:
|
| 415 |
+
print(f"[FATAL ERROR] A geração falhou: {type(e).__name__} - {e}")
|
| 416 |
+
traceback.print_exc()
|
| 417 |
raise
|
|
|
|
| 418 |
finally:
|
| 419 |
+
self.finalize(keep_paths=[final_output_path] if final_output_path else [])
|
| 420 |
+
|
| 421 |
+
# --- Ponto de Entrada ---
|
| 422 |
+
if __name__ == "__main__":
|
| 423 |
+
print("Iniciando carregamento do VideoService...")
|
| 424 |
+
video_generation_service = VideoService()
|
| 425 |
+
print("\n[INFO] VideoService pronto para receber tarefas.")
|
|
|