euiiiia commited on
Commit
6b637f7
·
verified ·
1 Parent(s): a01cea0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +105 -139
app.py CHANGED
@@ -1,4 +1,5 @@
1
- # app_refactored_with_postprod.py (com Presets de Guiagem e Opções LTX Completas)
 
2
 
3
  import gradio as gr
4
  import os
@@ -7,211 +8,176 @@ import traceback
7
  from pathlib import Path
8
 
9
  # --- Import dos Serviços de Backend ---
10
- try:
11
- from api.ltx_server_refactored import video_generation_service
12
- except ImportError:
13
- print("ERRO FATAL: Não foi possível importar 'video_generation_service' de 'api.ltx_server_refactored'.")
14
- sys.exit(1)
15
-
16
- try:
17
- from api.seedvr_server import SeedVRServer
18
- except ImportError:
19
- print("AVISO: Não foi possível importar SeedVRServer. A aba de upscaling SeedVR será desativada.")
20
- SeedVRServer = None
21
 
 
 
 
 
 
 
 
 
22
  seedvr_inference_server = SeedVRServer() if SeedVRServer else None
23
 
24
  # --- ESTADO DA SESSÃO ---
25
  def create_initial_state():
26
- return {"low_res_video": None, "low_res_latents": None, "used_seed": None}
 
 
 
 
 
 
27
 
28
  # --- FUNÇÕES WRAPPER PARA A UI ---
29
 
30
- def run_generate_base_video(
31
- # Parâmetros de Geração
32
- generation_mode, prompt, neg_prompt, start_img, height, width, duration, cfg, seed, randomize_seed,
33
- fp_guidance_preset, fp_guidance_scale_list, fp_stg_scale_list, fp_timesteps_list,
34
- progress=gr.Progress(track_tqdm=True)
35
- ):
36
- """
37
- Função wrapper que decide qual pipeline de backend chamar, passando todas as configurações LTX.
38
- """
39
- print(f"UI: Iniciando geração no modo: {generation_mode}")
40
-
41
- try:
42
- initial_image_conditions = []
43
  if start_img:
44
  num_frames_estimate = int(duration * 24)
45
  items_list = [[start_img, 0, 1.0]]
46
- initial_image_conditions = video_generation_service.prepare_condition_items(items_list, height, width, num_frames_estimate)
47
 
48
  used_seed = None if randomize_seed else seed
 
 
 
 
 
 
49
 
50
- # Agrupa todas as configurações LTX em um único dicionário para o backend
51
- ltx_configs = {
52
- "guidance_preset": fp_guidance_preset,
53
- "guidance_scale_list": fp_guidance_scale_list,
54
- "stg_scale_list": fp_stg_scale_list,
55
- "timesteps_list": fp_timesteps_list,
56
  }
57
-
58
- # Decide qual função de backend chamar com base no modo
59
- if generation_mode == "Narrativa (Múltiplos Prompts)":
60
- video_path, tensor_path, final_seed = video_generation_service.generate_narrative_low(
61
- prompt=prompt, negative_prompt=neg_prompt,
62
- height=height, width=width, duration=duration,
63
- guidance_scale=cfg, seed=used_seed,
64
- initial_image_conditions=initial_image_conditions,
65
- ltx_configs_override=ltx_configs,
66
- )
67
- else: # Modo "Simples (Prompt Único)"
68
- video_path, tensor_path, final_seed = video_generation_service.generate_single_low(
69
- prompt=prompt, negative_prompt=neg_prompt,
70
- height=height, width=width, duration=duration,
71
- guidance_scale=cfg, seed=used_seed,
72
- initial_image_conditions=initial_image_conditions,
73
- ltx_configs_override=ltx_configs,
74
- )
75
-
76
- new_state = {"low_res_video": video_path, "low_res_latents": tensor_path, "used_seed": final_seed}
77
 
78
  return video_path, new_state, gr.update(visible=True)
