euIaxs22 commited on
Commit
8467ee4
·
verified ·
1 Parent(s): 9f03507

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +488 -197
app.py CHANGED
@@ -1,230 +1,521 @@
1
- # app.py
2
- #
3
- # Versão 18.1.0 (Interface Alinhada com a Arquitetura V2)
4
- #
5
- # - A chamada de evento da UI agora aciona o novo pipeline do Composer2D.
6
- # - A função de processamento de stream (`process_chat_stream`) foi corrigida
7
- # para ser compatível com o objeto `GenerationState`, resolvendo o AttributeError.
8
- # - A extração de dados para a galeria e o vídeo foi atualizada para ler a
9
- # nova estrutura do DNA (`storyboard_producao`).
10
-
11
  import gradio as gr
12
- import yaml
13
- import logging
 
 
14
  import os
15
- import sys
16
- import time
 
 
17
  from PIL import Image
 
 
18
 
19
- # --- 1. CONFIGURAÇÃO DE LOGGING E TEMAS ---
20
- logging.basicConfig(level=logging.INFO, format='%(message)s')
21
- logger = logging.getLogger(__name__)
22
- cinematic_theme = gr.themes.Base(primary_hue=gr.themes.colors.purple).set(body_background_fill="#111827", body_text_color="#D1D5DB", button_primary_background_fill="linear-gradient(90deg, #6D28D9, #4F46E5)", block_background_fill="#1F2937", block_border_width="1px", block_border_color="#374151")
 
 
 
 
 
 
 
23
 
24
- # --- 2. INICIALIZAÇÃO CRÍTICA DO FRAMEWORK ---
25
- try:
26
- import aduc_framework
27
- # Importa os tipos necessários para a UI
28
- from aduc_framework.types import PreProductionParams, ProductionParams
29
 
30
- with open("config.yaml", 'r') as f:
31
- config = yaml.safe_load(f)
32
- WORKSPACE_DIR = config['application']['workspace_dir']
33
-
34
- # Cria a instância global do ADUC
35
- aduc = aduc_framework.create_aduc_instance(workspace_root=WORKSPACE_DIR)
36
-
37
- logger.info("Framework ADUC e interface Gradio inicializados com sucesso.")
38
-
39
- except Exception as e:
40
- logger.critical(f"ERRO CRÍTICO NA INICIALIZAÇÃO DO ADUC FRAMEWORK: {e}", exc_info=True)
41
- with gr.Blocks(theme=cinematic_theme) as demo_error:
42
- gr.Markdown("# ERRO CRÍTICO NA INICIALIZAÇÃO")
43
- gr.Markdown("Não foi possível iniciar o Aduc Framework. A aplicação não pode continuar. Verifique os logs.")
44
- gr.Textbox(value=str(e), label="Detalhes do Erro", lines=10)
45
- demo_error.launch()
46
- sys.exit(1)
47
-
48
- # --- 3. FUNÇÕES WRAPPER E LÓGICA DA UI ---
49
-
50
- def chat_beautifier(role):
51
- # (Função auxiliar para deixar os nomes dos especialistas bonitos no chat)
52
- if "Composer2D" in role: return "Arquiteto Narrativo"
53
- if "Neura_Link" in role: return "Interface Neural"
54
- if "Planner5D" in role: return "Diretor de Set"
55
- return "Sistema"
56
-
57
- def process_chat_stream(generator, initial_chat_history=[]):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
  """
59
- Processa um gerador que produz objetos GenerationState e formata
60
- o histórico de chat e outros dados para a UI.
 
61
  """
62
- chatbot_display_history = initial_chat_history.copy()
63
- fully_displayed_message_count = len(initial_chat_history)
64
-
65
- for dna_state in generator: # O gerador agora produz objetos 'GenerationState'
66
- # --- CORREÇÃO DE `AttributeError` ---
67
- # Acessamos os atributos do objeto Pydantic diretamente
68
- backend_chat_history = dna_state.chat_history
 
69
 
70
- gallery_images = []
71
- if dna_state.storyboard_producao: # Lendo da nova estrutura V2
72
- for scene in dna_state.storyboard_producao:
73
- gallery_images.extend([kf.caminho_pixel for kf in scene.keyframes])
74
- elif dna_state.scenes: # Fallback para a estrutura legada
75
- for scene in dna_state.scenes:
76
- gallery_images.extend([kf.caminho_pixel for kf in scene.keyframes])
77
-
78
- final_video_path = dna_state.caminho_filme_final_bruto or dna_state.caminho_filme_final_masterizado
79
- dna_json = dna_state.model_dump() # Converte o estado atual para JSON para exibição
80
- # ------------------------------------
81
-
82
- # Lógica para "digitar" a mensagem
83
- while len(backend_chat_history) > fully_displayed_message_count:
84
- new_message_obj = backend_chat_history[fully_displayed_message_count]
85
- role = chat_beautifier(new_message_obj.get('role', 'Sistema'))
86
- content_to_type = new_message_obj.get('content', '')
87
- is_ai_message = "Sistema" not in role
88
- chatbot_display_history.append({"role": "assistant" if is_ai_message else "user", "content": ""})
89
- full_typed_message = ""
90
- for char in f"**{role}:** {content_to_type}":
91
- full_typed_message += char
92
- chatbot_display_history[-1]["content"] = full_typed_message + "▌"
93
- yield chatbot_display_history, gr.update(value=gallery_images), gr.update(value=dna_json), gr.update(value=final_video_path)
94
- time.sleep(0.005)
95
- chatbot_display_history[-1]["content"] = full_typed_message
96
- fully_displayed_message_count += 1
97
 
