File size: 24,065 Bytes
4dccf1d
f50f18c
 
 
4dccf1d
f50f18c
 
b58cdd5
c098942
f50f18c
4dccf1d
f50f18c
 
dc5f6ab
9248120
4dccf1d
f50f18c
 
 
 
 
5fef682
 
f50f18c
8436088
30b7bc6
97067cd
 
a5da205
f349942
658df6d
4dccf1d
 
 
97067cd
8436088
97067cd
4dccf1d
97067cd
9248120
 
 
97067cd
a5da205
97067cd
 
 
 
a5da205
97067cd
4dccf1d
 
 
 
892f774
4dccf1d
892f774
4dccf1d
 
 
 
 
892f774
 
 
5fef682
892f774
8436088
892f774
 
 
 
 
 
 
 
 
 
 
5fef682
 
 
97067cd
4dccf1d
c098942
4dccf1d
 
c098942
 
 
 
 
4dccf1d
c098942
 
 
 
 
4dccf1d
dc5f6ab
4dccf1d
c098942
4dccf1d
dc5f6ab
 
 
 
8436088
 
 
 
 
 
 
 
4dccf1d
dc5f6ab
 
 
 
4dccf1d
dc5f6ab
 
c098942
 
 
 
97067cd
c098942
97067cd
c098942
 
97067cd
dc5f6ab
 
4dccf1d
 
c098942
4dccf1d
 
 
 
 
 
 
 
 
 
 
 
 
7f0069a
4dccf1d
683fa1b
4dccf1d
23f5054
 
 
 
 
 
 
 
 
 
 
 
 
 
4dccf1d
60d2a4b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23f5054
60d2a4b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f349942
23a28c1
 
 
 
 
 
 
 
 
 
f349942
 
8dec72a
 
 
 
 
 
 
f349942
4dccf1d
 
f349942
c098942
 
 
d19c70c
4dccf1d
 
8dec72a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4dccf1d
078c9a8
af422ae
7f0069a
bae2dd5
 
9d0dec9
 
 
 
af422ae
 
 
9d0dec9
c098942
9d0dec9
c098942
 
 
 
 
 
9d0dec9
 
 
 
 
 
 
 
af422ae
 
9d0dec9
23f5054
af422ae
 
9d0dec9
 
c098942
9d0dec9
c098942
9d0dec9
 
 
4dccf1d
a5da205
 
 
4dccf1d
c098942
4dccf1d
ae51348
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4dccf1d
bb26f6a
4dccf1d
 
 
b58cdd5
4dccf1d
ae51348
4dccf1d
c2e7b5b
 
 
 
 
 
 
 
 
 
 
4dccf1d
 
d19c70c
 
4dccf1d
 
 
c098942
4dccf1d
 
bae2dd5
56dd245
7f0069a
a5da205
c098942
 
 
 
b58cdd5
bae2dd5
c098942
b58cdd5
 
a5da205
c098942
 
 
 
b58cdd5
bae2dd5
c098942
b58cdd5
 
a5da205
c098942
 
 
 
b58cdd5
bae2dd5
c098942
b58cdd5
 
60d2a4b
56dd245
 
4dccf1d
 
892f774
4dccf1d
 
892f774
4dccf1d
 
 
 
 
 
 
5fef682
 
 
892f774
 
 
5fef682
892f774
5fef682
 
4dccf1d
 
 
 
b58cdd5
4dccf1d
 
b58cdd5
4dccf1d
 
b58cdd5
4dccf1d
 
 
 
 
 
 
 
b506727
b58cdd5
 
 
 
 
 
 
 
9373955
b58cdd5
 
 
 
 
b506727
 
 
 
 
 
 
 
 
b58cdd5
 
 
 
 
 
 
 
 
 
 
9373955
b58cdd5
 
 
 
 
 
 
c098942
 
 
 
 
 
 
 
4dccf1d
 
9248120
 
30b7bc6
 
 
 
9248120
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a5da205
9248120
 
 
 
 
 
 
 
 
 
 
 
 
 
