File size: 13,436 Bytes
572d3da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa7332a
572d3da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a338937
572d3da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
PaperBanana β€” Gradio app for HuggingFace Spaces.

Turns methodology text into publication-ready architecture diagrams
using a 5-agent pipeline (Retriever β†’ Planner β†’ Stylist β†’ Visualizer β†’ Critic).
"""

import os
import json
import tempfile
import mimetypes
from pathlib import Path
from typing import List, Dict, Any, Optional

import gradio as gr
from google import genai
from google.genai import types

from agents import RetrieverAgent, PlannerAgent, StylistAgent, VisualizerAgent, CriticAgent
from aesthetic_guidelines import AESTHETIC_GUIDELINE
import config

# ── Load reference set at startup ───────────────────────────────────────────
REF_SET_PATH = Path("data/spotlight_reference_set.json")
REFERENCE_SET: List[Dict[str, Any]] = []
if REF_SET_PATH.exists():
    with open(REF_SET_PATH) as f:
        REFERENCE_SET = json.load(f)
    print(f"Loaded {len(REFERENCE_SET)} reference examples")

# ── Example gallery images ──────────────────────────────────────────────────
EXAMPLE_IMAGES = {
    "Transformer": "examples/readme/transformer_iter3_0.jpg",
    "ResNet": "examples/readme/resnet_iter3_0.jpg",
    "DDPM": "examples/readme/ddpm_iter3_0.jpg",
}

# ── Preset examples ─────────────────────────────────────────────────────────
PRESET_EXAMPLES = [
    [
        # Transformer
        """The Transformer model follows an encoder-decoder structure using stacked self-attention and fully connected layers.

Encoder: Stack of N=6 identical layers. Each layer has two sub-layers: (1) multi-head self-attention, and (2) position-wise feed-forward network. Residual connections around each sub-layer, followed by layer normalization.

Decoder: Stack of N=6 identical layers. In addition to the two encoder sub-layers, the decoder inserts a third sub-layer for multi-head cross-attention over the encoder output. Masked self-attention prevents attending to subsequent positions.

Multi-Head Attention: Linearly project queries, keys, values h times, perform scaled dot-product attention in parallel, concatenate and project again.

Positional Encoding: Sinusoidal positional encodings added to input embeddings.""",
        "The Transformer β€” model architecture (Vaswani et al., 2017)",
        2,
    ],
    [
        # ResNet
        """We present a residual learning framework. Instead of learning H(x) directly, layers fit a residual mapping F(x) = H(x) - x. The building block is y = F(x, {W_i}) + x via identity shortcut connections.

Architecture: Input 224Γ—224 β†’ 7Γ—7 conv, 64, stride 2 β†’ BN β†’ ReLU β†’ 3Γ—3 max pool β†’ Stage 1: 3 blocks, 64 filters β†’ Stage 2: 4 blocks, 128 filters β†’ Stage 3: 6 blocks, 256 filters β†’ Stage 4: 3 blocks, 512 filters β†’ Global avg pool β†’ 1000-d FC β†’ softmax.

For deeper networks (50/101/152), bottleneck blocks: 1Γ—1 conv (reduce) β†’ 3Γ—3 conv β†’ 1Γ—1 conv (restore), with shortcut bypassing all three layers.""",
        "Architecture of ResNet with residual learning building blocks (He et al., 2016)",
        2,
    ],
    [
        # DDPM
        """Denoising diffusion probabilistic models (DDPMs): Forward process gradually adds Gaussian noise over T timesteps: q(x_t|x_{t-1}) = N(x_t; √(1-Ξ²_t)x_{t-1}, Ξ²_tI). After T steps, x_T β‰ˆ N(0,I).

Reverse process learns to denoise: p_ΞΈ(x_{t-1}|x_t) = N(x_{t-1}; ΞΌ_ΞΈ(x_t,t), Ξ£_ΞΈ(x_t,t)). Starting from x_T ~ N(0,I), iteratively produces clean x_0.

