gremlin97 commited on
Commit
fd803db
·
1 Parent(s): d9fe250

Use gradio-leaderboard component for clickable author links

Browse files
Files changed (2) hide show
  1. app.py +25 -66
  2. requirements.txt +1 -0
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import gradio as gr
2
  import pandas as pd
3
  import os
 
4
 
5
  # Load data from CSV files
6
  DATA_DIR = "data"
@@ -40,39 +41,6 @@ def filter_and_search(df, search, datasets, models, organizations, columns):
40
  return filtered
41
 
42
 
43
- def df_to_html(df):
44
- """Convert DataFrame to HTML table with clickable links in First Author column"""
45
- if df.empty:
46
- return "<p>No data to display</p>"
47
-
48
- html = '<table class="dataframe">\n<thead>\n<tr>'
49
-
50
- # Add headers
51
- for col in df.columns:
52
- if col != "Author Link": # Skip Author Link column in display
53
- html += f'<th>{col}</th>'
54
- html += '</tr>\n</thead>\n<tbody>\n'
55
-
56
- # Add rows
57
- for _, row in df.iterrows():
58
- html += '<tr>'
59
- for col in df.columns:
60
- if col != "Author Link": # Skip Author Link column in display
61
- if col == "First Author" and "Author Link" in df.columns:
62
- # Create clickable link for First Author
63
- author_link = row["Author Link"]
64
- if pd.notna(author_link) and author_link.strip():
65
- html += f'<td><a href="{author_link}" target="_blank">{row[col]}</a></td>'
66
- else:
67
- html += f'<td>{row[col]}</td>'
68
- else:
69
- html += f'<td>{row[col]}</td>'
70
- html += '</tr>\n'
71
-
72
- html += '</tbody>\n</table>'
73
- return html
74
-
75
-
76
  def build_tab(df, name):
77
  """Build a leaderboard tab from a DataFrame"""
78
  if df.empty:
@@ -94,7 +62,13 @@ def build_tab(df, name):
94
  first_author = model_org_data["First Author"].values[0]
95
  author_link = model_org_data["Author Link"].values[0]
96
 
97
- row = {"Model": model, "Organization": org, "First Author": first_author, "Author Link": author_link}
 
 
 
 
 
 
98
  for dataset in datasets:
99
  dataset_data = model_org_data[model_org_data["Dataset"] == dataset]
100
  if not dataset_data.empty:
@@ -117,9 +91,6 @@ def build_tab(df, name):
117
 
118
  all_cols = ["Model", "Organization", "First Author"] + metric_combo_cols
119
 
120
- # Keep Author Link for internal use but don't show in column selector
121
- display_cols_for_selector = [col for col in all_cols if col != "Author Link"]
122
-
123
  with gr.TabItem(name, elem_id="llm-benchmark-tab-table"):
124
  with gr.Row():
125
  with gr.Column(scale=4):
@@ -130,15 +101,17 @@ def build_tab(df, name):
130
  )
131
 
132
  col_selector = gr.CheckboxGroup(
133
- choices=display_cols_for_selector,
134
- value=display_cols_for_selector,
135
  label="Select Columns to Display:",
136
  elem_classes="column-select"
137
  )
138
 
139
- table = gr.HTML(
140
- value=df_to_html(pivoted_df),
141
- elem_id="leaderboard-table"
 
 
142
  )
143
 
144
  with gr.Column(scale=1):
@@ -171,13 +144,10 @@ def build_tab(df, name):
171
  filtered = filtered[filtered["Organization"].isin(org)]
172
 
173
  if cols:
174
- # Always include "Author Link" for rendering, even if not in selection
175
  display_cols = [col for col in cols if col in filtered.columns]
176
- if "First Author" in display_cols and "Author Link" in filtered.columns:
177
- display_cols.append("Author Link")
178
  filtered = filtered[display_cols]
179
 
180
- return df_to_html(filtered)
181
 
182
  search_bar.change(update, [search_bar, model_filter, org_filter, col_selector], table)
183
  model_filter.change(update, [search_bar, model_filter, org_filter, col_selector], table)
@@ -192,39 +162,29 @@ custom_css = """
192
 
193
  #leaderboard-table {
194
  margin-top: 15px;
195
- max-height: 600px;
196
- overflow-y: auto;
197
- overflow-x: auto;
198
  }
199
 
200
- #leaderboard-table table.dataframe {
201
  width: 100%;
202
- border-collapse: collapse;
203
  table-layout: auto;
204
  }
205
 
206
- #leaderboard-table table.dataframe thead th {
207
  font-weight: bold;
208
  text-align: center;
209
  padding: 12px 8px;
210
  white-space: normal;
211
  word-wrap: break-word;
212
- border-bottom: 2px solid #ddd;
213
  }
214
 
215
- #leaderboard-table table.dataframe tbody td {
216
  text-align: center;
217
  padding: 10px 8px;
218
  white-space: nowrap;
219
- border-bottom: 1px solid #eee;
220
- }
221
-
222
- #leaderboard-table table.dataframe tbody tr:hover {
223
- background-color: #f5f5f5;
224
  }
225
 
226
- #leaderboard-table table.dataframe tbody td:first-child,
227
- #leaderboard-table table.dataframe thead th:first-child {
228
  text-align: left;
229
  font-weight: 500;
230
  min-width: 120px;
@@ -232,14 +192,14 @@ custom_css = """
232
  white-space: nowrap;
