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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -15
app.py CHANGED
@@ -5,7 +5,7 @@ import random
5
  questions = ["colorful", "worried", "excited", "showing", "respected", "doubters", "success"]
6
  answers = ["vibrant", "anxious", "thrilled", "depicting", "admired", "skeptics", "achievement"]
7
 
8
- answered_questions = set() # Track which questions have been answered
9
 
10
  def get_options(question):
11
  index = questions.index(question)
@@ -17,20 +17,21 @@ def get_options(question):
17
  random.shuffle(options)
18
  return ", ".join(options), correct_answer, index
19
 
20
- def check_answer(user_input, correct_answer, score, question_index):
21
  if question_index in answered_questions:
22
- return f"You have already answered this question. Your score remains {score}.", score
23
  if user_input.strip().lower() == correct_answer.lower():
24
- score += 1
25
- feedback = f"Correct! Your score is now {score}."
 
26
  else:
27
- feedback = f"Incorrect. The correct answer was '{correct_answer}'. Your score remains {score}."
28
- answered_questions.add(question_index)
29
  return feedback, score
30
 
31
  with gr.Blocks() as app:
32
  with gr.Row():
33
- question_dropdown = gr.Dropdown(choices=questions, label="Select a question", value=questions[0])
34
  show_options_button = gr.Button("Show me options")
35
  options_output = gr.Textbox(label="Options", interactive=False)
36
  correct_answer_store = gr.State()
@@ -50,20 +51,20 @@ with gr.Blocks() as app:
50
 
51
  show_options_button.click(
52
  fn=show_options,
53
- inputs=question_dropdown,
54
  outputs=[options_output, correct_answer_store, question_index_store]
55
  )
56
 
57
  submit_button.click(
58
- fn=lambda user_input, correct_answer_store=correct_answer_store, score_store=score_store, question_index_store=question_index_store: submit_response(user_input, correct_answer_store.value, score_store.value, question_index_store.value),
59
  inputs=user_input,
60
- outputs=[feedback_label, score_store]
61
  )
62
 
63
- def submit_response(user_input, correct_answer, score, question_index):
64
- feedback, updated_score = check_answer(user_input, correct_answer, score, question_index)
65
- score_store.value = updated_score # Ensure the score is updated in the state
66
- return feedback, updated_score
67
 
68
  complete_button.click(
69
  fn=lambda score_store=score_store: f"Session Complete. Your final score is {score_store.value}.",
 
5
  questions = ["colorful", "worried", "excited", "showing", "respected", "doubters", "success"]
6
  answers = ["vibrant", "anxious", "thrilled", "depicting", "admired", "skeptics", "achievement"]
7
 
8
+ answered_questions = set() # Track which questions have been correctly answered
9
 
10
  def get_options(question):
11
  index = questions.index(question)
 
17
  random.shuffle(options)
18
  return ", ".join(options), correct_answer, index
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
26
+ answered_questions.add(question_index) # Only add to answered if correct
27
  else:
28
+ feedback = f"Incorrect. The correct answer was '{correct_answer}'."
29
+ score = 0
30
  return feedback, score
31
 
32
  with gr.Blocks() as app:
33
  with gr.Row():
34
+ question_dropdown = gr.Dropdown(choices=questions, label="Select a question")
35
  show_options_button = gr.Button("Show me options")
36
  options_output = gr.Textbox(label="Options", interactive=False)
37
  correct_answer_store = gr.State()
 
51
 
52
  show_options_button.click(
53
  fn=show_options,
54
+ inputs=[question_dropdown],
55
  outputs=[options_output, correct_answer_store, question_index_store]
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}.",