kokuma commited on
Commit
2f402b2
·
verified ·
1 Parent(s): be12f0c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -40
app.py CHANGED
@@ -1046,42 +1046,34 @@ request_header = {
1046
  }
1047
 
1048
 
1049
- def change_language(randomize_imgs, randomize_labels):
1050
  # compute text embeddings
1051
  labels = babel_imagenet["EN"][1]
1052
  class_order = list(range(len(labels)))
1053
  np.random.shuffle(class_order)
1054
- text_features = None
1055
  correct_text = gr.Text(
1056
  f"Correct was: ''. Question 1/{len(babel_imagenet['EN'][0])} ", label="Game"
1057
  )
1058
  player_score_text = gr.Text(f"Your choice: (Score: 0) ", label="Player")
1059
- clip_score_text = gr.Text(f"mSigLIP chose: '' (Score: 0)", label="Opponent")
1060
 
1061
  return (
1062
- text_features,
1063
  -1,
1064
  class_order,
1065
  correct_text,
1066
  player_score_text,
1067
- clip_score_text,
1068
- 0,
1069
  0,
1070
  )
1071
 
1072
 
1073
- def select(idx, choice, correct, model_choice, player_score, clip_score, choices):
1074
- # checks if answer choice is correct and updated scores
1075
  correct_name, correct_value = correct
1076
- model_choice_name, model_choice_value = model_choice
1077
  player_choice = choices[choice][0]
1078
-
1079
  player_correct = choice == correct_value
1080
- model_correct = model_choice_value == correct_value
1081
-
1082
  player_score = player_score + int(player_correct)
1083
- clip_score = clip_score + int(model_correct)
1084
-
1085
  correct_text = gr.Text(
1086
  f"Correct was: '{correct_name}'. Question {idx+1}/{len(babel_imagenet['EN'][0])} ",
1087
  label="Game",
@@ -1090,15 +1082,11 @@ def select(idx, choice, correct, model_choice, player_score, clip_score, choices
1090
  f"Your choice: {player_choice} {'✅' if player_correct else '❌'} (Score: {player_score}) ",
1091
  label="Player",
1092
  )
1093
- clip_score_text = gr.Text(
1094
- f"mSigLIP chose: '{model_choice_name}' {'✅' if model_correct else '❌'} (Score: {clip_score})",
1095
- label="Opponent",
1096
- )
1097
 
1098
- return correct_text, player_score_text, clip_score_text, player_score, clip_score
1099
 
1100
 
1101
- def prepare(raw_idx, text_embeddings, class_order):
1102
  # prepared next question, loads image, and computes choices
1103
 
1104
  raw_idx = (raw_idx + 1) % len(babel_imagenet["EN"][0])
@@ -1123,15 +1111,15 @@ def prepare(raw_idx, text_embeddings, class_order):
1123
  ) # precomputing script uses torch.topk which sorts in reverse here
1124
  if idx not in choices:
1125
  choices = [idx] + choices[1:]
1126
- model_choice_idx = choices[-1]
1127
 
1128
  numpy.random.shuffle(choices)
1129
 
1130
  choice_names = [class_labels[idx] for idx in choices]
1131
  choice_values = [0, 1, 2, 3]
1132
 
1133
- model_choice_idx = choices.index(model_choice_idx)
1134
- model_choice = [choice_names[model_choice_idx], choice_values[model_choice_idx]]
1135
  correct_choice_idx = choices.index(idx)
