File size: 12,555 Bytes
23cdeed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
from __future__ import annotations

from dataclasses import dataclass
from pathlib import Path
from xml.sax.saxutils import escape

import fitz
from pypdf import PdfReader
from reportlab.lib import colors
from reportlab.lib.pagesizes import A4
from reportlab.lib.styles import ParagraphStyle
from reportlab.pdfgen import canvas
from reportlab.platypus import Paragraph


ROOT = Path(__file__).resolve().parents[1]
OUTPUT_DIR = ROOT / "output" / "pdf"
TMP_DIR = ROOT / "tmp" / "pdfs"
PDF_PATH = OUTPUT_DIR / "pluto_app_summary_one_page.pdf"
PNG_PATH = TMP_DIR / "pluto_app_summary_one_page-1.png"

PAGE_WIDTH, PAGE_HEIGHT = A4
MARGIN_X = 34
MARGIN_TOP = 34
MARGIN_BOTTOM = 28
GUTTER = 16
COLUMN_WIDTH = (PAGE_WIDTH - (2 * MARGIN_X) - GUTTER) / 2

NAVY = colors.HexColor("#17324D")
TEAL = colors.HexColor("#2A7F8C")
INK = colors.HexColor("#1D2430")
MUTED = colors.HexColor("#5C6773")
CARD_BG = colors.HexColor("#F5F8FB")
CARD_BORDER = colors.HexColor("#D6E1EA")
ACCENT_BG = colors.HexColor("#E8F4F4")
WHITE = colors.white


@dataclass
class SectionBlock:
    title: str
    items: list[tuple[str, str]]


def build_blocks() -> tuple[list[SectionBlock], list[SectionBlock]]:
    left = [
        SectionBlock(
            title="What it is",
            items=[
                (
                    "body",
                    "Pluto is an AI-powered document extraction and question-answering app. "
                    "It lets a user upload documents into a corpus, run a multi-stage pipeline, "
                    "and inspect the answer with evidence, trace, and confidence signals.",
                ),
            ],
        ),
        SectionBlock(
            title="Who it's for",
            items=[
                ("body", "Primary user/persona: Not found in repo."),
                (
                    "body",
                    "Closest repo evidence: a person asking research-style questions "
                    "over uploaded documents and reviewing evidence-backed results.",
                ),
            ],
        ),
        SectionBlock(
            title="What it does",
            items=[
                ("bullet", "Uploads PDF, DOCX/DOC, TXT, and Markdown files into a corpus."),
                ("bullet", "Converts uploads to Markdown, chunks them, classifies them, and tracks readiness."),
                ("bullet", "Runs a 4-stage pipeline: route, extract, merge, evidence_check."),
                ("bullet", "Streams live progress and upload status to the dashboard."),
                ("bullet", "Queries the full corpus or selected ready documents."),
                ("bullet", "Shows final sections, evidence, trace, confidence, and a benchmark view."),
            ],
        ),
    ]

    right = [
        SectionBlock(
            title="How it works",
            items=[
                (
                    "bullet",
                    "Frontend: `frontend/index.html` + `app.js` call `/api/upload`, `/api/corpus`, "
                    "`/api/run`, `/api/stream`, and `/api/compare`.",
                ),
                (
                    "bullet",
                    "Server: `pluto/server.py` serves the UI, handles uploads, streams SSE progress, "
                    "and runs `PipelineRunner` in a worker thread.",
                ),
                (
                    "bullet",
                    "Ingest path: uploaded file -> Markdown in `corpus/` -> chunk split/classification "
                    "-> `DocIndex` registration; background Phase A stores overview/status in "
                    "`corpus/.doc_index.json`.",
                ),
                (
                    "bullet",
                    "Query path: selected docs -> S0 route -> S1 extract -> S2 merge -> S3 evidence_check "
                    "-> JSON result + cache stats -> UI panels; final JSON also writes to "
                    "`output/final_output.json`.",
                ),
                (
                    "bullet",
                    "Support layers: `ExtractionCache` reuses extractions; `CorpusTools` reads/searches chunks. "
                    "NVIDIA embedding/rerank code paths exist, and chunking falls back when NVIDIA keys are absent.",
                ),
            ],
        ),
        SectionBlock(
            title="How to run",
            items=[
                ("bullet", "From `mp1/`: `pip install -r requirements.txt`"),
                (
                    "bullet",
                    "Create `.env` and set `GROQ_API_KEY` (explicitly named in `README.md`).",
                ),
                ("bullet", "Run `python main.py --serve`"),
                ("bullet", "Open `http://localhost:8000`"),
                (
                    "bullet",
                    "Upload docs, wait for Understanding to finish, then submit a query. "
                    "Other required provider keys: Not found in repo.",
                ),
            ],
        ),
    ]
    return left, right


