juan-all-hands commited on
Commit
ab75229
·
1 Parent(s): 81071cf

Fix open model filter callback reliability (#38)

Browse files

- Fix open model filter callback reliability (5c02fd06b29fccf7afb556e9f4eb5d647f6ea33e)

Files changed (3) hide show
  1. leaderboard_transformer.py +103 -100
  2. ui_components.py +9 -1
  3. visualizations.py +11 -1
leaderboard_transformer.py CHANGED
@@ -12,6 +12,38 @@ from constants import FONT_FAMILY, FONT_FAMILY_SHORT
12
 
13
  logger = logging.getLogger(__name__)
14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  # Company logo mapping for graphs - maps model name patterns to company logo files
16
  COMPANY_LOGO_MAP = {
17
  "anthropic": {"path": "assets/logo-anthropic.svg", "name": "Anthropic"},
@@ -109,42 +141,34 @@ def get_openhands_logo_images():
109
  images = []
110
 
111
  # Light mode logo (visible in light mode, hidden in dark mode)
112
- if os.path.exists(OPENHANDS_LOGO_PATH_LIGHT):
113
- try:
114
- with open(OPENHANDS_LOGO_PATH_LIGHT, "rb") as f:
115
- logo_data = base64.b64encode(f.read()).decode('utf-8')
116
- images.append(dict(
117
- source=f"data:image/png;openhands=lightlogo;base64,{logo_data}",
118
- xref="paper",
119
- yref="paper",
120
- x=0,
121
- y=-0.15,
122
- sizex=0.15,
123
- sizey=0.15,
124
- xanchor="left",
125
- yanchor="bottom",
126
- ))
127
- except Exception:
128
- pass
129
 
130
  # Dark mode logo (hidden in light mode, visible in dark mode)
131
- if os.path.exists(OPENHANDS_LOGO_PATH_DARK):
132
- try:
133
- with open(OPENHANDS_LOGO_PATH_DARK, "rb") as f:
134
- logo_data = base64.b64encode(f.read()).decode('utf-8')
135
- images.append(dict(
136
- source=f"data:image/png;openhands=darklogo;base64,{logo_data}",
137
- xref="paper",
138
- yref="paper",
139
- x=0,
140
- y=-0.15,
141
- sizex=0.15,
142
- sizey=0.15,
143
- xanchor="left",
144
- yanchor="bottom",
145
- ))
146
- except Exception:
147
- pass
148
 
149
  return images
150
 
@@ -511,54 +535,50 @@ def create_scatter_chart(
511
  marker_info = get_marker_icon(model_name, openness, mark_by)
512
  logo_path = marker_info['path']
513
 
514
- if os.path.exists(logo_path):
515
- try:
516
- with open(logo_path, 'rb') as f:
517
- encoded_logo = base64.b64encode(f.read()).decode('utf-8')
518
- logo_uri = f"data:image/svg+xml;base64,{encoded_logo}"
519
-
520
- if x_type == "date":
521
- # For date axes, use data coordinates directly
522
- layout_images.append(dict(
523
- source=logo_uri,
524
- xref="x",
525
- yref="y",
526
- x=x_val,
527
- y=y_val,
528
- sizex=15 * 24 * 60 * 60 * 1000, # ~15 days in milliseconds
529
- sizey=3, # score units
530
- xanchor="center",
531
- yanchor="middle",
532
- layer="above"
533
- ))
534
- else:
535
- # For log axes, use domain coordinates (0-1 range)
536
- if x_type == "log" and x_val > 0:
537
- log_x = np.log10(x_val)
538
- domain_x = (log_x - x_range_log[0]) / (x_range_log[1] - x_range_log[0])
539
- else:
540
- domain_x = 0.5
541
-
542
- domain_y = (y_val - y_range[0]) / (y_range[1] - y_range[0]) if (y_range[1] - y_range[0]) > 0 else 0.5
543
-
544
- # Clamp to valid range
545
- domain_x = max(0, min(1, domain_x))
546
- domain_y = max(0, min(1, domain_y))
547
-
548
- layout_images.append(dict(
549
- source=logo_uri,
550
- xref="x domain",
551
- yref="y domain",
552
- x=domain_x,
553
- y=domain_y,
554
- sizex=0.04,
555
- sizey=0.06,
556
- xanchor="center",
557
- yanchor="middle",
558
- layer="above"
559
- ))
560
- except Exception:
561
- pass
562
 
563
  # Add labels for frontier points only
564
  for row in frontier_rows:
@@ -1193,25 +1213,8 @@ def _plot_scatter_plotly(
1193
  y_min = min_score - 5 if min_score > 5 else 0
1194
  y_max = max_score + 5
1195
 
1196
- # Cache base64-encoded logos across rows — every Claude model on the
1197
- # Alternative Agents page points at the same assets/harness-claude-code.svg,
1198
- # so decoding once per path is ~N× cheaper than once per point.
1199
- _logo_cache: dict[str, str] = {}
1200
  def _encode_logo(path: str) -> Optional[str]:
1201
- if path in _logo_cache:
1202
- return _logo_cache[path]
1203
- if not os.path.exists(path):
1204
- return None
1205
- try:
1206
- with open(path, "rb") as f:
1207
- encoded = base64.b64encode(f.read()).decode("utf-8")
1208
- except Exception as e:
1209
- logger.warning(f"Could not load logo {path}: {e}")
1210
- return None
1211
- mime = "svg+xml" if path.lower().endswith(".svg") else "png"
1212
- uri = f"data:image/{mime};base64,{encoded}"
1213
- _logo_cache[path] = uri
1214
- return uri
1215
 
1216
  # Composite markers: on the Alternative Agents page the dataframe carries
1217
  # an "Agent" column (Claude Code / Codex / Gemini CLI / OpenHands Sub-agents),
 
12
 
13
  logger = logging.getLogger(__name__)
14
 
15
+ _DATA_URI_CACHE: dict[str, str] = {}
16
+
17
+
18
+ def get_asset_data_uri(path: str) -> Optional[str]:
19
+ """Return a cached data URI for a local image asset."""
20
+ if path in _DATA_URI_CACHE:
21
+ return _DATA_URI_CACHE[path]
22
+
23
+ if not os.path.exists(path):
24
+ _DATA_URI_CACHE[path] = ""
25
+ return None
26
+
27
+ try:
28
+ with open(path, "rb") as f:
29
+ encoded = base64.b64encode(f.read()).decode("utf-8")
30
+ except Exception as e:
31
+ logger.warning(f"Could not load image asset {path}: {e}")
32
+ _DATA_URI_CACHE[path] = ""
33
+ return None
34
+
35
+ ext = os.path.splitext(path)[1].lower()
36
+ if ext == ".svg":
37
+ mime = "image/svg+xml"
38
+ elif ext == ".png":
39
+ mime = "image/png"
40
+ else:
41
+ mime = "application/octet-stream"
42
+
43
+ uri = f"data:{mime};base64,{encoded}"
44
+ _DATA_URI_CACHE[path] = uri
45
+ return uri
46
+
47
  # Company logo mapping for graphs - maps model name patterns to company logo files
48
  COMPANY_LOGO_MAP = {
49
  "anthropic": {"path": "assets/logo-anthropic.svg", "name": "Anthropic"},
 
141
  images = []
142
 
143
  # Light mode logo (visible in light mode, hidden in dark mode)
144
+ light_logo_uri = get_asset_data_uri(OPENHANDS_LOGO_PATH_LIGHT)
145
+ if light_logo_uri:
146
+ images.append(dict(
147
+ source=light_logo_uri.replace("data:image/png;base64,", "data:image/png;openhands=lightlogo;base64,"),
148
+ xref="paper",
149
+ yref="paper",
150
+ x=0,
151
+ y=-0.15,
152
+ sizex=0.15,
153
+ sizey=0.15,
154
+ xanchor="left",
155
+ yanchor="bottom",
156
+ ))
 
 
 
 
157
 
158
  # Dark mode logo (hidden in light mode, visible in dark mode)
159
+ dark_logo_uri = get_asset_data_uri(OPENHANDS_LOGO_PATH_DARK)
160
+ if dark_logo_uri:
161
+ images.append(dict(
162
+ source=dark_logo_uri.replace("data:image/png;base64,", "data:image/png;openhands=darklogo;base64,"),
163
+ xref="paper",
164
+ yref="paper",
165
+ x=0,
166
+ y=-0.15,
167
+ sizex=0.15,
168
+ sizey=0.15,
169
+ xanchor="left",
170
+ yanchor="bottom",
171
+ ))
 
 
 
 
172
 
173
  return images
174
 
 
535
  marker_info = get_marker_icon(model_name, openness, mark_by)
536
  logo_path = marker_info['path']
537
 
538
+ logo_uri = get_asset_data_uri(logo_path)
539
+ if not logo_uri:
540
+ continue
541
+
542
+ if x_type == "date":
543
+ # For date axes, use data coordinates directly
544
+ layout_images.append(dict(
545
+ source=logo_uri,
546
+ xref="x",
547
+ yref="y",
548
+ x=x_val,
549
+ y=y_val,
550
+ sizex=15 * 24 * 60 * 60 * 1000, # ~15 days in milliseconds
551
+ sizey=3, # score units
552
+ xanchor="center",
553
+ yanchor="middle",
554
+ layer="above"
555
+ ))
556
+ else:
557
+ # For log axes, use domain coordinates (0-1 range)
558
+ if x_type == "log" and x_val > 0:
559
+ log_x = np.log10(x_val)
560
+ domain_x = (log_x - x_range_log[0]) / (x_range_log[1] - x_range_log[0])
561
+ else:
562
+ domain_x = 0.5
563
+
564
+ domain_y = (y_val - y_range[0]) / (y_range[1] - y_range[0]) if (y_range[1] - y_range[0]) > 0 else 0.5
565
+
566
+ # Clamp to valid range
567
+ domain_x = max(0, min(1, domain_x))
568
+ domain_y = max(0, min(1, domain_y))
569
+
570
+ layout_images.append(dict(
571
+ source=logo_uri,
572
+ xref="x domain",
573
+ yref="y domain",
574
+ x=domain_x,
575
+ y=domain_y,
576
+ sizex=0.04,
577
+ sizey=0.06,
578
+ xanchor="center",
579
+ yanchor="middle",
580
+ layer="above"
581
+ ))
 
 
 
 
582
 
583
  # Add labels for frontier points only
584
  for row in frontier_rows:
 
1213
  y_min = min_score - 5 if min_score > 5 else 0
1214
  y_max = max_score + 5
1215
 
 
 
 
 
1216
  def _encode_logo(path: str) -> Optional[str]:
1217
+ return get_asset_data_uri(path)
 
 
 
 
 
 
 
 
 
 
 
 
 
1218
 
1219
  # Composite markers: on the Alternative Agents page the dataframe carries
1220
  # an "Agent" column (Claude Code / Codex / Gemini CLI / OpenHands Sub-agents),
ui_components.py CHANGED
@@ -43,6 +43,8 @@ from content import (
43
  api = HfApi()
44
  os.makedirs(EXTRACTED_DATA_DIR, exist_ok=True)
45
 
 
 
46
 
47
  def get_company_logo_html(model_name: str) -> str:
48
  """
@@ -81,12 +83,18 @@ OPENNESS_SVG_MAP = {
81
 
82
  def get_svg_as_data_uri(path: str) -> str:
83
  """Reads an SVG file and returns it as a base64-encoded data URI."""
 
 
 
84
  try:
85
  with open(path, "rb") as svg_file:
86
  encoded_svg = base64.b64encode(svg_file.read()).decode("utf-8")
87
- return f"data:image/svg+xml;base64,{encoded_svg}"
 
 
88
  except FileNotFoundError:
89
  print(f"Warning: SVG file not found at {path}")
 
90
  return ""
91
 
92
 
 
43
  api = HfApi()
44
  os.makedirs(EXTRACTED_DATA_DIR, exist_ok=True)
45
 
46
+ _SVG_DATA_URI_CACHE: dict[str, str] = {}
47
+
48
 
49
  def get_company_logo_html(model_name: str) -> str:
50
  """
 
83
 
84
  def get_svg_as_data_uri(path: str) -> str:
85
  """Reads an SVG file and returns it as a base64-encoded data URI."""
86
+ if path in _SVG_DATA_URI_CACHE:
87
+ return _SVG_DATA_URI_CACHE[path]
88
+
89
  try:
90
  with open(path, "rb") as svg_file:
91
  encoded_svg = base64.b64encode(svg_file.read()).decode("utf-8")
92
+ uri = f"data:image/svg+xml;base64,{encoded_svg}"
93
+ _SVG_DATA_URI_CACHE[path] = uri
94
+ return uri
95
  except FileNotFoundError:
96
  print(f"Warning: SVG file not found at {path}")
97
+ _SVG_DATA_URI_CACHE[path] = ""
98
  return ""
99
 
100
 
visualizations.py CHANGED
@@ -108,7 +108,17 @@ def create_accuracy_by_size_chart(df: pd.DataFrame, mark_by: str = None) -> go.F
108
  open_aliases = [aliases.CANONICAL_OPENNESS_OPEN] + list(
109
  aliases.OPENNESS_ALIASES.get(aliases.CANONICAL_OPENNESS_OPEN, [])
110
  )
111
- openness_col = 'Openness' if 'Openness' in df.columns else 'openness'
 
 
 
 
 
 
 
 
 
 
112
 
113
  plot_df = df[
114
  (df[param_col].notna()) &
 
108
  open_aliases = [aliases.CANONICAL_OPENNESS_OPEN] + list(
109
  aliases.OPENNESS_ALIASES.get(aliases.CANONICAL_OPENNESS_OPEN, [])
110
  )
111
+ openness_col = _find_column(df, ['Openness', 'openness'])
112
+ if openness_col is None:
113
+ fig = go.Figure()
114
+ fig.add_annotation(
115
+ text="No openness data available",
116
+ xref="paper", yref="paper",
117
+ x=0.5, y=0.5, showarrow=False,
118
+ font=STANDARD_FONT
119
+ )
120
+ fig.update_layout(**STANDARD_LAYOUT, title="Open Model Accuracy by Size")
121
+ return fig
122
 
123
  plot_df = df[
124
  (df[param_col].notna()) &