gardarjuto commited on
Commit
1d38d16
·
1 Parent(s): ea33853

fix so we don't use a shared quiz state

Browse files
Files changed (1) hide show
  1. app.py +17 -9
app.py CHANGED
@@ -5,10 +5,9 @@ from quiz import BenchmarkQuiz, BENCHMARKS, QuestionData
5
 
6
  mpl.rcParams["figure.dpi"] = 300
7
 
8
- quiz = BenchmarkQuiz()
9
 
10
 
11
- def update_quiz_screen(question_data: QuestionData):
12
  quiz_state = quiz.state
13
  return {
14
  quiz_screen: gr.update(visible=True),
@@ -68,16 +67,18 @@ def update_score_screen(plot, results_data):
68
 
69
 
70
  def start_quiz_handler(benchmark_name):
 
71
  quiz.start_quiz(benchmark_name)
72
  question_data = quiz.update_question()
73
  return {
74
  start_screen: gr.update(visible=False),
75
  score_screen: gr.update(visible=False),
76
- **update_quiz_screen(question_data),
 
77
  }
78
 
79
 
80
- def next_question_handler(answer_input, free_text_input):
81
  answer = (
82
  answer_input
83
  if BENCHMARKS[quiz.state.benchmark_name]["type"] == "multiple_choice"
@@ -88,14 +89,16 @@ def next_question_handler(answer_input, free_text_input):
88
  return {
89
  quiz_screen: gr.update(visible=False),
90
  **update_score_screen(result["plot"], result["results_data"]),
 
91
  }
92
  else:
93
- return update_quiz_screen(result["question_data"])
94
 
95
 
96
- def previous_question_handler():
 
97
  question_data = quiz.previous_question()
98
- return update_quiz_screen(question_data)
99
 
100
 
101
  def reset_quiz_handler():
@@ -160,6 +163,8 @@ with demo:
160
  for card in result_cards:
161
  with card:
162
  gr.Markdown("")
 
 
163
 
164
  for benchmark_name, button in benchmark_buttons.items():
165
  button.click(
@@ -175,12 +180,13 @@ with demo:
175
  free_text_input,
176
  next_button,
177
  previous_button,
 
178
  ],
179
  )
180
 
181
  next_button.click(
182
  fn=next_question_handler,
183
- inputs=[answer_input, free_text_input],
184
  outputs=[
185
  quiz_screen,
186
  score_screen,
@@ -194,12 +200,13 @@ with demo:
194
  results_container,
195
  *result_cards,
196
  *[child for card in result_cards for child in card.children],
 
197
  ],
198
  )
199
 
200
  previous_button.click(
201
  fn=previous_question_handler,
202
- inputs=[],
203
  outputs=[
204
  quiz_screen,
205
  question_number,
@@ -208,6 +215,7 @@ with demo:
208
  free_text_input,
209
  next_button,
210
  previous_button,
 
211
  ],
212
  )
213
 
 
5
 
6
  mpl.rcParams["figure.dpi"] = 300
7
 
 
8
 
9
 
10
+ def update_quiz_screen(quiz: BenchmarkQuiz, question_data: QuestionData):
11
  quiz_state = quiz.state
12
  return {
13
  quiz_screen: gr.update(visible=True),
 
67
 
68
 
69
  def start_quiz_handler(benchmark_name):
70
+ quiz = BenchmarkQuiz()
71
  quiz.start_quiz(benchmark_name)
72
  question_data = quiz.update_question()
73
  return {
74
  start_screen: gr.update(visible=False),
75
  score_screen: gr.update(visible=False),
76
+ **update_quiz_screen(quiz, question_data),
77
+ quiz_state: quiz,
78
  }
79
 
80
 
81
+ def next_question_handler(quiz, answer_input, free_text_input):
82
  answer = (
83
  answer_input
84
  if BENCHMARKS[quiz.state.benchmark_name]["type"] == "multiple_choice"
 
89
  return {
90
  quiz_screen: gr.update(visible=False),
91
  **update_score_screen(result["plot"], result["results_data"]),
92
+ quiz_state: quiz,
93
  }
94
  else:
95
+ return {**update_quiz_screen(quiz, result["question_data"]), quiz_state: quiz}
96
 
97
 
98
+
99
+ def previous_question_handler(quiz):
100
  question_data = quiz.previous_question()
101
+ return {**update_quiz_screen(quiz, question_data), quiz_state: quiz}
102
 
103
 
104
  def reset_quiz_handler():
 
163
  for card in result_cards:
164
  with card:
165
  gr.Markdown("")
166
+
167
+ quiz_state = gr.State()
168
 
169
  for benchmark_name, button in benchmark_buttons.items():
170
  button.click(
 
180
  free_text_input,
181
  next_button,
182
  previous_button,
183
+ quiz_state,
184
  ],
185
  )
186
 
187
  next_button.click(
188
  fn=next_question_handler,
189
+ inputs=[quiz_state, answer_input, free_text_input],
190
  outputs=[
191
  quiz_screen,
192
  score_screen,
 
200
  results_container,
201
  *result_cards,
202
  *[child for card in result_cards for child in card.children],
203
+ quiz_state,
204
  ],
205
  )
206
 
207
  previous_button.click(
208
  fn=previous_question_handler,
209
+ inputs=[quiz_state],
210
  outputs=[
211
  quiz_screen,
212
  question_number,
 
215
  free_text_input,
216
  next_button,
217
  previous_button,
218
+ quiz_state,
219
  ],
220
  )
221