|
|
|
|
|
|
|
|
import gradio as gr |
|
|
import os |
|
|
import sys |
|
|
import traceback |
|
|
from pathlib import Path |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from api.ltx_server_refactored import video_generation_service |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from api.seedvr_server import SeedVRServer |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
seedvr_inference_server = SeedVRServer() if SeedVRServer else None |
|
|
|
|
|
|
|
|
def create_initial_state(): |
|
|
return { |
|
|
"low_res_video": None, |
|
|
"low_res_latents": None, |
|
|
"refined_video_ltx": None, |
|
|
"refined_latents_ltx": None, |
|
|
"used_seed": None |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
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") |
|
|
try: |
|
|
conditioning_items = [] |
|
|
if start_img: |
|
|
num_frames_estimate = int(duration * 24) |
|
|
items_list = [[start_img, 0, 1.0]] |
|
|
conditioning_items = video_generation_service.prepare_condition_items(items_list, height, width, num_frames_estimate) |
|
|
|
|
|
used_seed = None if randomize_seed else seed |
|
|
video_path, tensor_path, final_seed = video_generation_service.generate_low( |
|
|
prompt=prompt, negative_prompt=neg_prompt, |
|
|
height=height, width=width, duration=duration, |
|
|
guidance_scale=cfg, seed=used_seed, |
|
|
conditioning_items=conditioning_items |
|
|
) |
|
|
|
|
|
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) |
|
|
except Exception as e: |
|
|
error_message = f"❌ Ocorreu um erro na Geração Base:\n{e}" |
|
|
print(f"{error_message}\nDetalhes: {traceback.format_exc()}") |
|
|
raise gr.Error(error_message) |
|
|
|
|
|
def run_ltx_refinement(state, prompt, neg_prompt, cfg, progress=gr.Progress(track_tqdm=True)): |
|
|
"""Executa o processo de refinamento e upscaling de textura com o pipeline LTX.""" |
|
|
print("UI: Chamando run_ltx_refinement (generate_upscale_denoise)") |
|
|
if not state or not state.get("low_res_latents"): |
|
|
raise gr.Error("Erro: Gere um vídeo base primeiro na Etapa 1.") |
|
|
|
|
|
try: |
|
|
video_path, tensor_path = video_generation_service.generate_upscale_denoise( |
|
|
latents_path=state["low_res_latents"], |
|
|
prompt=prompt, |
|
|
negative_prompt=neg_prompt, |
|
|
guidance_scale=cfg, |
|
|
seed=state["used_seed"] |
|
|
) |
|
|
|
|
|
|
|
|
state["refined_video_ltx"] = video_path |
|
|
state["refined_latents_ltx"] = tensor_path |
|
|
|
|
|
return video_path, state |
|
|
except Exception as e: |
|
|
error_message = f"❌ Ocorreu um erro durante o Refinamento LTX:\n{e}" |
|
|
print(f"{error_message}\nDetalhes: {traceback.format_exc()}") |
|
|
raise gr.Error(error_message) |
|
|
|
|
|
def run_seedvr_upscaling(state, seed, resolution, batch_size, fps, progress=gr.Progress(track_tqdm=True)): |
|
|
"""Executa o processo de upscaling com SeedVR.""" |
|
|
if not state or not state.get("low_res_video"): |
|
|
raise gr.Error("Erro: Gere um vídeo base primeiro na Etapa 1.") |
|
|
if not seedvr_inference_server: |
|
|
raise gr.Error("Erro: O servidor SeedVR não está disponível.") |
|
|
|
|
|
video_path = state["low_res_video"] |
|
|
print(f"▶️ Iniciando processo de upscaling SeedVR para o vídeo: {video_path}") |
|
|
|
|
|
try: |
|
|
def progress_wrapper(p, desc=""): |
|
|
progress(p, desc=desc) |
|
|
output_filepath = seedvr_inference_server.run_inference( |
|
|
file_path=video_path, seed=seed, resolution=resolution, |
|
|
batch_size=batch_size, fps=fps, progress=progress_wrapper |
|
|
) |
|
|
final_message = f"✅ Processo SeedVR concluído!\nVídeo salvo em: {output_filepath}" |
|
|
return gr.update(value=output_filepath, interactive=True), gr.update(value=final_message, interactive=False) |
|
|
except Exception as e: |
|
|
error_message = f"❌ Ocorreu um erro grave durante o upscaling com SeedVR:\n{e}" |
|
|
print(f"{error_message}\nDetalhes: {traceback.format_exc()}") |
|
|
return None, gr.update(value=error_message, interactive=False) |
|
|
|
|
|
|
|
|
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()) |
|
|
|
|
|
|
|
|
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(interactive=False) |
|
|
|
|
|
|
|
|
with gr.Group(visible=False) as post_prod_group: |
|
|
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(): |
|
|
|
|
|
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) |
|
|
|
|
|
|
|
|
with gr.TabItem("✨ Upscaler SeedVR"): |
|
|
with gr.Row(): |
|
|
with gr.Column(scale=1): |
|
|
gr.Markdown("### Parâmetros do SeedVR") |
|
|
seedvr_seed = gr.Slider(minimum=0, maximum=999999, value=42, step=1, label="Seed") |
|
|
seedvr_resolution = gr.Slider(minimum=720, maximum=1440, value=1072, step=8, label="Resolução Vertical (Altura)") |
|
|
seedvr_batch_size = gr.Slider(minimum=1, maximum=16, value=4, step=1, label="Batch Size por GPU") |
|
|
seedvr_fps_output = gr.Number(label="FPS de Saída (0 = original)", value=0) |
|
|
run_seedvr_button = gr.Button("Iniciar Upscaling SeedVR", variant="primary", interactive=(seedvr_inference_server is not None)) |
|
|
if not seedvr_inference_server: |
|
|
gr.Markdown("Serviço SeedVR não disponível.") |
|
|
with gr.Column(scale=1): |
|
|
gr.Markdown("### Resultado do Upscaling") |
|
|
seedvr_video_output = gr.Video(label="Vídeo com Upscale SeedVR", interactive=False) |
|
|
seedvr_status_box = gr.Textbox(label="Status do Processamento", value="Aguardando...", lines=3, interactive=False) |
|
|
|
|
|
|
|
|
with gr.TabItem("🔊 Áudio (MM-Audio)"): |
|
|
gr.Markdown("*(Funcionalidade futura para adicionar som aos vídeos)*") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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] |
|
|
) |
|
|
|
|
|
|
|
|
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] |
|
|
) |
|
|
|
|
|
|
|
|
run_seedvr_button.click( |
|
|
fn=run_seedvr_upscaling, |
|
|
inputs=[app_state, seedvr_seed, seedvr_resolution, seedvr_batch_size, seedvr_fps_output], |
|
|
outputs=[seedvr_video_output, seedvr_status_box] |
|
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
|
demo.queue().launch(server_name="0.0.0.0", server_port=7860, debug=True, show_error=True) |