File size: 14,905 Bytes
280cfe1
 
 
db47818
280cfe1
db47818
280cfe1
9ac7175
 
280cfe1
9ac7175
 
280cfe1
 
 
 
 
9ac7175
280cfe1
 
 
 
 
 
 
 
 
 
 
9ac7175
 
280cfe1
 
 
9ac7175
 
280cfe1
 
 
 
 
 
8bbdce0
280cfe1
 
8bbdce0
280cfe1
 
8bbdce0
280cfe1
 
 
 
 
 
 
 
9ac7175
280cfe1
 
 
 
 
 
9ac7175
280cfe1
 
 
 
 
 
 
 
 
 
 
 
9ac7175
994d098
280cfe1
 
9ac7175
280cfe1
 
9ac7175
280cfe1
 
9ac7175
280cfe1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9ac7175
280cfe1
 
 
 
9ac7175
280cfe1
 
 
 
 
 
 
9ac7175
 
280cfe1
 
 
 
9ac7175
280cfe1
 
 
 
 
 
9ac7175
280cfe1
9ac7175
280cfe1
9ac7175
280cfe1
 
 
 
994d098
280cfe1
 
 
 
 
9ac7175
280cfe1
 
9ac7175
280cfe1
 
9ac7175
280cfe1
9ac7175
280cfe1
 
 
7720807
280cfe1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7720807
280cfe1
 
 
 
9ac7175
280cfe1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9ac7175
 
280cfe1
 
 
 
 
 
 
994d098
280cfe1
 
 
 
 
 
 
 
 
db47818
280cfe1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35e29a5
db47818
280cfe1
 
 
9ac7175
280cfe1
 
 
 
 
 
 
 
 
 
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
# FILE: app_complete.py
# DESCRIPTION: Gradio web interface for the LTX-Video generation service.
#              Provides a user-friendly, step-by-step workflow for creating videos.

import gradio as gr
import traceback
import sys

# ==============================================================================
# --- BACKEND SERVICES IMPORT ---
# ==============================================================================

# Encapsulate imports in a try-except block for robust error handling at startup.
try:
    # This assumes the backend file is named 'ltx_server_refactored_complete.py'
    # and is in a reachable path (e.g., 'api/').
    from api.ltx_server_refactored_complete import video_generation_service
    
    # Placeholder for SeedVR server.
    # from api.seedvr_server import SeedVRServer
    # seedvr_inference_server = SeedVRServer()
    seedvr_inference_server = None 
    print("Backend services imported successfully.")
except ImportError as e:
    print(f"FATAL ERROR: Could not import backend services. Ensure the backend file is accessible. Details: {e}")
    sys.exit(1)
except Exception as e:
    print(f"FATAL ERROR: An unexpected error occurred during backend initialization. Details: {e}")
    sys.exit(1)

# ==============================================================================
# --- UI WRAPPER FUNCTIONS ---
# These functions act as a bridge between the Gradio UI and the backend service.
# They handle data conversion, error catching, and UI updates.
# ==============================================================================