79
-
80
- except Exception as e:
81
- error_message = f"❌ Ocorreu um erro na Geração Base:\n{e}"
82
- print(f"{error_message}\nDetalhes: {traceback.format_exc()}")
83
- raise gr.Error(error_message)
84
-
85
  def run_ltx_refinement(state, prompt, neg_prompt, cfg, progress=gr.Progress(track_tqdm=True)):
86
- if not state or not state.get("low_res_latents"):
87
- raise gr.Error("Erro: Gere um vídeo base primeiro na Etapa 1.")
88
- try:
 
89
  video_path, tensor_path = video_generation_service.generate_upscale_denoise(
90
- latents_path=state["low_res_latents"], prompt=prompt,
91
- negative_prompt=neg_prompt, guidance_scale=cfg, seed=state["used_seed"]
 
 
 
92
  )
93
- state["refined_video_ltx"] = video_path; state["refined_latents_ltx"] = tensor_path
 
 
 
 
94
  return video_path, state
95
- except Exception as e:
96
- raise gr.Error(f"Erro no Refinamento LTX: {e}")
97
-
98
  def run_seedvr_upscaling(state, seed, resolution, batch_size, fps, progress=gr.Progress(track_tqdm=True)):
99
- if not state or not state.get("low_res_video"):
100
- raise gr.Error("Erro: Gere um vídeo base primeiro na Etapa 1.")
101
- if not seedvr_inference_server:
102
- raise gr.Error("Erro: O servidor SeedVR não está disponível.")
103
- try:
104
- def progress_wrapper(p, desc=""): progress(p, desc=desc)
 
 
105
  output_filepath = seedvr_inference_server.run_inference(
106
- file_path=state["low_res_video"], seed=seed, resolution=resolution,
107
  batch_size=batch_size, fps=fps, progress=progress_wrapper
108
  )
109
- return gr.update(value=output_filepath), gr.update(value=f"✅ Concluído!\nSalvo em: {output_filepath}")
110
- except Exception as e:
111
- return None, gr.update(value=f"❌ Erro no SeedVR:\n{e}")
112
-
113
  # --- DEFINIÇÃO DA INTERFACE GRADIO ---
114
  with gr.Blocks() as demo:
115
  gr.Markdown("# LTX Video - Geração e Pós-Produção por Etapas")
116
 
117
  app_state = gr.State(value=create_initial_state())
118
 
 
119
  with gr.Row():
120
  with gr.Column(scale=1):
121
  gr.Markdown("### Etapa 1: Configurações de Geração")
 
 
 
122
 
