Spaces:
Running
Running
johnpaulbin
commited on
Commit
•
c0746ee
1
Parent(s):
650e31e
Update app.py
Browse files
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,
|
70 |
data = load_mocktest()
|
71 |
correct_answer = data[mock_index]["correct-answer"]
|
|
|
72 |
|
73 |
if answer == correct_answer:
|
74 |
score += 1
|
75 |
-
|
76 |
-
|
77 |
|
78 |
mock_index += 1
|
79 |
if mock_index >= len(data):
|
80 |
-
|
81 |
-
result =
|
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,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
|
86 |
def restart_test():
|
87 |
-
|
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,
|
98 |
data = load_mocktest()
|
99 |
if mock_index >= len(data):
|
100 |
-
|
|
|
101 |
|
102 |
-
question, ans1, ans2, ans3, ans4, correct = display_mock_question(mock_index, score,
|
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 |
-
|
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,
|
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,
|
160 |
-
outputs=[question_output, restart_button, answer_1, answer_2, answer_3, answer_4, mock_index, score,
|
161 |
).then(
|
162 |
update_question,
|
163 |
-
inputs=[mock_index, score,
|
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,
|
172 |
).then(
|
173 |
update_question,
|
174 |
-
inputs=[mock_index, score,
|
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 |
|