File size: 3,993 Bytes
86e797b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Save a search as a portable report for reviewing retrieved evidence."""

from datetime import UTC, datetime
from html import escape
from pathlib import Path
from shutil import copyfile

from visual_doc_rag.data import write_json


def export_search(
    query: str,
    method: str,
    source: dict,
    results: list[dict],
    corpus,
    *,
    answer: dict | None = None,
) -> Path:
    created = datetime.now(UTC)
    root = Path("runs/search") / created.strftime("%Y%m%dT%H%M%S.%fZ")
    (root / "images").mkdir(parents=True, exist_ok=False)
    selected = {page["corpus_id"] for page in results}
    markdown = {
        row["corpus_id"]: row["markdown"] or ""
        for row in corpus.remove_columns("image")
        if row["corpus_id"] in selected
    }
    pages, sections = [], []
    for page in results:
        image = f"images/{page['corpus_id']}.png"
        copyfile(page["image_path"], root / image)
        text = markdown[page["corpus_id"]]
        pages.append({**page, "image_path": image, "markdown": text})
        cited = answer is not None and page["corpus_id"] in answer["cited_pages"]
        sections.append(
            f"<section><h2>#{page['rank']} · Page ID {page['corpus_id']}</h2>"
            + ("<p><strong>Cited by the answer</strong></p>" if cited else "")
            + f"<p>Document: {escape(page['source_file_name'])}<br>"
            f"Page in document: {page['page_number_in_doc']}<br>"
            f"License: {escape(page['license'])}</p>"
            f"<p>Source URL: {escape(page['source_url'])}</p>"
            f'<div class="evidence"><a href="{image}"><img src="{image}" '
            f'alt="Retrieved page {page["corpus_id"]}"></a>'
            f"<div><h3>Supplied page text</h3><pre>"
            f"{escape(text or '(No supplied markdown for this page.)')}</pre></div>"
            "</div></section>"
        )
    write_json(
        root / "results.json",
        {
            "query": query,
            "method": method,
            "created_at": created.isoformat(),
            "dataset": source,
            "pages": pages,
            **(
                {"generated_answer": {k: v for k, v in answer.items() if k != "pages"}}
                if answer
                else {}
            ),
        },
    )
    report = root / "report.html"
    answer_html = (
        "<h2>Generated answer</h2>"
        f"<pre>{escape(answer['answer'])}</pre>"
        f"<p>Abstain: {answer['abstain']} · Cited page IDs: {escape(str(answer['cited_pages']))}<br>"
        f"Model: {escape(answer['model'])} · Cached: {answer['cached']}</p>"
        "<p>Answer quality has not been evaluated. Check the cited images.</p>"
        if answer
        else "<p>Retrieved evidence and supplied dataset text. No generated answer or relevance "
        "judgment is included.</p>"
    )
    report.write_text(
        '<!doctype html><html lang="en"><meta charset="utf-8">'
        '<meta name="viewport" content="width=device-width, initial-scale=1">'
        "<title>Retrieval report</title><style>"
        "body{font-family:system-ui,sans-serif;max-width:1400px;margin:32px auto;padding:0 20px}"
        "section{border-top:1px solid #ccc;margin-top:32px;padding-top:16px}"
        ".evidence{display:grid;grid-template-columns:1fr 1fr;gap:24px}"
        "img{width:100%;height:auto}pre{white-space:pre-wrap;overflow-wrap:anywhere}"
        "@media(max-width:800px){.evidence{grid-template-columns:1fr}}"
        "</style><h1>Retrieval report</h1>"
        f"<p><strong>Query:</strong> {escape(query)}</p>"
        f"<p>Method: {escape(method)} · Returned pages: {len(results)}<br>"
        f"Dataset: {escape(source['repo_id'])} · Revision: {escape(source['revision'])}<br>"
        f"Created: {created.isoformat()}</p>"
        + answer_html
        + "<p>Compare each image with the query; supplied text can differ from the image.</p>"
        + "".join(sections)
        + "</html>\n",
        encoding="utf-8",
    )
    return report