Spaces:
Running
Running
johnpaulbin
commited on
Commit
•
6582ca7
1
Parent(s):
d8ca418
Update app.py
Browse files
app.py
CHANGED
@@ -51,7 +51,6 @@ def load_mocktest():
|
|
51 |
except FileNotFoundError:
|
52 |
return None
|
53 |
|
54 |
-
# Function to display the mock test question and answers
|
55 |
def display_mock_question(index, score, incorrect_questions):
|
56 |
data = load_mocktest()
|
57 |
if not data:
|
@@ -65,44 +64,46 @@ def display_mock_question(index, score, incorrect_questions):
|
|
65 |
|
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 |
-
|
78 |
|
79 |
mock_index += 1
|
80 |
if mock_index >= len(data):
|
81 |
-
result = generate_test_result(score, len(data),
|
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,
|
83 |
|
84 |
-
return gr.update(), gr.update(visible=False), gr.update(), gr.update(), gr.update(), gr.update(), mock_index, score,
|
85 |
-
|
86 |
-
def generate_test_result(score, total_questions,
|
87 |
-
|
88 |
-
result
|
89 |
-
|
90 |
-
result +=
|
|
|
|
|
|
|
|
|
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,
|
97 |
data = load_mocktest()
|
98 |
if mock_index >= len(data):
|
99 |
-
result = generate_test_result(score, len(data),
|
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,
|
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 |
-
|
106 |
# Gradio interface
|
107 |
with gr.Blocks() as demo:
|
108 |
available_weeks = get_available_weeks()
|
@@ -136,8 +137,8 @@ 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():
|
143 |
answer_1 = gr.Button("Answer 1")
|
@@ -145,33 +146,33 @@ with gr.Blocks() as demo:
|
|
145 |
with gr.Row():
|
146 |
answer_3 = gr.Button("Answer 3")
|
147 |
answer_4 = gr.Button("Answer 4")
|
148 |
-
|
149 |
result_output = gr.Markdown(visible=False)
|
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 |
-
|
167 |
# Restart test button
|
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 |
|
|
|
51 |
except FileNotFoundError:
|
52 |
return None
|
53 |
|
|
|
54 |
def display_mock_question(index, score, incorrect_questions):
|
55 |
data = load_mocktest()
|
56 |
if not data:
|
|
|
64 |
|
65 |
return question, all_answers[0], all_answers[1], all_answers[2], all_answers[3], correct_answer
|
66 |
|
67 |
+
def handle_answer(answer, mock_index, score, incorrect_questions):
|
|
|
68 |
data = load_mocktest()
|
69 |
correct_answer = data[mock_index]["correct-answer"]
|
70 |
current_question = data[mock_index]["question"]
|
71 |
|
72 |
if answer == correct_answer:
|
73 |
score += 1
|
74 |
+
else:
|
75 |
+
incorrect_questions.append((current_question, correct_answer))
|
76 |
|
77 |
mock_index += 1
|
78 |
if mock_index >= len(data):
|
79 |
+
result = generate_test_result(score, len(data), incorrect_questions)
|
80 |
+
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
|
81 |
|
82 |
+
return gr.update(), gr.update(visible=False), gr.update(), gr.update(), gr.update(), gr.update(), mock_index, score, incorrect_questions
|
83 |
+
|
84 |
+
def generate_test_result(score, total_questions, incorrect_questions):
|
85 |
+
percentage = (score / total_questions) * 100
|
86 |
+
result = f"Your score: {score}/{total_questions} ({percentage:.2f}%)\n\n"
|
87 |
+
if incorrect_questions:
|
88 |
+
result += "Incorrect Questions and Correct Answers:\n\n"
|
89 |
+
for i, (question, answer) in enumerate(incorrect_questions, 1):
|
90 |
+
result += f"{i}. Question: {question}\n Correct Answer: {answer}\n\n"
|
91 |
+
else:
|
92 |
+
result += "Congratulations! You answered all questions correctly."
|
93 |
return result
|
94 |
|
95 |
def restart_test():
|
96 |
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)
|
97 |
|
98 |
+
def update_question(mock_index, score, incorrect_questions):
|
99 |
data = load_mocktest()
|
100 |
if mock_index >= len(data):
|
101 |
+
result = generate_test_result(score, len(data), incorrect_questions)
|
102 |
return gr.update(value=result), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
103 |
|
104 |
+
question, ans1, ans2, ans3, ans4, correct = display_mock_question(mock_index, score, incorrect_questions)
|
105 |
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)
|
106 |
|
|
|
107 |
# Gradio interface
|
108 |
with gr.Blocks() as demo:
|
109 |
available_weeks = get_available_weeks()
|
|
|
137 |
with gr.TabItem("Mock Test"):
|
138 |
mock_index = gr.State(0)
|
139 |
score = gr.State(0)
|
140 |
+
incorrect_questions = gr.State([])
|
141 |
+
|
142 |
question_output = gr.Markdown(label="Question")
|
143 |
with gr.Row():
|
144 |
answer_1 = gr.Button("Answer 1")
|
|
|
146 |
with gr.Row():
|
147 |
answer_3 = gr.Button("Answer 3")
|
148 |
answer_4 = gr.Button("Answer 4")
|
149 |
+
|
150 |
result_output = gr.Markdown(visible=False)
|
151 |
restart_button = gr.Button("Restart Test", visible=False)
|
152 |
|
153 |
# Initialize the first question
|
154 |
+
demo.load(update_question, inputs=[mock_index, score, incorrect_questions], outputs=[question_output, answer_1, answer_2, answer_3, answer_4])
|
155 |
+
|
156 |
# Handle answer selection
|
157 |
for answer_btn in [answer_1, answer_2, answer_3, answer_4]:
|
158 |
answer_btn.click(
|
159 |
handle_answer,
|
160 |
+
inputs=[answer_btn, mock_index, score, incorrect_questions],
|
161 |
+
outputs=[question_output, restart_button, answer_1, answer_2, answer_3, answer_4, mock_index, score, incorrect_questions]
|
162 |
).then(
|
163 |
update_question,
|
164 |
+
inputs=[mock_index, score, incorrect_questions],
|
165 |
outputs=[question_output, answer_1, answer_2, answer_3, answer_4]
|
166 |
)
|
167 |
+
|
168 |
# Restart test button
|
169 |
restart_button.click(
|
170 |
restart_test,
|
171 |
inputs=[],
|
172 |
+
outputs=[mock_index, score, incorrect_questions, question_output, answer_1, answer_2, answer_3, answer_4, result_output, restart_button]
|
173 |
).then(
|
174 |
update_question,
|
175 |
+
inputs=[mock_index, score, incorrect_questions],
|
176 |
outputs=[question_output, answer_1, answer_2, answer_3, answer_4]
|
177 |
)
|
178 |
|