233
  }
234
 
235
- #leaderboard-table table.dataframe tbody td:nth-child(2),
236
- #leaderboard-table table.dataframe thead th:nth-child(2) {
237
  text-align: left;
238
  min-width: 100px;
239
  }
240
 
241
- #leaderboard-table table.dataframe tbody td:nth-child(3),
242
- #leaderboard-table table.dataframe thead th:nth-child(3) {
243
  text-align: left;
244
  min-width: 120px;
245
  }
@@ -252,7 +212,6 @@ custom_css = """
252
 
253
  #leaderboard-table a:hover {
254
  text-decoration: underline;
255
- color: #004499;
256
  }
257
 
258
  #search-bar {
 
1
  import gradio as gr
2
  import pandas as pd
3
  import os
4
+ from gradio_leaderboard import Leaderboard, ColumnFilter, SelectColumns
5
 
6
  # Load data from CSV files
7
  DATA_DIR = "data"
 
41
  return filtered
42
 
43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  def build_tab(df, name):
45
  """Build a leaderboard tab from a DataFrame"""
46
  if df.empty:
 
62
  first_author = model_org_data["First Author"].values[0]
63
  author_link = model_org_data["Author Link"].values[0]
64
 
65
+ # Format as markdown link if author_link exists
66
+ if pd.notna(author_link) and author_link.strip():
67
+ author_display = f"[{first_author}]({author_link})"
68
+ else:
69
+ author_display = first_author
70
+
71
+ row = {"Model": model, "Organization": org, "First Author": author_display}
72
  for dataset in datasets:
73
  dataset_data = model_org_data[model_org_data["Dataset"] == dataset]
74
  if not dataset_data.empty:
 
91
 
92
  all_cols = ["Model", "Organization", "First Author"] + metric_combo_cols
93
 
 
 
 
94
  with gr.TabItem(name, elem_id="llm-benchmark-tab-table"):
95
  with gr.Row():
96
  with gr.Column(scale=4):
 
101
  )
102
 
103
  col_selector = gr.CheckboxGroup(
104
+ choices=all_cols,
105
+ value=all_cols,
106
  label="Select Columns to Display:",
107
  elem_classes="column-select"
108
  )
109
 
110
+ table = Leaderboard(
111
+ value=pivoted_df,
112
+ elem_id="leaderboard-table",
113
+ interactive=False,
114
+ select_columns=False
115
  )
116
 
117
  with gr.Column(scale=1):
 
144
  filtered = filtered[filtered["Organization"].isin(org)]
145
 
146
  if cols:
 
147
  display_cols = [col for col in cols if col in filtered.columns]
 
 
148
  filtered = filtered[display_cols]
149
 
150
+ return filtered
151
 
152
  search_bar.change(update, [search_bar, model_filter, org_filter, col_selector], table)
153
  model_filter.change(update, [search_bar, model_filter, org_filter, col_selector], table)
 
162
 
163
  #leaderboard-table {
164
  margin-top: 15px;
 
 
 
165
  }
166
 
167
+ #leaderboard-table table {
168
  width: 100%;
 
169
  table-layout: auto;
170
  }
171
 
172
+ #leaderboard-table thead th {
173
  font-weight: bold;
174
  text-align: center;
175
  padding: 12px 8px;
176
  white-space: normal;
177
  word-wrap: break-word;
 
178
  }
179
 
180
+ #leaderboard-table tbody td {
181
  text-align: center;
182
  padding: 10px 8px;
183
  white-space: nowrap;
 
 
 
 
 
184
  }
185
 
186
+ #leaderboard-table tbody td:first-child,
187
+ #leaderboard-table thead th:first-child {
188
  text-align: left;
189
  font-weight: 500;
190
  min-width: 120px;
 
192
  white-space: nowrap;
193
  }
194
 
195
+ #leaderboard-table tbody td:nth-child(2),
196
+ #leaderboard-table thead th:nth-child(2) {
197
  text-align: left;
198
  min-width: 100px;
199
  }
200
 
201
+ #leaderboard-table tbody td:nth-child(3),
202
+ #leaderboard-table thead th:nth-child(3) {
203
  text-align: left;
204
  min-width: 120px;
205
  }
 
212
 
213
  #leaderboard-table a:hover {
214
  text-decoration: underline;
 
215
  }
216
 
217
  #search-bar {
requirements.txt CHANGED
@@ -1,2 +1,3 @@
1
  gradio>=4.0.0
2
  pandas>=2.0.0
 
 
1
  gradio>=4.0.0
2
  pandas>=2.0.0
3
+ gradio-leaderboard