98
- # Garante que a UI seja atualizada mesmo que não haja novas mensagens de chat
99
- yield chatbot_display_history, gr.update(value=gallery_images), gr.update(value=dna_json), gr.update(value=final_video_path)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
100
 
101
- def run_story_and_keyframes_wrapper(project_name, prompt, num_scenes, ref_files, duration_per_fragment):
 
 
 
 
 
102
  """
103
- Este wrapper inicia o pipeline de PRÉ-PRODUÇÃO V2.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
104
  """
105
- if not project_name or not project_name.strip():
106
- raise gr.Error("Por favor, forneça um nome para o projeto.")
107
- aduc.load_project(project_name.strip())
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
 
109
- if not ref_files: raise gr.Error("Por favor, forneça pelo menos uma imagem de referência.")
110
- ref_paths = [aduc.process_image_for_story(f.name, f"ref_{i}.jpg") for i, f in enumerate(ref_files)]
 
 
111
 
112
- params = PreProductionParams(
113
- prompt=prompt,
114
- num_scenes=int(num_scenes),
115
- ref_paths=ref_paths,
116
- duration_per_fragment=duration_per_fragment
117
- )
118
 
119
- # Chama a função V2 do AducSdr, que espera `params` e retorna um gerador de `GenerationState`
120
- generator = aduc.task_run_story_and_keyframes(params)
 
 
 
 
 
 
121
 
122
- final_state_json = {}
123
- for update in process_chat_stream(generator):
124
- yield update
125
- if update[2] is not gr.skip():
126
- final_state_json = update[2] # O terceiro item é o DNA em formato JSON
127
-
128
- return final_state_json
129
 
130
- def run_production_wrapper(project_name, state_dict, trim, handler, dest, guidance, stg, steps):
131
- """
132
- Este wrapper inicia o pipeline de PRODUÇÃO V2.
133
- """
134
- if not project_name or not project_name.strip():
135
- raise gr.Error("O nome do projeto parece ter sido perdido.")
136
- aduc.load_project(project_name.strip())
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
137
 
138
- params = ProductionParams(trim_percent=int(trim), handler_strength=handler, destination_convergence_strength=dest, guidance_scale=guidance, stg_scale=stg, inference_steps=int(steps))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
139
 
140
- # A nova função de produção do AducSdr pode precisar ser ajustada para receber os params
141
- # ou lê-los diretamente do DNA que já foi atualizado na pré-produção.
142
- # Por enquanto, mantemos a chamada.
143
- prod_generator = aduc.task_produce_movie(params)
 
144
 
145
- # O `state_dict` já é um dicionário, então podemos extrair o histórico de chat dele
146
- initial_chat = state_dict.get("chat_history", [])
 
 
 
 
147
 
148
- final_state_json = {}
149
- for update in process_chat_stream(prod_generator, initial_chat_history=initial_chat):
150
- yield update
151
- if update[2] is not gr.skip():
152
- final_state_json = update[2]
 
 
 
 
 
 
 
 
 
 
153
 
154
- return final_state_json
155
 
156
- # --- 4. DEFINIÇÃO DA UI ---
157
- with gr.Blocks(theme=cinematic_theme, css="style.css") as demo:
158
- generation_state_holder = gr.State({})
159
- gr.Markdown("<h1>ADUC-SDR 🎬 - O Diretor de Cinema IA</h1>")
160
-
161
- with gr.Accordion("Configurações do Projeto", open=True):
162
- project_name_input = gr.Textbox(label="Nome do Projeto", value="Meu_Filme_01", info="O progresso será salvo em uma pasta com este nome.")
 
 
 
 
 
 
 
 
 
163
 
 
 
 
 
164
  with gr.Row():
165
- with gr.Column(scale=2):
166
- with gr.Accordion("Etapa 1: Pré-Produção (Roteiro e Storyboard)", open=True):
167
- prompt_input = gr.Textbox(label="Ideia Geral do Filme", value="Um robô solitário explora as ruínas de uma cidade coberta pela natureza.")
168
- ref_image_input = gr.File(label="Imagens de Referência", file_count="multiple", file_types=["image"])
169
- with gr.Row():
170
- num_scenes_slider = gr.Slider(minimum=1, maximum=10, value=3, step=1, label="Número de Cenas")
171
- duration_per_fragment_slider = gr.Slider(label="Duração de cada Ato (s)", minimum=2.0, maximum=10.0, value=5.0, step=0.1)
172
- start_pre_prod_button = gr.Button("1. Gerar Roteiro e Storyboard", variant="primary")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
173
 
174
- with gr.Accordion("Etapa 2: Produção do Vídeo", open=False, visible=False) as step2_accordion:
175
- trim_percent_slider = gr.Slider(minimum=10, maximum=90, value=50, step=5, label="Poda Causal (%)")
176
- handler_strength_slider = gr.Slider(label="Força do Déjà-Vu", minimum=0.0, maximum=1.0, value=0.5, step=0.05)
177
- dest_strength_slider = gr.Slider(label="Força da Âncora Final", minimum=0.0, maximum=1.0, value=0.75, step=0.05)
178
- guidance_scale_slider = gr.Slider(minimum=1.0, maximum=10.0, value=2.0, step=0.1, label="Escala de Orientação")
179
- stg_scale_slider = gr.Slider(minimum=0.0, maximum=1.0, value=0.025, step=0.005, label="Escala STG")
180
- inference_steps_slider = gr.Slider(minimum=10, maximum=50, value=20, step=1, label="Passos de Inferência")
181
- produce_original_button = gr.Button("2. Produzir Vídeo", variant="primary", interactive=False)
182
-
183
- with gr.Column(scale=3):
184
- final_video_output = gr.Video(label="Último Clipe Gerado / Filme Final", interactive=False)
185
- chat_history_chatbot = gr.Chatbot(label="Diário da Produção", height=600, type='messages')
186
- keyframe_gallery = gr.Gallery(label="Keyframes Gerados", object_fit="contain", height="auto")
187
 
