juancauma commited on
Commit
c5d25c1
·
1 Parent(s): 40fa55f
Files changed (1) hide show
  1. app.py +76 -61
app.py CHANGED
@@ -146,7 +146,7 @@ def create_grouped_leaderboard(selected_mwoz, selected_tau_airline, selected_tau
146
  })
147
 
148
  df = pd.DataFrame(aggregated)
149
- # Sort by allowed numeric columns.
150
  allowed_sort_cols = ["Average Score", "Conversation Consistency", "Backend Consistency", "Policy Completeness"]
151
  sort_by = sort_state.get("sort_by") if sort_state else None
152
  ascending = sort_state.get("ascending") if sort_state else True
@@ -157,8 +157,8 @@ def create_grouped_leaderboard(selected_mwoz, selected_tau_airline, selected_tau
157
  def update_sort_state(current_state, clicked_column):
158
  """
159
  Update the sort state based on the clicked column.
160
- If the same column is clicked, toggle its sort order;
161
- otherwise, set the new column with ascending order.
162
  """
163
  if current_state is None:
164
  current_state = {"sort_by": clicked_column, "ascending": True}
@@ -170,9 +170,21 @@ def update_sort_state(current_state, clicked_column):
170
  current_state["ascending"] = True
171
  return current_state
172
 
 
 
 
 
 
 
 
 
 
 
 
 
173
  def get_color_for_value(value, min_val, max_val):
174
  """
175
- Compute a text color for a given value based on its normalized position.
176
  Interpolates from red (lowest) to yellow (mid) to green (highest).
177
  """
178
  if max_val == min_val:
@@ -194,8 +206,7 @@ def get_color_for_value(value, min_val, max_val):
194
  def generate_html_table(df):
195
  """
196
  Generate an HTML table from the DataFrame.
197
- The header row contains interactive buttons (for sortable columns) that call a JavaScript function.
198
- Numeric cells are styled so that their text color reflects performance.
199
  """
200
  numeric_cols = ["Average Score", "Conversation Consistency", "Backend Consistency", "Policy Completeness"]
201
  col_min = {}
@@ -204,45 +215,21 @@ def generate_html_table(df):
204
  col_min[col] = df[col].min() if not df.empty else 0
205
  col_max[col] = df[col].max() if not df.empty else 0
206
 
207
- # JavaScript to handle header button clicks.
208
- script = """
209
- <script>
210
- function sortBy(column) {
211
- var hiddenSortState = document.getElementById("hidden_sort_state");
212
- var currentState = JSON.parse(hiddenSortState.value);
213
- if (currentState.sort_by === column) {
214
- currentState.ascending = !currentState.ascending;
215
- } else {
216
- currentState.sort_by = column;
217
- currentState.ascending = true;
218
- }
219
- hiddenSortState.value = JSON.stringify(currentState);
220
- var event = new Event('change');
221
- hiddenSortState.dispatchEvent(event);
222
- }
223
- </script>
224
- """
225
- # Build header row with buttons for numeric columns.
226
- header = "<tr>"
227
- headers = ["Model", "Average Score", "Conversation Consistency", "Backend Consistency", "Policy Completeness", "Judge Model"]
228
- for col in headers:
229
- if col in numeric_cols:
230
- header += f"<th style='padding: 8px;'><button onclick=\"sortBy('{col}')\" style='width: 100%;'>{col}</button></th>"
231
- else:
232
- header += f"<th style='padding: 8px;'>{col}</th>"
233
- header += "</tr>"
234
 
235
- html = script + "<table border='1' style='border-collapse: collapse; text-align: center; width: 100%;'>"
236
- html += header
237
-
238
- # Generate data rows.
239
  for _, row in df.iterrows():
240
  html += "<tr>"
241
  for col in df.columns:
242
  cell_value = row[col]
243
  if col in numeric_cols:
244
  color = get_color_for_value(cell_value, col_min[col], col_max[col])
245
- # Apply color to text instead of background.
246
  html += f"<td style='padding: 8px; color: {color};'>{cell_value}</td>"
247
  else:
248
  html += f"<td style='padding: 8px;'>{cell_value}</td>"
@@ -250,12 +237,10 @@ def generate_html_table(df):
250
  html += "</table>"
251
  return html
252
 
253
- def update_leaderboard(selected_mwoz, selected_tau_airline, selected_tau_retail, sort_state_json):
254
  """
255
- Update the leaderboard by aggregating the data and generating the HTML table.
256
- The sort_state_json is a JSON string that represents the current sort state.
257
  """
258
- sort_state = json.loads(sort_state_json)
259
  df = create_grouped_leaderboard(selected_mwoz, selected_tau_airline, selected_tau_retail, sort_state)
260
  html_table = generate_html_table(df)
261
  return html_table
@@ -278,30 +263,60 @@ with gr.Blocks(title="Rome Leaderboard") as demo:
278
  cb_tau_airline = gr.Checkbox(label="tau-airline", value=True)
279
  cb_tau_retail = gr.Checkbox(label="tau-retail", value=True)
280
 
281
- # Hidden textbox to store sort state; its elem_id is used by the embedded JS.
282
- hidden_sort_state = gr.Textbox(value=json.dumps({"sort_by": "Average Score", "ascending": False}),
283
- visible=False, elem_id="hidden_sort_state")
 
 
 
 
 
 
284
 
285
  leaderboard_display = gr.HTML(label="Aggregated Model Rankings")
286
 
287
- # Update leaderboard when checkboxes or the hidden sort state change.
288
- cb_mwoz.change(fn=update_leaderboard,
289
- inputs=[cb_mwoz, cb_tau_airline, cb_tau_retail, hidden_sort_state],
290
- outputs=leaderboard_display)
291
- cb_tau_airline.change(fn=update_leaderboard,
292
- inputs=[cb_mwoz, cb_tau_airline, cb_tau_retail, hidden_sort_state],
293
- outputs=leaderboard_display)
294
- cb_tau_retail.change(fn=update_leaderboard,
295
- inputs=[cb_mwoz, cb_tau_airline, cb_tau_retail, hidden_sort_state],
296
- outputs=leaderboard_display)
297
- hidden_sort_state.change(fn=update_leaderboard,
298
- inputs=[cb_mwoz, cb_tau_airline, cb_tau_retail, hidden_sort_state],
299
- outputs=leaderboard_display)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
300
 
301
  # Load initial leaderboard on app start.
302
- demo.load(fn=update_leaderboard,
303
- inputs=[cb_mwoz, cb_tau_airline, cb_tau_retail, hidden_sort_state],
304
- outputs=leaderboard_display)
 
 
305
 
306
  if __name__ == "__main__":
307
  demo.launch()
 
146
  })