30b7bc6
 
 
 
 
 
 
9248120
60d2a4b
 
 
4dccf1d
9248120
4dccf1d
60d2a4b
cfee822
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
import os
import json
import time
import random
import logging
import threading
from pathlib import Path
from datetime import datetime, timedelta
from collections import deque

import numpy as np
import gradio as gr
from dotenv import load_dotenv
from datasets import load_dataset
from huggingface_hub import CommitScheduler

from db import (
    compute_elo_scores,
    get_all_votes,
    add_vote,
    is_running_in_space,
    fill_database_once,
    compute_votes_per_model
)
from utils.birefnet import iterate_over_directory as birefnet_iterate

# Load environment variables
load_dotenv()
huggingface_token = os.getenv("HUGGINGFACE_HUB_TOKEN")
google_analytics_tracking_id = os.getenv("GOOGLE_ANALYTICS_TRACKING_ID")

# Configure logging
logging.basicConfig(level=logging.INFO)

# Load datasets and initialize database
dataset = load_dataset("bgsys/background-removal-arena_v0_clothing_checkered", split='train')
fill_database_once()

# Directory setup for JSON dataset
JSON_DATASET_DIR = Path("data/json_dataset")
JSON_DATASET_DIR.mkdir(parents=True, exist_ok=True)

# Initialize CommitScheduler if running in space
commit_scheduler = CommitScheduler(
    repo_id="bgsys/votes_datasets_test2",
    repo_type="dataset",
    folder_path=JSON_DATASET_DIR,
    path_in_repo="data",
    token=huggingface_token
) if is_running_in_space() else None

def fetch_elo_scores():
    """Fetch and log Elo scores."""
    try:
        median_elo_scores, model_rating_q025, model_rating_q975, variance = compute_elo_scores()
        logging.info("Elo scores successfully computed.")
        return median_elo_scores, model_rating_q025, model_rating_q975, variance
    except Exception as e:
        logging.error("Error computing Elo scores: %s", str(e))
        return None

def update_rankings_table():
    """Update and return the rankings table based on Elo scores and vote counts."""
    median_elo_scores, model_rating_q025, model_rating_q975, variance = fetch_elo_scores() or {}
    model_vote_counts = compute_votes_per_model()
    try:
        # Create a list of models to iterate over
        models = ["Clipdrop", "Photoroom", "RemoveBG", "BRIA RMBG 2.0", "BiRefNet v2"]
        rankings = []

        for model in models:
            elo_score = int(median_elo_scores.get(model, 0))
            model_variance = int(variance.get(model, 0))
            ci_95 = f"{int(model_rating_q025.get(model, 0))} - {int(model_rating_q975.get(model, 0))}"
            vote_count = model_vote_counts.get(model, 0)
            rankings.append([model, elo_score, model_variance, ci_95, vote_count])

        # Sort rankings by Elo score in descending order
        rankings.sort(key=lambda x: x[1], reverse=True)
    except KeyError as e:
        logging.error("Missing score for model: %s", str(e))
        return []
    return rankings

def select_new_image(last_used_indices):
    """Select a new image and its segmented versions."""
    max_attempts = 10
    
    # Initialize empty deque if None
    if last_used_indices is None:
        last_used_indices = deque(maxlen=10)
    
    for _ in range(max_attempts):
        # Filter out recently used indices
        available_indices = [
            i for i in range(len(dataset)) 
            if i not in last_used_indices
        ]
        
        if not available_indices:
            logging.error("No available images to select from.")
            return None, last_used_indices

        random_index = random.choice(available_indices)
        sample = dataset[random_index]
        input_image = sample['original_image']

        segmented_images = [sample.get(key) for key in [
            'clipdrop_image', 'bria_image', 'photoroom_image', 
            'removebg_image', 'birefnet_image'
        ]]
        segmented_sources = [
            'Clipdrop', 'BRIA RMBG 2.0', 'Photoroom', 
            'RemoveBG', 'BiRefNet v2'
        ]
        
        if segmented_images.count(None) > 2:
            logging.error("Not enough segmented images found for: %s. Resampling another image.", sample['original_filename'])
            continue

        try:
            selected_indices = random.sample([i for i, img in enumerate(segmented_images) if img is not None], 2)
            model_a_index, model_b_index = selected_indices
            
            # Add the used index to our history
            last_used_indices.append(random_index)
            
            return (
                (sample['original_filename'], input_image,
                segmented_images[model_a_index], segmented_images[model_b_index],
                segmented_sources[model_a_index], segmented_sources[model_b_index]),
                last_used_indices
            )
        except Exception as e:
            logging.error("Error processing images: %s. Resampling another image.", str(e))

    logging.error("Failed to select a new image after %d attempts.", max_attempts)
    return None, last_used_indices