def run_generate_base_video(
    generation_mode: str, prompt: str, neg_prompt: str, start_img: str, 
    height: int, width: int, duration: float, seed: int, randomize_seed: bool,
    fp_guidance_preset: str, fp_guidance_scale_list: str, fp_stg_scale_list: str,
    progress=gr.Progress(track_tqdm=True)
) -> tuple:
    """
    Wrapper to call the backend for generating the initial low-resolution video.
    It decides whether to use the 'narrative' or 'single' generation mode.
    """
    try:
        print(f"[UI] Request received for base video generation. Mode: {generation_mode}")
        
        initial_conditions = []
        if start_img:
            # Estimate total frames for conditioning context
            num_frames_estimate = int(duration * 24)
            items_list = [[start_img, 0, 1.0]] # [[media, frame, weight]]
            initial_conditions = video_generation_service.prepare_condition_items(
                items_list, height, width, num_frames_estimate
            )

        # Package advanced LTX settings for the backend
        ltx_configs = {
            "guidance_preset": fp_guidance_preset,
            "guidance_scale_list": fp_guidance_scale_list,
            "stg_scale_list": fp_stg_scale_list,
        }

        # Select the appropriate backend function based on UI mode
        if generation_mode == "Narrativa (Múltiplos Prompts)":
            func_to_call = video_generation_service.generate_narrative_low
        else:
            func_to_call = video_generation_service.generate_single_low

        video_path, tensor_path, final_seed = func_to_call(
            prompt=prompt, negative_prompt=neg_prompt,
            height=height, width=width, duration=duration,
            seed=None if randomize_seed else int(seed),
            initial_conditions=initial_conditions,
            ltx_configs_override=ltx_configs
        )
        
        if not video_path:
            raise RuntimeError("Backend failed to return a valid video path.")

        # Update the session state with the results
        new_state = {"low_res_video": video_path, "low_res_latents": tensor_path, "used_seed": final_seed}
        
        print(f"[UI] Base video generation successful. Path: {video_path}")
        return video_path, new_state, gr.update(visible=True)
        
    except Exception as e:
        error_message = f"❌ An error occurred during base generation:\n{e}"
        print(f"{error_message}\nDetails: {traceback.format_exc()}")
        raise gr.Error(error_message)


def run_ltx_refinement(
    state: dict, prompt: str, neg_prompt: str,
    progress=gr.Progress(track_tqdm=True)
) -> tuple:
    """Wrapper to call the LTX texture refinement and upscaling backend function."""
    if not state or not state.get("low_res_latents"):
        raise gr.Error("Error: Please generate a base video in Step 1 before refining.")
    
    try:
        print("[UI] Request received for LTX refinement.")
        video_path, tensor_path = video_generation_service.generate_upscale_denoise(
            latents_path=state["low_res_latents"],
            prompt=prompt,
            negative_prompt=neg_prompt,
            seed=state["used_seed"]
        )
        # Update state with refined assets
        state["refined_video_ltx"] = video_path
        state["refined_latents_ltx"] = tensor_path
        print(f"[UI] LTX refinement successful. Path: {video_path}")
        return video_path, state
    except Exception as e:
        error_message = f"❌ An error occurred during LTX Refinement:\n{e}"
        print(f"{error_message}\nDetails: {traceback.format_exc()}")
        raise gr.Error(error_message)


def run_seedvr_upscaling(
    state: dict, seed: int, resolution: int, batch_size: int, fps: int,
    progress=gr.Progress(track_tqdm=True)
) -> tuple:
    """Wrapper to call the SeedVR upscaling backend service."""
    if not state or not state.get("low_res_video"):
        raise gr.Error("Error: Please generate a base video in Step 1 before upscaling.")
    if not seedvr_inference_server:
        raise gr.Error("Error: The SeedVR upscaling server is not available.")
    
    try:
        print("[UI] Request received for SeedVR upscaling.")
        def progress_wrapper(p, desc=""): progress(p, desc=desc)
        
        output_filepath = seedvr_inference_server.run_inference(
            file_path=state["low_res_video"], seed=seed, resolution=resolution,
            batch_size=batch_size, fps=fps, progress=progress_wrapper
        )
        
        status_message = f"✅ Upscaling complete!\nSaved to: {output_filepath}"
        print(f"[UI] SeedVR upscaling successful. Path: {output_filepath}")
        return gr.update(value=output_filepath), gr.update(value=status_message)
    except Exception as e:
        error_message = f"❌ An error occurred during SeedVR Upscaling:\n{e}"
        print(f"{error_message}\nDetails: {traceback.format_exc()}")
        return None, gr.update(value=error_message)


# ==============================================================================
# --- UI BUILDER ---
# Functions dedicated to creating parts of the Gradio interface.
# ==============================================================================

