johnpaulbin commited on
Commit
c0746ee
1 Parent(s): 650e31e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -26
app.py CHANGED
@@ -66,40 +66,40 @@ def display_mock_question(index, score, incorrect_questions):
66
  return question, all_answers[0], all_answers[1], all_answers[2], all_answers[3], correct_answer
67
 
68
 
69
- def handle_answer(answer, mock_index, score, incorrect_questions):
70
  data = load_mocktest()
71
  correct_answer = data[mock_index]["correct-answer"]
 
72
 
73
  if answer == correct_answer:
74
  score += 1
75
- else:
76
- incorrect_questions.append(data[mock_index]["question"])
77
 
78
  mock_index += 1
79
  if mock_index >= len(data):
80
- incorrect_str = "Questions to review:\n\n" + "\n\n".join(incorrect_questions) if incorrect_questions else "No questions to review."
81
- result = f"Your score: {score}/{len(data)}\n\n{incorrect_str}"
82
- return gr.update(value=result), gr.update(visible=True), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), mock_index, score, incorrect_questions
83
 
84
- return gr.update(), gr.update(visible=False), gr.update(), gr.update(), gr.update(), gr.update(), mock_index, score, incorrect_questions
 
 
 
 
 
 
 
85
 
86
  def restart_test():
87
- mock_index = 0
88
- score = 0
89
- incorrect_questions = []
90
- data = load_mocktest()
91
- if data:
92
- question, ans1, ans2, ans3, ans4, _ = display_mock_question(mock_index, score, incorrect_questions)
93
- return mock_index, score, incorrect_questions, question, ans1, ans2, ans3, ans4, gr.update(visible=False), gr.update(visible=False)
94
- else:
95
- return mock_index, score, incorrect_questions, "Error loading mock test data", "", "", "", "", gr.update(visible=False), gr.update(visible=False)
96
 
97
- def update_question(mock_index, score, incorrect_questions):
98
  data = load_mocktest()
99
  if mock_index >= len(data):
100
- return gr.update(), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
 
101
 
102
- question, ans1, ans2, ans3, ans4, correct = display_mock_question(mock_index, score, incorrect_questions)
103
  return gr.update(value=question), gr.update(value=ans1, visible=True), gr.update(value=ans2, visible=True), gr.update(value=ans3, visible=True), gr.update(value=ans4, visible=True)
104
 
105
 
@@ -136,7 +136,7 @@ with gr.Blocks() as demo:
136
  with gr.TabItem("Mock Test"):
137
  mock_index = gr.State(0)
138
  score = gr.State(0)
139
- incorrect_questions = gr.State([])
140
 
141
  question_output = gr.Markdown(label="Question")
142
  with gr.Row():
@@ -150,17 +150,17 @@ with gr.Blocks() as demo:
150
  restart_button = gr.Button("Restart Test", visible=False)
151
 
152
  # Initialize the first question
153
- demo.load(update_question, inputs=[mock_index, score, incorrect_questions], outputs=[question_output, answer_1, answer_2, answer_3, answer_4])
154
 
155
  # Handle answer selection
156
  for answer_btn in [answer_1, answer_2, answer_3, answer_4]:
157
  answer_btn.click(
158
  handle_answer,
159
- inputs=[answer_btn, mock_index, score, incorrect_questions],
160
- outputs=[question_output, restart_button, answer_1, answer_2, answer_3, answer_4, mock_index, score, incorrect_questions]
161
  ).then(
162
  update_question,
163
- inputs=[mock_index, score, incorrect_questions],
164
  outputs=[question_output, answer_1, answer_2, answer_3, answer_4]
165
  )
166
 
@@ -168,10 +168,10 @@ with gr.Blocks() as demo:
168
  restart_button.click(
169
  restart_test,
170
  inputs=[],
171
- outputs=[mock_index, score, incorrect_questions, question_output, answer_1, answer_2, answer_3, answer_4, result_output, restart_button]
172
  ).then(
173
  update_question,
174
- inputs=[mock_index, score, incorrect_questions],
175
  outputs=[question_output, answer_1, answer_2, answer_3, answer_4]
176
  )