def get_notice_markdown():
    """Generate the notice markdown with dynamic vote count."""
    total_votes = len(get_all_votes())
    return f"""

    ## 📜 How It Works
    - **Blind Test**: You will see two images with their background removed from two anonymous background removal models (Clipdrop, RemoveBG, Photoroom, BRIA RMBG 2.0).
    - **Vote for the Best**: Choose the best result, if none stand out choose "Tie". 

    ## 📊 Stats
    - **Total #votes**: {total_votes}


    """

def compute_mask_difference(segmented_a, segmented_b):
    """Compute the difference between two images across all channels."""
    mask_a = np.asarray(segmented_a, dtype=np.float32) / 255.0
    mask_b = np.asarray(segmented_b, dtype=np.float32) / 255.0
    
    # Compute absolute difference across all channels (RGBA)
    difference = np.abs(mask_a - mask_b)
    
    # Take the maximum difference across channels at each pixel
    max_diff = np.max(difference, axis=-1)
    
    # Apply non-linear transformation to enhance visibility of smaller differences
    # Using square root to compress the range while maintaining continuity
    # Multiply by 3 to make it more visible
    return np.sqrt(max_diff) * 3

js = r"""
function load_zoom() {
    setTimeout(function() {

    // Select all images from the three displayed image containers.
    const images = document.querySelectorAll('.image-container img');

    // Set transform origin so scaling and translating feels "natural".
    images.forEach(img => {
        img.style.transformOrigin = 'top left';
        img.style.transition = 'transform 0.1s ease-out';
        img.style.cursor = 'zoom-in';
    });

    // Choose a scale factor
    const scale = 5;

    function handleMouseMove(e) {
        const rect = e.currentTarget.getBoundingClientRect();
        const xPercent = (e.clientX - rect.left) / rect.width;
        const yPercent = (e.clientY - rect.top) / rect.height;
        const offsetX = xPercent * (scale - 1) * 100;
        const offsetY = yPercent * (scale - 1) * 100;

        images.forEach(img => {
            img.style.transform = `translate(-${offsetX}%, -${offsetY}%) scale(${scale})`;
        });
    }

    function handleMouseEnter(e) {
        e.currentTarget.addEventListener('mousemove', handleMouseMove);
    }

    function handleMouseLeave(e) {
        e.currentTarget.removeEventListener('mousemove', handleMouseMove);
        images.forEach(img => {
            img.style.transform = 'translate(0,0) scale(1)';
        });
    }

    const containers = document.querySelectorAll('.image-container');

    containers.forEach(container => {
        container.addEventListener('mouseenter', handleMouseEnter);
        container.addEventListener('mouseleave', handleMouseLeave);
    });
}, 1000); // 1 second timeout
}
"""

head = """

<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-7EKCV97SG2"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', '""" + f"{google_analytics_tracking_id}" + """');
</script>
"""

def get_default_username(profile: gr.OAuthProfile | None) -> str:
    """
    Returns the username if the user is logged in, or an empty string if not logged in.
    """
    if profile is None:
        return ""
    return profile.username

