Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
CHANGED
|
@@ -53,7 +53,11 @@ def load_counters():
|
|
| 53 |
with open(COUNTER_FILE, "r") as f:
|
| 54 |
return json.load(f)
|
| 55 |
else:
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
def save_counters(counters):
|
| 59 |
with open(COUNTER_FILE, "w") as f:
|
|
@@ -69,20 +73,28 @@ save_counters(counters)
|
|
| 69 |
# -------------------------
|
| 70 |
def submit_answer(selected, q_index, score):
|
| 71 |
"""Process answer and move to next question or show final score."""
|
| 72 |
-
|
| 73 |
-
return "Please select an answer before submitting.", gr.update(choices=quiz_questions[q_index]["options"], value=None), "", score, q_index, counters["visitors"], counters["completions"]
|
| 74 |
-
|
| 75 |
-
question = quiz_questions[q_index]
|
| 76 |
explanation = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
-
|
|
|
|
| 79 |
score += 1
|
| 80 |
-
explanation = f"✅ Correct!\n\n{
|
| 81 |
else:
|
| 82 |
-
explanation = f"❌ Incorrect.\n\n{
|
| 83 |
|
| 84 |
next_index = q_index + 1
|
| 85 |
-
counters = load_counters()
|
| 86 |
|
| 87 |
if next_index >= len(quiz_questions):
|
| 88 |
# Quiz completed → update completion counter
|
|
@@ -125,7 +137,7 @@ with gr.Blocks() as quiz_app:
|
|
| 125 |
# Quiz area
|
| 126 |
question_md = gr.Markdown(f"### Question 1: {quiz_questions[0]['question']}")
|
| 127 |
options_radio = gr.Radio(quiz_questions[0]["options"], label="Select your answer")
|
| 128 |
-
|
| 129 |
|
| 130 |
# Buttons
|
| 131 |
with gr.Row():
|
|
@@ -140,13 +152,13 @@ with gr.Blocks() as quiz_app:
|
|
| 140 |
submit_btn.click(
|
| 141 |
fn=submit_answer,
|
| 142 |
inputs=[options_radio, q_index_state, score_state],
|
| 143 |
-
outputs=[question_md, options_radio,
|
| 144 |
)
|
| 145 |
|
| 146 |
hint_btn.click(
|
| 147 |
fn=show_hint,
|
| 148 |
inputs=q_index_state,
|
| 149 |
-
outputs=
|
| 150 |
)
|
| 151 |
|
| 152 |
# Launch app
|
|
|
|
| 53 |
with open(COUNTER_FILE, "r") as f:
|
| 54 |
return json.load(f)
|
| 55 |
else:
|
| 56 |
+
# create if missing
|
| 57 |
+
counters = {"visitors": 0, "completions": 0}
|
| 58 |
+
with open(COUNTER_FILE, "w") as f:
|
| 59 |
+
json.dump(counters, f)
|
| 60 |
+
return counters
|
| 61 |
|
| 62 |
def save_counters(counters):
|
| 63 |
with open(COUNTER_FILE, "w") as f:
|
|
|
|
| 73 |
# -------------------------
|
| 74 |
def submit_answer(selected, q_index, score):
|
| 75 |
"""Process answer and move to next question or show final score."""
|
| 76 |
+
counters = load_counters()
|
|
|
|
|
|
|
|
|
|
| 77 |
explanation = ""
|
| 78 |
+
|
| 79 |
+
if selected is None:
|
| 80 |
+
return (
|
| 81 |
+
f"### Question {q_index+1}: {quiz_questions[q_index]['question']}",
|
| 82 |
+
gr.update(choices=quiz_questions[q_index]["options"], value=None),
|
| 83 |
+
"⚠️ Please select an answer before submitting.",
|
| 84 |
+
score,
|
| 85 |
+
q_index,
|
| 86 |
+
counters["visitors"],
|
| 87 |
+
counters["completions"]
|
| 88 |
+
)
|
| 89 |
|
| 90 |
+
# Check answer
|
| 91 |
+
if selected == quiz_questions[q_index]["answer"]:
|
| 92 |
score += 1
|
| 93 |
+
explanation = f"✅ Correct!\n\n{quiz_questions[q_index]['explanation']}"
|
| 94 |
else:
|
| 95 |
+
explanation = f"❌ Incorrect.\n\n{quiz_questions[q_index]['explanation']}"
|
| 96 |
|
| 97 |
next_index = q_index + 1
|
|
|
|
| 98 |
|
| 99 |
if next_index >= len(quiz_questions):
|
| 100 |
# Quiz completed → update completion counter
|
|
|
|
| 137 |
# Quiz area
|
| 138 |
question_md = gr.Markdown(f"### Question 1: {quiz_questions[0]['question']}")
|
| 139 |
options_radio = gr.Radio(quiz_questions[0]["options"], label="Select your answer")
|
| 140 |
+
explanation_md = gr.Markdown("")
|
| 141 |
|
| 142 |
# Buttons
|
| 143 |
with gr.Row():
|
|
|
|
| 152 |
submit_btn.click(
|
| 153 |
fn=submit_answer,
|
| 154 |
inputs=[options_radio, q_index_state, score_state],
|
| 155 |
+
outputs=[question_md, options_radio, explanation_md, score_state, q_index_state, visitor_md, completion_md]
|
| 156 |
)
|
| 157 |
|
| 158 |
hint_btn.click(
|
| 159 |
fn=show_hint,
|
| 160 |
inputs=q_index_state,
|
| 161 |
+
outputs=explanation_md
|
| 162 |
)
|
| 163 |
|
| 164 |
# Launch app
|