188
- with gr.Accordion("🧬 DNA Digital (Estado do Projeto)", open=False):
189
- dna_display = gr.JSON()
190
-
191
- # --- 5. CONEXÕES DE EVENTOS ---
192
- start_pre_prod_button.click(
193
- fn=lambda: gr.update(interactive=False),
194
- outputs=[start_pre_prod_button]
195
- ).then(
196
- fn=run_story_and_keyframes_wrapper,
197
- inputs=[project_name_input, prompt_input, num_scenes_slider, ref_image_input, duration_per_fragment_slider],
198
- outputs=[chat_history_chatbot, keyframe_gallery, dna_display, final_video_output]
199
- ).then(
200
- fn=lambda data: (data, gr.update(visible=True, open=True), gr.update(interactive=True)),
201
- inputs=dna_display,
202
- outputs=[generation_state_holder, step2_accordion, produce_original_button]
203
  )
204
-
205
- produce_original_button.click(
206
- fn=lambda: gr.update(interactive=False),
207
- outputs=[produce_original_button]
208
- ).then(
209
- fn=run_production_wrapper,
210
- inputs=[
211
- project_name_input,
212
- generation_state_holder,
213
- trim_percent_slider,
214
- handler_strength_slider,
215
- dest_strength_slider,
216
- guidance_scale_slider,
217
- stg_scale_slider,
218
- inference_steps_slider
219
- ],
220
- outputs=[chat_history_chatbot, keyframe_gallery, dna_display, final_video_output]
221
  )
222
 
 
 
 
 
 
 
 
 
 
 
 
 
 
223
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
224
  if __name__ == "__main__":
225
- os.makedirs(WORKSPACE_DIR, exist_ok=True)
226
- logger.info("Aplicação Gradio pronta. Lançando interface...")
227
- demo.launch(
228
- server_name="0.0.0.0",
229
- server_port=int(os.getenv("PORT", "7860")),
230
- )
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import torch
3
+
4
+ import numpy as np
5
+ import random
6
  import os
7
+ import yaml
8
+ from pathlib import Path
9
+ import imageio
10
+ import tempfile
11
  from PIL import Image
12
+ from huggingface_hub import hf_hub_download
13
+ import shutil
14
 
15
+ from inference import (
16
+ create_ltx_video_pipeline,
17
+ create_latent_upsampler,
18
+ load_image_to_tensor_with_resize_and_crop,
19
+ seed_everething,
20
+ get_device,
21
+ calculate_padding,
22
+ load_media_file
23
+ )
24
+ from ltx_video.pipelines.pipeline_ltx_video import ConditioningItem, LTXMultiScalePipeline, LTXVideoPipeline
25
+ from ltx_video.utils.skip_layer_strategy import SkipLayerStrategy
26
 
27
+ config_file_path = "configs/ltxv-13b-0.9.8-dev-fp8.yaml"
28
+ with open(config_file_path, "r") as file:
29
+ PIPELINE_CONFIG_YAML = yaml.safe_load(file)
 
 
30
 