def gradio_interface():
    """Create and return the Gradio interface."""
    with gr.Blocks(js=js, head=head, fill_width=True) as demo:
        # Initialize session state for last used indices
        last_used_indices_state = gr.State()
        
        button_name = "Difference between masks"

        with gr.Tabs() as tabs:
            with gr.Row(equal_height=True):
                    def on_enter_contest(username):
                        feedback_message = f"Thank you, {username or 'anonymous'}! You can see how you rank in the Hall of Fame."
                        logging.info(feedback_message)
                        return feedback_message

                    with gr.Column(scale=1):
                        username_input = gr.Textbox(
                            label="Enter your username (optional)",
                            placeholder="✨ Enter your username (optional)",
                            show_label=False,
                            submit_btn="Enter",
                            interactive=True
                        )
                    demo.load(fn=get_default_username, inputs=None, outputs=username_input)


                    with gr.Column(scale=3):
                        feedback_output = gr.Textbox(
                            label="Feedback",
                            interactive=False,
                            show_label=False
                        )

                    with gr.Column(scale=1):
                         gr.LoginButton()
                    

                    username_input.submit(
                        fn=on_enter_contest,
                        inputs=username_input,
                        outputs=feedback_output
                    )
                    
            with gr.Tab("⚔️ Arena (battle)", id=0):
                image_width = None
                image_height = 600  # Limit image height to fit on a standard screen
              

                with gr.Row():
                    # Initialize components with empty states
                    state_filename = gr.State("")
                    state_model_a_name = gr.State("")
                    state_model_b_name = gr.State("")
                    image_a = gr.Image(label="Image A", width=image_width, height=image_height)
                    input_image_display = gr.AnnotatedImage(label="Input Image", width=image_width, height=image_height)
                    image_b = gr.Image(label="Image B", width=image_width, height=image_height)

                    def refresh_states(state_filename, state_model_a_name, state_model_b_name, last_used_indices):
                        # Call select_new_image to get new image data
                        result, new_last_used_indices = select_new_image(last_used_indices)
                        if result is None:
                            return [state_filename, image_a, image_b, state_model_a_name, state_model_b_name, 
                                   input_image_display, new_last_used_indices]
                            
                        filename, input_image, segmented_a, segmented_b, model_a_name, model_b_name = result
                        mask_difference = compute_mask_difference(segmented_a, segmented_b)
                        
                        # Update states with new data
                        state_filename.value = filename
                        state_model_a_name.value = model_a_name
                        state_model_b_name.value = model_b_name

                        # Create new gr.Image components with updated values
                        image_a = gr.Image(value=segmented_a, label="Image A", width=image_width, height=image_height)
                        image_b = gr.Image(value=segmented_b, label="Image B", width=image_width, height=image_height)
                        input_image_display = gr.AnnotatedImage(
                            value=(input_image, [(mask_difference, button_name)]), 
                            width=image_width,
                            height=image_height
                        )
                        
                        return [
                            state_filename, image_a, image_b, state_model_a_name, state_model_b_name, 
                            input_image_display, new_last_used_indices
                        ]

                    
                with gr.Row():
                    vote_a_button = gr.Button("👈  A is better")
                    vote_tie_button = gr.Button("🤝  Tie")
                    vote_b_button = gr.Button("👉  B is better")

                def vote_for_model(choice, original_filename, model_a_name, model_b_name, user_username, last_used_indices):
                    """Submit a vote for a model and return updated images and model names."""
                  

                    if not original_filename.value:
                        logging.error("The field 'original_filename' is empty or None.")
                        raise ValueError("The field 'original_filename' must be provided and non-empty.")
                    if not model_a_name.value:
                        logging.error("The field 'model_a_name' is empty or None.")
                        raise ValueError("The field 'model_a_name' must be provided and non-empty.")
                    if not model_b_name.value:
                        logging.error("The field 'model_b_name' is empty or None.")
                        raise ValueError("The field 'model_b_name' must be provided and non-empty.")
                    if not choice:
                        logging.error("The field 'choice' is empty or None.")
                        raise ValueError("The field 'choice' must be provided and non-empty.")

                    vote_data = {
                        "image_id": original_filename.value,
                        "model_a": model_a_name.value,
                        "model_b": model_b_name.value,
                        "winner": choice,
                        "user_id": user_username or "anonymous"
                    }
                    logging.debug(vote_data)

                     # Create a gr.Info message with model names and the user's choice
                    voted_model = vote_data[vote_data["winner"]] if vote_data["winner"] in ["model_a", "model_b"] else "Tie"
                    voted_model_emoji = "👈" if choice == "model_a" else "👉" if choice == "model_b" else "🤝"
                    voted_model_color = "green" if choice == "model_a" else "blue" if choice == "model_b" else "gray"
                    info_message = (
                        f"<p>You voted for <strong style='color:{voted_model_color};'>{voted_model_emoji} {voted_model}</strong>.</p>"
                        f"<p><span style='color:green;'>👈 {model_a_name.value}</span> - "
                        f"<span style='color:blue;'>👉 {model_b_name.value}</span></p>"
                    )
                    gr.Info(info_message)

                    try:
                        logging.debug("Adding vote data to the database: %s", vote_data)
                        result = add_vote(vote_data)
                        logging.info("Vote successfully recorded in the database with ID: %s", result["id"])
                    except Exception as e:
                        logging.error("Error recording vote: %s", str(e))

                    outputs = refresh_states(state_filename, state_model_a_name, state_model_b_name, last_used_indices)
                    new_notice_markdown = get_notice_markdown()

                    return outputs + [new_notice_markdown]
                            
                notice_markdown = gr.Markdown(get_notice_markdown(), elem_id="notice_markdown")
                vote_a_button.click(
                    fn=lambda username, last_used_indices: vote_for_model(
                        "model_a", state_filename, state_model_a_name, state_model_b_name, username, last_used_indices
                    ),
                    inputs=[username_input, last_used_indices_state],
                    outputs=[
                        state_filename, image_a, image_b, state_model_a_name, state_model_b_name, 
                        input_image_display, last_used_indices_state, notice_markdown
                    ]
                )
                vote_b_button.click(
                    fn=lambda username, last_used_indices: vote_for_model(
                        "model_b", state_filename, state_model_a_name, state_model_b_name, username, last_used_indices
                    ),
                    inputs=[username_input, last_used_indices_state],
                    outputs=[
                        state_filename, image_a, image_b, state_model_a_name, state_model_b_name, 
                        input_image_display, last_used_indices_state, notice_markdown
                    ]
                )
                vote_tie_button.click(
                    fn=lambda username, last_used_indices: vote_for_model(
                        "tie", state_filename, state_model_a_name, state_model_b_name, username, last_used_indices
                    ),
                    inputs=[username_input, last_used_indices_state],
                    outputs=[
                        state_filename, image_a, image_b, state_model_a_name, state_model_b_name, 
                        input_image_display, last_used_indices_state, notice_markdown
                    ]
                )
            
               

            with gr.Tab("🏆 Leaderboard", id=1) as leaderboard_tab:
                rankings_table = gr.Dataframe(
                    headers=["Model", "Elo score", "Variance", "95% CI", "Selections"],
                    value=update_rankings_table(),
                    label="Current Model Rankings",
                    column_widths=[180, 60, 60, 60, 60],
                    row_count=4
                )

                leaderboard_tab.select(
                    fn=lambda: update_rankings_table(),
                    outputs=rankings_table
                )

                # Explanation of Bootstrapped Elo Score
                explanation_text = """
                The Elo score was calculated using bootstrapping with num_rounds=1000. This method provides a 
                distribution of Elo scores by repeatedly sampling the data, which helps in 
                understanding the variability and confidence in the model's ranking.

                We used the approach from the Chatbot Arena [rating system code](https://github.com/lm-sys/FastChat/blob/main/fastchat/serve/monitor/rating_systems.py#L153).
                """
                gr.Markdown(explanation_text)

            with gr.Tab("📊 Vote Data", id=2) as vote_data_tab:
                def update_vote_data():
                    votes = get_all_votes()
                    return [[vote.id, vote.image_id, vote.model_a, vote.model_b, vote.winner, vote.user_id, vote.timestamp] for vote in votes]

                vote_table = gr.Dataframe(
                    headers=["ID", "Image ID", "Model A", "Model B", "Winner", "user_id", "Timestamp"],
                    value=update_vote_data(),
                    label="Vote Data",
                    column_widths=[20, 150, 100, 100, 100, 100, 150],
                    row_count=0
                )

                vote_data_tab.select(
                    fn=lambda: update_vote_data(),
                    outputs=vote_table
                )

            with gr.Tab("👥 Hall of Fame", id=3) as user_leaderboard_tab:
                current_time = datetime.now()
                start_of_week = current_time - timedelta(days=current_time.weekday())

                def get_weekly_user_leaderboard():
                    """Get the leaderboard of users with the most votes in the current week, excluding anonymous votes."""
                    votes = get_all_votes()
                    weekly_votes = [
                        vote for vote in votes 
                        if vote.user_id and vote.user_id != "anonymous"
                    ]
                    user_vote_count = {}
                    for vote in weekly_votes:
                        user_vote_count[vote.user_id] = user_vote_count.get(vote.user_id, 0) + 1
                    sorted_users = sorted(user_vote_count.items(), key=lambda x: x[1], reverse=True)
                    
                    # Add medals for the top 3 users
                    medals = ["🥇", "🥈", "🥉"]
                    leaderboard = []
                    for index, (user, count) in enumerate(sorted_users):
                        medal = medals[index] if index < len(medals) else ""
                        leaderboard.append([f"{medal} {user}", count])
                    
                    return leaderboard

                user_leaderboard_table = gr.Dataframe(
                    headers=["User", "Votes"],
                    value=get_weekly_user_leaderboard(),
                    label="User Vote Leaderboard (This Week)",
                    column_widths=[150, 100],
                    row_count=0
                )

                leaderboard_info = gr.Markdown(
                    value=f"""
                    This leaderboard shows the ranking of users based on the number of votes they have cast since the start of the arena.
                    """
                )

                user_leaderboard_tab.select(
                    fn=lambda: get_weekly_user_leaderboard(),
                    outputs=user_leaderboard_table
                )
        demo.load(
            lambda: refresh_states(state_filename, state_model_a_name, state_model_b_name, None), 
            inputs=None, 
            outputs=[
                state_filename, image_a, image_b, state_model_a_name, state_model_b_name, 
                input_image_display, last_used_indices_state
            ]
        )
    return demo

