Eueuiaa commited on
Commit
ac04fcc
·
verified ·
1 Parent(s): 7720807

Update api/ltx_server_refactored.py

Browse files
Files changed (1) hide show
  1. api/ltx_server_refactored.py +41 -25
api/ltx_server_refactored.py CHANGED
@@ -228,7 +228,7 @@ class VideoService:
228
  conditioning_items.append(ConditioningItem(tensor, safe_frame, float(weight)))
229
  return conditioning_items
230
 
231
- def generate_low_old(self, prompt, negative_prompt, height, width, duration, guidance_scale, seed, conditioning_items=None):
232
  used_seed = random.randint(0, 2**32 - 1) if seed is None else int(seed)
233
  seed_everething(used_seed)
234
  FPS = 24.0
@@ -267,13 +267,17 @@ class VideoService:
267
  torch.cuda.ipc_collect()
268
  self.finalize(keep_paths=[])
269
 
 
 
 
270
  def _generate_single_chunk_low(self, prompt, negative_prompt, height, width, num_frames, guidance_scale, seed, initial_latent_condition=None, image_conditions=None, ltx_configs_override=None):
271
  """
272
  [NÓ DE GERAÇÃO]
273
  Gera um ÚNICO chunk de latentes brutos. Esta é a unidade de trabalho fundamental.
274
  """
275
- # (Esta função auxiliar permanece a mesma da nossa última versão, com a lógica de override)
276
  print("\n" + "-"*20 + " INÍCIO: _generate_single_chunk_low " + "-"*20)
 
 