Denoising network Ξ΅_ΞΈ(x_t,t) is a U-Net: downsampling with ResNet blocks + self-attention at 16Γ—16, bottleneck with self-attention, upsampling with skip connections. Timestep conditioning via sinusoidal embeddings. Training minimizes L = E[||Ξ΅ - Ξ΅_ΞΈ(x_t,t)||Β²].""",
        "Overview of the denoising diffusion probabilistic model (Ho et al., 2020)",
        2,
    ],
]


# ── Core generation logic (streaming-friendly) ─────────────────────────────
def generate_diagram(
    methodology_text: str,
    caption: str,
    num_iterations: int,
    api_key: str | None = None,
    progress=gr.Progress(track_tqdm=True),
):
    """Run the full PaperBanana pipeline and yield intermediate results."""

    # Resolve API key: user input > env var
    gemini_key = (api_key or "").strip() or config.GEMINI_API_KEY
    if not gemini_key:
        raise gr.Error(
            "No Gemini API key found. Paste one in the field above, "
            "or set GEMINI_API_KEY as a Space secret."
        )

    # Patch config so all agents pick it up
    config.GEMINI_API_KEY = gemini_key

    num_iterations = int(num_iterations)
    logs: list[str] = []

    def log(msg: str):
        logs.append(msg)
        return "\n".join(logs)

    # ── 1. Retriever ────────────────────────────────────────────────────────
    yield None, log("πŸ” [1/5] Retriever: finding relevant references…")
    retriever = RetrieverAgent(REFERENCE_SET)
    reference_examples = []
    if REFERENCE_SET:
        reference_examples = retriever.retrieve(
            methodology_text, caption, n=config.NUM_REFERENCE_EXAMPLES
        )
        yield None, log(f"   βœ“ Retrieved {len(reference_examples)} references")
    else:
        yield None, log("   ⏭ Skipped (no reference set loaded)")

    # ── 2. Planner ──────────────────────────────────────────────────────────
    yield None, log("πŸ“ [2/5] Planner: creating visual description…")
    planner = PlannerAgent()
    current_description = planner.plan(methodology_text, caption, reference_examples)
    yield None, log(f"   βœ“ Description ready ({len(current_description)} chars)")

    # ── 3. Stylist ──────────────────────────────────────────────────────────
    yield None, log("🎨 [3/5] Stylist: applying aesthetic guidelines…")
    stylist = StylistAgent()
    current_description = stylist.refine(current_description)
    yield None, log(f"   βœ“ Styled ({len(current_description)} chars)")

    # ── 4/5. Visualize β†’ Critique loop ──────────────────────────────────────
    latest_image_path = None
    critic = CriticAgent()

    for i in range(1, num_iterations + 1):
        yield latest_image_path, log(
            f"πŸ–ΌοΈ [4/5] Visualizer: generating image (iteration {i}/{num_iterations})…"
        )

        with tempfile.TemporaryDirectory() as tmpdir:
            out_base = os.path.join(tmpdir, f"iter{i}")
            visualizer = VisualizerAgent(mode="diagram")
            img_path = visualizer.visualize(current_description, out_base)

            if img_path and os.path.exists(img_path):
                # Copy to a persistent temp file so Gradio can serve it
                import shutil

                ext = Path(img_path).suffix or ".jpg"
                persist = tempfile.NamedTemporaryFile(
                    suffix=ext, delete=False, dir=tempfile.gettempdir()
                )
                shutil.copy2(img_path, persist.name)
                latest_image_path = persist.name

        yield latest_image_path, log(f"   βœ“ Image generated (iteration {i})")

        # Skip critique on last iteration
        if i >= num_iterations:
            break

        yield latest_image_path, log(
            f"πŸ”¬ [5/5] Critic: evaluating (iteration {i})…"
        )
        critique = critic.critique(
            methodology_text, caption, current_description, latest_image_path, i
        )
        n_issues = len(critique["issues"])
        yield latest_image_path, log(f"   βœ“ {n_issues} issues found")

        if not critique["should_continue"]:
            yield latest_image_path, log("   βœ“ Quality threshold reached β€” done!")
            break

        # Refine
        yield latest_image_path, log("πŸ“ [2/5] Planner: refining description…")
        refinement_prompt = critic.generate_refinement_prompt(
            current_description, critique
        )
        client = genai.Client(api_key=gemini_key)
        contents = [
            types.Content(
                role="user",
                parts=[types.Part.from_text(text=refinement_prompt)],
            )
        ]
        refined = ""
        for chunk in client.models.generate_content_stream(
            model=config.VLM_MODEL,
            contents=contents,
            config=types.GenerateContentConfig(
                thinking_config=types.ThinkingConfig(thinking_level="HIGH")
            ),
        ):
            refined += chunk.text
        current_description = refined.strip()
        yield latest_image_path, log(
            f"   βœ“ Refined ({len(current_description)} chars)"
        )

        # Re-style
        yield latest_image_path, log("🎨 [3/5] Stylist: re-applying style…")
        current_description = stylist.refine(current_description)
        yield latest_image_path, log(f"   βœ“ Styled ({len(current_description)} chars)")

    yield latest_image_path, log("\nβœ… Generation complete!")


# ── Gradio UI ───────────────────────────────────────────────────────────────
DESCRIPTION_MD = """\
# 🍌 PaperBanana

