File size: 3,481 Bytes
f5403c7
 
5215eed
 
f5403c7
 
2db49aa
f5403c7
 
 
 
 
 
 
 
 
 
2db49aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f5403c7
5215eed
 
f5403c7
 
 
 
 
 
5215eed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f5403c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55510cd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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)])