277
  height_padded = ((height - 1) // 8 + 1) * 8
278
  width_padded = ((width - 1) // 8 + 1) * 8
279
  generator = torch.Generator(device=self.device).manual_seed(seed)
@@ -286,6 +290,7 @@ class VideoService:
286
  x_height = int(height_padded * downscale_factor)
287
  downscaled_height = x_height - (x_height % vae_scale_factor)
288
 
 
289
  all_conditions = []
290
  if image_conditions: all_conditions.extend(image_conditions)
291
  if initial_latent_condition: all_conditions.append(initial_latent_condition)
@@ -294,12 +299,23 @@ class VideoService:
294
 
295
  if ltx_configs_override:
296
  print("[DEBUG] Sobrepondo configurações do LTX com valores da UI...")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
297
  if "first_pass_num_inference_steps" in ltx_configs_override:
298
  first_pass_config["num_inference_steps"] = ltx_configs_override["first_pass_num_inference_steps"]
299
- #if "first_pass_guidance_scale" in ltx_configs_override:
300
- # max_val = max(first_pass_config.get("guidance_scale", [1]))
301
- # new_max_val = ltx_configs_override["first_pass_guidance_scale"]
302
- # first_pass_config["guidance_scale"] = [new_max_val if x==max_val else x for x in first_pass_config["guidance_scale"]]
303
 
304
  first_pass_kwargs = {
305
  "prompt": prompt, "negative_prompt": negative_prompt, "height": downscaled_height, "width": downscaled_width,
@@ -307,10 +323,8 @@ class VideoService:
307
  "conditioning_items": all_conditions if all_conditions else None,
308
  **first_pass_config
309
  }
310
- # Removido guidance_scale daqui pois agora está dentro do first_pass_config
311
- #if "guidance_scale" in first_pass_kwargs:
312
- # del first_pass_kwargs['guidance_scale']
313
-
314
  with torch.autocast(device_type="cuda", dtype=self.runtime_autocast_dtype, enabled=self.device.type == 'cuda'):
315
  latents_bruto = self.pipeline(**first_pass_kwargs).images
316
  log_tensor_info(latents_bruto, f"Latente Bruto Gerado para: '{prompt[:40]}...'")
@@ -318,6 +332,9 @@ class VideoService:
318
  print("-" * 20 + " FIM: _generate_single_chunk_low " + "-"*20)
319
  return latents_bruto
320
 
 
 
 
321
  def generate_narrative_low(self, prompt: str, negative_prompt, height, width, duration, guidance_scale, seed, initial_image_conditions=None, overlap_frames: int = 8, ltx_configs_override: dict = None):
322
  """
323
  [ORQUESTRADOR NARRATIVO]
@@ -377,9 +394,7 @@ class VideoService:
377
 
378
  latentes_bruto = self._generate_single_chunk_low(
379
  prompt=chunk_prompt, negative_prompt=negative_prompt, height=height, width=width,
380
- num_frames=num_frames_para_gerar,
381
- #guidance_scale=guidance_scale,
382
- seed=used_seed + i,
383
  initial_latent_condition=condition_item_latent_overlap, image_conditions=current_image_conditions,
384
  ltx_configs_override=ltx_configs_override
385
  )
@@ -404,12 +419,15 @@ class VideoService:
404
  torch.save(final_latents.cpu(), tensor_path)
405
 
406
  with torch.autocast(device_type="cuda", dtype=self.runtime_autocast_dtype, enabled=self.device.type == 'cuda'):
407
- pixel_tensor = vae_manager_singleton.decode(final_latents, decode_timestep=float(self.config.get("decode_timestep", 0.05)))
408
  video_path = self._save_and_log_video(pixel_tensor, "narrative_video", FPS, temp_dir, results_dir, used_seed)
409
 
410
  self.finalize(keep_paths=[video_path, tensor_path])
411
  return video_path, tensor_path, used_seed
412
-
 
 
 
413
  def generate_single_low(self, prompt: str, negative_prompt, height, width, duration, guidance_scale, seed, initial_image_conditions=None, ltx_configs_override: dict = None):
414
  """
415
  [ORQUESTRADOR SIMPLES]
@@ -430,14 +448,9 @@ class VideoService:
430
 
431
  # Chama a função de geração de chunk único para fazer todo o trabalho
432
  final_latents = self._generate_single_chunk_low(
433
- prompt=prompt,
434
- negative_prompt=negative_prompt,
435
- height=height, width=width,
436
- num_frames=total_actual_frames,
437
- #guidance_scale=guidance_scale,
438
- seed=used_seed,
439
- image_conditions=initial_image_conditions,
440
- ltx_configs_override=ltx_configs_override
441
  )
442
 
443
  print("\n--- Finalizando Geração Simples: Salvando e decodificando ---")
@@ -447,12 +460,15 @@ class VideoService:
447
  torch.save(final_latents.cpu(), tensor_path)
448
 
449
  with torch.autocast(device_type="cuda", dtype=self.runtime_autocast_dtype, enabled=self.device.type == 'cuda'):
450
- pixel_tensor = vae_manager_singleton.decode(final_latents, decode_timestep=float(self.config.get("decode_timestep", 0.05)))
451
  video_path = self._save_and_log_video(pixel_tensor, "single_video", FPS, temp_dir, results_dir, used_seed)
452
 
453
  self.finalize(keep_paths=[video_path, tensor_path])
454
  return video_path, tensor_path, used_seed
455
-
 
 
 
456
  def generate_upscale_denoise(self, latents_path, prompt, negative_prompt, guidance_scale, seed):
457
  used_seed = random.randint(0, 2**32 - 1) if seed is None else int(seed)
458
  seed_everething(used_seed)
 
228
  conditioning_items.append(ConditioningItem(tensor, safe_frame, float(weight)))
229
  return conditioning_items
230
 
231
+ def generate_low(self, prompt, negative_prompt, height, width, duration, guidance_scale, seed, conditioning_items=None):
232
  used_seed = random.randint(0, 2**32 - 1) if seed is None else int(seed)
233
  seed_everething(used_seed)
234
  FPS = 24.0
 
267
  torch.cuda.ipc_collect()
268
  self.finalize(keep_paths=[])
269
 
270
+ # ==============================================================================
271
+ # --- FUNÇÃO #1: GERADOR DE CHUNK ÚNICO (AUXILIAR INTERNA) ---
272
+ # ==============================================================================
273
  def _generate_single_chunk_low(self, prompt, negative_prompt, height, width, num_frames, guidance_scale, seed, initial_latent_condition=None, image_conditions=None, ltx_configs_override=None):
274
  """
275
  [NÓ DE GERAÇÃO]
276
  Gera um ÚNICO chunk de latentes brutos. Esta é a unidade de trabalho fundamental.
277
  """
 
278
  print("\n" + "-"*20 + " INÍCIO: _generate_single_chunk_low " + "-"*20)
279
+
280
+ # --- NÓ 1.1: SETUP DE PARÂMETROS ---
281
  height_padded = ((height - 1) // 8 + 1) * 8
282
  width_padded = ((width - 1) // 8 + 1) * 8
283
  generator = torch.Generator(device=self.device).manual_seed(seed)
 
290
  x_height = int(height_padded * downscale_factor)
291
  downscaled_height = x_height - (x_height % vae_scale_factor)
292
 
293
+ # --- NÓ 1.2: MONTAGEM DE CONDIÇÕES E OVERRIDES ---
294
  all_conditions = []
295
  if image_conditions: all_conditions.extend(image_conditions)
296
  if initial_latent_condition: all_conditions.append(initial_latent_condition)
 
299
 
300
  if ltx_configs_override:
301
  print("[DEBUG] Sobrepondo configurações do LTX com valores da UI...")
302
+ preset = ltx_configs_override.get("guidance_preset")
303
+ if preset == "Customizado":
304
+ try:
305
+ first_pass_config["guidance_scale"] = json.loads(ltx_configs_override["guidance_scale_list"])
306
+ first_pass_config["stg_scale"] = json.loads(ltx_configs_override["stg_scale_list"])
307
+ first_pass_config["guidance_timesteps"] = json.loads(ltx_configs_override["timesteps_list"])
308
+ except Exception as e:
309
+ print(f" > ERRO ao parsear valores customizados: {e}. Usando Padrão como fallback.")
310
+ elif preset == "Agressivo":
311
+ first_pass_config["guidance_scale"] = [1, 2, 8, 12, 8, 2, 1]
312
+ first_pass_config["stg_scale"] = [0, 0, 5, 6, 5, 3, 2]
313
+ elif preset == "Suave":
314
+ first_pass_config["guidance_scale"] = [1, 1, 4, 5, 4, 1, 1]
315
+ first_pass_config["stg_scale"] = [0, 0, 2, 2, 2, 1, 0]
316
+
317
  if "first_pass_num_inference_steps" in ltx_configs_override:
318
  first_pass_config["num_inference_steps"] = ltx_configs_override["first_pass_num_inference_steps"]
 
 
 
 
319
 
320
  first_pass_kwargs = {
321
  "prompt": prompt, "negative_prompt": negative_prompt, "height": downscaled_height, "width": downscaled_width,
 
323
  "conditioning_items": all_conditions if all_conditions else None,
324
  **first_pass_config
325
  }
326
+
327
+ # --- 1.3: CHAMADA AO PIPELINE ---
 
 
328
  with torch.autocast(device_type="cuda", dtype=self.runtime_autocast_dtype, enabled=self.device.type == 'cuda'):
329
  latents_bruto = self.pipeline(**first_pass_kwargs).images
330
  log_tensor_info(latents_bruto, f"Latente Bruto Gerado para: '{prompt[:40]}...'")
 
332
  print("-" * 20 + " FIM: _generate_single_chunk_low " + "-"*20)
333
  return latents_bruto
334
 
335
+ # ==============================================================================
336
+ # --- FUNÇÃO #2: ORQUESTRADOR NARRATIVO (MÚLTIPLOS PROMPTS) ---
337
+ # ==============================================================================
338
  def generate_narrative_low(self, prompt: str, negative_prompt, height, width, duration, guidance_scale, seed, initial_image_conditions=None, overlap_frames: int = 8, ltx_configs_override: dict = None):
339
  """
340
  [ORQUESTRADOR NARRATIVO]
 
394
 
395
  latentes_bruto = self._generate_single_chunk_low(
396
  prompt=chunk_prompt, negative_prompt=negative_prompt, height=height, width=width,
397
+ num_frames=num_frames_para_gerar, guidance_scale=guidance_scale, seed=used_seed + i,
 
 
398
  initial_latent_condition=condition_item_latent_overlap, image_conditions=current_image_conditions,
399
  ltx_configs_override=ltx_configs_override
400
  )
 
419
  torch.save(final_latents.cpu(), tensor_path)
420
 
421
  with torch.autocast(device_type="cuda", dtype=self.runtime_autocast_dtype, enabled=self.device.type == 'cuda'):
422
+ pixel_tensor = vae_manager_singleton.decode(final_latents, decode_timestep=float(ltx_configs_override.get("decode_timestep", 0.05) if ltx_configs_override else 0.05))
423
  video_path = self._save_and_log_video(pixel_tensor, "narrative_video", FPS, temp_dir, results_dir, used_seed)
424
 
425
  self.finalize(keep_paths=[video_path, tensor_path])
426
  return video_path, tensor_path, used_seed
427
+
428
+ # ==============================================================================
429
+ # --- FUNÇÃO #3: ORQUESTRADOR SIMPLES (PROMPT ÚNICO) ---
430
+ # ==============================================================================
431
  def generate_single_low(self, prompt: str, negative_prompt, height, width, duration, guidance_scale, seed, initial_image_conditions=None, ltx_configs_override: dict = None):
432
  """
433
  [ORQUESTRADOR SIMPLES]
 
448
 
449
  # Chama a função de geração de chunk único para fazer todo o trabalho
450
  final_latents = self._generate_single_chunk_low(
451
+ prompt=prompt, negative_prompt=negative_prompt, height=height, width=width,
452
+ num_frames=total_actual_frames, guidance_scale=guidance_scale, seed=used_seed,
453
+ image_conditions=initial_image_conditions, ltx_configs_override=ltx_configs_override
 
 
 
 
 
454
  )
455
 
456
  print("\n--- Finalizando Geração Simples: Salvando e decodificando ---")
 
460
  torch.save(final_latents.cpu(), tensor_path)
461
 
462
  with torch.autocast(device_type="cuda", dtype=self.runtime_autocast_dtype, enabled=self.device.type == 'cuda'):
463
+ pixel_tensor = vae_manager_singleton.decode(final_latents, decode_timestep=float(ltx_configs_override.get("decode_timestep", 0.05) if ltx_configs_override else 0.05))
464
  video_path = self._save_and_log_video(pixel_tensor, "single_video", FPS, temp_dir, results_dir, used_seed)
465
 
466
  self.finalize(keep_paths=[video_path, tensor_path])
467
  return video_path, tensor_path, used_seed
468
+
469
+ # ==============================================================================
470
+ # --- FUNÇÃO #4: ORQUESTRADOR (Upscaler + texturas hd) ---
471
+ # ==============================================================================
472
  def generate_upscale_denoise(self, latents_path, prompt, negative_prompt, guidance_scale, seed):
473
  used_seed = random.randint(0, 2**32 - 1) if seed is None else int(seed)
474
  seed_everething(used_seed)