File size: 7,710 Bytes
82a3b34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""

Report Export Component



Gradio component for exporting evaluation reports.

"""

import json
import logging
from typing import Any, Dict, List, Optional, Tuple

import gradio as gr

from dashboard.schemas import ExportFormat, RunSummary
from dashboard.utils import export_report, log_dashboard_event

logger = logging.getLogger(__name__)


def create_export_panel() -> Tuple[gr.Dropdown, gr.Checkbox, gr.Button, gr.JSON]:
    """

    Create export panel component.

    

    Returns:

        Tuple of (format_dropdown, include_config_checkbox, export_button, output_json) components

    """
    
    # Format selector
    format_dropdown = gr.Dropdown(
        label="Export Format",
        choices=["json", "csv"],
        value="json",
        interactive=True,
    )
    
    # Options
    include_config_checkbox = gr.Checkbox(
        label="Include Configuration",
        value=True,
        interactive=True,
    )
    
    # Export button
    export_button = gr.Button(
        "Export Report",
        variant="primary",
    )
    
    # Output
    output_json = gr.JSON(
        label="Export Output",
        visible=True,
    )
    
    return format_dropdown, include_config_checkbox, export_button, output_json


def handle_export(

    run_summary: Optional[RunSummary],

    format: str,

    include_config: bool,

) -> Tuple[str, str]:
    """

    Handle export button click.

    

    Args:

        run_summary: Run summary data

        format: Export format (json or csv)

        include_config: Include configuration in export

        

    Returns:

        Tuple of (output, filename)

    """
    if run_summary is None:
        return "Error: No run data available", "error.txt"
    
    try:
        export_format = ExportFormat.JSON if format == "json" else ExportFormat.CSV
        
        output = export_report(
            run_summary,
            format=export_format,
            include_config=include_config,
            include_raw_outputs=False,
        )
        
        # Log event
        log_dashboard_event(
            "DASHBOARD_EXPORT_REPORT",
            run_id=str(run_summary.metadata.run_id),
            extra={"format": format},
        )
        
        # Generate filename
        filename = f"report_{run_summary.metadata.run_id}.{format}"
        
        return output, filename
        
    except Exception as e:
        logger.error(f"Error exporting report: {e}")
        return f"Error: {str(e)}", "error.txt"


def create_export_section() -> Tuple[gr.Button, gr.DownloadButton]:
    """

    Create export section with buttons.

    

    Returns:

        Tuple of (json_button, csv_button) components

    """
    
    json_button = gr.DownloadButton(
        label="Download JSON Report",
        value=None,
        interactive=False,
    )
    
    csv_button = gr.DownloadButton(
        label="Download CSV Report",
        value=None,
        interactive=False,
    )
    
    return json_button, csv_button


class ReportExporter:
    """

    Report exporter component with state management.

    """
    
    def __init__(self):
        """Initialize report exporter."""
        self._current_summary: Optional[RunSummary] = None
        self._export_cache: Dict[str, str] = {}
    
    def set_summary(self, summary: RunSummary) -> None:
        """

        Set current run summary.

        

        Args:

            summary: Run summary data

        """
        self._current_summary = summary
        # Clear cache when summary changes
        self._export_cache = {}
    
    def get_summary(self) -> Optional[RunSummary]:
        """

        Get current run summary.

        

        Returns:

            Current run summary or None

        """
        return self._current_summary
    
    def export_json(self, include_config: bool = True) -> Optional[str]:
        """

        Export report as JSON.

        

        Args:

            include_config: Include configuration in export

            

        Returns:

            JSON string or None

        """
        if self._current_summary is None:
            return None
        
        cache_key = f"json_{include_config}"
        if cache_key in self._export_cache:
            return self._export_cache[cache_key]
        
        output = export_report(
            self._current_summary,
            format=ExportFormat.JSON,
            include_config=include_config,
            include_raw_outputs=False,
        )
        
        self._export_cache[cache_key] = output
        return output
    
    def export_csv(self) -> Optional[str]:
        """

        Export report as CSV.

        

        Returns:

            CSV string or None

        """
        if self._current_summary is None:
            return None
        
        cache_key = "csv"
        if cache_key in self._export_cache:
            return self._export_cache[cache_key]
        
        output = export_report(
            self._current_summary,
            format=ExportFormat.CSV,
            include_config=False,
            include_raw_outputs=False,
        )
        
        self._export_cache[cache_key] = output
        return output
    
    def get_filename(self, format: ExportFormat) -> str:
        """

        Get export filename.

        

        Args:

            format: Export format

            

        Returns:

            Filename string

        """
        if self._current_summary is None:
            return f"report.{format.value}"
        
        return f"report_{self._current_summary.metadata.run_id}.{format.value}"


def create_export_options() -> Tuple[gr.Checkbox, gr.Checkbox, gr.Checkbox]:
    """

    Create export options checkboxes.

    

    Returns:

        Tuple of checkbox components

    """
    
    include_config = gr.Checkbox(
        label="Include Configuration (config_hash, weights)",
        value=True,
        interactive=True,
    )
    
    include_metadata = gr.Checkbox(
        label="Include Metadata (model, dataset versions)",
        value=True,
        interactive=True,
    )
    
    include_raw = gr.Checkbox(
        label="Include Raw Outputs (privacy sensitive)",
        value=False,
        interactive=True,
    )
    
    return include_config, include_metadata, include_raw


def get_export_info() -> str:
    """

    Get export information text.

    

    Returns:

        Information string

    """
    return """

## Export Requirements



The exported report includes:



1. **Run Metadata**:

   - Run ID

   - Model name and version

   - Dataset version

   - Timestamp

   - Status



2. **Aggregate Metrics**:

   - Mean, Std, Min, Max for each metric

   - Sample count

   - Composite score

   - Vulnerability index



3. **Configuration** (optional):

   - Config hash for reproducibility

   - Scoring weights



4. **Raw Outputs** (optional, privacy sensitive):

   - Raw model outputs

   - Processed prompts



Note: Raw outputs are disabled by default to protect sensitive information.

"""


def create_share_report_section() -> Tuple[gr.Textbox, gr.Button]:
    """

    Create share report section.

    

    Returns:

        Tuple of (share_link, copy_button) components

    """
    
    share_link = gr.Textbox(
        label="Share Link",
        interactive=False,
        visible=False,
    )
    
    copy_button = gr.Button(
        "Copy Link",
        visible=False,
    )
    
    return share_link, copy_button