File size: 35,424 Bytes
2701e1f
2de1ae7
d7c623e
fd4abdb
2701e1f
 
d7c623e
2701e1f
d7c623e
2701e1f
 
0f094a3
 
2701e1f
 
 
 
fd4abdb
d7c623e
2701e1f
 
 
 
 
 
 
 
 
42dccc7
953982d
2701e1f
 
 
cae55d9
ab2fc5d
 
cae55d9
 
 
 
 
 
 
 
 
21e9173
2701e1f
 
ab2fc5d
 
 
2701e1f
ab2fc5d
 
2701e1f
 
 
ab2fc5d
2701e1f
ab2fc5d
2701e1f
 
 
ab2fc5d
 
2701e1f
ab2fc5d
2701e1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d7c623e
ab2fc5d
2701e1f
ab2fc5d
 
2701e1f
ab2fc5d
2701e1f
 
 
 
ab2fc5d
d7c623e
f736eae
fd4abdb
 
2701e1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2de1ae7
fd4abdb
 
2701e1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fd4abdb
2701e1f
fd4abdb
 
 
2701e1f
 
 
 
fd4abdb
2701e1f
 
fd4abdb
 
2701e1f
 
 
 
fd4abdb
 
 
 
 
2134e1a
2701e1f
 
 
2134e1a
2701e1f
 
 
fd4abdb
2701e1f
 
36ce571
 
 
 
 
 
 
 
2701e1f
 
 
 
d7c623e
2701e1f
 
 
1054840
2701e1f
d7c623e
36ce571
2701e1f
e90d2e4
2701e1f
 
 
e90d2e4
2701e1f
 
 
d7c623e
6658b64
0f2ffe2
 
 
 
0f094a3
 
0f2ffe2
0f094a3
 
 
2134e1a
 
 
d7c623e
2134e1a
2701e1f
 
 
 
 
 
 
 
 
 
 
d8ff10a
fd9ba1f
 
dfa1859
 
fd9ba1f
 
 
 
 
 
 
 
 
 
dfa1859
 
 
2134e1a
2701e1f
 
 
9068907
2701e1f
 
 
 
 
 
 
 
 
 
 
 
 
 
75504ac
 
2134e1a
2701e1f
 
9068907
 
5114d43
 
36ce571
5114d43
 
36ce571
 
 
5114d43
2134e1a
5114d43
2134e1a
5114d43
 
 
 
2134e1a
 
 
 
 
 
 
17c08fd
2134e1a
 
 
 
 
 
 
940bd69
 
e90d2e4
 
 
2134e1a
 
 
 
 
 
 
 
 
 
 
 
 
 
0df2c16
2134e1a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d94efa4
17c08fd
2134e1a
cd335a7
9068907
17c08fd
cd335a7
 
2134e1a
 
 
 
 
525556c
 
 
2134e1a
 
 
525556c
940bd69
682884a
 
 
 
4b08af0
2134e1a
 
 
 
 
 
 
 
 
 
 
d94efa4
2134e1a
 
 
 
 
 
 
 
 
e90d2e4
2134e1a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dfa1859
d94efa4
2134e1a
dfa1859
d94efa4
e4bd0c1
2134e1a
 
 
 
 
 
 
17c08fd
 
 
 
 
 
 
 
 
 
2134e1a
e5155e9
 
 
2134e1a
85b5ada
 
 
 
 
 
 
940bd69
 
dfa1859
d94efa4
 
dfa1859
0f2ffe2
 
fec3865
 
0f2ffe2
 
79b2061
5967c90
0f2ffe2
d94efa4
dfa1859
525556c
2134e1a
85b5ada
dfa1859
2134e1a
 
0f094a3
9068907
0df2c16
525556c
 
0f094a3
 
99efd77
 
e5155e9
91a0fac
525556c
 
 
 
 
 
e5155e9
2134e1a
525556c
 
e5155e9
2134e1a
 
 
e5155e9
525556c
e5155e9
 
 
 
2134e1a
 
e5155e9
 
6658b64
e5155e9
2134e1a
 
 
 
e5155e9
 
 
 
 
 
 
2134e1a
 
940bd69
 
 
4b08af0
2134e1a
 
 
 
 
 
 
 
 
 
 
99efd77
2134e1a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99efd77
2134e1a
 
99efd77
2134e1a
 
 
 
 
 
 
 
99efd77
 
 
 
 
 
 
2134e1a
4b08af0
 
 
 
2134e1a
 
 
 
 
 
 
ac04fcc
 
 
465c791
2134e1a
 
465c791
5a3bb22
 
 
 
 
36ce571
 
 
 
5a3bb22
36ce571
 
 
 
 
 
 
5a3bb22
36ce571
 
5a3bb22
 
 
 
 
df311a5
 
5a3bb22
 
 
 
 
36ce571
 
 
 
5a3bb22
50604bb
 
5a3bb22
50604bb
 
 
 
 
d6d47ed
 
5a3bb22
2701e1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cae55d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2134e1a
cae55d9
 
 
 
 
 
 
 
