Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,7 @@ import json
|
|
| 3 |
import os
|
| 4 |
|
| 5 |
# -------------------------
|
| 6 |
-
# Quiz
|
| 7 |
# -------------------------
|
| 8 |
quiz_questions = [
|
| 9 |
{
|
|
@@ -62,73 +62,38 @@ def save_counters(counters):
|
|
| 62 |
with open(COUNTER_FILE, "w") as f:
|
| 63 |
json.dump(counters, f)
|
| 64 |
|
| 65 |
-
# Increment visitor count
|
| 66 |
counters = load_counters()
|
| 67 |
counters["visitors"] += 1
|
| 68 |
save_counters(counters)
|
| 69 |
|
| 70 |
# -------------------------
|
| 71 |
-
#
|
| 72 |
# -------------------------
|
| 73 |
-
def
|
| 74 |
question = quiz_questions[question_index]
|
| 75 |
-
explanation = ""
|
| 76 |
-
|
| 77 |
if selected_option == question["answer"]:
|
| 78 |
-
|
| 79 |
-
explanation = f"β
Correct!\n\n{question['explanation']}"
|
| 80 |
-
else:
|
| 81 |
-
explanation = f"β Incorrect.\n\n{question['explanation']}"
|
| 82 |
-
|
| 83 |
-
next_index = question_index + 1
|
| 84 |
-
if next_index >= len(quiz_questions):
|
| 85 |
-
# Quiz completed β update completions
|
| 86 |
-
counters = load_counters()
|
| 87 |
-
counters["completions"] += 1
|
| 88 |
-
save_counters(counters)
|
| 89 |
-
return ("π Quiz Completed!",
|
| 90 |
-
"",
|
| 91 |
-
explanation + f"\n\n**Your Score: {score}/{len(quiz_questions)}**",
|
| 92 |
-
score,
|
| 93 |
-
next_index,
|
| 94 |
-
counters["visitors"],
|
| 95 |
-
counters["completions"])
|
| 96 |
else:
|
| 97 |
-
|
| 98 |
-
return (f"### Question {next_index+1}: {next_question['question']}",
|
| 99 |
-
gr.update(choices=next_question["options"], value=None),
|
| 100 |
-
explanation,
|
| 101 |
-
score,
|
| 102 |
-
next_index,
|
| 103 |
-
counters["visitors"],
|
| 104 |
-
counters["completions"])
|
| 105 |
|
| 106 |
def show_hint(question_index):
|
| 107 |
-
return
|
| 108 |
|
| 109 |
# -------------------------
|
| 110 |
# Build interface
|
| 111 |
# -------------------------
|
| 112 |
def create_question_block(i, q):
|
| 113 |
with gr.Blocks() as block:
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
score_state = gr.State(0)
|
| 118 |
-
index_state = gr.State(i)
|
| 119 |
visitor_md = gr.Markdown(f"π₯ Visitors: {counters['visitors']}")
|
| 120 |
completion_md = gr.Markdown(f"β
Completions: {counters['completions']}")
|
| 121 |
|
| 122 |
with gr.Row():
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
submit_btn.click(
|
| 127 |
-
fn=check_answer_and_next,
|
| 128 |
-
inputs=[index_state, options_radio, score_state],
|
| 129 |
-
outputs=[question_md, options_radio, explanation_md, score_state, index_state, visitor_md, completion_md]
|
| 130 |
-
)
|
| 131 |
-
hint_btn.click(fn=show_hint, inputs=index_state, outputs=explanation_md)
|
| 132 |
return block
|
| 133 |
|
| 134 |
# -------------------------
|
|
|
|
| 3 |
import os
|
| 4 |
|
| 5 |
# -------------------------
|
| 6 |
+
# Quiz data
|
| 7 |
# -------------------------
|
| 8 |
quiz_questions = [
|
| 9 |
{
|
|
|
|
| 62 |
with open(COUNTER_FILE, "w") as f:
|
| 63 |
json.dump(counters, f)
|
| 64 |
|
| 65 |
+
# Increment visitor count on page load
|
| 66 |
counters = load_counters()
|
| 67 |
counters["visitors"] += 1
|
| 68 |
save_counters(counters)
|
| 69 |
|
| 70 |
# -------------------------
|
| 71 |
+
# Functions
|
| 72 |
# -------------------------
|
| 73 |
+
def check_answer(question_index, selected_option):
|
| 74 |
question = quiz_questions[question_index]
|
|
|
|
|
|
|
| 75 |
if selected_option == question["answer"]:
|
| 76 |
+
return f"Correct! π\n\n{question['explanation']}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
else:
|
| 78 |
+
return f"Incorrect β\n\n{question['explanation']}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
def show_hint(question_index):
|
| 81 |
+
return quiz_questions[question_index]["hint"]
|
| 82 |
|
| 83 |
# -------------------------
|
| 84 |
# Build interface
|
| 85 |
# -------------------------
|
| 86 |
def create_question_block(i, q):
|
| 87 |
with gr.Blocks() as block:
|
| 88 |
+
gr.Markdown(f"### Question {i+1}: {q['question']}")
|
| 89 |
+
options_dropdown = gr.Radio(q["options"], label="Select your answer")
|
| 90 |
+
output = gr.Markdown("")
|
|
|
|
|
|
|
| 91 |
visitor_md = gr.Markdown(f"π₯ Visitors: {counters['visitors']}")
|
| 92 |
completion_md = gr.Markdown(f"β
Completions: {counters['completions']}")
|
| 93 |
|
| 94 |
with gr.Row():
|
| 95 |
+
gr.Button("Submit Answer").click(fn=lambda x: check_answer(i, x), inputs=options_dropdown, outputs=output)
|
| 96 |
+
gr.Button("Show Hint").click(fn=lambda: show_hint(i), inputs=None, outputs=output)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
return block
|
| 98 |
|
| 99 |
# -------------------------
|