File size: 6,778 Bytes
c00dfc6
 
53de092
 
953686b
1cb67c9
 
 
 
 
 
953686b
c00dfc6
6f6213d
c00dfc6
618b3b5
6f6213d
d68340f
 
 
 
c00dfc6
1cb67c9
 
c00dfc6
 
 
 
1cb67c9
 
 
c00dfc6
1cb67c9
c00dfc6
 
 
 
 
504e476
1cb67c9
c00dfc6
 
1cb67c9
c00dfc6
1cb67c9
faea5a1
1cb67c9
c00dfc6
 
504e476
1cb67c9
c00dfc6
1cb67c9
c00dfc6
 
 
 
1cb67c9
 
 
 
cac6b1e
c00dfc6
 
 
 
 
 
faea5a1
c00dfc6
 
 
 
 
 
 
 
 
 
 
 
 
cac6b1e
c00dfc6
cac6b1e
c00dfc6
 
 
cac6b1e
c00dfc6
 
 
 
 
1cb67c9
 
53de092
c00dfc6
53de092
1cb67c9
c00dfc6
 
 
 
1cb67c9
 
c00dfc6
 
1cb67c9
c00dfc6
1cb67c9
c00dfc6
1cb67c9
c00dfc6
1cb67c9
 
c00dfc6
 
1cb67c9
c00dfc6
1cb67c9
 
c00dfc6
 
1cb67c9
 
c00dfc6
 
 
 
 
 
 
 
 
 
 
1cb67c9
c00dfc6
 
1eab4b6
1cb67c9
c00dfc6
1cb67c9
53de092
c00dfc6
53de092
c00dfc6
 
 
1cb67c9
c00dfc6
 
 
 
 
 
 
 
1cb67c9
 
1eab4b6
53de092
 
1cb67c9
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

# app_refactored_with_postprod.py (FINAL VERSION with LTX Refinement)

import gradio as gr
import os
import sys
import traceback
from pathlib import Path
import torch
import numpy as np
from PIL import Image

# --- Import dos Serviços de Backend ---

# Serviço LTX para geração de vídeo base e refinamento de textura
from api.ltx_video_complete import VideoService

video_service = VideoService()



# --- ESTADO DA SESSÃO ---
def create_initial_state():
    return {
        "low_res_video": None,
        "low_res_latents": None,
        "refined_video_ltx": None,
        "refined_latents_ltx": None,
        "used_seed": None
    }

# --- FUNÇÕES WRAPPER PARA A UI ---

def run_generate_low(prompt, neg_prompt, start_img, height, width, duration, cfg, seed, randomize_seed, progress=gr.Progress(track_tqdm=True)):
    """Executa a primeira etapa: geração de um vídeo base em baixa resolução."""
    print("UI: Chamando generate_low")
    if True:
        
        image_filepaths = []
        if start_img:
            image_filepaths.append(start_img)

            
        used_seed = None if randomize_seed else seed
        
        video_path, tensor_path, final_seed = video_service.generate_low_resolution(
            prompt=prompt, negative_prompt=neg_prompt,
            height=height, width=width, duration_secs=duration,
            guidance_scale=cfg, seed=used_seed,
            image_filepaths=image_filepaths
        )
        
        new_state = {
            "low_res_video": video_path,
            "low_res_latents": tensor_path,
            "refined_video_ltx": None,
            "refined_latents_ltx": None,
            "used_seed": final_seed
        }
        
        return video_path, new_state, gr.update(visible=True)

def run_ltx_refinement(state, prompt, neg_prompt, cfg, progress=gr.Progress(track_tqdm=True)):
    """Executa o processo de refinamento secundário."""
    print("UI: Chamando a função ponte 'apply_secondary_refinement'")
    
    try:
        # AQUI ESTÁ A MUDANÇA: Chamamos a nova função ponte
        video_path, tensor_path = video_service.apply_secondary_refinement(
            latents_path=state["low_res_latents"],
            prompt=prompt,
            negative_prompt=neg_prompt,
            guidance_scale=cfg,
            seed=state["used_seed"]
        )
        
        # Atualiza o estado com os novos artefatos refinados
        state["refined_video_ltx"] = video_path
        state["refined_latents_ltx"] = tensor_path
        
        return video_path, state
    
    except Exception as e:
        print(f"[ERRO na UI] Falha durante o refinamento secundário: {e}")
        traceback.print_exc()
        # Você pode retornar uma mensagem de erro para a UI aqui se quiser
        # Ex: return None, state
        raise gr.Error(f"Falha no Refinamento: {e}")

    
    