def make_styles(scale: float) -> dict[str, ParagraphStyle]:
    return {
        "title": ParagraphStyle(
            "title",
            fontName="Helvetica-Bold",
            fontSize=21 * scale,
            leading=25 * scale,
            textColor=WHITE,
            spaceAfter=0,
        ),
        "subtitle": ParagraphStyle(
            "subtitle",
            fontName="Helvetica",
            fontSize=9.6 * scale,
            leading=12 * scale,
            textColor=colors.HexColor("#DCE7F3"),
        ),
        "eyebrow": ParagraphStyle(
            "eyebrow",
            fontName="Helvetica-Bold",
            fontSize=7.4 * scale,
            leading=9 * scale,
            textColor=colors.HexColor("#B9D6DA"),
        ),
        "section_title": ParagraphStyle(
            "section_title",
            fontName="Helvetica-Bold",
            fontSize=10.6 * scale,
            leading=12.5 * scale,
            textColor=NAVY,
        ),
        "body": ParagraphStyle(
            "body",
            fontName="Helvetica",
            fontSize=8.6 * scale,
            leading=11 * scale,
            textColor=INK,
        ),
        "bullet": ParagraphStyle(
            "bullet",
            fontName="Helvetica",
            fontSize=8.5 * scale,
            leading=10.7 * scale,
            textColor=INK,
            leftIndent=10 * scale,
            firstLineIndent=-7 * scale,
        ),
        "footer": ParagraphStyle(
            "footer",
            fontName="Helvetica",
            fontSize=7.1 * scale,
            leading=8.5 * scale,
            textColor=MUTED,
        ),
    }


def escape_inline(text: str) -> str:
    escaped = escape(text)
    return escaped.replace("`", "<font name='Courier'>").replace("</font><font name='Courier'>", "")


def format_text(text: str) -> str:
    parts = text.split("`")
    if len(parts) == 1:
        return escape(text)

    result: list[str] = []
    code = False
    for part in parts:
        if code:
            result.append(f"<font name='Courier'>{escape(part)}</font>")
        else:
            result.append(escape(part))
        code = not code
    return "".join(result)


def paragraph_for(kind: str, text: str, styles: dict[str, ParagraphStyle]) -> Paragraph:
    style_name = "bullet" if kind == "bullet" else "body"
    content = f"- {text}" if kind == "bullet" else text
    return Paragraph(format_text(content), styles[style_name])


def measure_section(block: SectionBlock, styles: dict[str, ParagraphStyle], width: float) -> tuple[float, list[Paragraph]]:
    title = Paragraph(format_text(block.title), styles["section_title"])
    rendered_items = [paragraph_for(kind, text, styles) for kind, text in block.items]

    title_height = title.wrap(width - 20, 1000)[1]
    items_height = 0.0
    for para in rendered_items:
        items_height += para.wrap(width - 20, 1000)[1]
        items_height += 5

    total = 14 + title_height + 8 + items_height + 10
    return total, [title, *rendered_items]


def choose_scale(left: list[SectionBlock], right: list[SectionBlock]) -> tuple[float, dict[str, ParagraphStyle], float]:
    header_space = 114
    footer_space = 18
    available = PAGE_HEIGHT - MARGIN_TOP - MARGIN_BOTTOM - header_space - footer_space

    for scale in (1.0, 0.97, 0.94, 0.91, 0.88, 0.85):
        styles = make_styles(scale)
        left_height = total_column_height(left, styles)
        right_height = total_column_height(right, styles)
        if max(left_height, right_height) <= available:
            return scale, styles, available

    raise RuntimeError("Content did not fit on a single page.")


def total_column_height(blocks: list[SectionBlock], styles: dict[str, ParagraphStyle]) -> float:
    total = 0.0
    for index, block in enumerate(blocks):
        section_height, _ = measure_section(block, styles, COLUMN_WIDTH)
        total += section_height
        if index < len(blocks) - 1:
            total += 10
    return total


