File size: 7,180 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 | """
Comparison Table Component
Gradio component for displaying model comparison tables.
"""
import logging
from typing import Any, List, Optional, Tuple
import gradio as gr
import plotly.graph_objects as go
from dashboard.schemas import ComparisonData, DeltaRobustnessData
from dashboard.utils import format_score, log_dashboard_event
logger = logging.getLogger(__name__)
def create_comparison_table() -> Tuple[gr.DataFrame, gr.Plot]:
"""
Create comparison table component.
Returns:
Tuple of (table, plot) components
"""
# Comparison table
table = gr.DataFrame(
headers=[
"Model",
"Hallucination",
"Toxicity",
"Bias",
"Confidence",
"Composite Score",
"Sample Count",
],
label="Model Comparison",
interactive=False,
)
# Delta chart
delta_plot = gr.Plot(
label="Delta Robustness",
)
return table, delta_plot
def update_comparison_table(
comparison_data: Optional[ComparisonData],
) -> List[List[str]]:
"""
Update comparison table with data.
Args:
comparison_data: Comparison data
Returns:
Table data as list of rows
"""
if comparison_data is None or not comparison_data.models:
return [["N/A", "N/A", "N/A", "N/A", "N/A", "N/A", "0"]]
table_data = []
for i, model in enumerate(comparison_data.models):
table_data.append([
model,
format_score(comparison_data.hallucination[i], 4),
format_score(comparison_data.toxicity[i], 4),
format_score(comparison_data.bias[i], 4),
format_score(comparison_data.confidence[i], 4),
format_score(comparison_data.composite_score[i], 4),
str(comparison_data.sample_count[i]),
])
return table_data
def update_delta_chart(
delta_data: List[DeltaRobustnessData],
) -> Any:
"""
Update delta chart with data.
Args:
delta_data: List of delta robustness data
Returns:
Plotly figure
"""
if not delta_data:
# Return empty chart
fig = go.Figure()
fig.update_layout(
title="No Data Available",
xaxis=dict(title="Model"),
yaxis=dict(title="Delta Robustness"),
)
return fig
log_dashboard_event("DASHBOARD_COMPARE_MODELS")
models = [d.model_name for d in delta_data]
deltas = [d.delta_robustness for d in delta_data]
composites = [d.composite_score for d in delta_data]
# Color based on delta (green for positive, red for negative)
colors = ["#22c55e" if d >= 0 else "#ef4444" for d in deltas]
fig = go.Figure(
data=go.Bar(
x=models,
y=deltas,
marker_color=colors,
text=[f"Δ={c:.3f}" for c in composites],
textposition="auto",
)
)
fig.update_layout(
title="Delta Robustness Comparison",
xaxis=dict(title="Model"),
yaxis=dict(
title="Delta Robustness",
range=[min(min(deltas) - 0.1, -0.1), max(max(deltas) + 0.1, 0.1)],
),
height=400,
width=600,
)
return fig
def create_ranking_table(
delta_data: List[DeltaRobustnessData],
) -> List[List[str]]:
"""
Create ranking table from delta data.
Args:
delta_data: List of delta robustness data
Returns:
Table data as list of rows
"""
if not delta_data:
return [["N/A", "N/A", "N/A", "N/A"]]
# Sort by composite score descending
sorted_data = sorted(delta_data, key=lambda x: x.composite_score, reverse=True)
table_data = []
for rank, data in enumerate(sorted_data, 1):
table_data.append([
str(rank),
data.model_name,
format_score(data.composite_score, 4),
format_score(data.delta_robustness, 4),
])
return table_data
class ComparisonTable:
"""
Comparison table component with state management.
"""
def __init__(self):
"""Initialize comparison table."""
self._comparison_data: Optional[ComparisonData] = None
self._delta_data: List[DeltaRobustnessData] = []
def set_comparison_data(self, data: ComparisonData) -> None:
"""
Set comparison data.
Args:
data: Comparison data
"""
self._comparison_data = data
def set_delta_data(self, data: List[DeltaRobustnessData]) -> None:
"""
Set delta data.
Args:
data: List of delta robustness data
"""
self._delta_data = data
def get_comparison_data(self) -> Optional[ComparisonData]:
"""
Get comparison data.
Returns:
Comparison data or None
"""
return self._comparison_data
def get_delta_data(self) -> List[DeltaRobustnessData]:
"""
Get delta data.
Returns:
List of delta robustness data
"""
return self._delta_data
def get_table_data(self) -> List[List[str]]:
"""
Get table data for display.
Returns:
Table data as list of rows
"""
return update_comparison_table(self._comparison_data)
def get_delta_figure(self) -> Any:
"""
Get delta chart figure.
Returns:
Plotly figure
"""
return update_delta_chart(self._delta_data)
def create_model_selector_multi() -> gr.Component:
"""
Create multi-select model selector.
Returns:
Gradio multi-select component
"""
selector = gr.Dropdown(
label="Select Models to Compare",
choices=[],
interactive=True,
multiselect=True,
)
return selector
def create_benchmark_comparison(
benchmark_runs: List[dict],
) -> gr.DataFrame:
"""
Create benchmark comparison table.
Args:
benchmark_runs: List of benchmark run data
Returns:
DataFrame component
"""
headers = ["Benchmark", "Model", "Score", "Status", "Date"]
table_data = []
for run in benchmark_runs:
table_data.append([
run.get("benchmark_name", "N/A"),
run.get("model_name", "N/A"),
format_score(run.get("composite_score"), 4),
run.get("status", "N/A"),
run.get("timestamp", "N/A")[:10],
])
table = gr.DataFrame(
headers=headers,
value=table_data if table_data else [["N/A"] * len(headers)],
label="Benchmark Comparison",
interactive=False,
)
return table
|