Spaces:
Sleeping
Sleeping
Set options to default values
Browse files
app.py
CHANGED
@@ -1053,8 +1053,7 @@ def change_language(randomize_imgs, randomize_labels):
|
|
1053 |
# compute text embeddings
|
1054 |
labels = babel_imagenet["EN"][1]
|
1055 |
class_order = list(range(len(labels)))
|
1056 |
-
|
1057 |
-
np.random.shuffle(class_order)
|
1058 |
### We use no prompt ensembling for now
|
1059 |
if not precomputed_results:
|
1060 |
text_tokens = tokenizer(labels).to(device)
|
@@ -1110,7 +1109,7 @@ def select(idx, choice, correct, model_choice, player_score, clip_score, choices
|
|
1110 |
return correct_text, player_score_text, clip_score_text, player_score, clip_score
|
1111 |
|
1112 |
|
1113 |
-
def prepare(raw_idx, text_embeddings, class_order
|
1114 |
# prepared next question, loads image, and computes choices
|
1115 |
|
1116 |
raw_idx = (raw_idx + 1) % len(babel_imagenet["EN"][0])
|
@@ -1126,10 +1125,9 @@ def prepare(raw_idx, text_embeddings, class_order, randomize_images):
|
|
1126 |
class_idx = lang_class_idxs[idx]
|
1127 |
|
1128 |
img_idx = 0
|
1129 |
-
|
1130 |
-
|
1131 |
-
|
1132 |
-
)
|
1133 |
img_url = babelnet_images[class_idx][img_idx]["url"]
|
1134 |
class_labels = babel_imagenet["EN"][1] if "EN" != "EN" else openai_en_classes
|
1135 |
|
@@ -1150,7 +1148,7 @@ def prepare(raw_idx, text_embeddings, class_order, randomize_images):
|
|
1150 |
except:
|
1151 |
gr.Warning("There is a problem with the next class. Skipping it.")
|
1152 |
return prepare(
|
1153 |
-
raw_idx, text_embeddings, class_order
|
1154 |
)
|
1155 |
|
1156 |
similarity = (text_embeddings @ image_features.cpu().numpy().T).squeeze()
|
@@ -1194,7 +1192,7 @@ def prepare(raw_idx, text_embeddings, class_order, randomize_images):
|
|
1194 |
return next_radio, next_image, raw_idx, correct_choice, model_choice, choice_values
|
1195 |
|
1196 |
|
1197 |
-
def reroll(raw_idx, text_embeddings, class_order
|
1198 |
# prepared next question, loads image, and computes choices
|
1199 |
|
1200 |
idx = class_order[raw_idx]
|
@@ -1202,10 +1200,9 @@ def reroll(raw_idx, text_embeddings, class_order, randomize_images):
|
|
1202 |
class_idx = lang_class_idxs[idx]
|
1203 |
|
1204 |
img_idx = 0
|
1205 |
-
|
1206 |
-
|
1207 |
-
|
1208 |
-
)
|
1209 |
img_url = babelnet_images[class_idx][img_idx]["url"]
|
1210 |
class_labels = babel_imagenet["EN"][1] if "EN" != "EN" else openai_en_classes
|
1211 |
|
@@ -1226,7 +1223,7 @@ def reroll(raw_idx, text_embeddings, class_order, randomize_images):
|
|
1226 |
except:
|
1227 |
gr.Warning("There is a problem with the next class. Skipping it.")
|
1228 |
return prepare(
|
1229 |
-
raw_idx, text_embeddings, class_order
|
1230 |
)
|
1231 |
|
1232 |
similarity = (text_embeddings @ image_features.cpu().numpy().T).squeeze()
|
@@ -1298,13 +1295,13 @@ with gr.Blocks(title="Babel-ImageNet Quiz") as demo:
|
|
1298 |
# interactive=True,
|
1299 |
# label="Select your language:",
|
1300 |
# )
|
1301 |
-
randomize_classes = gr.Checkbox(
|
1302 |
-
|
1303 |
-
)
|
1304 |
-
randomize_images = gr.Checkbox(
|
1305 |
-
|
1306 |
-
|
1307 |
-
)
|
1308 |
start_btn = gr.Button(value="Start", variant="primary")
|
1309 |
|
1310 |
# quiz area
|
@@ -1350,14 +1347,13 @@ with gr.Blocks(title="Babel-ImageNet Quiz") as demo:
|
|
1350 |
class_idx,
|
1351 |
text_embeddings,
|
1352 |
class_order,
|
1353 |
-
randomize_images,
|
1354 |
],
|
1355 |
outputs=[options, image, class_idx, correct_choice, model_choice, choices],
|
1356 |
)
|
1357 |
|
1358 |
start_btn.click(
|
1359 |
fn=change_language,
|
1360 |
-
inputs=[
|
1361 |
outputs=[
|
1362 |
text_embeddings,
|
1363 |
class_idx,
|
@@ -1374,7 +1370,6 @@ with gr.Blocks(title="Babel-ImageNet Quiz") as demo:
|
|
1374 |
class_idx,
|
1375 |
text_embeddings,
|
1376 |
class_order,
|
1377 |
-
randomize_images,
|
1378 |
],
|
1379 |
outputs=[options, image, class_idx, correct_choice, model_choice, choices],
|
1380 |
)
|
@@ -1385,7 +1380,6 @@ with gr.Blocks(title="Babel-ImageNet Quiz") as demo:
|
|
1385 |
class_idx,
|
1386 |
text_embeddings,
|
1387 |
class_order,
|
1388 |
-
randomize_images,
|
1389 |
],
|
1390 |
outputs=[options, image, class_idx, correct_choice, model_choice, choices],
|
1391 |
)
|
|
|
1053 |
# compute text embeddings
|
1054 |
labels = babel_imagenet["EN"][1]
|
1055 |
class_order = list(range(len(labels)))
|
1056 |
+
np.random.shuffle(class_order)
|
|
|
1057 |
### We use no prompt ensembling for now
|
1058 |
if not precomputed_results:
|
1059 |
text_tokens = tokenizer(labels).to(device)
|
|
|
1109 |
return correct_text, player_score_text, clip_score_text, player_score, clip_score
|
1110 |
|
1111 |
|
1112 |
+
def prepare(raw_idx, text_embeddings, class_order):
|
1113 |
# prepared next question, loads image, and computes choices
|
1114 |
|
1115 |
raw_idx = (raw_idx + 1) % len(babel_imagenet["EN"][0])
|
|
|
1125 |
class_idx = lang_class_idxs[idx]
|
1126 |
|
1127 |
img_idx = 0
|
1128 |
+
img_idx = np.random.choice(
|
1129 |
+
min(len(babelnet_images[class_idx]), max_image_choices)
|
1130 |
+
)
|
|
|
1131 |
img_url = babelnet_images[class_idx][img_idx]["url"]
|
1132 |
class_labels = babel_imagenet["EN"][1] if "EN" != "EN" else openai_en_classes
|
1133 |
|
|
|
1148 |
except:
|
1149 |
gr.Warning("There is a problem with the next class. Skipping it.")
|
1150 |
return prepare(
|
1151 |
+
raw_idx, text_embeddings, class_order
|
1152 |
)
|
1153 |
|
1154 |
similarity = (text_embeddings @ image_features.cpu().numpy().T).squeeze()
|
|
|
1192 |
return next_radio, next_image, raw_idx, correct_choice, model_choice, choice_values
|
1193 |
|
1194 |
|
1195 |
+
def reroll(raw_idx, text_embeddings, class_order):
|
1196 |
# prepared next question, loads image, and computes choices
|
1197 |
|
1198 |
idx = class_order[raw_idx]
|
|
|
1200 |
class_idx = lang_class_idxs[idx]
|
1201 |
|
1202 |
img_idx = 0
|
1203 |
+
img_idx = np.random.choice(
|
1204 |
+
min(len(babelnet_images[class_idx]), max_image_choices)
|
1205 |
+
)
|
|
|
1206 |
img_url = babelnet_images[class_idx][img_idx]["url"]
|
1207 |
class_labels = babel_imagenet["EN"][1] if "EN" != "EN" else openai_en_classes
|
1208 |
|
|
|
1223 |
except:
|
1224 |
gr.Warning("There is a problem with the next class. Skipping it.")
|
1225 |
return prepare(
|
1226 |
+
raw_idx, text_embeddings, class_order
|
1227 |
)
|
1228 |
|
1229 |
similarity = (text_embeddings @ image_features.cpu().numpy().T).squeeze()
|
|
|
1295 |
# interactive=True,
|
1296 |
# label="Select your language:",
|
1297 |
# )
|
1298 |
+
# randomize_classes = gr.Checkbox(
|
1299 |
+
# label="Randomize class order (or play in canonic order)", value=True
|
1300 |
+
# )
|
1301 |
+
# randomize_images = gr.Checkbox(
|
1302 |
+
# label="Randomize images (if unchecked, will always show the same image). Other images might be less relevant.",
|
1303 |
+
# value=True,
|
1304 |
+
# )
|
1305 |
start_btn = gr.Button(value="Start", variant="primary")
|
1306 |
|
1307 |
# quiz area
|
|
|
1347 |
class_idx,
|
1348 |
text_embeddings,
|
1349 |
class_order,
|
|
|
1350 |
],
|
1351 |
outputs=[options, image, class_idx, correct_choice, model_choice, choices],
|
1352 |
)
|
1353 |
|
1354 |
start_btn.click(
|
1355 |
fn=change_language,
|
1356 |
+
inputs=[],
|
1357 |
outputs=[
|
1358 |
text_embeddings,
|
1359 |
class_idx,
|
|
|
1370 |
class_idx,
|
1371 |
text_embeddings,
|
1372 |
class_order,
|
|
|
1373 |
],
|
1374 |
outputs=[options, image, class_idx, correct_choice, model_choice, choices],
|
1375 |
)
|
|
|
1380 |
class_idx,
|
1381 |
text_embeddings,
|
1382 |
class_order,
|
|
|
1383 |
],
|
1384 |
outputs=[options, image, class_idx, correct_choice, model_choice, choices],
|
1385 |
)
|