def draw_header(pdf: canvas.Canvas, styles: dict[str, ParagraphStyle]) -> float:
    header_height = 94
    header_y = PAGE_HEIGHT - MARGIN_TOP - header_height

    pdf.setFillColor(NAVY)
    pdf.roundRect(MARGIN_X, header_y, PAGE_WIDTH - (2 * MARGIN_X), header_height, 14, stroke=0, fill=1)
    pdf.setFillColor(TEAL)
    pdf.roundRect(PAGE_WIDTH - MARGIN_X - 110, header_y, 110, header_height, 14, stroke=0, fill=1)

    eyebrow = Paragraph("ONE-PAGE APP SUMMARY", styles["eyebrow"])
    title = Paragraph("Pluto", styles["title"])
    subtitle = Paragraph(
        "Repo-backed overview of the document extraction and question-answering dashboard.",
        styles["subtitle"],
    )

    x = MARGIN_X + 18
    y = PAGE_HEIGHT - MARGIN_TOP - 16

    for para, width in ((eyebrow, 210), (title, 260), (subtitle, PAGE_WIDTH - (2 * MARGIN_X) - 150)):
        _, height = para.wrap(width, 1000)
        para.drawOn(pdf, x, y - height)
        y -= height + 4

    note = Paragraph("Evidence source: README + app server, pipeline, ingest, index, and UI files.", styles["subtitle"])
    note_width = 92
    _, note_height = note.wrap(note_width, 1000)
    note.drawOn(pdf, PAGE_WIDTH - MARGIN_X - 98, header_y + header_height - 18 - note_height)

    return header_y - 12


def draw_column(
    pdf: canvas.Canvas,
    blocks: list[SectionBlock],
    x: float,
    top_y: float,
    styles: dict[str, ParagraphStyle],
) -> None:
    y = top_y
    for block in blocks:
        section_height, items = measure_section(block, styles, COLUMN_WIDTH)

        pdf.setFillColor(CARD_BG if block.title != "How to run" else ACCENT_BG)
        pdf.setStrokeColor(CARD_BORDER)
        pdf.roundRect(x, y - section_height, COLUMN_WIDTH, section_height, 12, stroke=1, fill=1)

        cursor = y - 14
        title = items[0]
        _, title_height = title.wrap(COLUMN_WIDTH - 20, 1000)
        title.drawOn(pdf, x + 10, cursor - title_height)
        cursor -= title_height + 8

        for para in items[1:]:
            _, para_height = para.wrap(COLUMN_WIDTH - 20, 1000)
            para.drawOn(pdf, x + 10, cursor - para_height)
            cursor -= para_height + 5

        y -= section_height + 10


def build_pdf() -> None:
    OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
    TMP_DIR.mkdir(parents=True, exist_ok=True)

    left, right = build_blocks()
    _, styles, _ = choose_scale(left, right)

    pdf = canvas.Canvas(str(PDF_PATH), pagesize=A4)
    pdf.setTitle("Pluto App Summary")
    pdf.setAuthor("OpenAI Codex")
    pdf.setSubject("One-page summary generated from repository evidence")

    top_y = draw_header(pdf, styles)
    draw_column(pdf, left, MARGIN_X, top_y, styles)
    draw_column(pdf, right, MARGIN_X + COLUMN_WIDTH + GUTTER, top_y, styles)

    footer = Paragraph(
        "Not found in repo items are labeled explicitly. Output generated as a single-page PDF.",
        styles["footer"],
    )
    _, footer_height = footer.wrap(PAGE_WIDTH - (2 * MARGIN_X), 1000)
    footer.drawOn(pdf, MARGIN_X, MARGIN_BOTTOM - 4)

    pdf.showPage()
    pdf.save()


def validate_outputs() -> None:
    reader = PdfReader(str(PDF_PATH))
    if len(reader.pages) != 1:
        raise RuntimeError(f"Expected 1 page, found {len(reader.pages)}")

    document = fitz.open(PDF_PATH)
    page = document.load_page(0)
    pix = page.get_pixmap(matrix=fitz.Matrix(2.0, 2.0), alpha=False)
    pix.save(PNG_PATH)
    document.close()


def main() -> None:
    build_pdf()
    validate_outputs()
    print(f"PDF_PATH={PDF_PATH}")
    print(f"PNG_PATH={PNG_PATH}")


if __name__ == "__main__":
    main()