File size: 13,838 Bytes
587f33e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
# Copyright 2026 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
Streamlit Visualizer for Pipeline Evolution
Shows the progression of diagrams through Planner β†’ Stylist β†’ Critic stages
"""

import streamlit as st
import json
import base64
from io import BytesIO
from PIL import Image
import os
import sys

# Ensure local imports work
sys.path.append(os.getcwd())

st.set_page_config(layout="wide", page_title="PaperVizAgent Pipeline Evolution", page_icon="🍌")



@st.cache_data
def load_data(path):
    """Read JSON or JSONL data."""
    data = []
    if not os.path.exists(path):
        return []
    try:
        with open(path, "r", encoding="utf-8") as f:
            content = f.read().strip()
            
        # Try to load as JSON array first
        if content.startswith("["):
            try:
                data = json.loads(content)
                if isinstance(data, list):
                    return data
            except json.JSONDecodeError:
                pass
        
        # If that fails, try JSONL format
        lines = content.split("\n")
        for line in lines:
            line = line.strip()
            if not line:
                continue
            try:
                data.append(json.loads(line))
            except json.JSONDecodeError:
                continue
                
    except Exception as e:
        st.error(f"Error reading file: {e}")
        return []
    return data

def base64_to_image(b64_str):
    if not b64_str:
        return None
    try:
        if "," in b64_str:
            b64_str = b64_str.split(",")[1]
        image_data = base64.b64decode(b64_str)
        return Image.open(BytesIO(image_data))
    except Exception:
        return None

def detect_task_type(item):
    """Detect whether data is for diagram or plot task."""
    # Check for plot-specific fields
    if "target_plot_desc0" in item or "target_plot_stylist_desc0" in item:
        return "plot"
    return "diagram"

def display_stage_comparison(item):
    """Display 2x2 grid comparison: Ground Truth + three pipeline stages."""
    st.markdown("### πŸ“Š Pipeline Evolution Comparison")
    
    task_type = detect_task_type(item)
    prefix = "target_plot" if task_type == "plot" else "target_diagram"
    
    # Create two rows with two columns each
    row1_col1, row1_col2 = st.columns(2)
    row2_col1, row2_col2 = st.columns(2)
    
    # Detect available stages dynamically
    available_stages = []
    
    # Human (Ground Truth) - always first
    available_stages.append({
        "title": "🎯 Human (Ground Truth)",
        "desc_key": None,
        "img_key": "annotation_info",
        "color": "orange",
        "is_human": True
    })
    
    # Planner / Vanilla
    planner_key = f"{prefix}_desc0"
    if planner_key in item:
        available_stages.append({
            "title": "πŸ“ Planner / Vanilla",
            "desc_key": planner_key,
            "img_key": f"{planner_key}_base64_jpg",
            "color": "blue",
            "is_human": False
        })
    
    # Stylist
    stylist_key = f"{prefix}_stylist_desc0"
    if stylist_key in item:
        available_stages.append({
            "title": "✨ Stylist",
            "desc_key": stylist_key,
            "img_key": f"{stylist_key}_base64_jpg",
            "color": "violet",
            "is_human": False
        })
    
    # Critic rounds (0, 1, 2)
    for round_idx in range(3):
        critic_desc_key = f"{prefix}_critic_desc{round_idx}"
        if critic_desc_key in item:
            emoji = ["πŸ”", "πŸ”πŸ”", "πŸ”πŸ”πŸ”"][round_idx]
            available_stages.append({
                "title": f"{emoji} Critic Round {round_idx}",
                "desc_key": critic_desc_key,
                "img_key": f"{critic_desc_key}_base64_jpg",
                "suggestions_key": f"{prefix}_critic_suggestions{round_idx}",
                "color": "green",
                "is_human": False,
                "round_idx": round_idx
            })
            
    # Create dynamic grid based on number of stages
    num_stages = len(available_stages)
    cols_per_row = 2
    stages = available_stages
    
    # Display stages in a grid
    for row_start in range(0, num_stages, cols_per_row):
        cols = st.columns(cols_per_row)
        for col_idx in range(cols_per_row):
            stage_idx = row_start + col_idx
            if stage_idx >= num_stages:
                break
            
            stage = stages[stage_idx]
            with cols[col_idx]:
                st.markdown(f"**{stage['title']}**")
                
                # Display image
                if stage["is_human"]:
                    # Handle Human (Ground Truth) image
                    human_path = item.get("path_to_gt_image")
                    if human_path and os.path.exists(human_path):
                        try:
                            img = Image.open(human_path)
                            st.image(img, use_container_width=True)
                        except Exception as e:
                            st.error(f"Failed to load Human image: {e}")
                    else:
                        st.info("No Human image available")
                    
                    # Show caption instead of description
                    caption = item.get("brief_desc", "No caption available")
                    with st.expander("View Caption", expanded=False):
                        st.write(caption)
                else:
                    # Handle pipeline stage images
                    img_b64 = item.get(stage["img_key"])
                    if img_b64:
                        img = base64_to_image(img_b64)
                        if img:
                            st.image(img, use_container_width=True)
                        else:
                            st.error("Failed to decode image")
                    else:
                        st.info("No image available")
                    
                    # Display description in expander
                    desc = item.get(stage["desc_key"], "No description available")
                    with st.expander("View Description", expanded=False):
                        if task_type == "plot" and desc:
                             # Try to format as code if it looks like code, or just text
                             st.code(desc, language="python") # Plots are usually python code
                        else:
                             st.write(desc)
                    
                    # Display critic suggestions if this is a critic stage
                    if "suggestions_key" in stage:
                        suggestions = item.get(stage["suggestions_key"], "")
                        if suggestions and suggestions.strip() != "No changes needed.":
                            with st.expander("πŸ’¬ Critic Suggestions", expanded=False):
                                st.write(suggestions)

def display_critique(item):
    """Display the critique if available."""
    if "critique0" in item and item["critique0"]:
        st.markdown("### πŸ’¬ Critic's Feedback")
        with st.expander("View Critique", expanded=False):
            st.write(item["critique0"])

def display_evaluation_results(item):
    """Display evaluation results if available."""
    dimensions = ["Faithfulness", "Conciseness", "Readability", "Aesthetics", "Overall"]
    
    has_eval = any(f"{dim.lower()}_outcome" in item for dim in dimensions)
    
    if has_eval:
        st.markdown("### πŸ“ˆ Evaluation Results")
        cols = st.columns(len(dimensions))
        
        for i, dim in enumerate(dimensions):
            outcome_key = f"{dim.lower()}_outcome"
            reasoning_key = f"{dim.lower()}_reasoning"
            outcome = item.get(outcome_key, "N/A")
            reasoning = item.get(reasoning_key, "N/A")
            
            with cols[i]:
                st.markdown(f"**{dim}**")
                if outcome == "Model":
                    st.success(outcome)
                elif outcome == "Human":
                    st.info(outcome)
                elif outcome == "Tie":
                    st.warning(outcome)
                else:
                    st.text(outcome)
                
                with st.expander("View Reasoning", expanded=False):
                    st.write(reasoning)

def main():
    st.sidebar.title("🍌 Pipeline Evolution Viewer")
    file_path = st.sidebar.text_input("Results JSONL Path", placeholder="Enter path to results file...")
    
    if st.sidebar.button("πŸ”„ Refresh Data"):
        load_data.clear()
        st.rerun()
    
    if not file_path:
        st.info("πŸ‘† Please enter a file path to begin")
        st.stop()
    
    if not os.path.exists(file_path):
        st.error(f"File not found: {file_path}")
        st.stop()
    
    data = load_data(file_path)
    
    # --- Search Functionality ---
    search_query = st.sidebar.text_input("πŸ” Search ID", value="", help="Filter by ID (case-insensitive)")
    if search_query:
        data = [item for item in data if search_query.lower() in item.get("id", "").lower()]
        st.sidebar.caption(f"Found {len(data)} matching cases")
    
    total_items = len(data)
    
    if total_items == 0:
        if search_query:
            st.warning(f"No samples found matching '{search_query}'.")
        else:
            st.warning("Data is empty or format is incorrect.")
        return
    
    st.title("🍌 PaperVizAgent Pipeline Evolution Viewer")
    st.markdown(f"Visualizing the progression through **Planner β†’ Stylist β†’ Critic** stages")
    
    st.divider()
    
    # --- Global Statistics ---
    with st.expander("πŸ“Š Global Statistics", expanded=False):
        total = len(data)
        
        # Simple heuristic: inspect the first item to guess task type for stats
        # (This assumes the file is consistent)
        sample = data[0] if data else {}
        is_plot = "target_plot_desc0" in sample or "target_plot_stylist_desc0" in sample
        
        if is_plot:
            has_all_stages = sum(1 for item in data if 
                item.get("target_plot_desc0") and 
                item.get("target_plot_stylist_desc0") and 
                item.get("target_plot_critic_desc0"))
        else:
            has_all_stages = sum(1 for item in data if 
                item.get("target_diagram_desc0") and 
                item.get("target_diagram_stylist_desc0") and 
                item.get("target_diagram_critic_desc0"))
        
        col1, col2, col3 = st.columns(3)
        col1.metric("Total Samples", total)
        col2.metric("Complete Pipeline", has_all_stages)
        col3.metric("Completion Rate", f"{has_all_stages/total*100:.1f}%")
    
    st.divider()
    
    # --- Pagination ---
    PAGE_SIZE = 10  # Changed from 5 to 10
    if "page" not in st.session_state:
        st.session_state.page = 0
    
    total_pages = max((total_items + PAGE_SIZE - 1) // PAGE_SIZE, 1)
    
    # Navigation buttons
    col_left, col_center, col_right = st.columns([1, 2, 1])
    
    with col_left:
        if st.button("⬅️ Previous Page", disabled=(st.session_state.page == 0)):
            st.session_state.page -= 1
            st.rerun()
    
    with col_center:
        page_input = st.number_input(
            "Page", 
            min_value=1, 
            max_value=total_pages, 
            value=st.session_state.page + 1,
            label_visibility="collapsed"
        )
        if page_input != st.session_state.page + 1:
            st.session_state.page = page_input - 1
            st.rerun()
        st.caption(f"Page {st.session_state.page + 1} of {total_pages}")
    
    with col_right:
        if st.button("Next Page ➑️", disabled=(st.session_state.page >= total_pages - 1)):
            st.session_state.page += 1
            st.rerun()
    
    start_idx = st.session_state.page * PAGE_SIZE
    end_idx = min(start_idx + PAGE_SIZE, total_items)
    batch = data[start_idx:end_idx]
    
    st.markdown(f"**Displaying {start_idx + 1} - {end_idx} of {total_items}**")
    
    # --- Display Samples ---
    for i, item in enumerate(batch):
        idx = start_idx + i
        anno = item  # Flattened structure
        
        with st.container(border=True):
            # Header
            st.subheader(f"#{idx + 1}: {item.get('visual_intent', 'N/A')}")
            st.caption(f"ID: `{item.get('id', 'Unknown')}`")
            
            # Method/Data section
            task_type = detect_task_type(item)
            label = "πŸ“š Raw Data" if task_type == "plot" else "πŸ“š Method Section"
            
            with st.expander(label, expanded=False):
                if task_type == "plot":
                    st.code(json.dumps(item.get('content', {}), indent=2), language="json")
                else:
                    method_content = item.get('content', 'N/A')
                    st.markdown(method_content)
            
            # Pipeline comparison
            display_stage_comparison(item)
            
            # Critique
            display_critique(item)
            
            # Evaluation results
            display_evaluation_results(item)
            
            st.divider()

if __name__ == "__main__":
    main()