**Turn methodology text into publication-ready architecture diagrams.**

Paste your paper's methodology section + a caption, and PaperBanana's 5-agent pipeline
(Retriever β†’ Planner β†’ Stylist β†’ Visualizer β†’ Critic) will generate a diagram for you.

> Based on [*PaperBanana: Automating Academic Illustration for AI Scientists*](https://arxiv.org/abs/2601.23265) (Zhu et al.).
"""

with gr.Blocks(
    title="PaperBanana",
    theme=gr.themes.Soft(primary_hue="amber", secondary_hue="blue"),
    css="footer { display: none !important; }",
) as demo:
    gr.Markdown(DESCRIPTION_MD)

    # ── Example gallery ─────────────────────────────────────────────────────
    with gr.Accordion("πŸ“Έ Example outputs (click to expand)", open=False):
        existing = {k: v for k, v in EXAMPLE_IMAGES.items() if Path(v).exists()}
        if existing:
            with gr.Row():
                for name, path in existing.items():
                    with gr.Column(min_width=200):
                        gr.Image(value=path, label=name)

    # ── Inputs ──────────────────────────────────────────────────────────────
    with gr.Row():
        with gr.Column(scale=1):
            methodology_input = gr.Textbox(
                label="Methodology text",
                placeholder="Paste your methodology / model description here…",
                lines=12,
            )
            caption_input = gr.Textbox(
                label="Diagram caption",
                placeholder='e.g. "Architecture of our proposed method"',
                lines=2,
            )
            iterations_slider = gr.Slider(
                minimum=1,
                maximum=3,
                value=2,
                step=1,
                label="Refinement iterations",
                info="More iterations = better quality, slower",
            )
            api_key_input = gr.Textbox(
                label="Gemini API key",
                type="password",
                placeholder="AIza…",
            )
            generate_btn = gr.Button("🍌 Generate diagram", variant="primary", size="lg")

        # ── Outputs ─────────────────────────────────────────────────────────
        with gr.Column(scale=1):
            output_image = gr.Image(label="Generated diagram", type="filepath")
            output_log = gr.Textbox(label="Pipeline log", lines=18, interactive=False)

    # ── Examples table ──────────────────────────────────────────────────────
    gr.Examples(
        examples=PRESET_EXAMPLES,
        inputs=[methodology_input, caption_input, iterations_slider],
        label="Try a classic paper",
    )

    # ── Wiring ──────────────────────────────────────────────────────────────
    generate_btn.click(
        fn=generate_diagram,
        inputs=[methodology_input, caption_input, iterations_slider, api_key_input],
        outputs=[output_image, output_log],
    )

if __name__ == "__main__":
    demo.queue().launch(server_name="0.0.0.0", server_port=7860)