177
 
 
66
  return question, all_answers[0], all_answers[1], all_answers[2], all_answers[3], correct_answer
67
 
68
 
69
+ def handle_answer(answer, mock_index, score, question_answers):
70
  data = load_mocktest()
71
  correct_answer = data[mock_index]["correct-answer"]
72
+ current_question = data[mock_index]["question"]
73
 
74
  if answer == correct_answer:
75
  score += 1
76
+
77
+ question_answers.append((current_question, correct_answer))
78
 
79
  mock_index += 1
80
  if mock_index >= len(data):
81
+ result = generate_test_result(score, len(data), question_answers)
82
+ return gr.update(value=result), gr.update(visible=True), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), mock_index, score, question_answers
 
83
 
84
+ return gr.update(), gr.update(visible=False), gr.update(), gr.update(), gr.update(), gr.update(), mock_index, score, question_answers
85
+
86
+ def generate_test_result(score, total_questions, question_answers):
87
+ result = f"Your score: {score}/{total_questions}\n\n"
88
+ result += "Questions and Correct Answers:\n\n"
89
+ for i, (question, answer) in enumerate(question_answers, 1):
90
+ result += f"{i}. Question: {question}\n Correct Answer: {answer}\n\n"
91
+ return result
92
 
93
  def restart_test():
94
+ return 0, 0, [], gr.update(value=""), gr.update(value="", visible=True), gr.update(value="", visible=True), gr.update(value="", visible=True), gr.update(value="", visible=True), gr.update(value="", visible=False), gr.update(visible=False)
 
 
 
 
 
 
 
 
95
 
96
+ def update_question(mock_index, score, question_answers):
97
  data = load_mocktest()
98
  if mock_index >= len(data):
99
+ result = generate_test_result(score, len(data), question_answers)
100
+ return gr.update(value=result), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
101
 
102
+ question, ans1, ans2, ans3, ans4, correct = display_mock_question(mock_index, score, question_answers)
103
  return gr.update(value=question), gr.update(value=ans1, visible=True), gr.update(value=ans2, visible=True), gr.update(value=ans3, visible=True), gr.update(value=ans4, visible=True)
104
 
105
 
 
136
  with gr.TabItem("Mock Test"):
137
  mock_index = gr.State(0)
138
  score = gr.State(0)
139
+ question_answers = gr.State([])
140
 
141
  question_output = gr.Markdown(label="Question")
142
  with gr.Row():
 
150
  restart_button = gr.Button("Restart Test", visible=False)
151
 
152
  # Initialize the first question
153
+ demo.load(update_question, inputs=[mock_index, score, question_answers], outputs=[question_output, answer_1, answer_2, answer_3, answer_4])
154
 
155
  # Handle answer selection
156
  for answer_btn in [answer_1, answer_2, answer_3, answer_4]:
157
  answer_btn.click(
158
  handle_answer,
159
+ inputs=[answer_btn, mock_index, score, question_answers],
160
+ outputs=[question_output, restart_button, answer_1, answer_2, answer_3, answer_4, mock_index, score, question_answers]
161
  ).then(
162
  update_question,
163
+ inputs=[mock_index, score, question_answers],
164
  outputs=[question_output, answer_1, answer_2, answer_3, answer_4]
165
  )
166
 
 
168
  restart_button.click(
169
  restart_test,
170
  inputs=[],
171
+ outputs=[mock_index, score, question_answers, question_output, answer_1, answer_2, answer_3, answer_4, result_output, restart_button]
172
  ).then(
173
  update_question,
174
+ inputs=[mock_index, score, question_answers],
175
  outputs=[question_output, answer_1, answer_2, answer_3, answer_4]
176
  )
177