File size: 5,604 Bytes
89dee4a
84e9e84
 
 
 
 
 
 
89dee4a
 
 
 
 
 
 
 
 
 
 
84e9e84
 
89dee4a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e4558d2
89dee4a
 
 
 
 
 
 
 
e4558d2
84e9e84
89dee4a
 
84e9e84
89dee4a
 
 
 
 
 
84e9e84
89dee4a
 
84e9e84
89dee4a
 
 
 
84e9e84
89dee4a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84e9e84
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
"""One styled workspace and a separate gallery around the pinned ZeroGPU runtime."""

from pathlib import Path

import gradio as gr

REPORT_URL = "https://gepingchen.github.io/agentic-tabcf/cigarette-tabpfn35.html"

PRESENTATION_CSS = """
#main-tabs > .tab-nav { margin: 1rem 0; }
#report-version-tabs > .tab-nav { margin-bottom: 1rem; }
#report-version-tabs .tabitem { min-width: 0; }
.demo-report-intro { color: var(--demo-muted); line-height: 1.55; }
.demo-report-frame iframe { display: block; width: 100%; height: 1050px;
  border: 1px solid var(--demo-line); border-radius: .35rem; }
#confirm-report-button.primary { min-height: 3rem; background: var(--demo-accent-deep);
  color: white; font-weight: 750; }
"""


def build_presentation(live_demo: gr.Blocks, report: Path) -> gr.Blocks:
    """Reparent existing controls so their IDs, state and callbacks stay intact."""
    input_tabs = next(
        block for block in live_demo.blocks.values()
        if isinstance(block, gr.Tabs) and block.elem_id == "input-tabs"
    )
    tabs = {child.id: child for child in input_tabs.children if isinstance(child, gr.Tab)}
    csv, historical = tabs["csv"], tabs["saved_example"]
    workspace = next(
        block for block in live_demo.blocks.values() if block.elem_id == "analysis-input"
    )
    input_column = next(child for child in workspace.children if input_tabs in child.children)
    original_csv_children = list(csv.children)
    original_roots = list(live_demo.children)
    policy_blocks = [
        block for block in original_roots[:original_roots.index(workspace)]
        if not (isinstance(block, gr.HTML) and "demo-hero" in str(block.value))
    ]
    for block in live_demo.blocks.values():
        if isinstance(block, gr.Button) and block.value == "Confirm and generate report":
            block.elem_id = "confirm-report-button"
    csv.label = "Analyze your data"
    historical.label = "TabPFN v2 路 Historical"
    historical.id = "historical"
    synthetic = tabs.get("example")
    if synthetic is not None:
        synthetic.visible = False

    with gr.Blocks(
        title="Agentic TabCF", analytics_enabled=False,
        fill_width=True, delete_cache=(300, 900),
    ) as demo:
        # Register every existing component and event before changing only the layout tree.
        live_demo.render()
        hero = gr.HTML(
            '<header class="demo-hero"><h1>Agentic TabCF</h1>'
            '<p class="demo-hero-copy">Upload your data. Describe your question. '
            'Explore treatment effects.</p></header>'
        )
        with gr.Tabs(selected="csv", elem_id="main-tabs") as main_tabs:
            with gr.Tab("Example reports", id="reports"):
                gr.Markdown(
                    "The same cigarette dataset and price question, analyzed in two "
                    "independent model runs. These saved reports do not rerun analysis "
                    "or require sign-in. Differences do not establish model superiority.",
                    elem_classes="demo-report-intro",
                )
                with gr.Tabs(selected="report35", elem_id="report-version-tabs") as report_tabs:
                    with gr.Tab("TabPFN 3.5", id="report35"):
                        gr.Markdown(
                            "### Cigarette sales 路 TabPFN 3.5\n\n"
                            "Saved `v3.5_default` run 路 exploratory point estimates 路 "
                            "`development_only`. State鈥搚ear per-capita sales, not individual "
                            "smoking. No confidence intervals, significance or policy-benefit "
                            "claim.\n\n"
                            f"[Open the full report]({REPORT_URL}) for full-width charts "
                            "and expandable evidence. The downloaded HTML also works offline."
                        )
                        gr.DownloadButton("Download standalone HTML report", value=str(report))
                        gr.HTML(
                            f'<iframe src="{REPORT_URL}" '
                            'title="Saved TabPFN-3.5 cigarette report" '
                            'referrerpolicy="no-referrer"></iframe>',
                            elem_classes="demo-report-frame",
                        )
        with csv:
            with gr.Accordion("Data and model policy", open=False) as policy:
                pass
        with historical:
            historical_note = gr.Markdown(
                "**Historical TabPFN v2 run.** This report comes from a separate saved "
                "analysis of the same data and question. Its results and downloads belong "
                "to that run, not the TabPFN 3.5 report.",
                elem_classes="demo-report-intro",
            )

    # Move the upload and shared results into one pane, without nesting a second app.
    input_column.children = original_csv_children + [
        child for child in input_column.children if child is not input_tabs
    ]
    policy.children = policy_blocks
    csv.children = [workspace, policy]
    main_tabs.children = [csv, main_tabs.children[0]] + ([synthetic] if synthetic else [])
    report_tabs.children.append(historical)
    historical.children.remove(historical_note)
    historical.children.insert(0, historical_note)
    demo.children = [hero, main_tabs] + [
        block for block in original_roots
        if block is not workspace and block not in policy_blocks
        and not (isinstance(block, gr.HTML) and "demo-hero" in str(block.value))
    ]
    return demo.queue(max_size=8, default_concurrency_limit=1)