MK-316 commited on
Commit
088ae3c
1 Parent(s): 2773a98

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -7
app.py CHANGED
@@ -19,7 +19,7 @@ def get_options(question):
19
 
20
  def check_answer(user_input, correct_answer, question_index):
21
  if question_index in answered_questions:
22
- return "Correct! You have already answered this question correctly.", 1
23
  if user_input.strip().lower() == correct_answer.lower():
24
  feedback = "Correct!"
25
  score = 1
@@ -56,18 +56,25 @@ with gr.Blocks() as app:
56
  )
57
 
58
  submit_button.click(
59
- fn=lambda user_input, correct_answer_store=correct_answer_store, question_index_store=question_index_store: submit_response(user_input, correct_answer_store.value, question_index_store.value),
60
  inputs=user_input,
61
- outputs=[feedback_label]
62
  )
63
 
64
- def submit_response(user_input, correct_answer, question_index):
65
  feedback, score = check_answer(user_input, correct_answer, question_index)
66
- score_store.value += score # Accumulate the score in the state without showing it immediately
67
- return feedback
 
 
 
 
 
 
 
68
 
69
  complete_button.click(
70
- fn=lambda score_store=score_store: f"Session Complete. Your final score is {score_store.value}.",
71
  inputs=[],
72
  outputs=feedback_label
73
  )
 
19
 
20
  def check_answer(user_input, correct_answer, question_index):
21
  if question_index in answered_questions:
22
+ return "Correct! You have already answered this question correctly.", 0
23
  if user_input.strip().lower() == correct_answer.lower():
24
  feedback = "Correct!"
25
  score = 1
 
56
  )
57
 
58
  submit_button.click(
59
+ fn=lambda user_input, correct_answer_store=correct_answer_store, question_index_store=question_index_store, score_store=score_store: submit_response(user_input, correct_answer_store.value, question_index_store.value, score_store),
60
  inputs=user_input,
61
+ outputs=[feedback_label, score_store]
62
  )
63
 
64
+ def submit_response(user_input, correct_answer, question_index, score_store):
65
  feedback, score = check_answer(user_input, correct_answer, question_index)
66
+ if score == 1:
67
+ score_store.value += score # Only accumulate the score if it's a new correct answer
68
+ return feedback, score_store.value
69
+
70
+ def complete_and_reset(score_store):
71
+ final_score = score_store.value
72
+ score_store.value = 0 # Reset the score
73
+ answered_questions.clear() # Clear the set of answered questions
74
+ return f"Session Complete. Your final score is {final_score}."
75
 
76
  complete_button.click(
77
+ fn=lambda score_store=score_store: complete_and_reset(score_store),
78
  inputs=[],
79
  outputs=feedback_label
80
  )