31
+ LTX_REPO = "Lightricks/LTX-Video"
32
+ MAX_IMAGE_SIZE = PIPELINE_CONFIG_YAML.get("max_resolution", 1280)
33
+ MAX_NUM_FRAMES = 257
34
+
35
+ FPS = 30.0
36
+
37
+ # --- Global variables for loaded models ---
38
+ pipeline_instance = None
39
+ latent_upsampler_instance = None
40
+ models_dir = "downloaded_models_gradio_cpu_init"
41
+ Path(models_dir).mkdir(parents=True, exist_ok=True)
42
+
43
+ print("Downloading models (if not present)...")
44
+ distilled_model_actual_path = hf_hub_download(
45
+ repo_id=LTX_REPO,
46
+ filename=PIPELINE_CONFIG_YAML["checkpoint_path"],
47
+ local_dir=models_dir,
48
+ local_dir_use_symlinks=False
49
+ )
50
+ PIPELINE_CONFIG_YAML["checkpoint_path"] = distilled_model_actual_path
51
+ print(f"Distilled model path: {distilled_model_actual_path}")
52
+
53
+ SPATIAL_UPSCALER_FILENAME = PIPELINE_CONFIG_YAML["spatial_upscaler_model_path"]
54
+ spatial_upscaler_actual_path = hf_hub_download(
55
+ repo_id=LTX_REPO,
56
+ filename=SPATIAL_UPSCALER_FILENAME,
57
+ local_dir=models_dir,
58
+ local_dir_use_symlinks=False
59
+ )
60
+ PIPELINE_CONFIG_YAML["spatial_upscaler_model_path"] = spatial_upscaler_actual_path
61
+ print(f"Spatial upscaler model path: {spatial_upscaler_actual_path}")
62
+
63
+ print("Creating LTX Video pipeline on CPU...")
64
+ pipeline_instance = create_ltx_video_pipeline(
65
+ ckpt_path=PIPELINE_CONFIG_YAML["checkpoint_path"],
66
+ precision=PIPELINE_CONFIG_YAML["precision"],
67
+ text_encoder_model_name_or_path=PIPELINE_CONFIG_YAML["text_encoder_model_name_or_path"],
68
+ sampler=PIPELINE_CONFIG_YAML["sampler"],
69
+ device="cpu",
70
+ enhance_prompt=False,
71
+ prompt_enhancer_image_caption_model_name_or_path=PIPELINE_CONFIG_YAML["prompt_enhancer_image_caption_model_name_or_path"],
72
+ prompt_enhancer_llm_model_name_or_path=PIPELINE_CONFIG_YAML["prompt_enhancer_llm_model_name_or_path"],
73
+ )
74
+ print("LTX Video pipeline created on CPU.")
75
+
76
+ if PIPELINE_CONFIG_YAML.get("spatial_upscaler_model_path"):
77
+ print("Creating latent upsampler on CPU...")
78
+ latent_upsampler_instance = create_latent_upsampler(
79
+ PIPELINE_CONFIG_YAML["spatial_upscaler_model_path"],
80
+ device="cpu"
81
+ )
82
+ print("Latent upsampler created on CPU.")
83
+
84
+ target_inference_device = "cuda"
85
+ print(f"Target inference device: {target_inference_device}")
86
+ pipeline_instance.to(target_inference_device)
87
+ if latent_upsampler_instance:
88
+ latent_upsampler_instance.to(target_inference_device)
89
+
90
+
91
+ # --- Helper function for dimension calculation ---
92
+ MIN_DIM_SLIDER = 256 # As defined in the sliders minimum attribute
93
+ TARGET_FIXED_SIDE = 768 # Desired fixed side length as per requirement
94
+
95
+ def calculate_new_dimensions(orig_w, orig_h):
96
  """
97
+ Calculates new dimensions for height and width sliders based on original media dimensions.
98
+ Ensures one side is TARGET_FIXED_SIDE, the other is scaled proportionally,
99
+ both are multiples of 32, and within [MIN_DIM_SLIDER, MAX_IMAGE_SIZE].
100
  """
101
+ if orig_w == 0 or orig_h == 0:
102
+ # Default to TARGET_FIXED_SIDE square if original dimensions are invalid
103
+ return int(TARGET_FIXED_SIDE), int(TARGET_FIXED_SIDE)
104
+
105
+ if orig_w >= orig_h: # Landscape or square
106
+ new_h = TARGET_FIXED_SIDE
107
+ aspect_ratio = orig_w / orig_h
108
+ new_w_ideal = new_h * aspect_ratio
109
 
110
+ # Round to nearest multiple of 32
111
+ new_w = round(new_w_ideal / 32) * 32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
112
 
113
+ # Clamp to [MIN_DIM_SLIDER, MAX_IMAGE_SIZE]
114
+ new_w = max(MIN_DIM_SLIDER, min(new_w, MAX_IMAGE_SIZE))
115
+ # Ensure new_h is also clamped (TARGET_FIXED_SIDE should be within these bounds if configured correctly)
116
+ new_h = max(MIN_DIM_SLIDER, min(new_h, MAX_IMAGE_SIZE))
117
+ else: # Portrait
118
+ new_w = TARGET_FIXED_SIDE
119
+ aspect_ratio = orig_h / orig_w # Use H/W ratio for portrait scaling
120
+ new_h_ideal = new_w * aspect_ratio
121
+
122
+ # Round to nearest multiple of 32
123
+ new_h = round(new_h_ideal / 32) * 32
124
+
125
+ # Clamp to [MIN_DIM_SLIDER, MAX_IMAGE_SIZE]
126
+ new_h = max(MIN_DIM_SLIDER, min(new_h, MAX_IMAGE_SIZE))
127
+ # Ensure new_w is also clamped
128
+ new_w = max(MIN_DIM_SLIDER, min(new_w, MAX_IMAGE_SIZE))
129
+
130
+ return int(new_h), int(new_w)
131
+
132
+ def get_duration(prompt, negative_prompt, input_image_filepath, input_video_filepath,
133
+ height_ui, width_ui, mode,
134
+ duration_ui, # Removed ui_steps
135
+ ui_frames_to_use,
136
+ seed_ui, randomize_seed, ui_guidance_scale, improve_texture_flag,
137
+ progress):
138
+ if duration_ui > 7:
139
+ return 75
140
+ else:
141
+ return 60
142
+
143
 
