|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import gradio as gr |
|
|
import yaml |
|
|
import logging |
|
|
import os |
|
|
import sys |
|
|
import shutil |
|
|
import time |
|
|
import json |
|
|
|
|
|
from aduc_orchestrator import AducOrchestrator |
|
|
|
|
|
|
|
|
|
|
|
LOG_FILE_PATH = "aduc_log.txt" |
|
|
if os.path.exists(LOG_FILE_PATH): |
|
|
os.remove(LOG_FILE_PATH) |
|
|
|
|
|
log_format = '%(asctime)s - %(levelname)s - [%(name)s:%(funcName)s] - %(message)s' |
|
|
root_logger = logging.getLogger() |
|
|
root_logger.setLevel(logging.INFO) |
|
|
root_logger.handlers.clear() |
|
|
|
|
|
stream_handler = logging.StreamHandler(sys.stdout) |
|
|
stream_handler.setLevel(logging.INFO) |
|
|
stream_handler.setFormatter(logging.Formatter(log_format)) |
|
|
root_logger.addHandler(stream_handler) |
|
|
|
|
|
file_handler = logging.FileHandler(LOG_FILE_PATH, mode='w', encoding='utf-8') |
|
|
file_handler.setLevel(logging.INFO) |
|
|
file_handler.setFormatter(logging.Formatter(log_format)) |
|
|
root_logger.addHandler(file_handler) |
|
|
|
|
|
logger = logging.getLogger(__name__) |
|
|
|
|
|
i18n = {} |
|
|
try: |
|
|
with open("i18n.json", "r", encoding="utf-8") as f: |
|
|
i18n = json.load(f) |
|
|
except Exception as e: |
|
|
logger.error(f"Erro ao carregar i18n.json: {e}") |
|
|
i18n = {"pt": {}, "en": {}, "zh": {}} |
|
|
|
|
|
if 'pt' not in i18n: i18n['pt'] = i18n.get('en', {}) |
|
|
if 'en' not in i18n: i18n['en'] = {} |
|
|
if 'zh' not in i18n: i18n['zh'] = i18n.get('en', {}) |
|
|
|
|
|
try: |
|
|
with open("config.yaml", 'r') as f: config = yaml.safe_load(f) |
|
|
WORKSPACE_DIR = config['application']['workspace_dir'] |
|
|
aduc = AducOrchestrator(workspace_dir=WORKSPACE_DIR) |
|
|
logger.info("Orquestrador ADUC e Especialistas inicializados com sucesso.") |
|
|
except Exception as e: |
|
|
logger.error(f"ERRO CRÍTICO ao inicializar: {e}", exc_info=True) |
|
|
exit() |
|
|
|
|
|
|
|
|
|
|
|
def run_mode_a_wrapper(prompt, num_keyframes, ref_files, resolution_str, duration_per_fragment, progress=gr.Progress()): |
|
|
if not ref_files: |
|
|
raise gr.Error("Por favor, forneça pelo menos uma imagem de referência.") |
|
|
|
|
|
ref_paths = [aduc.process_image_for_story(f.name, 480, f"ref_processed_{i}.png") for i, f in enumerate(ref_files)] |
|
|
|
|
|
progress(0.1, desc="Gerando roteiro...") |
|
|
storyboard, initial_ref_path, _ = aduc.task_generate_storyboard(prompt, num_keyframes, ref_paths, progress) |
|
|
|
|
|
resolution = int(resolution_str.split('x')[0]) |
|
|
|
|
|
def cb_factory(scene_index, total_scenes): |
|
|
start_time = time.time() |
|
|
total_steps = 12 |
|
|
def callback(pipe_self, step, timestep, callback_kwargs): |
|
|
elapsed = time.time() - start_time |
|
|
current_step = step + 1 |
|
|
if current_step > 0: |
|
|
it_per_sec = current_step / elapsed |
|
|
eta = (total_steps - current_step) / it_per_sec if it_per_sec > 0 else 0 |
|
|
desc = f"Keyframe {scene_index}/{total_scenes}: {int((current_step/total_steps)*100)}% | {current_step}/{total_steps} [{elapsed:.0f}s<{eta:.0f}s, {it_per_sec:.2f}it/s]" |
|
|
base_progress = 0.2 + (scene_index - 1) * (0.8 / total_scenes) |
|
|
step_progress = (current_step / total_steps) * (0.8 / total_scenes) |
|
|
progress(base_progress + step_progress, desc=desc) |
|
|
return {} |
|
|
return callback |
|
|
|
|
|
final_keyframes = aduc.task_generate_keyframes(storyboard, initial_ref_path, prompt, resolution, cb_factory) |
|
|
|
|
|
return gr.update(value=storyboard), gr.update(value=final_keyframes), gr.update(visible=True, open=True) |
|
|
|
|
|
def run_mode_b_wrapper(prompt, num_keyframes, ref_files, progress=gr.Progress()): |
|
|
if not ref_files or len(ref_files) < 2: |
|
|
raise gr.Error("Modo Fotógrafo requer pelo menos 2 imagens: uma base e uma para o banco de cenas.") |
|
|
|
|
|
base_ref_paths = [aduc.process_image_for_story(ref_files[0].name, 480, "base_ref_processed_0.png")] |
|
|
pool_ref_paths = [aduc.process_image_for_story(f.name, 480, f"pool_ref_{i+1}.png") for i, f in enumerate(ref_files[1:])] |
|
|
|
|
|
progress(0.1, desc="Gerando roteiro...") |
|
|
storyboard, _, _ = aduc.task_generate_storyboard(prompt, num_keyframes, base_ref_paths, progress) |
|
|
|
|
|
progress(0.5, desc="IA (Fotógrafo) está selecionando as melhores cenas...") |
|
|
selected_keyframes = aduc.task_select_keyframes(storyboard, base_ref_paths, pool_ref_paths) |
|
|
|
|
|
return gr.update(value=storyboard), gr.update(value=selected_keyframes), gr.update(visible=True, open=True) |
|
|
|
|
|
def run_video_production_wrapper(keyframes, prompt, duration, |
|
|
trim_percent, handler_strength, destination_convergence_strength, |
|
|
use_upscaler, use_refiner, use_hd, use_audio, |
|
|
video_resolution, |
|
|
progress=gr.Progress()): |
|
|
yield { |
|
|
final_video_output: gr.update(value=None, visible=True, label="🎬 Produzindo seu filme... Por favor, aguarde.") |
|
|
} |
|
|
|
|
|
resolution = int(video_resolution.split('x')[0]) |
|
|
final_movie_path = None |
|
|
|
|
|
for update in aduc.task_produce_final_movie_with_feedback( |
|
|
keyframes, prompt, duration, |
|
|
int(trim_percent), handler_strength, destination_convergence_strength, |
|
|
use_upscaler, use_refiner, use_hd, use_audio, |
|
|
resolution, use_continuity_director=True, progress=progress |
|
|
): |
|
|
if "final_path" in update and update["final_path"]: |
|
|
final_movie_path = update["final_path"] |
|
|
break |
|
|
|
|
|
yield { |
|
|
final_video_output: gr.update(value=final_movie_path, label="🎉 FILME COMPLETO 🎉") |
|
|
} |
|
|
|
|
|
def get_log_content(): |
|
|
try: |
|
|
with open(LOG_FILE_PATH, "r", encoding="utf-8") as f: |
|
|
return f.read() |
|
|
except FileNotFoundError: |
|
|
return "Arquivo de log ainda não criado. Inicie uma geração." |
|
|
|
|
|
def update_ui_language(lang_code): |
|
|
lang_map = i18n.get(lang_code, i18n.get('en', {})) |
|
|
return { |
|
|
title_md: gr.update(value=f"# {lang_map.get('app_title')}"), |
|
|
subtitle_md: gr.update(value=lang_map.get('app_subtitle')), |
|
|
lang_selector: gr.update(label=lang_map.get('lang_selector_label')), |
|
|
step1_accordion: gr.update(label=lang_map.get('step1_accordion')), |
|
|
prompt_input: gr.update(label=lang_map.get('prompt_label')), |
|
|
ref_image_input: gr.update(label=lang_map.get('ref_images_label')), |
|
|
num_keyframes_slider: gr.update(label=lang_map.get('keyframes_label')), |
|
|
duration_per_fragment_slider: gr.update(label=lang_map.get('duration_label'), info=lang_map.get('duration_info')), |
|
|
storyboard_and_keyframes_button: gr.update(value=lang_map.get('storyboard_and_keyframes_button')), |
|
|
storyboard_from_photos_button: gr.update(value=lang_map.get('storyboard_from_photos_button')), |
|
|
step1_mode_b_info_md: gr.update(value=f"*{lang_map.get('step1_mode_b_info')}*"), |
|
|
storyboard_output: gr.update(label=lang_map.get('storyboard_output_label')), |
|
|
keyframe_gallery: gr.update(label=lang_map.get('keyframes_gallery_label')), |
|
|
step2_accordion: gr.update(label=lang_map.get('step3_accordion')), |
|
|
step2_description_md: gr.update(value=lang_map.get('step3_description')), |
|
|
produce_button: gr.update(value=lang_map.get('produce_button')), |
|
|
final_video_output: gr.update(label=lang_map.get('final_movie_with_audio_label')), |
|
|
log_accordion: gr.update(label=lang_map.get('log_accordion_label')), |
|
|
log_display: gr.update(label=lang_map.get('log_display_label')), |
|
|
update_log_button: gr.update(value=lang_map.get('update_log_button')), |
|
|
advanced_options_accordion: gr.update(label=lang_map.get('advanced_options_accordion')), |
|
|
causality_controls_title_md: gr.update(value=f"**{lang_map.get('causality_controls_title')}**"), |
|
|
trim_percent_slider: gr.update(label=lang_map.get('trim_percent_label'), info=lang_map.get('trim_percent_info')), |
|
|
forca_guia_slider: gr.update(label=lang_map.get('forca_guia_label'), info=lang_map.get('forca_guia_info')), |
|
|
convergencia_destino_slider: gr.update(label=lang_map.get('convergencia_final_label'), info=lang_map.get('convergencia_final_info')), |
|
|
post_production_controls_title_md: gr.update(value=f"**{lang_map.get('post_production_controls_title')}**"), |
|
|
use_upscaler_checkbox: gr.update(label=lang_map.get('use_upscaler_label')), |
|
|
use_refiner_checkbox: gr.update(label=lang_map.get('use_refiner_label')), |
|
|
use_hd_checkbox: gr.update(label=lang_map.get('use_hd_label')), |
|
|
use_audio_checkbox: gr.update(label=lang_map.get('use_audio_label')), |
|
|
} |
|
|
|
|
|
|
|
|
with gr.Blocks(theme=gr.themes.Soft()) as demo: |
|
|
default_lang = i18n.get('pt', {}) |
|
|
|
|
|
title_md = gr.Markdown(f"# {default_lang.get('app_title')}") |
|
|
subtitle_md = gr.Markdown(default_lang.get('app_subtitle')) |
|
|
|
|
|
with gr.Row(): |
|
|
lang_selector = gr.Radio(["pt", "en", "zh"], value="pt", label=default_lang.get('lang_selector_label')) |
|
|
resolution_selector = gr.Radio(["480x480"], value="480x480", label="Resolução Base") |
|
|
|
|
|
with gr.Accordion(default_lang.get('step1_accordion'), open=True) as step1_accordion: |
|
|
prompt_input = gr.Textbox(label=default_lang.get('prompt_label'), value="A majestic lion walks across the savanna, sits down, and then roars at the setting sun.") |
|
|
ref_image_input = gr.File(label=default_lang.get('ref_images_label'), file_count="multiple", file_types=["image"]) |
|
|
with gr.Row(): |
|
|
num_keyframes_slider = gr.Slider(minimum=3, maximum=42, value=5, step=1, label=default_lang.get('keyframes_label')) |
|
|
duration_per_fragment_slider = gr.Slider(label=default_lang.get('duration_label'), info=default_lang.get('duration_info'), minimum=2.0, maximum=10.0, value=4.0, step=0.1) |
|
|
with gr.Row(): |
|
|
storyboard_and_keyframes_button = gr.Button(default_lang.get('storyboard_and_keyframes_button'), variant="primary") |
|
|
storyboard_from_photos_button = gr.Button(default_lang.get('storyboard_from_photos_button')) |
|
|
step1_mode_b_info_md = gr.Markdown(f"*{default_lang.get('step1_mode_b_info')}*") |
|
|
storyboard_output = gr.JSON(label=default_lang.get('storyboard_output_label')) |
|
|
keyframe_gallery = gr.Gallery(label=default_lang.get('keyframes_gallery_label'), visible=True, object_fit="contain", height="auto", type="filepath") |
|
|
|
|
|
with gr.Accordion(default_lang.get('step3_accordion'), open=False, visible=False) as step2_accordion: |
|
|
step2_description_md = gr.Markdown(default_lang.get('step3_description')) |
|
|
|
|
|
with gr.Accordion(default_lang.get('advanced_options_accordion'), open=True) as advanced_options_accordion: |
|
|
causality_controls_title_md = gr.Markdown(f"**{default_lang.get('causality_controls_title')}**") |
|
|
trim_percent_slider = gr.Slider(minimum=10, maximum=90, value=50, step=5, |
|
|
label=default_lang.get('trim_percent_label'), |
|
|
info=default_lang.get('trim_percent_info')) |
|
|
with gr.Row(): |
|
|
forca_guia_slider = gr.Slider(label=default_lang.get('forca_guia_label'), minimum=0.0, maximum=1.0, value=0.5, step=0.05, info=default_lang.get('forca_guia_info')) |
|
|
convergencia_destino_slider = gr.Slider(label=default_lang.get('convergencia_final_label'), minimum=0.0, maximum=1.0, value=0.75, step=0.05, info=default_lang.get('convergencia_final_info')) |
|
|
|
|
|
gr.Markdown("---") |
|
|
post_production_controls_title_md = gr.Markdown(f"**{default_lang.get('post_production_controls_title')}**") |
|
|
with gr.Row(): |
|
|
use_upscaler_checkbox = gr.Checkbox(label=default_lang.get('use_upscaler_label'), value=False) |
|
|
use_refiner_checkbox = gr.Checkbox(label=default_lang.get('use_refiner_label'), value=False) |
|
|
use_hd_checkbox = gr.Checkbox(label=default_lang.get('use_hd_label'), value=False) |
|
|
use_audio_checkbox = gr.Checkbox(label=default_lang.get('use_audio_label'), value=False) |
|
|
|
|
|
produce_button = gr.Button(default_lang.get('produce_button'), variant="primary") |
|
|
|
|
|
final_video_output = gr.Video(label=default_lang.get('final_movie_with_audio_label'), visible=False) |
|
|
|
|
|
with gr.Accordion(default_lang.get('log_accordion_label'), open=False) as log_accordion: |
|
|
log_display = gr.Textbox(label=default_lang.get('log_display_label'), lines=20, interactive=False, autoscroll=True) |
|
|
update_log_button = gr.Button(default_lang.get('update_log_button')) |
|
|
|
|
|
|
|
|
all_ui_components = list(update_ui_language('pt').keys()) |
|
|
lang_selector.change(fn=update_ui_language, inputs=lang_selector, outputs=all_ui_components) |
|
|
|
|
|
storyboard_and_keyframes_button.click( |
|
|
fn=run_mode_a_wrapper, |
|
|
inputs=[prompt_input, num_keyframes_slider, ref_image_input, resolution_selector, duration_per_fragment_slider], |
|
|
outputs=[storyboard_output, keyframe_gallery, step2_accordion] |
|
|
) |
|
|
|
|
|
storyboard_from_photos_button.click( |
|
|
fn=run_mode_b_wrapper, |
|
|
inputs=[prompt_input, num_keyframes_slider, ref_image_input], |
|
|
outputs=[storyboard_output, keyframe_gallery, step2_accordion] |
|
|
) |
|
|
|
|
|
produce_button.click( |
|
|
fn=run_video_production_wrapper, |
|
|
inputs=[ |
|
|
keyframe_gallery, prompt_input, duration_per_fragment_slider, |
|
|
trim_percent_slider, forca_guia_slider, convergencia_destino_slider, |
|
|
use_upscaler_checkbox, use_refiner_checkbox, use_hd_checkbox, use_audio_checkbox, |
|
|
resolution_selector |
|
|
], |
|
|
outputs=[final_video_output] |
|
|
) |
|
|
|
|
|
update_log_button.click(fn=get_log_content, inputs=[], outputs=[log_display]) |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
if os.path.exists(WORKSPACE_DIR): |
|
|
logger.info(f"Limpando o workspace anterior em: {WORKSPACE_DIR}") |
|
|
shutil.rmtree(WORKSPACE_DIR) |
|
|
os.makedirs(WORKSPACE_DIR) |
|
|
logger.info(f"Aplicação iniciada. Lançando interface Gradio...") |
|
|
demo.queue().launch() |