def build_ui():
    """Constructs the entire Gradio application UI."""
    
    with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as demo:
        # App state persists across interactions within a session
        app_state = gr.State(value={"low_res_video": None, "low_res_latents": None, "used_seed": None})
        
        gr.Markdown("# LTX Video - Geração e Pós-Produção por Etapas", elem_id="main-title")
        
        ui_components = {} # Dictionary to hold all key UI components

        with gr.Row():
            with gr.Column(scale=1):
                # Build the main generation controls (Step 1)
                _build_generation_controls(ui_components)
            
            with gr.Column(scale=1):
                gr.Markdown("### Vídeo Base Gerado")
                ui_components['low_res_video_output'] = gr.Video(
                    label="O resultado da Etapa 1 aparecerá aqui", interactive=False
                )
        
        # Build the post-production section (Step 2), initially hidden
        _build_postprod_controls(ui_components)
        
        # Connect all UI events to their corresponding functions
        _register_event_handlers(app_state, ui_components)

    return demo

def _build_generation_controls(ui: dict):
    """Builds the UI components for Step 1: Base Video Generation."""
    gr.Markdown("### Etapa 1: Configurações de Geração")
    
    ui['generation_mode'] = gr.Radio(
        label="Modo de Geração", 
        choices=["Simples (Prompt Único)", "Narrativa (Múltiplos Prompts)"],
        value="Narrativa (Múltiplos Prompts)", 
        info="Simples para uma ação contínua, Narrativa para uma sequência de cenas (uma por linha)."
    )
    ui['prompt'] = gr.Textbox(label="Prompt(s)", value="Um leão majestoso caminha pela savana\nEle sobe em uma grande pedra e olha o horizonte", lines=4)
    ui['neg_prompt'] = gr.Textbox(label="Negative Prompt", value="blurry, low quality, bad anatomy, deformed", lines=2)
    ui['start_image'] = gr.Image(label="Imagem de Início (Opcional)", type="filepath", sources=["upload"])
    
    with gr.Accordion("Parâmetros Principais", open=True):
        ui['duration'] = gr.Slider(label="Duração Total (s)", value=4, step=1, minimum=1, maximum=30)
        with gr.Row():
            ui['height'] = gr.Slider(label="Height", value=432, step=16, minimum=256, maximum=1024)
            ui['width'] = gr.Slider(label="Width", value=768, step=16, minimum=256, maximum=1024)
        with gr.Row():
            ui['seed'] = gr.Number(label="Seed", value=42, precision=0)
            ui['randomize_seed'] = gr.Checkbox(label="Randomize Seed", value=True)

    with gr.Accordion("Opções Avançadas de Guiagem (First Pass)", open=False):
        ui['fp_guidance_preset'] = gr.Dropdown(
            label="Preset de Guiagem",
            choices=["Padrão (Recomendado)", "Agressivo", "Suave", "Customizado"],
            value="Padrão (Recomendado)", 
            info="Controla como a guiagem de texto se comporta ao longo da difusão."
        )
        with gr.Group(visible=False) as ui['custom_guidance_group']:
            gr.Markdown("⚠️ Edite as listas em formato JSON. Ex: `[1.0, 2.5, 3.0]`")
            ui['fp_guidance_scale_list'] = gr.Textbox(label="Lista de Guidance Scale", value="[1, 1, 6, 8, 6, 1, 1]")
            ui['fp_stg_scale_list'] = gr.Textbox(label="Lista de STG Scale (Movimento)", value="[0, 0, 4, 4, 4, 2, 1]")
    
    ui['generate_low_btn'] = gr.Button("1. Gerar Vídeo Base", variant="primary")

