abidlabs HF staff commited on
Commit
f2c12ed
1 Parent(s): a0a2e9f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -9
app.py CHANGED
@@ -37,7 +37,9 @@ def model_classify(radio, im):
37
  outputs = model(**inputs)
38
  logits = outputs.logits
39
  predicted_class_idx = logits.argmax(-1).item()
40
- return model.config.id2label[predicted_class_idx]
 
 
41
 
42
  def random_image():
43
  imname = random.choice(images)
@@ -51,13 +53,18 @@ def random_image():
51
  options = [classes[int(i)] for i in options]
52
  return im, label, gr.Radio.update(value=None, choices=options), None
53
 
54
- def check_score(pred, truth, current_score, total_score):
55
- if pred == classes[int(truth)]:
56
- total_score +=1
57
- return current_score + 1, f"Your score is {current_score+1} out of {total_score}"
 
 
 
 
 
58
  else:
59
- total_score +=1
60
- return current_score, f"Your score is {current_score} out of {total_score}"
61
 
62
 
63
  def compare_score(userclass, prediction):
@@ -71,6 +78,7 @@ with gr.Blocks() as demo:
71
  image_label = gr.State()
72
  prediction = gr.State()
73
  total_score = gr.State(0)
 
74
 
75
  with gr.Row():
76
  with gr.Column(min_width= 900):
@@ -84,10 +92,11 @@ with gr.Blocks() as demo:
84
  btn = gr.Button("Next image")
85
 
86
  demo.load(random_image, None, [image, image_label, radio, prediction])
87
- radio.change(model_classify, [radio, image], prediction)
88
- radio.change(check_score, [radio, image_label, user_score, total_score], [user_score, score])
89
  #radio.change(compare_score, [radio, prediction], message)
90
  btn.click(random_image, None, [image, image_label, radio, prediction])
 
91
 
92
 
93
  demo.launch()
 
37
  outputs = model(**inputs)
38
  logits = outputs.logits
39
  predicted_class_idx = logits.argmax(-1).item()
40
+ return model.config.id2label[predicted_class_idx], True
41
+ else:
42
+ return None, False
43
 
44
  def random_image():
45
  imname = random.choice(images)
 
53
  options = [classes[int(i)] for i in options]
54
  return im, label, gr.Radio.update(value=None, choices=options), None
55
 
56
+ def check_score(pred, truth, current_score, total_score, has_guessed):
57
+ if not(has_guessed):
58
+ if pred == "lion":
59
+ total_score +=1
60
+ return current_score + 1, f"Your score is {current_score+1} out of {total_score}", total_score
61
+ else:
62
+ if pred is not None:
63
+ total_score +=1
64
+ return current_score, f"Your score is {current_score} out of {total_score}", total_score
65
  else:
66
+ return current_score, f"Your score is {current_score} out of {total_score}", total_score
67
+
68
 
69
 
70
  def compare_score(userclass, prediction):
 
78
  image_label = gr.State()
79
  prediction = gr.State()
80
  total_score = gr.State(0)
81
+ has_guessed = gr.State(False)
82
 
83
  with gr.Row():
84
  with gr.Column(min_width= 900):
 
92
  btn = gr.Button("Next image")
93
 
94
  demo.load(random_image, None, [image, image_label, radio, prediction])
95
+ radio.change(model_classify, [radio, image], [prediction, has_guessed])
96
+ radio.change(check_score, [radio, image_label, user_score, total_score, has_guessed], [user_score, score, total_score])
97
  #radio.change(compare_score, [radio, prediction], message)
98
  btn.click(random_image, None, [image, image_label, radio, prediction])
99
+ btn.click(lambda :False, None, has_guessed)
100
 
101
 
102
  demo.launch()