123
- generation_mode_input = gr.Radio(
124
- label="Modo de Geração", choices=["Simples (Prompt Único)", "Narrativa (Múltiplos Prompts)"],
125
- value="Narrativa (Múltiplos Prompts)", info="Simples para uma ação, Narrativa para uma sequência (uma cena por linha)."
126
- )
127
- prompt_input = 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)
128
- neg_prompt_input = gr.Textbox(label="Negative Prompt", value="blurry, low quality, bad anatomy", lines=2)
129
- start_image = gr.Image(label="Imagem de Início (Opcional)", type="filepath", sources=["upload"])
130
-
131
- with gr.Accordion("Parâmetros Principais", open=False):
132
- duration_input = gr.Slider(label="Duração Total (s)", value=1, step=1, minimum=1, maximum=40)
133
- with gr.Row():
134
- height_input = gr.Slider(label="Height", value=720, step=32, minimum=256, maximum=1024)
135
- width_input = gr.Slider(label="Width", value=720, step=32, minimum=256, maximum=1024)
136
- with gr.Row():
137
- seed_input = gr.Number(label="Seed", value=42, precision=0)
138
- randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
139
-
140
- with gr.Accordion("Opções Adicionais LTX (Avançado)", open=False):
141
- cfg_input = gr.Slider(label="Guidance Scale (CFG)", info="Afeta o refinamento (se usado) e não tem efeito no First Pass dos modelos 'distilled'.", value=0.0, step=1, minimum=0.0, maximum=10.0)
142
- fp_num_inference_steps = gr.Slider(label="Passos de Inferência (First Pass)", minimum=10, maximum=100, step=1, value=10)
143
- ship_initial_inference_steps = gr.Slider(label="Passos de Inferência (Ship First)", minimum=0, maximum=100, step=1, value=0)
144
- ship_final_inference_steps = gr.Slider(label="Passos de Inferência (Ship Last)", minimum=0, maximum=100, step=1, value=0)
145
-
146
- with gr.Tabs():
147
- with gr.TabItem("Guiagem (First Pass)"):
148
- fp_guidance_preset = gr.Dropdown(
149
- label="Preset de Guiagem",
150
- choices=["Padrão (Recomendado)", "Agressivo", "Suave", "Customizado"],
151
- value="Padrão (Recomendado)", info="Muda o comportamento da guiagem ao longo da difusão."
152
- )
153
- with gr.Group(visible=False) as custom_guidance_group:
154
- gr.Markdown("⚠️ Edite as listas em formato JSON. Ex: `[1, 2, 3]`")
155
- fp_guidance_scale_list = gr.Textbox(label="Lista de Guidance Scale", value="[1, 1, 6, 8, 6, 1, 1]")
156
- fp_stg_scale_list = gr.Textbox(label="Lista de STG Scale (Movimento)", value="[0, 0, 4, 4, 4, 2, 1]")
157
- fp_timesteps_list = gr.Textbox(label="Lista de Guidance Timesteps", value="[1.0, 0.996, 0.9933, 0.9850, 0.9767, 0.9008, 0.6180]")
158
-
159
- generate_low_btn = gr.Button("1. Gerar Vídeo Base", variant="primary")
160
 
161
  with gr.Column(scale=1):
162
  gr.Markdown("### Vídeo Base Gerado")
163
  low_res_video_output = gr.Video(label="O resultado da Etapa 1 aparecerá aqui", interactive=False)
164
 
 
165
  with gr.Group(visible=False) as post_prod_group:
 
166
  gr.Markdown("## Etapa 2: Pós-Produção")
167
-
 
168
  with gr.Tabs():
 
169
  with gr.TabItem("🚀 Upscaler Textura (LTX)"):
170
  with gr.Row():
171
  with gr.Column(scale=1):
172
- gr.Markdown("Reutiliza o prompt e CFG para refinar a textura.")
173
- ltx_refine_btn = gr.Button("Aplicar Refinamento LTX", variant="primary")
 
174
  with gr.Column(scale=1):
175
- ltx_refined_video_output = gr.Video(label="Vídeo com Textura Refinada", interactive=False)
176
-
 
 
177
  with gr.TabItem("✨ Upscaler SeedVR"):
178
  with gr.Row():
179
  with gr.Column(scale=1):
 
180
  seedvr_seed = gr.Slider(minimum=0, maximum=999999, value=42, step=1, label="Seed")
181
- seedvr_resolution = gr.Slider(minimum=720, maximum=1440, value=1072, step=8, label="Resolução Vertical")
182
  seedvr_batch_size = gr.Slider(minimum=1, maximum=16, value=4, step=1, label="Batch Size por GPU")
183
  seedvr_fps_output = gr.Number(label="FPS de Saída (0 = original)", value=0)
184
  run_seedvr_button = gr.Button("Iniciar Upscaling SeedVR", variant="primary", interactive=(seedvr_inference_server is not None))
 
 
185
  with gr.Column(scale=1):
 
186
  seedvr_video_output = gr.Video(label="Vídeo com Upscale SeedVR", interactive=False)
187
- seedvr_status_box = gr.Textbox(label="Status", value="Aguardando...", lines=3, interactive=False)
188
 
189
- # --- LÓGICA DE EVENTOS ---
190
- def update_custom_guidance_visibility(preset_choice):
191
- return gr.update(visible=(preset_choice == "Customizado"))
192
-
193
- fp_guidance_preset.change(fn=update_custom_guidance_visibility, inputs=fp_guidance_preset, outputs=custom_guidance_group)
194
 