147
 
148
  df = pd.DataFrame(aggregated)
149
+ # Sort if a valid column is provided.
150
  allowed_sort_cols = ["Average Score", "Conversation Consistency", "Backend Consistency", "Policy Completeness"]
151
  sort_by = sort_state.get("sort_by") if sort_state else None
152
  ascending = sort_state.get("ascending") if sort_state else True
 
157
  def update_sort_state(current_state, clicked_column):
158
  """
159
  Update the sort state based on the clicked column.
160
+ If the same column is clicked, toggle the sort order;
161
+ otherwise, switch to the new column with ascending order.
162
  """
163
  if current_state is None:
164
  current_state = {"sort_by": clicked_column, "ascending": True}
 
170
  current_state["ascending"] = True
171
  return current_state
172
 
173
+ def sort_by_avg(sort_state):
174
+ return update_sort_state(sort_state, "Average Score")
175
+
176
+ def sort_by_conv(sort_state):
177
+ return update_sort_state(sort_state, "Conversation Consistency")
178
+
179
+ def sort_by_backend(sort_state):
180
+ return update_sort_state(sort_state, "Backend Consistency")
181
+
182
+ def sort_by_policy(sort_state):
183
+ return update_sort_state(sort_state, "Policy Completeness")
184
+
185
  def get_color_for_value(value, min_val, max_val):
186
  """
187
+ Compute a color for a given value based on its normalized position.
188
  Interpolates from red (lowest) to yellow (mid) to green (highest).
189
  """
190
  if max_val == min_val:
 
206
  def generate_html_table(df):
207
  """
208
  Generate an HTML table from the DataFrame.
209
+ For each numeric column, apply a text color based on its relative value.
 
210
  """
211
  numeric_cols = ["Average Score", "Conversation Consistency", "Backend Consistency", "Policy Completeness"]
212
  col_min = {}
 
215
  col_min[col] = df[col].min() if not df.empty else 0
216
  col_max[col] = df[col].max() if not df.empty else 0
217
 
218
+ html = "<table border='1' style='border-collapse: collapse; text-align: center; width: 100%;'>"
219
+ # Header row
220
+ html += "<tr>"
221
+ for col in df.columns:
222
+ html += f"<th style='padding: 8px;'>{col}</th>"
223
+ html += "</tr>"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
224
 
