Spaces:
Running
Running
johnpaulbin
commited on
Commit
•
d8ca418
1
Parent(s):
d8c3602
Update app.py
Browse files
app.py
CHANGED
@@ -2,6 +2,7 @@ import gradio as gr
|
|
2 |
import json
|
3 |
import os
|
4 |
import random
|
|
|
5 |
|
6 |
# Function to get available JSON files in the working directory
|
7 |
def get_available_weeks():
|
@@ -17,6 +18,30 @@ def load_questions(week_file):
|
|
17 |
except FileNotFoundError:
|
18 |
return None, f"File {week_file} not found."
|
19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
# Function to load mock test questions
|
21 |
def load_mocktest():
|
22 |
try:
|
@@ -41,46 +66,43 @@ def display_mock_question(index, score, incorrect_questions):
|
|
41 |
return question, all_answers[0], all_answers[1], all_answers[2], all_answers[3], correct_answer
|
42 |
|
43 |
|
44 |
-
def handle_answer(answer, mock_index, score,
|
45 |
data = load_mocktest()
|
46 |
correct_answer = data[mock_index]["correct-answer"]
|
47 |
current_question = data[mock_index]["question"]
|
48 |
|
49 |
-
if answer
|
50 |
-
|
|
|
|
|
51 |
|
52 |
mock_index += 1
|
53 |
if mock_index >= len(data):
|
54 |
-
|
55 |
-
|
56 |
-
result = generate_test_result(percentage, incorrect_questions)
|
57 |
-
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
|
58 |
|
59 |
-
return gr.update(), gr.update(visible=False), gr.update(), gr.update(), gr.update(), gr.update(), mock_index, score,
|
60 |
-
|
61 |
-
def generate_test_result(
|
62 |
-
result = f"Your score: {
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
result += "Incorrect Questions and Correct Answers:\n\n"
|
67 |
-
for i, (question, answer) in enumerate(incorrect_questions, 1):
|
68 |
-
result += f"{i}. Question: {question}\n Correct Answer: {answer}\n\n"
|
69 |
return result
|
70 |
|
71 |
def restart_test():
|
72 |
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)
|
73 |
|
74 |
-
def update_question(mock_index, score,
|
75 |
data = load_mocktest()
|
76 |
if mock_index >= len(data):
|
77 |
-
|
78 |
-
result = generate_test_result(percentage, incorrect_questions)
|
79 |
return gr.update(value=result), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
80 |
|
81 |
-
question, ans1, ans2, ans3, ans4, correct = display_mock_question(mock_index, score,
|
82 |
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)
|
83 |
|
|
|
84 |
# Gradio interface
|
85 |
with gr.Blocks() as demo:
|
86 |
available_weeks = get_available_weeks()
|
@@ -114,7 +136,7 @@ with gr.Blocks() as demo:
|
|
114 |
with gr.TabItem("Mock Test"):
|
115 |
mock_index = gr.State(0)
|
116 |
score = gr.State(0)
|
117 |
-
|
118 |
|
119 |
question_output = gr.Markdown(label="Question")
|
120 |
with gr.Row():
|
@@ -128,17 +150,17 @@ with gr.Blocks() as demo:
|
|
128 |
restart_button = gr.Button("Restart Test", visible=False)
|
129 |
|
130 |
# Initialize the first question
|
131 |
-
demo.load(update_question, inputs=[mock_index, score,
|
132 |
|
133 |
# Handle answer selection
|
134 |
for answer_btn in [answer_1, answer_2, answer_3, answer_4]:
|
135 |
answer_btn.click(
|
136 |
handle_answer,
|
137 |
-
inputs=[answer_btn, mock_index, score,
|
138 |
-
outputs=[question_output, restart_button, answer_1, answer_2, answer_3, answer_4, mock_index, score,
|
139 |
).then(
|
140 |
update_question,
|
141 |
-
inputs=[mock_index, score,
|
142 |
outputs=[question_output, answer_1, answer_2, answer_3, answer_4]
|
143 |
)
|
144 |
|
@@ -146,11 +168,11 @@ with gr.Blocks() as demo:
|
|
146 |
restart_button.click(
|
147 |
restart_test,
|
148 |
inputs=[],
|
149 |
-
outputs=[mock_index, score,
|
150 |
).then(
|
151 |
update_question,
|
152 |
-
inputs=[mock_index, score,
|
153 |
outputs=[question_output, answer_1, answer_2, answer_3, answer_4]
|
154 |
)
|
155 |
|
156 |
-
demo.launch()
|
|
|
2 |
import json
|
3 |
import os
|
4 |
import random
|
5 |
+
import google.generativeai as genai
|
6 |
|
7 |
# Function to get available JSON files in the working directory
|
8 |
def get_available_weeks():
|
|
|
18 |
except FileNotFoundError:
|
19 |
return None, f"File {week_file} not found."
|
20 |
|
21 |
+
# Flashcard UI function
|
22 |
+
def flashcard_ui(week_file, index):
|
23 |
+
data, error = load_questions(week_file)
|
24 |
+
if error:
|
25 |
+
return f"Error: {error}", None
|
26 |
+
question = data[index]["question"]
|
27 |
+
total = len(data)
|
28 |
+
return f"Question {index + 1}/{total}: {question}", ""
|
29 |
+
|
30 |
+
# Reveal answer function
|
31 |
+
def reveal_answer(week_file, index):
|
32 |
+
data, error = load_questions(week_file)
|
33 |
+
if error:
|
34 |
+
return None
|
35 |
+
answer = data[index]["answer"]
|
36 |
+
return answer
|
37 |
+
|
38 |
+
# Function to handle navigation
|
39 |
+
def change_question(week_file, index, direction):
|
40 |
+
data, _ = load_questions(week_file)
|
41 |
+
total = len(data)
|
42 |
+
index = (index + direction) % total
|
43 |
+
return index, *flashcard_ui(week_file, index)
|
44 |
+
|
45 |
# Function to load mock test questions
|
46 |
def load_mocktest():
|
47 |
try:
|
|
|
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 |
+
|
106 |
# Gradio interface
|
107 |
with gr.Blocks() as demo:
|
108 |
available_weeks = get_available_weeks()
|
|
|
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 |
|
178 |
+
demo.launch()
|