def _build_postprod_controls(ui: dict):
    """Builds the UI components for Step 2: Post-Production."""
    with gr.Group(visible=False) as ui['post_prod_group']:
        gr.Markdown("--- \n## Etapa 2: Pós-Produção", elem_id="postprod-title")
        
        with gr.Tabs():
            with gr.TabItem("🚀 Upscaler de Textura (LTX)"):
                with gr.Row():
                    with gr.Column(scale=1):
                         gr.Markdown("Usa o prompt e a semente originais para refinar o vídeo, adicionando detalhes e texturas de alta qualidade.")
                         ui['ltx_refine_btn'] = gr.Button("2. Aplicar Refinamento LTX", variant="primary")
                    with gr.Column(scale=1):
                        ui['ltx_refined_video_output'] = gr.Video(label="Vídeo com Textura Refinada", interactive=False)
            
            with gr.TabItem("✨ Upscaler de Resolução (SeedVR)"):
                is_seedvr_available = seedvr_inference_server is not None
                if not is_seedvr_available:
                    gr.Markdown("🔴 *O serviço SeedVR não está disponível nesta instância.*")
                
                with gr.Row():
                    with gr.Column(scale=1):
                        ui['seedvr_seed'] = gr.Slider(minimum=0, maximum=999999, value=42, step=1, label="Seed")
                        ui['seedvr_resolution'] = gr.Slider(minimum=720, maximum=1440, value=1072, step=8, label="Resolução Vertical")
                        ui['seedvr_batch_size'] = gr.Slider(minimum=1, maximum=16, value=4, step=1, label="Batch Size por GPU")
                        ui['seedvr_fps'] = gr.Number(label="FPS de Saída (0 = original)", value=0)
                        ui['run_seedvr_btn'] = gr.Button("2. Iniciar Upscaling SeedVR", variant="primary", interactive=is_seedvr_available)
                    with gr.Column(scale=1):
                        ui['seedvr_video_output'] = gr.Video(label="Vídeo com Upscale SeedVR", interactive=False)
                        ui['seedvr_status_box'] = gr.Textbox(label="Status", value="Aguardando...", lines=3, interactive=False)


# ==============================================================================
# --- EVENT HANDLERS ---
# Connects UI component events (like clicks) to the wrapper functions.
# ==============================================================================

def _register_event_handlers(app_state: gr.State, ui: dict):
    """Registers all Gradio event handlers."""
    
    # --- Handler for custom guidance visibility ---
    def toggle_custom_guidance(preset_choice: str) -> gr.update:
        return gr.update(visible=(preset_choice == "Customizado"))
    
    ui['fp_guidance_preset'].change(
        fn=toggle_custom_guidance,
        inputs=ui['fp_guidance_preset'],
        outputs=ui['custom_guidance_group']
    )

    # --- Handler for the main "Generate" button ---
    gen_inputs = [
        ui['generation_mode'], ui['prompt'], ui['neg_prompt'], ui['start_image'],
        ui['height'], ui['width'], ui['duration'], ui['seed'], ui['randomize_seed'],
        ui['fp_guidance_preset'], ui['fp_guidance_scale_list'], ui['fp_stg_scale_list']
    ]
    gen_outputs = [
        ui['low_res_video_output'], app_state, ui['post_prod_group']
    ]
    ui['generate_low_btn'].click(fn=run_generate_base_video, inputs=gen_inputs, outputs=gen_outputs)

    # --- Handler for the LTX Refine button ---
    refine_inputs = [app_state, ui['prompt'], ui['neg_prompt']]
    refine_outputs = [ui['ltx_refined_video_output'], app_state]
    ui['ltx_refine_btn'].click(fn=run_ltx_refinement, inputs=refine_inputs, outputs=refine_outputs)
    
    # --- Handler for the SeedVR Upscale button ---
    if 'run_seedvr_btn' in ui:
        seedvr_inputs = [app_state, ui['seedvr_seed'], ui['seedvr_resolution'], ui['seedvr_batch_size'], ui['seedvr_fps']]
        seedvr_outputs = [ui['seedvr_video_output'], ui['seedvr_status_box']]
        ui['run_seedvr_btn'].click(fn=run_seedvr_upscaling, inputs=seedvr_inputs, outputs=seedvr_outputs)


# ==============================================================================
# --- APPLICATION ENTRY POINT ---
# ==============================================================================

if __name__ == "__main__":
    print("Building Gradio UI...")
    gradio_app = build_ui()
    print("Launching Gradio app...")
    gradio_app.queue().launch(
        server_name="0.0.0.0", 
        server_port=7860, 
        debug=True, 
        show_error=True
    )