Use hugging face dataset
Browse files
app.py
CHANGED
@@ -13,9 +13,12 @@ from uuid import uuid4
|
|
13 |
import logging
|
14 |
import threading
|
15 |
import time
|
16 |
-
|
17 |
from huggingface_hub import CommitScheduler
|
18 |
|
|
|
|
|
|
|
19 |
# Configure logging
|
20 |
logging.basicConfig(level=logging.INFO)
|
21 |
|
@@ -67,41 +70,41 @@ def update_rankings_table():
|
|
67 |
|
68 |
def select_new_image():
|
69 |
"""Select a new image and its segmented versions."""
|
70 |
-
image_paths = load_images_from_directory("data/web-original-images")
|
71 |
-
random.shuffle(image_paths) # Shuffle images to ensure randomness on reload
|
72 |
-
last_image_path = None
|
73 |
max_attempts = 10
|
|
|
74 |
|
75 |
for _ in range(max_attempts):
|
76 |
-
|
77 |
|
78 |
-
if not
|
79 |
logging.error("No available images to select from.")
|
80 |
return None
|
81 |
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
"RemoveBG": os.path.join("data/resized/removebg", image_filename),
|
90 |
-
"BRIA RMBG 2.0": os.path.join("data/resized/bria", image_filename)
|
91 |
-
}
|
92 |
|
|
|
|
|
|
|
|
|
|
|
93 |
try:
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
return (
|
101 |
-
|
102 |
-
except
|
103 |
-
logging.error("
|
104 |
-
|
105 |
|
106 |
logging.error("Failed to select a new image after %d attempts.", max_attempts)
|
107 |
return None
|
@@ -272,12 +275,6 @@ def gradio_interface():
|
|
272 |
|
273 |
return demo
|
274 |
|
275 |
-
def load_images_from_directory(directory):
|
276 |
-
"""Load and return image paths from a directory."""
|
277 |
-
image_files = [f for f in os.listdir(directory) if f.endswith(('.png', '.jpg', '.jpeg'))]
|
278 |
-
return [os.path.join(directory, f) for f in image_files]
|
279 |
-
|
280 |
-
|
281 |
def dump_database_to_json():
|
282 |
"""Dump the database to a JSON file and upload it to Hugging Face."""
|
283 |
votes = get_all_votes()
|
|
|
13 |
import logging
|
14 |
import threading
|
15 |
import time
|
16 |
+
from datasets import load_dataset
|
17 |
from huggingface_hub import CommitScheduler
|
18 |
|
19 |
+
# Load datasets
|
20 |
+
dataset = load_dataset("bgsys/background-removal-arena-test", split='train')
|
21 |
+
|
22 |
# Configure logging
|
23 |
logging.basicConfig(level=logging.INFO)
|
24 |
|
|
|
70 |
|
71 |
def select_new_image():
|
72 |
"""Select a new image and its segmented versions."""
|
|
|
|
|
|
|
73 |
max_attempts = 10
|
74 |
+
last_image_index = None
|
75 |
|
76 |
for _ in range(max_attempts):
|
77 |
+
available_indices = [i for i in range(len(dataset)) if i != last_image_index]
|
78 |
|
79 |
+
if not available_indices:
|
80 |
logging.error("No available images to select from.")
|
81 |
return None
|
82 |
|
83 |
+
random_index = random.choice(available_indices)
|
84 |
+
sample = dataset[random_index]
|
85 |
+
input_image = sample['original_image']
|
86 |
+
|
87 |
+
segmented_images = [sample['clipdrop_image'], sample['bria_image'],
|
88 |
+
sample['photoroom_image'], sample['removebg_image']]
|
89 |
+
segmented_sources = ['Clipdrop', 'BRIA RMBG 2.0', 'Photoroom', 'RemoveBG']
|
|
|
|
|
|
|
90 |
|
91 |
+
if segmented_images.count(None) > 2:
|
92 |
+
logging.error("Not enough segmented images found for: %s. Resampling another image.", sample['original_filename'])
|
93 |
+
last_image_index = random_index
|
94 |
+
continue
|
95 |
+
|
96 |
try:
|
97 |
+
selected_indices = random.sample([i for i, img in enumerate(segmented_images) if img is not None], 2)
|
98 |
+
model_a_index, model_b_index = selected_indices
|
99 |
+
model_a_output_image = segmented_images[model_a_index]
|
100 |
+
model_b_output_image = segmented_images[model_b_index]
|
101 |
+
model_a_name = segmented_sources[model_a_index]
|
102 |
+
model_b_name = segmented_sources[model_b_index]
|
103 |
+
return (sample['original_image'], input_image, model_a_output_image, model_a_output_image,
|
104 |
+
model_b_output_image, model_b_output_image, model_a_name, model_b_name)
|
105 |
+
except Exception as e:
|
106 |
+
logging.error("Error processing images: %s. Resampling another image.", str(e))
|
107 |
+
last_image_index = random_index
|
108 |
|
109 |
logging.error("Failed to select a new image after %d attempts.", max_attempts)
|
110 |
return None
|
|
|
275 |
|
276 |
return demo
|
277 |
|
|
|
|
|
|
|
|
|
|
|
|
|
278 |
def dump_database_to_json():
|
279 |
"""Dump the database to a JSON file and upload it to Hugging Face."""
|
280 |
votes = get_all_votes()
|