144
+ def generate(prompt, negative_prompt, input_image_filepath=None, input_video_filepath=None,
145
+ height_ui=512, width_ui=704, mode="text-to-video",
146
+ duration_ui=2.0,
147
+ ui_frames_to_use=9,
148
+ seed_ui=42, randomize_seed=True, ui_guidance_scale=3.0, improve_texture_flag=True,
149
+ progress=gr.Progress(track_tqdm=True)):
150
  """
151
+ Generate high-quality videos using LTX Video model with support for text-to-video, image-to-video, and video-to-video modes.
152
+
153
+ Args:
154
+ prompt (str): Text description of the desired video content. Required for all modes.
155
+ negative_prompt (str): Text describing what to avoid in the generated video. Optional, can be empty string.
156
+ input_image_filepath (str or None): Path to input image file. Required for image-to-video mode, None for other modes.
157
+ input_video_filepath (str or None): Path to input video file. Required for video-to-video mode, None for other modes.
158
+ height_ui (int): Height of the output video in pixels, must be divisible by 32. Default: 512.
159
+ width_ui (int): Width of the output video in pixels, must be divisible by 32. Default: 704.
160
+ mode (str): Generation mode. Required. One of "text-to-video", "image-to-video", or "video-to-video". Default: "text-to-video".
161
+ duration_ui (float): Duration of the output video in seconds. Range: 0.3 to 8.5. Default: 2.0.
162
+ ui_frames_to_use (int): Number of frames to use from input video. Only used in video-to-video mode. Must be N*8+1. Default: 9.
163
+ seed_ui (int): Random seed for reproducible generation. Range: 0 to 2^32-1. Default: 42.
164
+ randomize_seed (bool): Whether to use a random seed instead of seed_ui. Default: True.
165
+ ui_guidance_scale (float): CFG scale controlling prompt influence. Range: 1.0 to 10.0. Higher values = stronger prompt influence. Default: 3.0.
166
+ improve_texture_flag (bool): Whether to use multi-scale generation for better texture quality. Slower but higher quality. Default: True.
167
+ progress (gr.Progress): Progress tracker for the generation process. Optional, used for UI updates.
168
+
169
+ Returns:
170
+ tuple: A tuple containing (output_video_path, used_seed) where output_video_path is the path to the generated video file and used_seed is the actual seed used for generation.
171
  """
172
+
173
+ # Validate mode-specific required parameters
174
+ if mode == "image-to-video":
175
+ if not input_image_filepath:
176
+ raise gr.Error("input_image_filepath is required for image-to-video mode")
177
+ elif mode == "video-to-video":
178
+ if not input_video_filepath:
179
+ raise gr.Error("input_video_filepath is required for video-to-video mode")
180
+ elif mode == "text-to-video":
181
+ # No additional file inputs required for text-to-video
182
+ pass
183
+ else:
184
+ raise gr.Error(f"Invalid mode: {mode}. Must be one of: text-to-video, image-to-video, video-to-video")
185
+
186
+ if randomize_seed:
187
+ seed_ui = random.randint(0, 2**32 - 1)
188
+ seed_everething(int(seed_ui))
189
 
190
+ target_frames_ideal = duration_ui * FPS
191
+ target_frames_rounded = round(target_frames_ideal)
192
+ if target_frames_rounded < 1:
193
+ target_frames_rounded = 1
194
 
195
+ n_val = round((float(target_frames_rounded) - 1.0) / 8.0)
196
+ actual_num_frames = int(n_val * 8 + 1)
197
+
198
+ actual_num_frames = max(9, actual_num_frames)
199
+ actual_num_frames = min(MAX_NUM_FRAMES, actual_num_frames)
 
200
 
