kokuma commited on
Commit
e0d6346
·
verified ·
1 Parent(s): d19f98b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -53
app.py CHANGED
@@ -9,6 +9,7 @@ from PIL import Image
9
  import requests
10
  import torch
11
  import warnings
 
12
 
13
  # import torch.nn.functional as F
14
  import numpy as np
@@ -1139,8 +1140,15 @@ def prepare(raw_idx, text_embeddings, class_order):
1139
 
1140
  choice_values = list(zip(choice_names, choice_values))
1141
 
1142
- warnings.warn(f"prepare correct_choice: {correct_choice}")
1143
- warnings.warn(f"prepare choice_values: {choice_values}")
 
 
 
 
 
 
 
1144
 
1145
  next_radio = gr.Radio(
1146
  choices=choice_values,
@@ -1158,57 +1166,57 @@ def prepare(raw_idx, text_embeddings, class_order):
1158
  return next_radio, next_image, raw_idx, correct_choice, model_choice, choice_values
1159
 
1160
 
1161
- def reroll(raw_idx, text_embeddings, class_order):
1162
- # prepared next question, loads image, and computes choices
1163
 
1164
- idx = class_order[raw_idx]
1165
- lang_class_idxs = babel_imagenet["EN"][0]
1166
- class_idx = lang_class_idxs[idx]
1167
 
1168
- img_idx = 0
1169
- img_idx = np.random.choice(min(len(babelnet_images[class_idx]), max_image_choices))
1170
- img_url = babelnet_images[class_idx][img_idx]["url"]
1171
- class_labels = openai_en_classes
1172
 
1173
- choices = list(
1174
- reversed(precomputed_results["EN"][idx][img_idx])
1175
- ) # precomputing script uses torch.topk which sorts in reverse here
1176
- if idx not in choices:
1177
- choices = [idx] + choices[1:]
1178
- model_choice_idx = choices[-1]
1179
 
1180
- numpy.random.shuffle(choices)
1181
 
1182
- choice_names = [class_labels[idx] for idx in choices]
1183
- choice_values = [0, 1, 2, 3]
1184
 
1185
- model_choice_idx = choices.index(model_choice_idx)
1186
- model_choice = [choice_names[model_choice_idx], choice_values[model_choice_idx]]
1187
- correct_choice_idx = choices.index(idx)
1188
- correct_choice = [
1189
- choice_names[correct_choice_idx],
1190
- choice_values[correct_choice_idx],
1191
- ]
1192
 
1193
- choice_values = list(zip(choice_names, choice_values))
1194
 
1195
- warnings.warn(f"reroll correct_choice: {correct_choice}")
1196
- warnings.warn(f"reroll choice_values: {choice_values}")
1197
 
1198
- next_radio = gr.Radio(
1199
- choices=choice_values,
1200
- interactive=True,
1201
- label="Select the correct answer:",
1202
- value=None,
1203
- )
1204
- next_image = gr.Image(
1205
- value=img_url,
1206
- width=IMG_WIDTH,
1207
- height=IMG_WIDTH,
1208
- label="What class does this image belong to?",
1209
- )
1210
 
1211
- return next_radio, next_image, raw_idx, correct_choice, model_choice, choice_values
1212
 
1213
 
1214
  with gr.Blocks(title="Babel-ImageNet Quiz", css=css) as demo:
@@ -1254,7 +1262,7 @@ with gr.Blocks(title="Babel-ImageNet Quiz", css=css) as demo:
1254
  correct_text = gr.Text("Please click start to begin.")
1255
  player_score_text = gr.Text(f"Player score: 0")
1256
  clip_score_text = gr.Text(f"mSigLIP score: 0")
1257
- reroll_btn = gr.Button(value="Reroll the image (for bad images or errors)")
1258
 
1259
  options.select(
1260
  fn=select,
@@ -1307,14 +1315,14 @@ with gr.Blocks(title="Babel-ImageNet Quiz", css=css) as demo:
1307
  outputs=[options, image, class_idx, correct_choice, model_choice, choices],
1308
  )
1309
 
1310
- reroll_btn.click(
1311
- fn=reroll,
1312
- inputs=[
1313
- class_idx,
1314
- text_embeddings,
1315
- class_order,
1316
- ],
1317
- outputs=[options, image, class_idx, correct_choice, model_choice, choices],
1318
- )
1319
 
1320
  demo.launch()
 
9
  import requests
10
  import torch
11
  import warnings
12
+ import random
13
 
14
  # import torch.nn.functional as F
15
  import numpy as np
 
1140
 
1141
  choice_values = list(zip(choice_names, choice_values))
1142
 
1143
+ correct_tuple = tuple(correct_choice)
1144
+ other_choices = [choice for choice in choice_values if choice != correct_tuple]
1145
+ keep = random.choice(other_choices)
1146
+ new_choice_values = [correct_tuple, keep]
1147
+ random.shuffle(new_choice_values)
1148
+
1149
+ warnings.warn(f"correct_choice: {correct_choice}")
1150
+ warnings.warn(f"choice_values: {choice_values}")
1151
+ warnings.warn(f"correct_choice: {new_choice_values}")
1152
 
1153
  next_radio = gr.Radio(
1154
  choices=choice_values,
 
1166
  return next_radio, next_image, raw_idx, correct_choice, model_choice, choice_values
1167
 
1168
 
1169
+ # def reroll(raw_idx, text_embeddings, class_order):
1170
+ # # prepared next question, loads image, and computes choices
1171
 
1172
+ # idx = class_order[raw_idx]
1173
+ # lang_class_idxs = babel_imagenet["EN"][0]
1174
+ # class_idx = lang_class_idxs[idx]
1175
 
1176
+ # img_idx = 0
1177
+ # img_idx = np.random.choice(min(len(babelnet_images[class_idx]), max_image_choices))
1178
+ # img_url = babelnet_images[class_idx][img_idx]["url"]
1179
+ # class_labels = openai_en_classes
1180
 
1181
+ # choices = list(
1182
+ # reversed(precomputed_results["EN"][idx][img_idx])
1183
+ # ) # precomputing script uses torch.topk which sorts in reverse here
1184
+ # if idx not in choices:
1185
+ # choices = [idx] + choices[1:]
1186
+ # model_choice_idx = choices[-1]
1187
 
1188
+ # numpy.random.shuffle(choices)
1189
 
1190
+ # choice_names = [class_labels[idx] for idx in choices]
1191
+ # choice_values = [0, 1, 2, 3]
1192
 
1193
+ # model_choice_idx = choices.index(model_choice_idx)
1194
+ # model_choice = [choice_names[model_choice_idx], choice_values[model_choice_idx]]
1195
+ # correct_choice_idx = choices.index(idx)
1196
+ # correct_choice = [
1197
+ # choice_names[correct_choice_idx],
1198
+ # choice_values[correct_choice_idx],
1199
+ # ]
1200
 
1201
+ # choice_values = list(zip(choice_names, choice_values))
1202
 
1203
+ # warnings.warn(f"reroll correct_choice: {correct_choice}")
1204
+ # warnings.warn(f"reroll choice_values: {choice_values}")
1205
 
1206
+ # next_radio = gr.Radio(
1207
+ # choices=choice_values,
1208
+ # interactive=True,
1209
+ # label="Select the correct answer:",
1210
+ # value=None,
1211
+ # )
1212
+ # next_image = gr.Image(
1213
+ # value=img_url,
1214
+ # width=IMG_WIDTH,
1215
+ # height=IMG_WIDTH,
1216
+ # label="What class does this image belong to?",
1217
+ # )
1218
 
1219
+ # return next_radio, next_image, raw_idx, correct_choice, model_choice, choice_values
1220
 
1221
 
1222
  with gr.Blocks(title="Babel-ImageNet Quiz", css=css) as demo:
 
1262
  correct_text = gr.Text("Please click start to begin.")
1263
  player_score_text = gr.Text(f"Player score: 0")
1264
  clip_score_text = gr.Text(f"mSigLIP score: 0")
1265
+ # reroll_btn = gr.Button(value="Reroll the image (for bad images or errors)")
1266
 
1267
  options.select(
1268
  fn=select,
 
1315
  outputs=[options, image, class_idx, correct_choice, model_choice, choices],
1316
  )
1317
 
1318
+ # reroll_btn.click(
1319
+ # fn=reroll,
1320
+ # inputs=[
1321
+ # class_idx,
1322
+ # text_embeddings,
1323
+ # class_order,
1324
+ # ],
1325
+ # outputs=[options, image, class_idx, correct_choice, model_choice, choices],
1326
+ # )
1327
 
1328
  demo.launch()