Update api/ltx_server_refactored.py
Browse files- api/ltx_server_refactored.py +1 -77
api/ltx_server_refactored.py
CHANGED
|
@@ -232,7 +232,7 @@ class VideoService:
|
|
| 232 |
t0 = time.perf_counter()
|
| 233 |
print("[INFO] Inicializando VideoService...")
|
| 234 |
self.device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 235 |
-
self.config = self._load_config("ltxv-13b-0.9.8-
|
| 236 |
|
| 237 |
self.pipeline, self.latent_upsampler = self._load_models_from_hub()
|
| 238 |
self._move_models_to_device()
|
|
@@ -653,82 +653,6 @@ class VideoService:
|
|
| 653 |
print("============ [SUCCESS] ETAPA 2 Concluída com Sucesso =============")
|
| 654 |
print("======================================================================\n")
|
| 655 |
return video_path_out, latents_path_out
|
| 656 |
-
|
| 657 |
-
def refine_texture_only1(
|
| 658 |
-
self,
|
| 659 |
-
latents_path: str,
|
| 660 |
-
prompt: str,
|
| 661 |
-
negative_prompt: str,
|
| 662 |
-
guidance_scale: float,
|
| 663 |
-
seed: Optional[int] = None,
|
| 664 |
-
conditioning_items: Optional[List[ConditioningItem]] = None
|
| 665 |
-
) -> Tuple[str, str]:
|
| 666 |
-
"""
|
| 667 |
-
ETAPA 2: Refina a textura dos latentes existentes SEM alterar sua resolução
|
| 668 |
-
e SEM dividi-los em pedaços. O tensor inteiro é processado de uma só vez para
|
| 669 |
-
garantir máxima consistência temporal.
|
| 670 |
-
"""
|
| 671 |
-
print("[INFO] Iniciando ETAPA 2: Refinamento de Textura...")
|
| 672 |
-
|
| 673 |
-
# --- Configuração de Seed e Diretórios ---
|
| 674 |
-
used_seed = random.randint(0, 2**32 - 1) if seed is None else int(seed)
|
| 675 |
-
#seed_everything(used_seed)
|
| 676 |
-
print(f" - Usando Seed (consistente com Etapa 1): {used_seed}")
|
| 677 |
-
|
| 678 |
-
temp_dir = tempfile.mkdtemp(prefix="ltxv_refine_single_")
|
| 679 |
-
self._register_tmp_dir(temp_dir)
|
| 680 |
-
|
| 681 |
-
# --- Carregamento dos Latentes ---
|
| 682 |
-
latents_to_refine = torch.load(latents_path).to(self.device)
|
| 683 |
-
print(f" - Shape dos latentes de entrada: {latents_to_refine.shape}")
|
| 684 |
-
|
| 685 |
-
if conditioning_items:
|
| 686 |
-
print(f" - Usando {len(conditioning_items)} item(ns) de condicionamento para o refinamento.")
|
| 687 |
-
|
| 688 |
-
# --- Execução da Pipeline ---
|
| 689 |
-
with torch.autocast(device_type=self.device.split(':')[0], dtype=self.runtime_autocast_dtype, enabled=(self.device == 'cuda')):
|
| 690 |
-
|
| 691 |
-
# As dimensões são as mesmas do tensor de entrada
|
| 692 |
-
refine_height = latents_to_refine.shape[3] * self.pipeline.vae_scale_factor
|
| 693 |
-
refine_width = latents_to_refine.shape[4] * self.pipeline.vae_scale_factor
|
| 694 |
-
|
| 695 |
-
second_pass_kwargs = {
|
| 696 |
-
"prompt": prompt,
|
| 697 |
-
"negative_prompt": negative_prompt,
|
| 698 |
-
"height": refine_height,
|
| 699 |
-
"width": refine_width,
|
| 700 |
-
"frame_rate": int(DEFAULT_FPS),
|
| 701 |
-
"num_frames": latents_to_refine.shape[2],
|
| 702 |
-
"latents": latents_to_refine, # O tensor completo é passado aqui
|
| 703 |
-
"guidance_scale": float(guidance_scale),
|
| 704 |
-
"output_type": "latent",
|
| 705 |
-
"generator": torch.Generator(device=self.device).manual_seed(used_seed),
|
| 706 |
-
"conditioning_items": conditioning_items,
|
| 707 |
-
**(self.config.get("second_pass", {}))
|
| 708 |
-
}
|
| 709 |
-
|
| 710 |
-
print(" - Enviando tensor completo para a pipeline de refinamento...")
|
| 711 |
-
final_latents = self.pipeline(**second_pass_kwargs).images
|
| 712 |
-
print(f" - Latentes refinados com shape: {final_latents.shape}")
|
| 713 |
-
|
| 714 |
-
# Decodifica os latentes refinados para pixels
|
| 715 |
-
pixel_tensor = vae_manager_singleton.decode(final_latents, decode_timestep=float(self.config.get("decode_timestep", 0.05)))
|
| 716 |
-
|
| 717 |
-
# Salva os artefatos de saída
|
| 718 |
-
video_path_out = self._save_video_from_tensor(pixel_tensor, "refined_video_single_pass", used_seed, temp_dir)
|
| 719 |
-
latents_path_out = self._save_latents_to_disk(final_latents, "latents_refined_single_pass", used_seed)
|
| 720 |
-
|
| 721 |
-
# --- Limpeza ---
|
| 722 |
-
# Libera os tensores da memória da GPU antes de finalizar.
|
| 723 |
-
del latents_to_refine
|
| 724 |
-
if 'final_latents' in locals():
|
| 725 |
-
del final_latents
|
| 726 |
-
if 'pixel_tensor' in locals():
|
| 727 |
-
del pixel_tensor
|
| 728 |
-
self._finalize()
|
| 729 |
-
|
| 730 |
-
print("[SUCCESS] ETAPA 2 Concluída.")
|
| 731 |
-
return video_path_out, latents_path_out
|
| 732 |
|
| 733 |
|
| 734 |
def encode_latents_to_mp4(self, latents_path: str, fps: int = int(DEFAULT_FPS)) -> str:
|
|
|
|
| 232 |
t0 = time.perf_counter()
|
| 233 |
print("[INFO] Inicializando VideoService...")
|
| 234 |
self.device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 235 |
+
self.config = self._load_config("ltxv-13b-0.9.8-distilled-fp8.yaml")
|
| 236 |
|
| 237 |
self.pipeline, self.latent_upsampler = self._load_models_from_hub()
|
| 238 |
self._move_models_to_device()
|
|
|
|
| 653 |
print("============ [SUCCESS] ETAPA 2 Concluída com Sucesso =============")
|
| 654 |
print("======================================================================\n")
|
| 655 |
return video_path_out, latents_path_out
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 656 |
|
| 657 |
|
| 658 |
def encode_latents_to_mp4(self, latents_path: str, fps: int = int(DEFAULT_FPS)) -> str:
|