201
+ actual_height = int(height_ui)
202
+ actual_width = int(width_ui)
203
+
204
+ height_padded = ((actual_height - 1) // 32 + 1) * 32
205
+ width_padded = ((actual_width - 1) // 32 + 1) * 32
206
+ num_frames_padded = ((actual_num_frames - 2) // 8 + 1) * 8 + 1
207
+ if num_frames_padded != actual_num_frames:
208
+ print(f"Warning: actual_num_frames ({actual_num_frames}) and num_frames_padded ({num_frames_padded}) differ. Using num_frames_padded for pipeline.")
209
 
210
+ padding_values = calculate_padding(actual_height, actual_width, height_padded, width_padded)
 
 
 
 
 
 
211
 
212
+ call_kwargs = {
213
+ "prompt": prompt,
214
+ "negative_prompt": negative_prompt,
215
+ "height": height_padded,
216
+ "width": width_padded,
217
+ "num_frames": num_frames_padded,
218
+ "frame_rate": int(FPS),
219
+ "generator": torch.Generator(device=target_inference_device).manual_seed(int(seed_ui)),
220
+ "output_type": "pt",
221
+ "conditioning_items": None,
222
+ "media_items": None,
223
+ "decode_timestep": PIPELINE_CONFIG_YAML["decode_timestep"],
224
+ "decode_noise_scale": PIPELINE_CONFIG_YAML["decode_noise_scale"],
225
+ "stochastic_sampling": PIPELINE_CONFIG_YAML["stochastic_sampling"],
226
+ "image_cond_noise_scale": 0.15,
227
+ "is_video": True,
228
+ "vae_per_channel_normalize": True,
229
+ "mixed_precision": (PIPELINE_CONFIG_YAML["precision"] == "mixed_precision"),
230
+ "offload_to_cpu": False,
231
+ "enhance_prompt": False,
232
+ }
233
+
234
+ stg_mode_str = PIPELINE_CONFIG_YAML.get("stg_mode", "attention_values")
235
+ if stg_mode_str.lower() in ["stg_av", "attention_values"]:
236
+ call_kwargs["skip_layer_strategy"] = SkipLayerStrategy.AttentionValues
237
+ elif stg_mode_str.lower() in ["stg_as", "attention_skip"]:
238
+ call_kwargs["skip_layer_strategy"] = SkipLayerStrategy.AttentionSkip
239
+ elif stg_mode_str.lower() in ["stg_r", "residual"]:
240
+ call_kwargs["skip_layer_strategy"] = SkipLayerStrategy.Residual
241
+ elif stg_mode_str.lower() in ["stg_t", "transformer_block"]:
242
+ call_kwargs["skip_layer_strategy"] = SkipLayerStrategy.TransformerBlock
243
+ else:
244
+ raise ValueError(f"Invalid stg_mode: {stg_mode_str}")
245
+
246
+ if mode == "image-to-video" and input_image_filepath:
247
+ try:
248
+ media_tensor = load_image_to_tensor_with_resize_and_crop(
249
+ input_image_filepath, actual_height, actual_width
250
+ )
251
+ media_tensor = torch.nn.functional.pad(media_tensor, padding_values)
252
+ call_kwargs["conditioning_items"] = [ConditioningItem(media_tensor.to(target_inference_device), 0, 1.0)]
253
+ except Exception as e:
254
+ print(f"Error loading image {input_image_filepath}: {e}")
255
+ raise gr.Error(f"Could not load image: {e}")
256
+ elif mode == "video-to-video" and input_video_filepath:
257
+ try:
258
+ call_kwargs["media_items"] = load_media_file(
259
+ media_path=input_video_filepath,
260
+ height=actual_height,
261
+ width=actual_width,
262
+ max_frames=int(ui_frames_to_use),
263
+ padding=padding_values
264
+ ).to(target_inference_device)
265
+ except Exception as e:
266
+ print(f"Error loading video {input_video_filepath}: {e}")
267
+ raise gr.Error(f"Could not load video: {e}")
268
+
269
+ print(f"Moving models to {target_inference_device} for inference (if not already there)...")
270
 
271
+ active_latent_upsampler = None
272
+ if improve_texture_flag and latent_upsampler_instance:
273
+ active_latent_upsampler = latent_upsampler_instance
274
+
275
+ result_images_tensor = None
276
+ if improve_texture_flag:
277
+ if not active_latent_upsampler:
278
+ raise gr.Error("Spatial upscaler model not loaded or improve_texture not selected, cannot use multi-scale.")
279
+
280
+ multi_scale_pipeline_obj = LTXMultiScalePipeline(pipeline_instance, active_latent_upsampler)
281
+
282
+ first_pass_args = PIPELINE_CONFIG_YAML.get("first_pass", {}).copy()
283
+ first_pass_args["guidance_scale"] = float(ui_guidance_scale) # UI overrides YAML
284
+ # num_inference_steps will be derived from len(timesteps) in the pipeline
285
+ first_pass_args.pop("num_inference_steps", None)
286
+
287
+
288
+ second_pass_args = PIPELINE_CONFIG_YAML.get("second_pass", {}).copy()
289
+ second_pass_args["guidance_scale"] = float(ui_guidance_scale) # UI overrides YAML
290
+ # num_inference_steps will be derived from len(timesteps) in the pipeline
291
+ second_pass_args.pop("num_inference_steps", None)
292
+
293
+ multi_scale_call_kwargs = call_kwargs.copy()
294
+ multi_scale_call_kwargs.update({
295
+ "downscale_factor": PIPELINE_CONFIG_YAML["downscale_factor"],
296
+ "first_pass": first_pass_args,
297
+ "second_pass": second_pass_args,
298
+ })
299
+
300
+ print(f"Calling multi-scale pipeline (eff. HxW: {actual_height}x{actual_width}, Frames: {actual_num_frames} -> Padded: {num_frames_padded}) on {target_inference_device}")
301
+ result_images_tensor = multi_scale_pipeline_obj(**multi_scale_call_kwargs).images
302
+ else:
303
+ single_pass_call_kwargs = call_kwargs.copy()
304
+ first_pass_config_from_yaml = PIPELINE_CONFIG_YAML.get("first_pass", {})
305
+
306
+ single_pass_call_kwargs["timesteps"] = first_pass_config_from_yaml.get("timesteps")
307
+ single_pass_call_kwargs["guidance_scale"] = float(ui_guidance_scale) # UI overrides YAML
308
+ single_pass_call_kwargs["stg_scale"] = first_pass_config_from_yaml.get("stg_scale")
309
+ single_pass_call_kwargs["rescaling_scale"] = first_pass_config_from_yaml.get("rescaling_scale")
310
+ single_pass_call_kwargs["skip_block_list"] = first_pass_config_from_yaml.get("skip_block_list")
311
+
312
+ # Remove keys that might conflict or are not used in single pass / handled by above
313
+ single_pass_call_kwargs.pop("num_inference_steps", None)
314
+ single_pass_call_kwargs.pop("first_pass", None)
315
+ single_pass_call_kwargs.pop("second_pass", None)
316
+ single_pass_call_kwargs.pop("downscale_factor", None)
317
+
318
+ print(f"Calling base pipeline (padded HxW: {height_padded}x{width_padded}, Frames: {actual_num_frames} -> Padded: {num_frames_padded}) on {target_inference_device}")
319
+ result_images_tensor = pipeline_instance(**single_pass_call_kwargs).images
320
+
321
+ if result_images_tensor is None:
322
+ raise gr.Error("Generation failed.")
323
+
324
+ pad_left, pad_right, pad_top, pad_bottom = padding_values
325
+ slice_h_end = -pad_bottom if pad_bottom > 0 else None
326
+ slice_w_end = -pad_right if pad_right > 0 else None
327
 
328
+ result_images_tensor = result_images_tensor[
329
+ :, :, :actual_num_frames, pad_top:slice_h_end, pad_left:slice_w_end
330
+ ]
331
+
332
+ video_np = result_images_tensor[0].permute(1, 2, 3, 0).cpu().float().numpy()
333
 
334
+ video_np = np.clip(video_np, 0, 1)
335
+ video_np = (video_np * 255).astype(np.uint8)
336
+
337
+ temp_dir = tempfile.mkdtemp()
338
+ timestamp = random.randint(10000,99999)
339
+ output_video_path = os.path.join(temp_dir, f"output_{timestamp}.mp4")
340
 
341
+ try:
342
+ with imageio.get_writer(output_video_path, fps=call_kwargs["frame_rate"], macro_block_size=1) as video_writer:
343
+ for frame_idx in range(video_np.shape[0]):
344
+ progress(frame_idx / video_np.shape[0], desc="Saving video")
345
+ video_writer.append_data(video_np[frame_idx])
346
+ except Exception as e:
347
+ print(f"Error saving video with macro_block_size=1: {e}")
348
+ try:
349
+ with imageio.get_writer(output_video_path, fps=call_kwargs["frame_rate"], format='FFMPEG', codec='libx264', quality=8) as video_writer:
350
+ for frame_idx in range(video_np.shape[0]):
351
+ progress(frame_idx / video_np.shape[0], desc="Saving video (fallback ffmpeg)")
352
+ video_writer.append_data(video_np[frame_idx])
353
+ except Exception as e2:
354
+ print(f"Fallback video saving error: {e2}")
355
+ raise gr.Error(f"Failed to save video: {e2}")
356
 
357
+ return output_video_path, seed_ui
358
 
359
+ def update_task_image():
360
+ return "image-to-video"
361
+
362
+ def update_task_text():
363
+ return "text-to-video"
364
+
365
+ def update_task_video():
366
+ return "video-to-video"
367
+
368
+ # --- Gradio UI Definition ---
369
+ css="""
370
+ #col-container {
371
+ margin: 0 auto;
372
+ max-width: 900px;
373
+ }
374
+ """
375
 
376
+ with gr.Blocks(css=css) as demo:
377
+ gr.Markdown("# LTX Video 0.9.8 13B Distilled")
378
+ gr.Markdown("Fast high quality video generation.**Update (17/07):** now with the new v0.9.8 for improved prompt understanding and detail generation" )
379
+ gr.Markdown("[Model](https://huggingface.co/Lightricks/LTX-Video/blob/main/ltxv-13b-0.9.8-distilled.safetensors) [GitHub](https://github.com/Lightricks/LTX-Video) [Diffusers](https://huggingface.co/Lightricks/LTX-Video-0.9.8-13B-distilled#diffusers-🧨)")
380
  with gr.Row():
381
+ with gr.Column():
382
+ with gr.Tab("image-to-video") as image_tab:
383
+ video_i_hidden = gr.Textbox(label="video_i", visible=False, value=None)
384
+ image_i2v = gr.Image(label="Input Image", type="filepath", sources=["upload", "webcam", "clipboard"])
385
+ i2v_prompt = gr.Textbox(label="Prompt", value="The creature from the image starts to move", lines=3)
386
+ i2v_button = gr.Button("Generate Image-to-Video", variant="primary")
387
+ with gr.Tab("text-to-video") as text_tab:
388
+ image_n_hidden = gr.Textbox(label="image_n", visible=False, value=None)
389
+ video_n_hidden = gr.Textbox(label="video_n", visible=False, value=None)
390
+ t2v_prompt = gr.Textbox(label="Prompt", value="A majestic dragon flying over a medieval castle", lines=3)
391
+ t2v_button = gr.Button("Generate Text-to-Video", variant="primary")
392
+ with gr.Tab("video-to-video", visible=False) as video_tab:
393
+ image_v_hidden = gr.Textbox(label="image_v", visible=False, value=None)
394
+ video_v2v = gr.Video(label="Input Video", sources=["upload", "webcam"]) # type defaults to filepath
395
+ frames_to_use = gr.Slider(label="Frames to use from input video", minimum=9, maximum=MAX_NUM_FRAMES, value=9, step=8, info="Number of initial frames to use for conditioning/transformation. Must be N*8+1.")
396
+ v2v_prompt = gr.Textbox(label="Prompt", value="Change the style to cinematic anime", lines=3)
397
+ v2v_button = gr.Button("Generate Video-to-Video", variant="primary")
398
+
399
+ duration_input = gr.Slider(
400
+ label="Video Duration (seconds)",
401
+ minimum=0.3,
402
+ maximum=8.5,
403
+ value=2,
404
+ step=0.1,
405
+ info=f"Target video duration (0.3s to 8.5s)"
406
+ )
407
+ improve_texture = gr.Checkbox(label="Improve Texture (multi-scale)", value=True,visible=False, info="Uses a two-pass generation for better quality, but is slower. Recommended for final output.")
408
+
409
+ with gr.Column():
410
+ output_video = gr.Video(label="Generated Video", interactive=False)
411
+ # gr.DeepLinkButton()
412
+
413
+ with gr.Accordion("Advanced settings", open=False):
414
+ mode = gr.Dropdown(["text-to-video", "image-to-video", "video-to-video"], label="task", value="image-to-video", visible=False)
415
+ negative_prompt_input = gr.Textbox(label="Negative Prompt", value="worst quality, inconsistent motion, blurry, jittery, distorted", lines=2)
416
+ with gr.Row():
417
+ seed_input = gr.Number(label="Seed", value=42, precision=0, minimum=0, maximum=2**32-1)
418
+ randomize_seed_input = gr.Checkbox(label="Randomize Seed", value=True)
419
+ with gr.Row(visible=False):
420
+ guidance_scale_input = gr.Slider(label="Guidance Scale (CFG)", minimum=1.0, maximum=10.0, value=PIPELINE_CONFIG_YAML.get("first_pass", {}).get("guidance_scale", 1.0), step=0.1, info="Controls how much the prompt influences the output. Higher values = stronger influence.")
421
+ with gr.Row():
422
+ height_input = gr.Slider(label="Height", value=512, step=32, minimum=MIN_DIM_SLIDER, maximum=MAX_IMAGE_SIZE, info="Must be divisible by 32.")
423
+ width_input = gr.Slider(label="Width", value=704, step=32, minimum=MIN_DIM_SLIDER, maximum=MAX_IMAGE_SIZE, info="Must be divisible by 32.")
424
+
425
+
426
+ # --- Event handlers for updating dimensions on upload ---
427
+ def handle_image_upload_for_dims(image_filepath, current_h, current_w):
428
+ if not image_filepath: # Image cleared or no image initially
429
+ # Keep current slider values if image is cleared or no input
430
+ return gr.update(value=current_h), gr.update(value=current_w)
431
+ try:
432
+ img = Image.open(image_filepath)
433
+ orig_w, orig_h = img.size
434
+ new_h, new_w = calculate_new_dimensions(orig_w, orig_h)
435
+ return gr.update(value=new_h), gr.update(value=new_w)
436
+ except Exception as e:
437
+ print(f"Error processing image for dimension update: {e}")
438
+ # Keep current slider values on error
439
+ return gr.update(value=current_h), gr.update(value=current_w)
440
+
441
+ def handle_video_upload_for_dims(video_filepath, current_h, current_w):
442
+ if not video_filepath: # Video cleared or no video initially
443
+ return gr.update(value=current_h), gr.update(value=current_w)
444
+ try:
445
+ # Ensure video_filepath is a string for os.path.exists and imageio
446
+ video_filepath_str = str(video_filepath)
447
+ if not os.path.exists(video_filepath_str):
448
+ print(f"Video file path does not exist for dimension update: {video_filepath_str}")
449
+ return gr.update(value=current_h), gr.update(value=current_w)
450
+
451
+ orig_w, orig_h = -1, -1
452
+ with imageio.get_reader(video_filepath_str) as reader:
453
+ meta = reader.get_meta_data()
454
+ if 'size' in meta:
455
+ orig_w, orig_h = meta['size']
456
+ else:
457
+ # Fallback: read first frame if 'size' not in metadata
458
+ try:
459
+ first_frame = reader.get_data(0)
460
+ # Shape is (h, w, c) for frames
461
+ orig_h, orig_w = first_frame.shape[0], first_frame.shape[1]
462
+ except Exception as e_frame:
463
+ print(f"Could not get video size from metadata or first frame: {e_frame}")
464
+ return gr.update(value=current_h), gr.update(value=current_w)
465
 
466
+ if orig_w == -1 or orig_h == -1: # If dimensions couldn't be determined
467
+ print(f"Could not determine dimensions for video: {video_filepath_str}")
468
+ return gr.update(value=current_h), gr.update(value=current_w)
469
+
470
+ new_h, new_w = calculate_new_dimensions(orig_w, orig_h)
471
+ return gr.update(value=new_h), gr.update(value=new_w)
472
+ except Exception as e:
473
+ # Log type of video_filepath for debugging if it's not a path-like string
474
+ print(f"Error processing video for dimension update: {e} (Path: {video_filepath}, Type: {type(video_filepath)})")
475
+ return gr.update(value=current_h), gr.update(value=current_w)
476
+
 
 
477
 
478
+ image_i2v.upload(
479
+ fn=handle_image_upload_for_dims,
480
+ inputs=[image_i2v, height_input, width_input],
481
+ outputs=[height_input, width_input]
 
 
 
 
 
 
 
 
 
 
 
482
  )
483
+ video_v2v.upload(
484
+ fn=handle_video_upload_for_dims,
485
+ inputs=[video_v2v, height_input, width_input],
486
+ outputs=[height_input, width_input]
 
 
 
 
 
 
 
 
 
 
 
 
 
487
  )
488
 
489
+ image_tab.select(
490
+ fn=update_task_image,
491
+ outputs=[mode]
492
+ )
493
+ text_tab.select(
494
+ fn=update_task_text,
495
+ outputs=[mode]
496
+ )
497
+
498
+ t2v_inputs = [t2v_prompt, negative_prompt_input, image_n_hidden, video_n_hidden,
499
+ height_input, width_input, mode,
500
+ duration_input, frames_to_use,
501
+ seed_input, randomize_seed_input, guidance_scale_input, improve_texture]
502
 
503
+ i2v_inputs = [i2v_prompt, negative_prompt_input, image_i2v, video_i_hidden,
504
+ height_input, width_input, mode,
505
+ duration_input, frames_to_use,
506
+ seed_input, randomize_seed_input, guidance_scale_input, improve_texture]
507
+
508
+ v2v_inputs = [v2v_prompt, negative_prompt_input, image_v_hidden, video_v2v,
509
+ height_input, width_input, mode,
510
+ duration_input, frames_to_use,
511
+ seed_input, randomize_seed_input, guidance_scale_input, improve_texture]
512
+
513
+ t2v_button.click(fn=generate, inputs=t2v_inputs, outputs=[output_video, seed_input], api_name="text_to_video")
514
+ i2v_button.click(fn=generate, inputs=i2v_inputs, outputs=[output_video, seed_input], api_name="image_to_video")
515
+ v2v_button.click(fn=generate, inputs=v2v_inputs, outputs=[output_video, seed_input], api_name="video_to_video")
516
+
517
  if __name__ == "__main__":
518
+ if os.path.exists(models_dir) and os.path.isdir(models_dir):
519
+ print(f"Model directory: {Path(models_dir).resolve()}")
520
+
521
+ demo.queue().launch(debug=True, share=False, mcp_server=True)