from pathlib import Path import sys import os from uuid import uuid4 import gradio as gr import gradio_client.utils as gr_client_utils ROOT = Path(__file__).resolve().parent SRC = ROOT / "src" if str(SRC) not in sys.path: sys.path.insert(0, str(SRC)) from dot2dot.pipeline import DotToDotPipeline from dot2dot.config import AGE_PRESETS, KDP_DEFAULTS def _patch_gradio_bool_schema_bug() -> None: original_get_type = gr_client_utils.get_type def patched_get_type(schema): # Gradio 4.44.x can crash on API schema generation when JSON Schema # uses a bare boolean for additionalProperties. if isinstance(schema, bool): return "boolean" return original_get_type(schema) gr_client_utils.get_type = patched_get_type _patch_gradio_bool_schema_bug() PIPELINE = DotToDotPipeline() RUNS_DIR = Path("/tmp/dot2dot_runs") RUNS_DIR.mkdir(parents=True, exist_ok=True) def run_single_image(image, age_key, title, include_reference, trim_size): if image is None: raise gr.Error("Envie uma imagem de contorno preto e branco.") temp_path = RUNS_DIR / uuid4().hex temp_path.mkdir(parents=True, exist_ok=True) result = PIPELINE.process_uploaded_image( image=image, output_dir=temp_path, age_key=age_key, page_title=title or "Dot to Dot", include_reference=include_reference, trim_size=trim_size, ) summary = [ f"Faixa etaria: {AGE_PRESETS[age_key].label}", f"Pontos gerados: {result.dot_count}", f"Tamanho final sugerido: {trim_size}", f"Miolo compativel com KDP: {'sim' if result.kdp_ready else 'revisar'}", f"Pacote salvo em: {result.bundle_zip.name}", ] return ( str(result.activity_image), str(result.answer_image), str(result.bundle_zip), "\n".join(summary), ) with gr.Blocks(theme=gr.themes.Soft(), title="Dot2Dot HF Pack") as demo: gr.Markdown( """ # Dot2Dot Generator for Kids Converte uma imagem de contorno em uma pagina de ligar os pontos pronta para workflow KDP. """ ) with gr.Row(): image_input = gr.Image(type="pil", label="Imagem de contorno") with gr.Column(): age_input = gr.Dropdown( choices=list(AGE_PRESETS.keys()), value="6-8", label="Faixa etaria", ) title_input = gr.Textbox(label="Titulo da pagina", value="Patinho") reference_input = gr.Checkbox( label="Incluir contorno de referencia em cinza claro", value=False, ) trim_input = gr.Dropdown( choices=sorted(KDP_DEFAULTS.allowed_trim_sizes), value=KDP_DEFAULTS.default_trim_size, label="Formato de miolo", ) run_button = gr.Button("Gerar") with gr.Row(): activity_output = gr.Image(label="Pagina de atividade") answer_output = gr.Image(label="Pagina de resposta") zip_output = gr.File(label="Pacote gerado") summary_output = gr.Textbox(label="Resumo", lines=6) run_button.click( fn=run_single_image, inputs=[image_input, age_input, title_input, reference_input, trim_input], outputs=[activity_output, answer_output, zip_output, summary_output], ) if __name__ == "__main__": demo.launch(show_api=False, allowed_paths=[str(RUNS_DIR)])