1136
  correct_choice = [
1137
  choice_names[correct_choice_idx],
@@ -1146,7 +1134,11 @@ def prepare(raw_idx, text_embeddings, class_order):
1146
  new_choice_values = [correct_tuple, keep]
1147
  random.shuffle(new_choice_values)
1148
 
1149
- warnings.warn(f"model_choice: {model_choice}\n\n")
 
 
 
 
1150
 
1151
  next_radio = gr.Radio(
1152
  choices=new_choice_values,
@@ -1161,7 +1153,14 @@ def prepare(raw_idx, text_embeddings, class_order):
1161
  label="What class does this image belong to?",
1162
  )
1163
 
1164
- return next_radio, next_image, raw_idx, correct_choice, model_choice, new_choice_values
 
 
 
 
 
 
 
1165
 
1166
 
1167
  with gr.Blocks(title="Babel-ImageNet Quiz", css=css) as demo:
@@ -1169,13 +1168,13 @@ with gr.Blocks(title="Babel-ImageNet Quiz", css=css) as demo:
1169
  # setup state
1170
  class_idx = gr.State(-1)
1171
  player_score = gr.State(0)
1172
- clip_score = gr.State(0)
1173
  class_order = gr.State([])
1174
  choices = gr.State([])
1175
 
1176
- text_embeddings = gr.State(None)
1177
  correct_choice = gr.State(["nan", 0]) # 0, 1, 2, 3
1178
- model_choice = gr.State(["nan", 0])
1179
 
1180
  # Title Area
1181
  gr.Markdown(
@@ -1206,7 +1205,7 @@ with gr.Blocks(title="Babel-ImageNet Quiz", css=css) as demo:
1206
  # with gr.Row():
1207
  correct_text = gr.Text("Please click start to begin.")
1208
  player_score_text = gr.Text(f"Player score: 0")
1209
- clip_score_text = gr.Text(f"mSigLIP score: 0")
1210
 
1211
  options.select(
1212
  fn=select,
@@ -1214,49 +1213,48 @@ with gr.Blocks(title="Babel-ImageNet Quiz", css=css) as demo:
1214
  class_idx,
1215
  options,
1216
  correct_choice,
1217
- model_choice,
1218
  player_score,
1219
- clip_score,
1220
  choices,
1221
  ],
1222
  outputs=[
1223
  correct_text,
1224
  player_score_text,
1225
- clip_score_text,
1226
  player_score,
1227
- clip_score,
1228
  ],
1229
  ).then(
1230
  fn=prepare,
1231
  inputs=[
1232
  class_idx,
1233
- text_embeddings,
1234
  class_order,
1235
  ],
1236
- outputs=[options, image, class_idx, correct_choice, model_choice, choices],
1237
  )
1238
 
1239
  start_btn.click(
1240
  fn=change_language,
1241
  inputs=[],
1242
  outputs=[
1243
- text_embeddings,
1244
  class_idx,
1245
  class_order,
1246
  correct_text,
1247
  player_score_text,
1248
- clip_score_text,
1249
  player_score,
1250
- clip_score,
1251
  ],
1252
  ).then(
1253
  fn=prepare,
1254
  inputs=[
1255
  class_idx,
1256
- text_embeddings,
1257
  class_order,
1258
  ],
1259
- outputs=[options, image, class_idx, correct_choice, model_choice, choices],
1260
  )
1261
 
1262
 
 
1046
  }
1047
 
1048
 
1049
+ def change_language():
1050
  # compute text embeddings
1051
  labels = babel_imagenet["EN"][1]
1052
  class_order = list(range(len(labels)))
1053
  np.random.shuffle(class_order)
 
1054
  correct_text = gr.Text(
1055
  f"Correct was: ''. Question 1/{len(babel_imagenet['EN'][0])} ", label="Game"
1056
  )
1057
  player_score_text = gr.Text(f"Your choice: (Score: 0) ", label="Player")
1058
+ # clip_score_text = gr.Text(f"mSigLIP chose: '' (Score: 0)", label="Opponent")
1059
 
1060
  return (
 
1061
  -1,
1062
  class_order,
1063
  correct_text,
1064
  player_score_text,
1065
+ # clip_score_text,
1066
+ # 0,
1067
  0,
1068
  )
1069
 
1070
 
1071
+ def select(idx, choice, correct, player_score, choices):
 
1072
  correct_name, correct_value = correct
 
1073
  player_choice = choices[choice][0]
 
1074
  player_correct = choice == correct_value
 
 
1075
  player_score = player_score + int(player_correct)
1076
+
 
1077
  correct_text = gr.Text(
1078
  f"Correct was: '{correct_name}'. Question {idx+1}/{len(babel_imagenet['EN'][0])} ",
1079
  label="Game",
 
1082
  f"Your choice: {player_choice} {'✅' if player_correct else '❌'} (Score: {player_score}) ",
1083
  label="Player",
1084
  )
 
 
 
 
