tdurbor commited on
Commit
b58cdd5
·
1 Parent(s): f50f18c

Include user leaderboard for challenge

Browse files
Files changed (1) hide show
  1. app.py +68 -26
app.py CHANGED
@@ -7,6 +7,7 @@ import threading
7
  from pathlib import Path
8
  from uuid import uuid4
9
  from typing import Tuple
 
10
 
11
  import numpy as np
12
  import gradio as gr
@@ -66,7 +67,6 @@ def update_rankings_table():
66
  if elo_scores:
67
  rankings = [
68
  ["Photoroom", int(elo_scores.get("Photoroom", 1000))],
69
- #["Clipdrop", int(elo_scores.get("Clipdrop", 1000))],
70
  ["RemoveBG", int(elo_scores.get("RemoveBG", 1000))],
71
  ["BRIA RMBG 2.0", int(elo_scores.get("BRIA RMBG 2.0", 1000))],
72
  ]
@@ -75,7 +75,6 @@ def update_rankings_table():
75
  else:
76
  return [
77
  ["Photoroom", -1],
78
- #["Clipdrop", -1],
79
  ["RemoveBG", -1],
80
  ["BRIA RMBG 2.0", -1],
81
  ]
@@ -172,6 +171,7 @@ def gradio_interface():
172
 
173
  # Compute the absolute difference between the masks
174
  mask_difference = compute_mask_difference(segmented_a, segmented_b)
 
175
 
176
  with gr.Row():