195
- all_ltx_inputs = [
196
- fp_guidance_preset, fp_guidance_scale_list, fp_stg_scale_list, fp_timesteps_list
197
- ]
198
-
199
  generate_low_btn.click(
200
- fn=run_generate_base_video,
201
- inputs=[
202
- generation_mode_input, prompt_input, neg_prompt_input, start_image, height_input, width_input,
203
- duration_input, cfg_input, seed_input, randomize_seed,
204
- *all_ltx_inputs
205
- ],
206
  outputs=[low_res_video_output, app_state, post_prod_group]
207
  )
208
 
 
209
  ltx_refine_btn.click(
210
  fn=run_ltx_refinement,
211
  inputs=[app_state, prompt_input, neg_prompt_input, cfg_input],
212
  outputs=[ltx_refined_video_output, app_state]
213
  )
214
 
 
215
  run_seedvr_button.click(
216
  fn=run_seedvr_upscaling,
217
  inputs=[app_state, seedvr_seed, seedvr_resolution, seedvr_batch_size, seedvr_fps_output],
@@ -219,4 +185,4 @@ with gr.Blocks() as demo:
219
  )
220
 
221
  if __name__ == "__main__":
222
- demo.queue().launch(server_name="0.0.0.0", server_port=7860, debug=True, show_error=True)
 
1
+
2
+ # app_refactored_with_postprod.py (FINAL VERSION with LTX Refinement)
3
 
4
  import gradio as gr
5
  import os
 
8
  from pathlib import Path
9
 
10
  # --- Import dos Serviços de Backend ---
 
 
 
 
 
 
 
 
 
 
 
11
 
12
+ # Serviço LTX para geração de vídeo base e refinamento de textura
13
+ from api.ltx_server_refactored import video_generation_service
14
+
15
+ # Serviço SeedVR para upscaling de alta qualidade
16
+ from api.seedvr_server import SeedVRServer
17
+
18
+
19
+ # Inicializa o servidor SeedVR uma vez, se disponível
20
  seedvr_inference_server = SeedVRServer() if SeedVRServer else None
21
 
22
  # --- ESTADO DA SESSÃO ---
23
  def create_initial_state():
24
+ return {
25
+ "low_res_video": None,
26
+ "low_res_latents": None,
27
+ "refined_video_ltx": None,
28
+ "refined_latents_ltx": None,
29
+ "used_seed": None
30
+ }
31
 
32
  # --- FUNÇÕES WRAPPER PARA A UI ---
33
 
34
+ def run_generate_low(prompt, neg_prompt, start_img, height, width, duration, cfg, seed, randomize_seed, progress=gr.Progress(track_tqdm=True)):
35
+ """Executa a primeira etapa: geração de um vídeo base em baixa resolução."""
36
+ print("UI: Chamando generate_low")
37
+ if True:
38
+ conditioning_items = []
 
 
 
 
 
 
 
 
39
  if start_img:
40
  num_frames_estimate = int(duration * 24)
41
  items_list = [[start_img, 0, 1.0]]
42
+ conditioning_items = video_generation_service.prepare_condition_items(items_list, height, width, num_frames_estimate)
43
 
44
  used_seed = None if randomize_seed else seed
45
+ video_path, tensor_path, final_seed = video_generation_service.generate_low(
46
+ prompt=prompt, negative_prompt=neg_prompt,
47
+ height=height, width=width, duration=duration,
48
+ guidance_scale=cfg, seed=used_seed,
49
+ conditioning_items=conditioning_items
50
+ )
51
 