2701e1f
cae55d9
 
 
 
 
d7c623e
2134e1a
 
42dccc7
2701e1f
cae55d9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
# ltx_server_refactored.py — VideoService (Modular Version with Simple Overlap Chunking)
# Em api/ltx_server_refactored.py

import warnings
from huggingface_hub import logging
import os, subprocess, shlex, tempfile
import torch
import json
import numpy as np
import random
import os
import io

import shlex
import yaml
from typing import List, Dict
from pathlib import Path
import imageio
from PIL import Image
import tempfile
from huggingface_hub import hf_hub_download
import sys
import subprocess
import gc
import shutil
import contextlib
import time
import traceback
from api.gpu_manager import gpu_manager
from einops import rearrange
import torch.nn.functional as F
from managers.vae_manager import vae_manager_singleton
from tools.video_encode_tool import video_encode_tool_singleton

DEPS_DIR = Path("/data")
LTX_VIDEO_REPO_DIR = DEPS_DIR / "LTX-Video"
logging.set_verbosity_error()
logging.set_verbosity_warning()
logging.set_verbosity_info()
logging.set_verbosity_debug()
LTXV_DEBUG=1
LTXV_FRAME_LOG_EVERY=8
warnings.filterwarnings("ignore", category=UserWarning)
warnings.filterwarnings("ignore", category=FutureWarning)
warnings.filterwarnings("ignore", message=".*")

# (Todas as funções de setup, helpers e inicialização da classe permanecem inalteradas)
# ... (run_setup, add_deps_to_path, _query_gpu_processes_via_nvml, etc.)
def run_setup():
    setup_script_path = "setup.py"
    if not os.path.exists(setup_script_path):
        print("[DEBUG] 'setup.py' não encontrado. Pulando clonagem de dependências.")
        return
    try:
        print("[DEBUG] Executando setup.py para dependências...")
        subprocess.run([sys.executable, setup_script_path], check=True)
        print("[DEBUG] Setup concluído com sucesso.")
    except subprocess.CalledProcessError as e:
        print(f"[DEBUG] ERRO no setup.py (code {e.returncode}). Abortando.")
        sys.exit(1)
if not LTX_VIDEO_REPO_DIR.exists():
    print(f"[DEBUG] Repositório não encontrado em {LTX_VIDEO_REPO_DIR}. Rodando setup...")
    run_setup()
def add_deps_to_path():
    repo_path = str(LTX_VIDEO_REPO_DIR.resolve())
    if str(LTX_VIDEO_REPO_DIR.resolve()) not in sys.path:
        sys.path.insert(0, repo_path)
        print(f"[DEBUG] Repo adicionado ao sys.path: {repo_path}")
def calculate_padding(orig_h, orig_w, target_h, target_w):
    pad_h = target_h - orig_h
    pad_w = target_w - orig_w
    pad_top = pad_h // 2
    pad_bottom = pad_h - pad_top
    pad_left = pad_w // 2
    pad_right = pad_w - pad_left
    return (pad_left, pad_right, pad_top, pad_bottom)
def log_tensor_info(tensor, name="Tensor"):
    if not isinstance(tensor, torch.Tensor):
        print(f"\n[INFO] '{name}' não é tensor.")
        return
    print(f"\n--- Tensor: {name} ---")
    print(f"  - Shape: {tuple(tensor.shape)}")
    print(f"  - Dtype: {tensor.dtype}")
    print(f"  - Device: {tensor.device}")
    if tensor.numel() > 0:
        try:
            print(f"  - Min: {tensor.min().item():.4f}  Max: {tensor.max().item():.4f}  Mean: {tensor.mean().item():.4f}")
        except Exception:
            pass
    print("------------------------------------------\n")

add_deps_to_path()
from ltx_video.pipelines.pipeline_ltx_video import ConditioningItem, LTXMultiScalePipeline
from ltx_video.utils.skip_layer_strategy import SkipLayerStrategy
from ltx_video.models.autoencoders.vae_encode import un_normalize_latents, normalize_latents
from ltx_video.pipelines.pipeline_ltx_video import adain_filter_latent
from api.ltx.inference import (
    create_ltx_video_pipeline,
    create_latent_upsampler,
    load_image_to_tensor_with_resize_and_crop,
    seed_everething,
)