# --- DEFINIÇÃO DA INTERFACE GRADIO ---
with gr.Blocks() as demo:
    gr.Markdown("# LTX Video - Geração e Pós-Produção por Etapas")
    
    app_state = gr.State(value=create_initial_state())

    # --- ETAPA 1: Geração Base ---
    with gr.Row():
        with gr.Column(scale=1):
            gr.Markdown("### Etapa 1: Configurações de Geração")
            prompt_input = gr.Textbox(label="Prompt", value="A majestic dragon flying over a medieval castle", lines=3)
            neg_prompt_input = gr.Textbox(visible=False, label="Negative Prompt", value="worst quality, blurry, low quality, jittery", lines=2)
            start_image = gr.Image(label="Imagem de Início (Opcional)", type="filepath", sources=["upload", "clipboard"])
            
            with gr.Accordion("Parâmetros Avançados", open=False):
                height_input = gr.Slider(label="Height", value=512, step=32, minimum=256, maximum=1024)
                width_input = gr.Slider(label="Width", value=704, step=32, minimum=256, maximum=1024)
                duration_input = gr.Slider(label="Duração (s)", value=4, step=1, minimum=1, maximum=10)
                cfg_input = gr.Slider(label="Guidance Scale (CFG)", value=3.0, step=0.1, minimum=1.0, maximum=10.0)
                seed_input = gr.Number(label="Seed", value=42, precision=0)
                randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)

            generate_low_btn = gr.Button("1. Gerar Vídeo Base (Low-Res)", variant="primary")
        
        with gr.Column(scale=1):
            gr.Markdown("### Vídeo Base Gerado")
            low_res_video_output = gr.Video(label="O resultado da Etapa 1 aparecerá aqui", interactive=False)

    # --- ETAPA 2: Pós-Produção (no rodapé, em abas) ---
    with gr.Group(visible=False) as post_prod_group:
        gr.Markdown("<hr style='margin-top: 20px; margin-bottom: 20px;'>")
        gr.Markdown("## Etapa 2: Pós-Produção")
        gr.Markdown("Use o vídeo gerado acima como entrada para as ferramentas abaixo. **O prompt e a CFG da Etapa 1 serão reutilizados.**")

        with gr.Tabs():
            # --- ABA LTX REFINEMENT (AGORA FUNCIONAL) ---
            with gr.TabItem("🚀 Upscaler Textura (LTX)"):
                with gr.Row():
                    with gr.Column(scale=1):
                         gr.Markdown("### Parâmetros de Refinamento")
                         gr.Markdown("Esta etapa reutiliza o prompt, o prompt negativo e a CFG da Etapa 1 para manter a consistência.")
                         ltx_refine_btn = gr.Button("Aplicar Refinamento de Textura LTX", variant="primary")
                    with gr.Column(scale=1):
                        gr.Markdown("### Resultado do Refinamento")
                        ltx_refined_video_output = gr.Video(label="Vídeo com Textura Refinada (LTX)", interactive=False)

            # --- ABA SEEDVR UPSCALER ---
            with gr.TabItem("✨ Upscaler SeedVR"):
                gr.Markdown("### Resultado do Upscaling")
                        
            # --- ABA MM-AUDIO ---
            with gr.TabItem("🔊 Áudio (MM-Audio)"):
                gr.Markdown("*(Funcionalidade futura para adicionar som aos vídeos)*")

    # --- LÓGICA DE EVENTOS DA UI ---

    # Botão da Etapa 1
    generate_low_btn.click(
        fn=run_generate_low,
        inputs=[prompt_input, neg_prompt_input, start_image, height_input, width_input, duration_input, cfg_input, seed_input, randomize_seed],
        outputs=[low_res_video_output, app_state, post_prod_group]
    )

    # Botão da Aba LTX Refinement
    ltx_refine_btn.click(
        fn=run_ltx_refinement,
        inputs=[app_state, prompt_input, neg_prompt_input, cfg_input],
        outputs=[ltx_refined_video_output, app_state]
    )

    

if __name__ == "__main__":
    demo.queue().launch(server_name="0.0.0.0", server_port=7860, debug=True, show_error=True)