52
+ new_state = {
53
+ "low_res_video": video_path,
54
+ "low_res_latents": tensor_path,
55
+ "refined_video_ltx": None,
56
+ "refined_latents_ltx": None,
57
+ "used_seed": final_seed
58
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
 
60
  return video_path, new_state, gr.update(visible=True)
61
+
 
 
 
 
 
62
  def run_ltx_refinement(state, prompt, neg_prompt, cfg, progress=gr.Progress(track_tqdm=True)):
63
+ """Executa o processo de refinamento e upscaling de textura com o pipeline LTX."""
64
+ print("UI: Chamando run_ltx_refinement (generate_upscale_denoise)")
65
+
66
+ if True:
67
  video_path, tensor_path = video_generation_service.generate_upscale_denoise(
68
+ latents_path=state["low_res_latents"],
69
+ prompt=prompt,
70
+ negative_prompt=neg_prompt,
71
+ guidance_scale=cfg,
72
+ seed=state["used_seed"]
73
  )
74
+
75
+ # Atualiza o estado com os novos artefatos refinados
76
+ state["refined_video_ltx"] = video_path
77
+ state["refined_latents_ltx"] = tensor_path
78
+
79
  return video_path, state
80
+
 
 
81
  def run_seedvr_upscaling(state, seed, resolution, batch_size, fps, progress=gr.Progress(track_tqdm=True)):
82
+ """Executa o processo de upscaling com SeedVR."""
83
+
84
+ video_path = state["low_res_video"]
85
+ print(f"▶️ Iniciando processo de upscaling SeedVR para o vídeo: {video_path}")
86
+
87
+ if True:
88
+ def progress_wrapper(p, desc=""):
89
+ progress(p, desc=desc)
90
  output_filepath = seedvr_inference_server.run_inference(
91
+ file_path=video_path, seed=seed, resolution=resolution,
92
  batch_size=batch_size, fps=fps, progress=progress_wrapper
93
  )
94
+ final_message = f"✅ Processo SeedVR concluído!\nVídeo salvo em: {output_filepath}"
95
+ return gr.update(value=output_filepath, interactive=True), gr.update(value=final_message, interactive=False)
96
+
 
97
  # --- DEFINIÇÃO DA INTERFACE GRADIO ---
98
  with gr.Blocks() as demo:
99
  gr.Markdown("# LTX Video - Geração e Pós-Produção por Etapas")
100
 
101
  app_state = gr.State(value=create_initial_state())
102
 
103
+ # --- ETAPA 1: Geração Base ---
104
  with gr.Row():
105
  with gr.Column(scale=1):
106
  gr.Markdown("### Etapa 1: Configurações de Geração")
107
+ prompt_input = gr.Textbox(label="Prompt", value="A majestic dragon flying over a medieval castle", lines=3)
108
+ neg_prompt_input = gr.Textbox(visible=False, label="Negative Prompt", value="worst quality, blurry, low quality, jittery", lines=2)
109
+ start_image = gr.Image(label="Imagem de Início (Opcional)", type="filepath", sources=["upload", "clipboard"])
110
 
111
+ with gr.Accordion("Parâmetros Avançados", open=False):
112
+ height_input = gr.Slider(label="Height", value=512, step=32, minimum=256, maximum=1024)
113
+ width_input = gr.Slider(label="Width", value=704, step=32, minimum=256, maximum=1024)
114
+ duration_input = gr.Slider(label="Duração (s)", value=4, step=1, minimum=1, maximum=10)
115
+ cfg_input = gr.Slider(label="Guidance Scale (CFG)", value=3.0, step=0.1, minimum=1.0, maximum=10.0)
116
+ seed_input = gr.Number(label="Seed", value=42, precision=0)
117
+ randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
118
+
119
+ generate_low_btn = gr.Button("1. Gerar Vídeo Base (Low-Res)", variant="primary")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
120
 
121
  with gr.Column(scale=1):
122
  gr.Markdown("### Vídeo Base Gerado")
123
  low_res_video_output = gr.Video(label="O resultado da Etapa 1 aparecerá aqui", interactive=False)
124
 
125
+ # --- ETAPA 2: Pós-Produção (no rodapé, em abas) ---
126
  with gr.Group(visible=False) as post_prod_group:
127
+ gr.Markdown("<hr style='margin-top: 20px; margin-bottom: 20px;'>")
128
  gr.Markdown("## Etapa 2: Pós-Produção")
129
+ 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.**")
130
+
131
  with gr.Tabs():
132
+ # --- ABA LTX REFINEMENT (AGORA FUNCIONAL) ---
133
  with gr.TabItem("🚀 Upscaler Textura (LTX)"):
134
  with gr.Row():
135
  with gr.Column(scale=1):
136
+ gr.Markdown("### Parâmetros de Refinamento")
137
+ gr.Markdown("Esta etapa reutiliza o prompt, o prompt negativo e a CFG da Etapa 1 para manter a consistência.")
138
+ ltx_refine_btn = gr.Button("Aplicar Refinamento de Textura LTX", variant="primary")
139
  with gr.Column(scale=1):
140
+ gr.Markdown("### Resultado do Refinamento")
141
+ ltx_refined_video_output = gr.Video(label="Vídeo com Textura Refinada (LTX)", interactive=False)
142
+
143
+ # --- ABA SEEDVR UPSCALER ---
144
  with gr.TabItem("✨ Upscaler SeedVR"):
145
  with gr.Row():
146
  with gr.Column(scale=1):
147
+ gr.Markdown("### Parâmetros do SeedVR")
148
  seedvr_seed = gr.Slider(minimum=0, maximum=999999, value=42, step=1, label="Seed")
149
+ seedvr_resolution = gr.Slider(minimum=720, maximum=1440, value=1072, step=8, label="Resolução Vertical (Altura)")
150
  seedvr_batch_size = gr.Slider(minimum=1, maximum=16, value=4, step=1, label="Batch Size por GPU")
151
  seedvr_fps_output = gr.Number(label="FPS de Saída (0 = original)", value=0)
152
  run_seedvr_button = gr.Button("Iniciar Upscaling SeedVR", variant="primary", interactive=(seedvr_inference_server is not None))
153
+ if not seedvr_inference_server:
154
+ gr.Markdown("<p style='color: red;'>Serviço SeedVR não disponível.</p>")
155
  with gr.Column(scale=1):
156
+ gr.Markdown("### Resultado do Upscaling")
157
  seedvr_video_output = gr.Video(label="Vídeo com Upscale SeedVR", interactive=False)
158
+ seedvr_status_box = gr.Textbox(label="Status do Processamento", value="Aguardando...", lines=3, interactive=False)
159
 
160
+ # --- ABA MM-AUDIO ---
161
+ with gr.TabItem("🔊 Áudio (MM-Audio)"):
162
+ gr.Markdown("*(Funcionalidade futura para adicionar som aos vídeos)*")
 
 
163
 
164
+ # --- LÓGICA DE EVENTOS DA UI ---
165
+
166
+ # Botão da Etapa 1
 
167
  generate_low_btn.click(
168
+ fn=run_generate_low,
169
+ inputs=[prompt_input, neg_prompt_input, start_image, height_input, width_input, duration_input, cfg_input, seed_input, randomize_seed],
 
 
 
 
170
  outputs=[low_res_video_output, app_state, post_prod_group]
171
  )
172
 
173
+ # Botão da Aba LTX Refinement
174
  ltx_refine_btn.click(
175
  fn=run_ltx_refinement,
176
  inputs=[app_state, prompt_input, neg_prompt_input, cfg_input],
177
  outputs=[ltx_refined_video_output, app_state]
178
  )
179
 
180
+ # Botão da Aba SeedVR
181
  run_seedvr_button.click(
182
  fn=run_seedvr_upscaling,
183
  inputs=[app_state, seedvr_seed, seedvr_resolution, seedvr_batch_size, seedvr_fps_output],
 
185
  )
186
 
187
  if __name__ == "__main__":
188
+ demo.queue().launch(server_name="0.0.0.0", server_port=7860, debug=True, show_error=True)