1085
 
1086
+ return correct_text, player_score_text, player_score
1087
 
1088
 
1089
+ def prepare(raw_idx, class_order):
1090
  # prepared next question, loads image, and computes choices
1091
 
1092
  raw_idx = (raw_idx + 1) % len(babel_imagenet["EN"][0])
 
1111
  ) # precomputing script uses torch.topk which sorts in reverse here
1112
  if idx not in choices:
1113
  choices = [idx] + choices[1:]
1114
+ # model_choice_idx = choices[-1]
1115
 
1116
  numpy.random.shuffle(choices)
1117
 
1118
  choice_names = [class_labels[idx] for idx in choices]
1119
  choice_values = [0, 1, 2, 3]
1120
 
1121
+ # model_choice_idx = choices.index(model_choice_idx)
1122
+ # model_choice = [choice_names[model_choice_idx], choice_values[model_choice_idx]]
1123
  correct_choice_idx = choices.index(idx)
1124
  correct_choice = [
1125
  choice_names[correct_choice_idx],
 
1134
  new_choice_values = [correct_tuple, keep]
1135
  random.shuffle(new_choice_values)
1136
 
1137
+ warnings.warn(
1138
+ f"""\
1139
+ choices: {choices}\n\n
1140
+ correct_choice: {correct_choice}\n\n"""
1141
+ )
1142
 
1143
  next_radio = gr.Radio(
1144
  choices=new_choice_values,
 
1153
  label="What class does this image belong to?",
1154
  )
1155
 
1156
+ return (
1157
+ next_radio,
1158
+ next_image,
1159
+ raw_idx,
1160
+ correct_choice,
1161
+ # model_choice,
1162
+ new_choice_values,
1163
+ )
1164
 
1165
 
1166
  with gr.Blocks(title="Babel-ImageNet Quiz", css=css) as demo:
 
1168
  # setup state
1169
  class_idx = gr.State(-1)
1170
  player_score = gr.State(0)
1171
+ # clip_score = gr.State(0)
1172
  class_order = gr.State([])
1173
  choices = gr.State([])
1174
 
1175
+ # text_embeddings = gr.State(None)
1176
  correct_choice = gr.State(["nan", 0]) # 0, 1, 2, 3
1177
+ # model_choice = gr.State(["nan", 0])
1178
 
1179
  # Title Area
1180
  gr.Markdown(
 
1205
  # with gr.Row():
1206
  correct_text = gr.Text("Please click start to begin.")
1207
  player_score_text = gr.Text(f"Player score: 0")
1208
+ # clip_score_text = gr.Text(f"mSigLIP score: 0")
1209
 
1210
  options.select(
1211
  fn=select,
 
1213
  class_idx,
1214
  options,
1215
  correct_choice,
1216
+ # model_choice,
1217
  player_score,
1218
+ # clip_score,
1219
  choices,
1220
  ],
1221
  outputs=[
1222
  correct_text,
1223
  player_score_text,
1224
+ # clip_score_text,
1225
  player_score,
1226
+ # clip_score,
1227
  ],
1228
  ).then(
1229
  fn=prepare,
1230
  inputs=[
1231
  class_idx,
 
1232
  class_order,
1233
  ],
1234
+ outputs=[options, image, class_idx, correct_choice, choices],
1235
  )
1236
 
1237
  start_btn.click(
1238
  fn=change_language,
1239
  inputs=[],
1240
  outputs=[
1241
+ # text_embeddings,
1242
  class_idx,
1243
  class_order,
1244
  correct_text,
1245
  player_score_text,
1246
+ # clip_score_text,
1247
  player_score,
1248
+ # clip_score,
1249
  ],
1250
  ).then(
1251
  fn=prepare,
1252
  inputs=[
1253
  class_idx,
1254
+ # text_embeddings,
1255
  class_order,
1256
  ],
1257
+ outputs=[options, image, class_idx, correct_choice, choices],
1258
  )
1259
 
1260