225
+ # Data rows
 
 
 
226
  for _, row in df.iterrows():
227
  html += "<tr>"
228
  for col in df.columns:
229
  cell_value = row[col]
230
  if col in numeric_cols:
231
  color = get_color_for_value(cell_value, col_min[col], col_max[col])
232
+ # Now applying the color to the text (color property) instead of background.
233
  html += f"<td style='padding: 8px; color: {color};'>{cell_value}</td>"
234
  else:
235
  html += f"<td style='padding: 8px;'>{cell_value}</td>"
 
237
  html += "</table>"
238
  return html
239
 
240
+ def update_leaderboard(selected_mwoz, selected_tau_airline, selected_tau_retail, sort_state):
241
  """
242
+ Update the leaderboard by creating the aggregated DataFrame and converting it to HTML.
 
243
  """
 
244
  df = create_grouped_leaderboard(selected_mwoz, selected_tau_airline, selected_tau_retail, sort_state)
245
  html_table = generate_html_table(df)
246
  return html_table
 
263
  cb_tau_airline = gr.Checkbox(label="tau-airline", value=True)
264
  cb_tau_retail = gr.Checkbox(label="tau-retail", value=True)
265
 
266
+ gr.Markdown("### Sort by (click a button to toggle ascending/descending):")
267
+ with gr.Row():
268
+ btn_avg = gr.Button("Average Score")
269
+ btn_conv = gr.Button("Conversation Consistency")
270
+ btn_backend = gr.Button("Backend Consistency")
271
+ btn_policy = gr.Button("Policy Completeness")
272
+
273
+ # Initialize sort state: default sort by Average Score descending.
274
+ sort_state = gr.State({"sort_by": "Average Score", "ascending": False})
275
 
276
  leaderboard_display = gr.HTML(label="Aggregated Model Rankings")
277
 
278
+ refresh_btn = gr.Button("🔄 Refresh Leaderboard")
279
+
280
+ # Sort button events.
281
+ btn_avg.click(fn=sort_by_avg, inputs=[sort_state], outputs=[sort_state]).then(
282
+ fn=update_leaderboard,
283
+ inputs=[cb_mwoz, cb_tau_airline, cb_tau_retail, sort_state],
284
+ outputs=leaderboard_display
285
+ )
286
+ btn_conv.click(fn=sort_by_conv, inputs=[sort_state], outputs=[sort_state]).then(
287
+ fn=update_leaderboard,
288
+ inputs=[cb_mwoz, cb_tau_airline, cb_tau_retail, sort_state],
289
+ outputs=leaderboard_display
290
+ )
291
+ btn_backend.click(fn=sort_by_backend, inputs=[sort_state], outputs=[sort_state]).then(
292
+ fn=update_leaderboard,
293
+ inputs=[cb_mwoz, cb_tau_airline, cb_tau_retail, sort_state],
294
+ outputs=leaderboard_display
295
+ )
296
+ btn_policy.click(fn=sort_by_policy, inputs=[sort_state], outputs=[sort_state]).then(
297
+ fn=update_leaderboard,
298
+ inputs=[cb_mwoz, cb_tau_airline, cb_tau_retail, sort_state],
299
+ outputs=leaderboard_display
300
+ )
301
+
302
+ # Refresh button event.
303
+ refresh_btn.click(
304
+ fn=update_leaderboard,
305
+ inputs=[cb_mwoz, cb_tau_airline, cb_tau_retail, sort_state],
306
+ outputs=leaderboard_display
307
+ )
308
+
309
+ # Update leaderboard immediately when any checkbox changes.
310
+ cb_mwoz.change(fn=update_leaderboard, inputs=[cb_mwoz, cb_tau_airline, cb_tau_retail, sort_state], outputs=leaderboard_display)
311
+ cb_tau_airline.change(fn=update_leaderboard, inputs=[cb_mwoz, cb_tau_airline, cb_tau_retail, sort_state], outputs=leaderboard_display)
312
+ cb_tau_retail.change(fn=update_leaderboard, inputs=[cb_mwoz, cb_tau_airline, cb_tau_retail, sort_state], outputs=leaderboard_display)
313
 
314
  # Load initial leaderboard on app start.
315
+ demo.load(
316
+ fn=update_leaderboard,
317
+ inputs=[cb_mwoz, cb_tau_airline, cb_tau_retail, sort_state],
318
+ outputs=leaderboard_display
319
+ )
320
 
321
  if __name__ == "__main__":
322
  demo.launch()