def dump_database_to_json():
    """Dump the database to a JSON file and upload it to Hugging Face."""
    if not is_running_in_space():
        logging.info("Not running in Hugging Face Spaces. Skipping database dump.")
        return

    votes = get_all_votes()
    json_data = [
        {
            "id": vote.id,
            "image_id": vote.image_id,
            "model_a": vote.model_a,
            "model_b": vote.model_b,
            "winner": vote.winner,
            "user_id": vote.user_id,
            "timestamp": vote.timestamp.isoformat()
        }
        for vote in votes
    ]

    json_file_path = JSON_DATASET_DIR / "votes.json"
    # Upload to Hugging Face
    with commit_scheduler.lock:
        with json_file_path.open("w") as f:
            json.dump(json_data, f, indent=4)

    logging.info("Database dumped to JSON")

def schedule_dump_database(interval=60):
    """Schedule the database dump to JSON every specified interval in seconds."""
    def run():
        while True:
            logging.info("Starting database dump to JSON.")
            dump_database_to_json()
            logging.info("Database dump completed. Sleeping for %d seconds.", interval)
            time.sleep(interval)

    if is_running_in_space():
        logging.info("Initializing database dump scheduler with interval: %d seconds.", interval)
        thread = threading.Thread(target=run, daemon=True)
        thread.start()
        logging.info("Database dump scheduler started.")
    else:
        logging.info("Not running in Hugging Face Spaces. Database dump scheduler not started.")




if __name__ == "__main__":
    schedule_dump_database()  # Start the periodic database dump
    demo = gradio_interface()

    demo.launch()