class VideoService:
    def _load_config(self):
        base = LTX_VIDEO_REPO_DIR / "configs"
        config_path = base / "ltxv-13b-0.9.8-distilled-fp8.yaml"
        with open(config_path, "r") as file:
            return yaml.safe_load(file)

    def finalize(self, keep_paths=None, extra_paths=None, clear_gpu=True):
        print("[DEBUG] Finalize: iniciando limpeza...")
        keep = set(keep_paths or []); extras = set(extra_paths or [])
        gc.collect()
        try:
            if clear_gpu and torch.cuda.is_available():
                torch.cuda.empty_cache()
                try:
                    torch.cuda.ipc_collect()
                except Exception:
                    pass
        except Exception as e:
            print(f"[DEBUG] Finalize: limpeza GPU falhou: {e}")
            
    def _load_models(self):
        t0 = time.perf_counter()
        LTX_REPO = "Lightricks/LTX-Video"
        print("[DEBUG] Baixando checkpoint principal...")
        distilled_model_path = hf_hub_download(
            repo_id=LTX_REPO,
            filename=self.config["checkpoint_path"],
            local_dir=os.getenv("HF_HOME"),
            cache_dir=os.getenv("HF_HOME_CACHE"),
            token=os.getenv("HF_TOKEN"),
        )
        self.config["checkpoint_path"] = distilled_model_path
        print(f"[DEBUG] Checkpoint em: {distilled_model_path}")

        print("[DEBUG] Baixando upscaler espacial...")
        spatial_upscaler_path = hf_hub_download(
            repo_id=LTX_REPO,
            filename=self.config["spatial_upscaler_model_path"],
            local_dir=os.getenv("HF_HOME"),
            cache_dir=os.getenv("HF_HOME_CACHE"),
            token=os.getenv("HF_TOKEN")
        )
        self.config["spatial_upscaler_model_path"] = spatial_upscaler_path
        print(f"[DEBUG] Upscaler em: {spatial_upscaler_path}")

        print("[DEBUG] Construindo pipeline...")
        pipeline = create_ltx_video_pipeline(
            ckpt_path=self.config["checkpoint_path"],
            precision=self.config["precision"],
            text_encoder_model_name_or_path=self.config["text_encoder_model_name_or_path"],
            sampler=self.config["sampler"],
            device="cpu",
            enhance_prompt=False,
            prompt_enhancer_image_caption_model_name_or_path=self.config["prompt_enhancer_image_caption_model_name_or_path"],
            prompt_enhancer_llm_model_name_or_path=self.config["prompt_enhancer_llm_model_name_or_path"],
        )
        print("[DEBUG] Pipeline pronto.")

        latent_upsampler = None
        if self.config.get("spatial_upscaler_model_path"):
            print("[DEBUG] Construindo latent_upsampler...")
            latent_upsampler = create_latent_upsampler(self.config["spatial_upscaler_model_path"], device="cpu")
            print("[DEBUG] Upsampler pronto.")
        print(f"[DEBUG] _load_models() tempo total={time.perf_counter()-t0:.3f}s")
        return pipeline, latent_upsampler

    def _apply_precision_policy(self):
        prec = str(self.config.get("precision", "")).lower()
        self.runtime_autocast_dtype = torch.float32
        if prec in ["float8_e4m3fn", "bfloat16"]:
            self.runtime_autocast_dtype = torch.bfloat16
        elif prec == "mixed_precision":
            self.runtime_autocast_dtype = torch.float16

    def _register_tmp_dir(self, d: str):
        if d and os.path.isdir(d):
            self._tmp_dirs.add(d); print(f"[DEBUG] Registrado tmp dir: {d}")

    @torch.no_grad()
    def _upsample_latents_internal(self, latents: torch.Tensor) -> torch.Tensor:
        try:
            if not self.latent_upsampler:
                raise ValueError("Latent Upsampler não está carregado.")
            latents_unnormalized = un_normalize_latents(latents, self.pipeline.vae, vae_per_channel_normalize=True)
            upsampled_latents = self.latent_upsampler(latents_unnormalized)
            return normalize_latents(upsampled_latents, self.pipeline.vae, vae_per_channel_normalize=True)
        except Exception as e:
            pass
        finally:
            torch.cuda.empty_cache()
            torch.cuda.ipc_collect()
            self.finalize(keep_paths=[])

    def _prepare_conditioning_tensor(self, filepath, height, width, padding_values):
        tensor = load_image_to_tensor_with_resize_and_crop(filepath, height, width)
        tensor = torch.nn.functional.pad(tensor, padding_values)
        log_tensor_info(tensor, f"_prepare_conditioning_tensor")
        return tensor.to(self.device, dtype=self.runtime_autocast_dtype)

            
    def _save_and_log_video(self, pixel_tensor, base_filename, fps, temp_dir, results_dir, used_seed, progress_callback=None):
        output_path = os.path.join(temp_dir, f"{base_filename}_.mp4")
        video_encode_tool_singleton.save_video_from_tensor(
            pixel_tensor, output_path, fps=fps, progress_callback=progress_callback
        )
        final_path = os.path.join(results_dir, f"{base_filename}_.mp4")
        shutil.move(output_path, final_path)
        print(f"[DEBUG] Vídeo salvo em: {final_path}")
        return final_path

    def _load_tensor(self, caminho):
        # Se já é um tensor, retorna diretamente
        if isinstance(caminho, torch.Tensor):
            return caminho
        # Se é bytes, carrega do buffer
        if isinstance(caminho, (bytes, bytearray)):
            return torch.load(io.BytesIO(caminho))
        # Caso contrário, assume que é um caminho de arquivo
        return torch.load(caminho)


    # ==============================================================================
    # --- FUNÇÕES MODULARES COM A LÓGICA DE CHUNKING SIMPLIFICADA ---
    # ==============================================================================

    def prepare_condition_items(self, items_list: List, height: int, width: int, num_frames: int):
        if not items_list: return []
        height_padded = ((height - 1) // 8 + 1) * 8
        width_padded = ((width - 1) // 8 + 1) * 8
        padding_values = calculate_padding(height, width, height_padded, width_padded)
        conditioning_items = []
        for media, frame, weight in items_list:
            tensor = self._prepare_conditioning_tensor(media, height, width, padding_values) if isinstance(media, str) else media.to(self.device, dtype=self.runtime_autocast_dtype)
            safe_frame = max(0, min(int(frame), num_frames - 1))
            conditioning_items.append(ConditioningItem(tensor, safe_frame, float(weight)))
        return conditioning_items

    def _prepare_condition_items_latent(self, items_list: List):
        if not items_list: 
            return []
        conditioning_items = []
        for tensor_patch, frame, weight in items_list:
            # Verifica se já é um tensor
            if isinstance(tensor_patch, torch.Tensor):
                tensor = tensor_patch.to(self.device)
            # Se é bytes, carrega do buffer
            elif isinstance(tensor_patch, (bytes, bytearray)):
                tensor = torch.load(io.BytesIO(tensor_patch)).to(self.device)
            # Caso contrário, assume que é um caminho de arquivo
            else:
                tensor = torch.load(tensor_patch).to(self.device)
            safe_frame = max(0, int(frame))
            conditioning_items.append(ConditioningItem(tensor, safe_frame, float(weight)))
        return conditioning_items

    def generate_low(self, prompt, negative_prompt, height, width, duration, guidance_scale, seed, conditioning_items=None):
        used_seed = random.randint(0, 2**32 - 1) if seed is None else int(seed)
        seed_everething(used_seed)
        FPS = 24.0
        
        actual_num_frames = max(9, int(round((round(duration * FPS) - 1) / 8.0) * 8 + 1))
        height_padded = ((height - 1) // 8 + 1) * 8
        width_padded = ((width - 1) // 8 + 1) * 8
        temp_dir = tempfile.mkdtemp(prefix="ltxv_low_"); self._register_tmp_dir(temp_dir)
        results_dir = "/app/output"; os.makedirs(results_dir, exist_ok=True)
        downscale_factor = self.config.get("downscale_factor", 0.6666666)
        vae_scale_factor = self.pipeline.vae_scale_factor
        x_width = int(width_padded * downscale_factor)
        downscaled_width = x_width - (x_width % vae_scale_factor)
        x_height = int(height_padded * downscale_factor)
        downscaled_height = x_height - (x_height % vae_scale_factor)
        first_pass_kwargs = {
            "prompt": prompt, "negative_prompt": negative_prompt, "height": downscaled_height, "width": downscaled_width,
            "num_frames": actual_num_frames, "frame_rate": int(FPS), "generator": torch.Generator(device=self.device).manual_seed(used_seed),
            "output_type": "latent", 
            #"conditioning_items": conditioning_items, 
            #"guidance_scale": float(guidance_scale),
            **(self.config.get("first_pass", {}))
        }

        print(f"[DEBUG]  generate_low.first_pass_kwargs: {first_pass_kwargs}")
        try: 
            with torch.autocast(device_type="cuda", dtype=self.runtime_autocast_dtype, enabled=self.device == 'cuda'):
                latents = self.pipeline(**first_pass_kwargs).images
                pixel_tensor = vae_manager_singleton.decode(latents.clone(), decode_timestep=float(self.config.get("decode_timestep", 0.05)))
                video_path = self._save_and_log_video(pixel_tensor, "low_res_video", FPS, temp_dir, results_dir, used_seed)
                latents_cpu = latents.detach().to("cpu")
                tensor_path = os.path.join(results_dir, f"latents_low_res_{used_seed}.pt")
                torch.save(latents_cpu, tensor_path)
            return video_path, tensor_path, used_seed
        
        except Exception as e:
            print(f"[DEBUG] falhou: {e}")
        finally:
            torch.cuda.empty_cache()
            torch.cuda.ipc_collect()
            self.finalize(keep_paths=[])

    # ==============================================================================
    # --- FUNÇÃO #1: GERADOR DE CHUNK ÚNICO (AUXILIAR INTERNA) ---
    # ==============================================================================
    def _generate_single_chunk_low(
        self, prompt, negative_prompt, 
        height, width, num_frames, guidance_scale, 
        seed, itens_conditions_itens, 
        ltx_configs_override=None):
        """
        [NÓ DE GERAÇÃO]
        Gera um ÚNICO chunk de latentes brutos. Esta é a unidade de trabalho fundamental.
        """
        print("\n" + "-"*20 + " INÍCIO: _generate_single_chunk_low " + "-"*20)

        num_frames = ((num_frames - 1)//8)*8 + 1
        
        used_seed = random.randint(0, 2**32 - 1) if seed is None else int(seed)
        seed_everething(used_seed)
        
        # --- NÓ 1.1: SETUP DE PARÂMETROS ---
        height_padded = ((height - 1) // 8 + 1) * 8
        width_padded = ((width - 1) // 8 + 1) * 8
        generator = torch.Generator(device=self.device).manual_seed(seed)
        
        downscale_factor = self.config.get("downscale_factor", 0.6666666)
        vae_scale_factor = self.pipeline.vae_scale_factor
        
        x_width = int(width_padded * downscale_factor)
        downscaled_width = x_width - (x_width % vae_scale_factor)
        x_height = int(height_padded * downscale_factor)
        downscaled_height = x_height - (x_height % vae_scale_factor)
        
        # --- NÓ 1.2: MONTAGEM DE CONDIÇÕES E OVERRIDES ---
        
        first_pass_config = self.config.get("first_pass", {}).copy()

        if ltx_configs_override:
            print("[DEBUG] Sobrepondo configurações do LTX com valores da UI...")
            preset = ltx_configs_override.get("guidance_preset")
            if preset == "Customizado":
                try:
                    first_pass_config["guidance_scale"] = json.loads(ltx_configs_override["guidance_scale_list"])
                    first_pass_config["stg_scale"] = json.loads(ltx_configs_override["stg_scale_list"])
                    #first_pass_config["guidance_timesteps"] = json.loads(ltx_configs_override["timesteps_list"])
                except Exception as e:
                    print(f"  > ERRO ao parsear valores customizados: {e}. Usando Padrão como fallback.")
            elif preset == "Agressivo":
                first_pass_config["guidance_scale"] = [1, 2, 8, 12, 8, 2, 1]
                first_pass_config["stg_scale"] = [0, 0, 5, 6, 5, 3, 2]
            elif preset == "Suave":
                first_pass_config["guidance_scale"] = [1, 1, 4, 5, 4, 1, 1]
                first_pass_config["stg_scale"] = [0, 0, 2, 2, 2, 1, 0]
                                        
        first_pass_kwargs = {
            "prompt": prompt, "negative_prompt": negative_prompt, "height": downscaled_height, "width": downscaled_width,
            "num_frames": num_frames, "frame_rate": 24, "generator": generator, "output_type": "latent",
            "conditioning_items": itens_conditions_itens,
            **first_pass_config,
        }


        
        results_dir = "/app/output"; os.makedirs(results_dir, exist_ok=True)
    
        
        # --- NÓ 1.3: CHAMADA AO PIPELINE ---
        try: 
            with torch.autocast(device_type="cuda", dtype=self.runtime_autocast_dtype, enabled=self.device.type == 'cuda'):
                latents_bruto = self.pipeline(**first_pass_kwargs).images
                #latents_cpu_bruto = latents_bruto.detach().to("cpu")
                #tensor_path_cpu = os.path.join(results_dir, f"latents_low_res.pt")
                #torch.save(latents_cpu_bruto, tensor_path_cpu)
                log_tensor_info(latents_bruto, f"Latente Bruto Gerado para: '{prompt[:40]}...'")
        
            print("-" * 20 + " FIM: _generate_single_chunk_low " + "-"*20)
            return latents_bruto

        except Exception as e:
            print("-" * 20 + " ERRO: _generate_single_chunk_low --------------------")
            traceback.print_exc()
            print("-" * 20 + " ----------------------------------------------")
            return None
        finally:
            torch.cuda.empty_cache()
            torch.cuda.ipc_collect()
            self.finalize(keep_paths=[])

    # ==============================================================================
    # --- FUNÇÃO #2: ORQUESTRADOR NARRATIVO (MÚLTIPLOS PROMPTS) ---
    # ==============================================================================
    def generate_narrative_low(
        self, prompt: str, negative_prompt, 
        height, width, duration, guidance_scale, 
        seed, initial_conditions, overlap_frames: int = 8,
        ltx_configs_override: dict = None):
        """
        [ORQUESTRADOR NARRATIVO]
        Gera um vídeo em múltiplos chunks sequenciais a partir de um prompt com várias linhas.
        """
        print("\n" + "="*80)
        print("======           INICIANDO GERAÇÃO NARRATIVA EM CHUNKS (LOW-RES)           ======")
        print("="*80)
        
        
        used_seed = random.randint(0, 2**32 - 1) if seed is None else int(seed)
        seed_everething(used_seed)
        FPS = 24.0

        prompt_list = [p.strip() for p in prompt.splitlines() if p.strip()]
        num_chunks = len(prompt_list)
        if num_chunks == 0: raise ValueError("O prompt está vazio ou não contém linhas válidas.")
        
        total_actual_frames = max(9, int(round((round(duration * FPS) - 1) / 8.0) * 8 + 1))

        if num_chunks > 1:
            total_blocks = (total_actual_frames - 1) // 8
            blocks_per_chunk = total_blocks // num_chunks
            blocks_last_chunk = total_blocks - (blocks_per_chunk * (num_chunks - 1))
            frames_per_chunk = blocks_per_chunk * 8 + 1
            frames_per_chunk_last = blocks_last_chunk * 8 + 1
        else:
            frames_per_chunk = total_actual_frames
            frames_per_chunk_last = total_actual_frames
        
        frames_per_chunk = max(9, frames_per_chunk)
        frames_per_chunk_last = max(9, frames_per_chunk_last)
        
        poda_latents_num = overlap_frames // self.pipeline.video_scale_factor if self.pipeline.video_scale_factor > 0 else 0

        
        latentes_chunk_video = []
        overlap_condition = []
        overlap_latents = None
        lista_patch_latentes_chunk = []
        condition_item_latent_overlap = None
        temp_dir = tempfile.mkdtemp(prefix="ltxv_narrative_"); self._register_tmp_dir(temp_dir)
        results_dir = "/app/output"; os.makedirs(results_dir, exist_ok=True)
        
        for i, chunk_prompt in enumerate(prompt_list):
            print(f"\n--- Gerando Chunk Narrativo {i+1}/{num_chunks}: '{chunk_prompt}' ---")

            #current_image_conditions = []
            #if initial_image_conditions:
            #    cond_item_original = initial_image_conditions[0]
            #    if i == 0:
            #        current_image_conditions.append(cond_item_original)
            #    else:
            #        cond_item_fraco = ConditioningItem(
            #            media_item=cond_item_original.media_item, media_frame_number=0, conditioning_strength=0.1
            #        )
            #        current_image_conditions.append(cond_item_fraco)


            poda_latents_num = 8
            
            if i > 0 and poda_latents_num > 0:
                frames_per_chunk += poda_latents_num
            else:
                frames_per_chunk = frames_per_chunk
           
            if i == num_chunks - 1:
                frames_per_chunk = frames_per_chunk_last+poda_latents_num

            frames_per_chunk = ((frames_per_chunk - 1)//8)*8 + 1


            if i> 0:
                 initial_conditions = []

            if i > 0:
                initial_conditions = []
            
            if overlap_latents is not None:
                # Já é um tensor, usa diretamente
                overlap_latents_r = overlap_latents
                items_list = [[overlap_latents_r, 0, 1.0]]
                overlap_condition = self._prepare_condition_items_latent(items_list)
                        
            itens_conditions_itens = initial_conditions + overlap_condition

            latentes_bruto_r = self._generate_single_chunk_low(
                prompt=chunk_prompt, negative_prompt=negative_prompt, height=height, width=width,
                num_frames=frames_per_chunk, guidance_scale=guidance_scale, seed=used_seed + i,
                itens_conditions_itens=itens_conditions_itens, 
                ltx_configs_override=ltx_configs_override
            )


            print(f"[DEBUG]  generate_narrative_low.frames_per_chunk: {frames_per_chunk}")
            log_tensor_info(latentes_bruto_r, f"latentes_bruto_r recebidk: {i}...'")
        
            #latent_path_bufer = load_tensor(latent_path)
            #final_latents = torch.cat(lista_tensores, dim=2).to(self.device)
            
            
            #poda inicio overlap
            if i > 0 and poda_latents_num > 0,:
                 latentes_bruto = latentes_bruto_r[:, :, poda_latents_num:, :, :].clone()
            else: 
                latentes_bruto = latentes_bruto_r[:, :, :, :, :].clone()
    
            log_tensor_info(latentes_bruto, f"latentes_bruto recebidk: {i}...'")
        
            # cria estado overlap para proximo
            if i < num_chunks - 1 and poda_latents_num > 0:
                overlap_latents = latentes_bruto_r[:, :, -poda_latents_num:, :, :].clone()
                log_tensor_info(overlap_latents, f"overlap_latents recebidk: {i}...'")
                overlap_latents = overlap_latents.detach().to(self.device)
                condition_item_latent_overlap = ConditioningItem(
                    media_item=overlap_latents, media_frame_number=0, conditioning_strength=1.0
                )


            #adiciona a lista
            tensor_path_podado = os.path.join(results_dir, f"latents_poda{i}_res.pt")
            torch.save(latentes_bruto, tensor_path_podado)
            lista_patch_latentes_chunk.append(tensor_path_podado)

        print("\n--- Finalizando Narrativa: Concatenando chunks ---")
        
        # Carrega cada tensor do disco
        lista_tensores = [self._load_tensor(c) for c in lista_patch_latentes_chunk]
        final_latents = torch.cat(lista_tensores, dim=2).to(self.device)
        log_tensor_info(final_latents, "Tensor de Latentes Final Concatenado")
        
        try: 
            with torch.autocast(device_type="cuda", dtype=self.runtime_autocast_dtype, enabled=self.device.type == 'cuda'):
                pixel_tensor = vae_manager_singleton.decode(final_latents, decode_timestep=float(self.config.get("decode_timestep", 0.05)))
                pixel_tensor_cpu = pixel_tensor.detach().to("cpu")
                video_path = self._save_and_log_video(pixel_tensor_cpu, "narrative_video", FPS, temp_dir, results_dir, used_seed)
                final_latents_cpu = final_latents.detach().to("cpu")
                final_latents_patch = os.path.join(results_dir, f"latents_low_fim.pt")
                torch.save(final_latents_cpu, final_latents_patch)
            return video_path, final_latents_patch, used_seed
        
        except Exception as e:
            print("-" * 20 + " ERRO: generate_narrative_low --------------------")
            traceback.print_exc()
            print("-" * 20 + " ----------------------------------------------")
            return None, None, None
        finally:
            torch.cuda.empty_cache()
            torch.cuda.ipc_collect()
            self.finalize(keep_paths=[])
            
    # ==============================================================================
    # --- FUNÇÃO #3: ORQUESTRADOR SIMPLES (PROMPT ÚNICO) ---
    # ==============================================================================
    def generate_single_low(
        self, prompt: str, negative_prompt, 
        height, width, duration, guidance_scale, 
        seed, initial_conditions=None,
        ltx_configs_override: dict = None):
        """
        [ORQUESTRADOR SIMPLES]
        Gera um vídeo completo em um único chunk. Ideal para prompts simples e curtos.
        """
        print("\n" + "="*80)
        print("======             INICIANDO GERAÇÃO SIMPLES EM CHUNK ÚNICO (LOW-RES)             ======")
        print("="*80)

        used_seed = random.randint(0, 2**32 - 1) if seed is None else int(seed)
        seed_everething(used_seed)
        FPS = 24.0

        total_actual_frames = max(9, int(round((round(duration * FPS) - 1) / 8.0) * 8 + 1))
        
        temp_dir = tempfile.mkdtemp(prefix="ltxv_single_"); self._register_tmp_dir(temp_dir)
        results_dir = "/app/output"; os.makedirs(results_dir, exist_ok=True)

        # Chama a função de geração de chunk único para fazer todo o trabalho
        final_latents = self._generate_single_chunk_low(
            prompt=prompt, negative_prompt=negative_prompt, height=height, width=width,
            num_frames=total_actual_frames, guidance_scale=guidance_scale, seed=used_seed,
            itens_conditions_itens=initial_conditions, 
            ltx_configs_override=ltx_configs_override
        )

        print("\n--- Finalizando Geração Simples: Salvando e decodificando ---")
        log_tensor_info(final_latents, "Tensor de Latentes Final")

        try: 
            with torch.autocast(device_type="cuda", dtype=self.runtime_autocast_dtype, enabled=self.device.type == 'cuda'):
                pixel_tensor = vae_manager_singleton.decode(final_latents, decode_timestep=float(self.config.get("decode_timestep", 0.05)))
                pixel_tensor_cpu = pixel_tensor.detach().to("cpu")
                video_path = self._save_and_log_video(pixel_tensor_cpu, "narrative_video", FPS, temp_dir, results_dir, used_seed)
                final_latents_cpu = final_latents.detach().to("cpu")
                final_latents_patch = os.path.join(results_dir, f"latents_low_fim.pt")
                torch.save(final_latents_cpu, final_latents_patch)
            return video_path, final_latents_patch, used_seed
        except Exception as e:
            print("-" * 20 + " ERRO: generate_single_low --------------------")
            traceback.print_exc()
            print("-" * 20 + " ----------------------------------------------")
            return None, None, None
        finally:
            torch.cuda.empty_cache()
            torch.cuda.ipc_collect()
            self.finalize(keep_paths=[])
        

        
    # ==============================================================================
    # --- FUNÇÃO #4: ORQUESTRADOR  (Upscaler + texturas hd) ---
    # ==============================================================================
    def generate_upscale_denoise(
        self, latents_path, prompt, negative_prompt, 
        guidance_scale, seed,
    ):
            used_seed = random.randint(0, 2**32 - 1) if seed is None else int(seed)
            seed_everething(used_seed)
            temp_dir = tempfile.mkdtemp(prefix="ltxv_up_"); self._register_tmp_dir(temp_dir)
            results_dir = "/app/output"; os.makedirs(results_dir, exist_ok=True)
            latents_low = torch.load(latents_path).to(self.device)
            with torch.autocast(device_type="cuda", dtype=self.runtime_autocast_dtype, enabled=self.device == 'cuda'):
                upsampled_latents = self._upsample_latents_internal(latents_low)
                upsampled_latents = adain_filter_latent(latents=upsampled_latents, reference_latents=latents_low)
                del latents_low; torch.cuda.empty_cache()
                
                # --- LÓGICA DE DIVISÃO SIMPLES COM OVERLAP ---
                total_frames = upsampled_latents.shape[2]
                # Garante que mid_point seja pelo menos 1 para evitar um segundo chunk vazio se houver poucos frames
                mid_point = max(1, total_frames // 2) 
                chunk1 = upsampled_latents[:, :, :mid_point, :, :]
                # O segundo chunk começa um frame antes para criar o overlap
                chunk2 = upsampled_latents[:, :, mid_point - 1:, :, :]
                
                final_latents_list = []
                for i, chunk in enumerate([chunk1, chunk2]):
                    if chunk.shape[2] <= 1: continue # Pula chunks inválidos ou vazios
                    second_pass_height = chunk.shape[3] * self.pipeline.vae_scale_factor
                    second_pass_width = chunk.shape[4] * self.pipeline.vae_scale_factor
                    second_pass_kwargs = {
                        "prompt": prompt, "negative_prompt": negative_prompt, "height": second_pass_height, "width": second_pass_width,
                        "num_frames": chunk.shape[2], "latents": chunk, 
                        #"guidance_scale": float(guidance_scale),
                        "output_type": "latent", "generator": torch.Generator(device=self.device).manual_seed(used_seed),
                        **(self.config.get("second_pass", {}))
                    }
                    refined_chunk = self.pipeline(**second_pass_kwargs).images
                    # Remove o overlap do primeiro chunk refinado antes de juntar
                    if i == 0:
                        final_latents_list.append(refined_chunk[:, :, :-1, :, :])
                    else:
                        final_latents_list.append(refined_chunk)
    
            final_latents = torch.cat(final_latents_list, dim=2)
            log_tensor_info(final_latents, "Latentes Upscaled/Refinados Finais")
    
            latents_cpu = final_latents.detach().to("cpu")
            tensor_path = os.path.join(results_dir, f"latents_refined_{used_seed}.pt")
            torch.save(latents_cpu, tensor_path)
            pixel_tensor = vae_manager_singleton.decode(final_latents, decode_timestep=float(self.config.get("decode_timestep", 0.05)))
            video_path = self._save_and_log_video(pixel_tensor, "refined_video", 24.0, temp_dir, results_dir, used_seed)
            return video_path, tensor_path    

    def encode_mp4(self, latents_path: str, fps: int = 24):
        latents = torch.load(latents_path)
        seed = random.randint(0, 99999)
        temp_dir = tempfile.mkdtemp(prefix="ltxv_enc_"); self._register_tmp_dir(temp_dir)
        results_dir = "/app/output"; os.makedirs(results_dir, exist_ok=True)
        
        # --- LÓGICA DE DIVISÃO SIMPLES COM OVERLAP ---
        total_frames = latents.shape[2]
        mid_point = max(1, total_frames // 2)
        chunk1_latents = latents[:, :, :mid_point, :, :]
        chunk2_latents = latents[:, :, mid_point - 1:, :, :]
        
        video_parts = []
        pixel_chunks_to_concat = []
        with torch.autocast(device_type="cuda", dtype=self.runtime_autocast_dtype, enabled=self.device == 'cuda'):
            for i, chunk in enumerate([chunk1_latents, chunk2_latents]):
                if chunk.shape[2] == 0: continue
                pixel_chunk = vae_manager_singleton.decode(chunk.to(self.device), decode_timestep=float(self.config.get("decode_timestep", 0.05)))
                # Remove o overlap do primeiro chunk de pixels
                if i == 0:
                    pixel_chunks_to_concat.append(pixel_chunk[:, :, :-1, :, :])
                else:
                    pixel_chunks_to_concat.append(pixel_chunk)
        
        final_pixel_tensor = torch.cat(pixel_chunks_to_concat, dim=2)
        final_video_path = self._save_and_log_video(final_pixel_tensor, f"final_concatenated_{seed}", fps, temp_dir, results_dir, seed)
        return final_video_path

    def __init__(self):
        t0 = time.perf_counter()
        print("[DEBUG] Inicializando VideoService...")
        
        # 1. Obter o dispositivo alvo a partir do gerenciador
        #    Não definimos `self.device` ainda, apenas guardamos o alvo.
        target_device = gpu_manager.get_ltx_device()
        print(f"[DEBUG] LTX foi alocado para o dispositivo: {target_device}")

        # 2. Carregar a configuração e os modelos (na CPU, como a função _load_models faz)
        self.config = self._load_config()
        self.pipeline, self.latent_upsampler = self._load_models()

        # 3. Mover os modelos para o dispositivo alvo e definir `self.device`
        self.move_to_device(target_device) # Usando a função que já criamos!

        # 4. Configurar o resto dos componentes com o dispositivo correto
        self._apply_precision_policy()
        vae_manager_singleton.attach_pipeline(
            self.pipeline,
            device=self.device, # Agora `self.device` está correto
            autocast_dtype=self.runtime_autocast_dtype
        )
        self._tmp_dirs = set()
        print(f"[DEBUG] VideoService pronto. boot_time={time.perf_counter()-t0:.3f}s")
    
    # A função move_to_device que criamos antes é essencial aqui
    def move_to_device(self, device):
        """Move os modelos do pipeline para o dispositivo especificado."""
        print(f"[LTX] Movendo modelos para {device}...")
        self.device = torch.device(device) # Garante que é um objeto torch.device
        self.pipeline.to(self.device)
        if self.latent_upsampler:
            self.latent_upsampler.to(self.device)
        print(f"[LTX] Modelos agora estão em {self.device}.")
        
    def move_to_cpu(self):
        """Move os modelos para a CPU para liberar VRAM."""
        self.move_to_device(torch.device("cpu"))
        if torch.cuda.is_available():
            torch.cuda.empty_cache()

    
# Instanciação limpa, sem usar `self` fora da classe.
print("Criando instância do VideoService...")
video_generation_service = VideoService()
print("Instância do VideoService pronta.")