File size: 9,409 Bytes
39b5db1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c96d602
 
 
 
39b5db1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c96d602
 
39b5db1
 
 
 
c96d602
 
39b5db1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
"""Page structure. Mirrors the workflow: source → configure → generate →
inspect ground truth → export. Widgets only; behaviour is in `handlers`."""
from __future__ import annotations

import gradio as gr

from src import config
from src.packager import REASSEMBLY_SNIPPET

from . import content, handlers
from .theme import CSS, build_theme


def _section(label: str) -> None:
    gr.HTML(f'<div class="dm-section">{label}</div>')


def build_ui() -> gr.Blocks:
    with gr.Blocks(
        theme=build_theme(),
        title="Dataset-Maker",
        css=CSS,
        # Gradio copies every returned file/image into its cache and never
        # frees it by default; sweep it on the same TTL as our temp files.
        delete_cache=(config.TEMP_FILE_TTL_S, config.TEMP_FILE_TTL_S),
    ) as demo:
        gr.HTML(content.HEADER_HTML)
        with gr.Accordion("How each guarantee is enforced", open=False):
            gr.HTML(content.GUARANTEES_HTML)

        # Previews of uploaded pages live in session state; expire it (and the
        # session's export) on the same TTL instead of keeping idle tabs forever.
        state = gr.State(None, time_to_live=config.TEMP_FILE_TTL_S,
                         delete_callback=handlers.release_view)

        with gr.Row(equal_height=False):
            # ---------------- left: source + configuration ----------------
            with gr.Column(scale=4, min_width=320):
                _section("1 · Source")
                pdf_in = gr.File(label="PDF", file_types=[".pdf"], type="filepath", height=120)
                sample_btn = gr.Button("Load sample PDF", size="sm", variant="secondary")

                _section("2 · Tear configuration")
                n_pieces = gr.Slider(
                    config.MIN_PIECES, config.MAX_PIECES, config.DEFAULT_PIECES, step=1,
                    label="Fragments per page",
                    info="Voronoi seeds (Poisson-disk). Produced count is reported and can be lower.",
                )
                seed = gr.Number(
                    value=0, precision=0, label="Master seed",
                    info="Page seed = (seed × 1000003 + page_index) & 0x7FFFFFFF. Recorded in the manifest.",
                )
                with gr.Accordion("Tear geometry", open=True):
                    noise_strength = gr.Slider(
                        config.MIN_NOISE_STRENGTH, config.MAX_NOISE_STRENGTH,
                        config.DEFAULT_NOISE_STRENGTH, step=1,
                        label="Edge displacement (px) · noise_strength",
                        info="Domain-warp amplitude. 0 = straight Voronoi edges.",
                    )
                    noise_scale = gr.Slider(
                        config.MIN_NOISE_SCALE, config.MAX_NOISE_SCALE,
                        config.DEFAULT_NOISE_SCALE, step=1,
                        label="Edge wavelength (px) · noise_scale",
                        info="Base wavelength of the warp noise. Displacement far above "
                             "wavelength folds the warp → multi-component fragments (reported).",
                    )
                with gr.Accordion("Rendering and output", open=False):
                    dpi = gr.Slider(
                        config.MIN_DPI, config.MAX_DPI, config.DEFAULT_DPI, step=1,
                        label="Render DPI",
                    )
                    dpi_md = gr.HTML(handlers.dpi_note(config.DEFAULT_DPI))
                    lossy = gr.Checkbox(
                        value=False, label="Lossy palette PNG (64 colours)",
                        info="Smaller ZIP. Offsets stay exact; pixel values become approximate.",
                    )

                _section("3 · Generate")
                with gr.Row():
                    run_btn = gr.Button("Generate dataset", variant="primary",
                                        elem_id="dm-generate", scale=3)
                    clear_btn = gr.Button("Clear", variant="secondary", scale=1)

            # ---------------- right: inspect + export ----------------
            with gr.Column(scale=9, min_width=480):
                _section("4 · Inspect ground truth  ·  5 · Export")
                with gr.Row(equal_height=True):
                    summary = gr.HTML(content.EMPTY_SUMMARY_HTML, elem_id="dm-summary")
                    page_dd = gr.Dropdown(
                        choices=[], value=None, label="Page (Preview, Ground truth)",
                        interactive=False, scale=0, min_width=300,
                    )
                with gr.Tabs():
                    with gr.Tab("Preview"):
                        with gr.Row():
                            src_img = gr.Image(label="Source page (A4 render)", type="numpy",
                                               interactive=False, height=560)
                            part_img = gr.Image(label="Partition map · tear lines + piece index",
                                                type="numpy", interactive=False, height=560)
                        gallery = gr.Gallery(
                            label=f"Fragments (preview resolution, first {handlers.MAX_THUMBNAILS})",
                            columns=6, height=300, object_fit="contain", elem_id="dm-gallery",
                        )
                    with gr.Tab("Ground truth"):
                        with gr.Row():
                            adj_img = gr.Image(label="Adjacency graph · node = fragment, edge = shared tear",
                                               type="numpy", interactive=False, height=560, scale=5)
                            with gr.Column(scale=4):
                                page_meta = gr.HTML("")
                                adj_code = gr.Code(label="pages[i].adjacency", language="json",
                                                   interactive=False, lines=10)
                        gt_table = gr.Dataframe(
                            headers=handlers.GT_HEADERS,
                            datatype=["number"] * 8 + ["str"],
                            label="Per-fragment ground truth (full-resolution px; x, y = top-left offset on page)",
                            interactive=False, wrap=False, height=320, elem_id="dm-gt-table",
                        )
                    with gr.Tab("manifest.json"):
                        with gr.Row():
                            with gr.Column(scale=5):
                                manifest_code = gr.Code(
                                    label="manifest.json of this run (arrays truncated for display)",
                                    language="json", interactive=False, lines=28,
                                )
                            with gr.Column(scale=4):
                                gr.HTML(content.MANIFEST_FIELDS_HTML)
                    with gr.Tab("Export"):
                        with gr.Row():
                            with gr.Column(scale=5):
                                zip_out = gr.File(label="Download dataset (.zip)", interactive=False)
                                zip_table = gr.Dataframe(
                                    headers=["path", "files", "bytes"],
                                    datatype=["str", "number", "number"],
                                    label="Archive contents of this run", interactive=False,
                                    height=220,
                                )
                                gr.HTML(content.EXPORT_LAYOUT_HTML)
                            with gr.Column(scale=4):
                                gr.Code(
                                    value=REASSEMBLY_SNIPPET, language="python",
                                    label="Reassemble pages from the export (also in README.txt)",
                                    interactive=False,
                                )
                                gr.HTML('<div class="dm-section">Known limitations</div>'
                                        + content.LIMITATIONS_HTML)

        gr.HTML(content.SCOPE_HTML)

        # ---------------- events ----------------
        sample_btn.click(handlers.load_sample, None, pdf_in, show_progress="hidden")
        dpi.change(handlers.dpi_note, dpi, dpi_md, show_progress="hidden")

        run_btn.click(
            handlers.generate,
            inputs=[pdf_in, dpi, n_pieces, noise_strength, noise_scale, lossy, seed, state],
            outputs=[state, src_img, part_img, gallery, zip_out],
            concurrency_limit=config.WORKER_CONCURRENCY,  # heavy job throttle
            api_name="generate",
        ).then(
            handlers.after_generate,
            inputs=state,
            outputs=[summary, page_dd, adj_img, gt_table, page_meta, adj_code,
                     manifest_code, zip_table],
            show_progress="hidden",
        )
        page_dd.input(
            handlers.show_page,
            inputs=[state, page_dd],
            outputs=[src_img, part_img, adj_img, gallery, gt_table, page_meta, adj_code],
            show_progress="minimal",
        )
        clear_btn.click(
            handlers.clear_session,
            inputs=state,
            outputs=[pdf_in, state, src_img, part_img, adj_img, gallery, gt_table,
                     page_meta, adj_code, manifest_code, zip_out, zip_table, summary, page_dd],
            show_progress="hidden",
        )
    return demo