Update app.py
Browse files
app.py
CHANGED
|
@@ -1,186 +1,57 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
import gradio as gr
|
| 4 |
-
from
|
| 5 |
-
import
|
| 6 |
-
import
|
| 7 |
-
|
| 8 |
-
# --- FUNÇÕES DE AJUDA PARA A UI ---
|
| 9 |
-
TARGET_FIXED_SIDE = 768
|
| 10 |
-
MIN_DIM_SLIDER = 256
|
| 11 |
-
MAX_IMAGE_SIZE = 1280
|
| 12 |
|
| 13 |
-
|
| 14 |
-
if orig_w == 0 or orig_h == 0: return int(TARGET_FIXED_SIDE), int(TARGET_FIXED_SIDE)
|
| 15 |
-
if orig_w >= orig_h:
|
| 16 |
-
new_h, aspect_ratio = TARGET_FIXED_SIDE, orig_w / orig_h
|
| 17 |
-
new_w = round((new_h * aspect_ratio) / 32) * 32
|
| 18 |
-
new_w = max(MIN_DIM_SLIDER, min(new_w, MAX_IMAGE_SIZE))
|
| 19 |
-
new_h = max(MIN_DIM_SLIDER, min(new_h, MAX_IMAGE_SIZE))
|
| 20 |
-
else:
|
| 21 |
-
new_w, aspect_ratio = TARGET_FIXED_SIDE, orig_h / orig_w
|
| 22 |
-
new_h = round((new_w * aspect_ratio) / 32) * 32
|
| 23 |
-
new_h = max(MIN_DIM_SLIDER, min(new_h, MAX_IMAGE_SIZE))
|
| 24 |
-
new_w = max(MIN_DIM_SLIDER, min(new_w, MAX_IMAGE_SIZE))
|
| 25 |
-
return int(new_h), int(new_w)
|
| 26 |
|
| 27 |
-
def
|
| 28 |
-
|
| 29 |
try:
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
orig_w, orig_h = img.size
|
| 33 |
-
else: # Assumir que é um vídeo
|
| 34 |
-
with imageio.get_reader(filepath) as reader:
|
| 35 |
-
meta = reader.get_meta_data()
|
| 36 |
-
orig_w, orig_h = meta.get('size', (current_w, current_h))
|
| 37 |
-
new_h, new_w = calculate_new_dimensions(orig_w, orig_h)
|
| 38 |
-
return gr.update(value=new_h), gr.update(value=new_w)
|
| 39 |
except Exception as e:
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
def
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
)
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
print(f"Erro inesperado na geração: {e}")
|
| 81 |
-
raise gr.Error("Ocorreu um erro inesperado. Verifique os logs.")
|
| 82 |
-
|
| 83 |
-
# --- DEFINIÇÃO DA INTERFACE GRADIO ---
|
| 84 |
-
css = "#col-container { margin: 0 auto; max-width: 900px; }"
|
| 85 |
-
with gr.Blocks(css=css) as demo:
|
| 86 |
-
gr.Markdown("# LTX Video com Keyframes e Latent Upscaler")
|
| 87 |
-
gr.Markdown("Gere vídeos em baixa resolução e opcionalmente duplique a resolução com um Upscaler.")
|
| 88 |
-
|
| 89 |
-
with gr.Row():
|
| 90 |
-
with gr.Column():
|
| 91 |
-
with gr.Tab("image-to-video (Keyframes)") as image_tab:
|
| 92 |
-
i2v_prompt = gr.Textbox(label="Prompt", value="Uma bela transição entre as imagens", lines=2)
|
| 93 |
-
with gr.Row():
|
| 94 |
-
with gr.Column(scale=1):
|
| 95 |
-
gr.Markdown("#### Keyframe de Início (Obrigatório)")
|
| 96 |
-
start_image_i2v = gr.Image(label="Imagem de Início", type="filepath", sources=["upload", "clipboard"])
|
| 97 |
-
with gr.Column(scale=1):
|
| 98 |
-
with gr.Accordion("Keyframe do Meio (Opcional)", open=False):
|
| 99 |
-
middle_image_i2v = gr.Image(label="Imagem do Meio", type="filepath", sources=["upload", "clipboard"]); middle_frame_i2v = gr.Slider(label="Frame Alvo", minimum=0, maximum=200, step=1, value=48); middle_weight_i2v = gr.Slider(label="Peso/Força", minimum=0.0, maximum=1.0, step=0.05, value=1.0)
|
| 100 |
-
with gr.Column(scale=1):
|
| 101 |
-
with gr.Accordion("Keyframe de Fim (Opcional)", open=False):
|
| 102 |
-
end_image_i2v = gr.Image(label="Imagem de Fim", type="filepath", sources=["upload", "clipboard"]); end_weight_i2v = gr.Slider(label="Peso/Força", minimum=0.0, maximum=1.0, step=0.05, value=1.0)
|
| 103 |
-
i2v_button = gr.Button("Generate Image-to-Video", variant="primary")
|
| 104 |
|
| 105 |
-
with gr.Tab("text-to-video") as text_tab:
|
| 106 |
-
t2v_prompt = gr.Textbox(label="Prompt", value="A majestic dragon flying over a medieval castle", lines=3); t2v_button = gr.Button("Generate Text-to-Video", variant="primary")
|
| 107 |
-
|
| 108 |
-
with gr.Tab("video-to-video") as video_tab:
|
| 109 |
-
video_v2v = gr.Video(label="Input Video", sources=["upload", "webcam"]); frames_to_use = gr.Slider(label="Frames to use from input video", minimum=9, maximum=257, value=9, step=8, info="Must be N*8+1."); v2v_prompt = gr.Textbox(label="Prompt", value="Change the style to cinematic anime", lines=3); v2v_button = gr.Button("Generate Video-to-Video", variant="primary")
|
| 110 |
-
|
| 111 |
-
duration_input = gr.Slider(label="Video Duration (seconds)", minimum=0.3, maximum=8.5, value=4, step=0.1)
|
| 112 |
-
|
| 113 |
-
with gr.Column():
|
| 114 |
-
output_video = gr.Video(label="Generated Video", interactive=False)
|
| 115 |
-
|
| 116 |
-
with gr.Accordion("Opções Avançadas", open=False):
|
| 117 |
-
mode = gr.Dropdown(["text-to-video", "image-to-video", "video-to-video"], label="task", value="image-to-video", visible=False)
|
| 118 |
-
|
| 119 |
-
with gr.Accordion("Ajustes de Geração (Sampling)", open=True):
|
| 120 |
-
negative_prompt_input = gr.Textbox(label="Negative Prompt", value="worst quality, blurry, jittery", lines=2)
|
| 121 |
-
with gr.Row():
|
| 122 |
-
seed_input = gr.Number(label="Seed", value=42, precision=0)
|
| 123 |
-
randomize_seed_input = gr.Checkbox(label="Randomize Seed", value=True)
|
| 124 |
-
guidance_scale_input = gr.Slider(label="Guidance Scale (CFG)", minimum=1.0, maximum=10.0, value=3.0, step=0.1)
|
| 125 |
-
|
| 126 |
-
with gr.Accordion("Dimensões do Vídeo (Base)", open=False):
|
| 127 |
-
with gr.Row():
|
| 128 |
-
height_input = gr.Slider(label="Height", value=512, step=32, minimum=MIN_DIM_SLIDER, maximum=MAX_IMAGE_SIZE)
|
| 129 |
-
width_input = gr.Slider(label="Width", value=704, step=32, minimum=MIN_DIM_SLIDER, maximum=MAX_IMAGE_SIZE)
|
| 130 |
-
|
| 131 |
-
with gr.Accordion("Configurações do Upscaler 2x (Etapa 2)", open=False):
|
| 132 |
-
with gr.Row():
|
| 133 |
-
enable_upscaler_input = gr.Checkbox(label="Enable Upscaler", value=True, info="Gera o vídeo final com o dobro da resolução base.")
|
| 134 |
-
adain_factor_input = gr.Slider(label="AdaIN Factor", minimum=0.0, maximum=2.0, value=1.0, step=0.05, info="Controla a transferência de estilo/cor para o vídeo ampliado.")
|
| 135 |
-
|
| 136 |
-
# --- LÓGICA DE EVENTOS DA UI ---
|
| 137 |
-
start_image_i2v.upload(fn=handle_media_upload_for_dims, inputs=[start_image_i2v, height_input, width_input], outputs=[height_input, width_input])
|
| 138 |
-
video_v2v.upload(fn=handle_media_upload_for_dims, inputs=[video_v2v, height_input, width_input], outputs=[height_input, width_input])
|
| 139 |
-
duration_input.change(fn=update_frame_slider, inputs=duration_input, outputs=middle_frame_i2v)
|
| 140 |
-
|
| 141 |
-
image_tab.select(fn=lambda: "image-to-video", outputs=[mode])
|
| 142 |
-
text_tab.select(fn=lambda: "text-to-video", outputs=[mode])
|
| 143 |
-
video_tab.select(fn=lambda: "video-to-video", outputs=[mode])
|
| 144 |
-
|
| 145 |
-
none_image = gr.Textbox(visible=False, value=None)
|
| 146 |
-
none_video = gr.Textbox(visible=False, value=None)
|
| 147 |
-
|
| 148 |
-
shared_params = [
|
| 149 |
-
height_input, width_input, duration_input, frames_to_use,
|
| 150 |
-
seed_input, randomize_seed_input, guidance_scale_input,
|
| 151 |
-
enable_upscaler_input, adain_factor_input
|
| 152 |
-
]
|
| 153 |
-
|
| 154 |
-
i2v_inputs = [i2v_prompt, negative_prompt_input, mode, start_image_i2v, middle_image_i2v, middle_frame_i2v, middle_weight_i2v, end_image_i2v, end_weight_i2v, none_video, *shared_params]
|
| 155 |
-
t2v_inputs = [t2v_prompt, negative_prompt_input, mode, none_image, none_image, gr.Number(value=-1, visible=False), gr.Slider(value=0, visible=False), none_image, gr.Slider(value=0, visible=False), none_video, *shared_params]
|
| 156 |
-
v2v_inputs = [v2v_prompt, negative_prompt_input, mode, none_image, none_image, gr.Number(value=-1, visible=False), gr.Slider(value=0, visible=False), none_image, gr.Slider(value=0, visible=False), video_v2v, *shared_params]
|
| 157 |
-
|
| 158 |
-
common_outputs = [output_video, seed_input]
|
| 159 |
-
|
| 160 |
-
i2v_button.click(fn=gradio_generate_wrapper, inputs=i2v_inputs, outputs=common_outputs, api_name="image_to_video_keyframes")
|
| 161 |
-
t2v_button.click(fn=gradio_generate_wrapper, inputs=t2v_inputs, outputs=common_outputs, api_name="text_to_video")
|
| 162 |
-
v2v_button.click(fn=gradio_generate_wrapper, inputs=v2v_inputs, outputs=common_outputs, api_name="video_to_video")
|
| 163 |
-
|
| 164 |
-
# --- <INÍCIO DA ATUALIZAÇÃO> ---
|
| 165 |
if __name__ == "__main__":
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
# Define os padrões de inicialização
|
| 169 |
-
share_option = False
|
| 170 |
-
server_name = "127.0.0.1"
|
| 171 |
-
|
| 172 |
-
# Verifica se argumentos foram passados na linha de comando
|
| 173 |
-
if '--share' in sys.argv:
|
| 174 |
-
share_option = True
|
| 175 |
-
print("Iniciando com a opção de compartilhamento público (share=True)")
|
| 176 |
-
|
| 177 |
-
if '--listen' in sys.argv:
|
| 178 |
-
server_name = "0.0.0.0"
|
| 179 |
-
print("Iniciando para ser acessível na rede local (listen)")
|
| 180 |
-
|
| 181 |
-
# Inicia a aplicação com as opções definidas
|
| 182 |
-
demo.queue().launch(
|
| 183 |
-
debug=True,
|
| 184 |
-
share=share_option,
|
| 185 |
-
server_name=server_name
|
| 186 |
-
)
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
from typing import List
|
| 5 |
+
from vincie_service import VincieService
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
svc = VincieService()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
def setup():
|
| 10 |
+
# opcional: preparar ambiente na primeira execução
|
| 11 |
try:
|
| 12 |
+
svc.ensure_repo()
|
| 13 |
+
svc.ensure_model()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
except Exception as e:
|
| 15 |
+
return f"Setup falhou: {e}"
|
| 16 |
+
return "Setup OK"
|
| 17 |
+
|
| 18 |
+
def ui_multi_turn(input_image, turns_text):
|
| 19 |
+
if not input_image or not turns_text.strip():
|
| 20 |
+
return "Forneça imagem e turns (um por linha)."
|
| 21 |
+
turns = [ln.strip() for ln in turns_text.splitlines() if ln.strip()]
|
| 22 |
+
out_dir = svc.multi_turn_edit(input_image, turns)
|
| 23 |
+
return f"Gerado em: {str(out_dir)}"
|
| 24 |
+
|
| 25 |
+
def ui_multi_concept(files, descs_text, final_prompt):
|
| 26 |
+
if not files or not descs_text.strip() or not final_prompt.strip():
|
| 27 |
+
return "Envie imagens, descrições (uma por linha) e o prompt final."
|
| 28 |
+
descs = [ln.strip() for ln in descs_text.splitlines() if ln.strip()]
|
| 29 |
+
if len(descs) != len(files):
|
| 30 |
+
return f"Número de descrições ({len(descs)}) difere do número de imagens ({len(files)})."
|
| 31 |
+
out_dir = svc.multi_concept_compose([f.name if hasattr(f, 'name') else str(f) for f in files],
|
| 32 |
+
descs, final_prompt)
|
| 33 |
+
return f"Gerado em: {str(out_dir)}"
|
| 34 |
+
|
| 35 |
+
with gr.Blocks(title="VINCIE Service") as demo:
|
| 36 |
+
gr.Markdown("# 🎨 VINCIE Service")
|
| 37 |
+
setup_btn = gr.Button("Preparar repositório e modelo (uma vez)")
|
| 38 |
+
setup_out = gr.Textbox(label="Status")
|
| 39 |
+
setup_btn.click(fn=setup, outputs=setup_out)
|
| 40 |
+
|
| 41 |
+
with gr.Tab("🔄 Multi-turn Editing"):
|
| 42 |
+
img = gr.Image(type="filepath", label="Imagem inicial")
|
| 43 |
+
turns = gr.Textbox(lines=6, label="Turns (um por linha)")
|
| 44 |
+
run1 = gr.Button("Executar")
|
| 45 |
+
out1 = gr.Textbox(label="Saída")
|
| 46 |
+
run1.click(ui_multi_turn, inputs=[img, turns], outputs=out1)
|
| 47 |
+
|
| 48 |
+
with gr.Tab("🎭 Multi-concept Composition"):
|
| 49 |
+
files = gr.File(file_count="multiple", file_types=["image"], label="Imagens conceito")
|
| 50 |
+
descs = gr.Textbox(lines=6, label="Descrições p/ <IMG0>, <IMG1>, ... (uma por linha)")
|
| 51 |
+
finalp = gr.Textbox(lines=3, label="Prompt final")
|
| 52 |
+
run2 = gr.Button("Executar")
|
| 53 |
+
out2 = gr.Textbox(label="Saída")
|
| 54 |
+
run2.click(ui_multi_concept, inputs=[files, descs, finalp], outputs=out2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
if __name__ == "__main__":
|
| 57 |
+
demo.launch(server_name="0.0.0.0", server_port=7861)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|