177
  image_a_display = gr.Image(
@@ -200,27 +200,7 @@ def gradio_interface():
200
  vote_tie = gr.Button("🤝 Tie")
201
  vote_b_btn = gr.Button("👉 B is better")
202
 
203
-
204
- vote_a_btn.click(
205
- fn=lambda: vote_for_model("model_a", fpath_input, model_a_name, model_b_name),
206
- outputs=[
207
- fpath_input, input_image_display, image_a_display, image_b_display, model_a_name, model_b_name, notice_markdown
208
- ]
209
- )
210
- vote_b_btn.click(
211
- fn=lambda: vote_for_model("model_b",fpath_input, model_a_name, model_b_name),
212
- outputs=[
213
- fpath_input, input_image_display, image_a_display, image_b_display, model_a_name, model_b_name, notice_markdown
214
- ]
215
- )
216
- vote_tie.click(
217
- fn=lambda: vote_for_model("tie", fpath_input, model_a_name, model_b_name),
218
- outputs=[
219
- fpath_input, input_image_display, image_a_display, image_b_display, model_a_name, model_b_name, notice_markdown
220
- ]
221
- )
222
-
223
- def vote_for_model(choice, original_filename, model_a_name, model_b_name):
224
  """Submit a vote for a model and return updated images and model names."""
225
  logging.info("Voting for model: %s", choice)
226
  vote_data = {
@@ -228,6 +208,7 @@ def gradio_interface():
228
  "model_a": model_a_name.value,
229
  "model_b": model_b_name.value,
230
  "winner": choice,
 
231
  }
232
 
233
  try:
@@ -249,6 +230,28 @@ def gradio_interface():
249
 
250
  return (fpath_input.value, (new_input_image, [(mask_difference, button_name)]), new_segmented_a,
251
  new_segmented_b, model_a_name.value, model_b_name.value, new_notice_markdown)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
252
 
253
  with gr.Tab("🏆 Leaderboard", id=1) as leaderboard_tab:
254
  rankings_table = gr.Dataframe(
@@ -267,13 +270,13 @@ def gradio_interface():
267
  with gr.Tab("📊 Vote Data", id=2) as vote_data_tab:
268
  def update_vote_data():
269
  votes = get_all_votes()
270
- return [[vote.id, vote.image_id, vote.model_a, vote.model_b, vote.winner, vote.timestamp] for vote in votes]
271
 
272
  vote_table = gr.Dataframe(
273
- headers=["ID", "Image ID", "Model A", "Model B", "Winner", "Timestamp"],
274
  value=update_vote_data(),
275
  label="Vote Data",
276
- column_widths=[20, 150, 100, 100, 100, 150],
277
  row_count=0
278
  )
279
 
@@ -282,6 +285,45 @@ def gradio_interface():
282
  outputs=vote_table
283
  )
284
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
285
  return demo
286
 
287
  def dump_database_to_json():
 
7
  from pathlib import Path
8
  from uuid import uuid4
9
  from typing import Tuple
10
+ from datetime import datetime, timedelta
11
 
12
  import numpy as np
13
  import gradio as gr
 
67
  if elo_scores:
68
  rankings = [
69
  ["Photoroom", int(elo_scores.get("Photoroom", 1000))],
 
70
  ["RemoveBG", int(elo_scores.get("RemoveBG", 1000))],
71
  ["BRIA RMBG 2.0", int(elo_scores.get("BRIA RMBG 2.0", 1000))],
72
  ]
 
75
  else:
76
  return [
77
  ["Photoroom", -1],
 
78
  ["RemoveBG", -1],
79
  ["BRIA RMBG 2.0", -1],
80
  ]
 
171
 
172
  # Compute the absolute difference between the masks
173
  mask_difference = compute_mask_difference(segmented_a, segmented_b)
174
+ username_input = gr.Textbox(label="Enter your username (optional)", placeholder="Username for prize notification")
175
 
176
  with gr.Row():
177
  image_a_display = gr.Image(
 
200
  vote_tie = gr.Button("🤝 Tie")
201
  vote_b_btn = gr.Button("👉 B is better")
202
 
203
+ def vote_for_model(choice, original_filename, model_a_name, model_b_name, user_username):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
204
  """Submit a vote for a model and return updated images and model names."""
205
  logging.info("Voting for model: %s", choice)
206
  vote_data = {
 
208
  "model_a": model_a_name.value,
209
  "model_b": model_b_name.value,
210
  "winner": choice,
211
+ "user_id": user_username or "anonymous"
212
  }
213
 
214
  try:
 
230
 
231
  return (fpath_input.value, (new_input_image, [(mask_difference, button_name)]), new_segmented_a,
232
  new_segmented_b, model_a_name.value, model_b_name.value, new_notice_markdown)
233
+
234
+ vote_a_btn.click(
235
+ fn=lambda username: vote_for_model("model_a", fpath_input, model_a_name, model_b_name, username),
236
+ inputs=username_input,
237
+ outputs=[
238
+ fpath_input, input_image_display, image_a_display, image_b_display, model_a_name, model_b_name, notice_markdown
239
+ ]
240
+ )
241
+ vote_b_btn.click(
242
+ fn=lambda username: vote_for_model("model_b", fpath_input, model_a_name, model_b_name, username),
243
+ inputs=username_input,
244
+ outputs=[
245
+ fpath_input, input_image_display, image_a_display, image_b_display, model_a_name, model_b_name, notice_markdown
246
+ ]
247
+ )
248
+ vote_tie.click(
249
+ fn=lambda username: vote_for_model("tie", fpath_input, model_a_name, model_b_name, username),
250
+ inputs=username_input,
251
+ outputs=[
252
+ fpath_input, input_image_display, image_a_display, image_b_display, model_a_name, model_b_name, notice_markdown
253
+ ]
254
+ )
255
 
256
  with gr.Tab("🏆 Leaderboard", id=1) as leaderboard_tab:
257
  rankings_table = gr.Dataframe(
 
270
  with gr.Tab("📊 Vote Data", id=2) as vote_data_tab:
271
  def update_vote_data():
272
  votes = get_all_votes()
273
+ return [[vote.id, vote.image_id, vote.model_a, vote.model_b, vote.winner, vote.user_id, vote.timestamp] for vote in votes]
274
 
275
  vote_table = gr.Dataframe(
276
+ headers=["ID", "Image ID", "Model A", "Model B", "Winner", "user_id", "Timestamp"],
277
  value=update_vote_data(),
278
  label="Vote Data",
279
+ column_widths=[20, 150, 100, 100, 100, 100, 150],
280
  row_count=0
281
  )
282
 
 
285
  outputs=vote_table
286
  )
287
 
288
+ with gr.Tab("👥 User Vote Leaderboard", id=3) as user_leaderboard_tab:
289
+ current_time = datetime.now()
290
+ start_of_week = current_time - timedelta(days=current_time.weekday())
291
+
292
+ def get_weekly_user_leaderboard():
293
+ """Get the leaderboard of users with the most votes in the current week, excluding anonymous votes."""
294
+ votes = get_all_votes()
295
+ weekly_votes = [
296
+ vote for vote in votes
297
+ if vote.timestamp >= start_of_week
298
+ and vote.user_id
299
+ and vote.user_id != "anonymous"
300
+ ]
301
+ user_vote_count = {}
302
+ for vote in weekly_votes:
303
+ user_vote_count[vote.user_id] = user_vote_count.get(vote.user_id, 0) + 1
304
+ sorted_users = sorted(user_vote_count.items(), key=lambda x: x[1], reverse=True)
305
+ return [[user, count] for user, count in sorted_users]
306
+
307
+ user_leaderboard_table = gr.Dataframe(
308
+ headers=["User", "Votes"],
309
+ value=get_weekly_user_leaderboard(),
310
+ label="User Vote Leaderboard (This Week)",
311
+ column_widths=[150, 100],
312
+ row_count=0
313
+ )
314
+
315
+ leaderboard_info = gr.Markdown(
316
+ value=f"""
317
+ This leaderboard shows the ranking of users based on the number of votes they have cast in the current week. The current ranking is based on votes cast from {start_of_week.strftime('%Y-%m-%d')} to {current_time.strftime('%Y-%m-%d')}.
318
+ It will be updated each week.
319
+ """
320
+ )
321
+
322
+ user_leaderboard_tab.select(
323
+ fn=lambda: get_weekly_user_leaderboard(),
324
+ outputs=user_leaderboard_table
325
+ )
326
+
327
  return demo
328
 
329
  def dump_database_to_json():