Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
CHANGED
|
@@ -44,7 +44,7 @@ quiz_questions = [
|
|
| 44 |
]
|
| 45 |
|
| 46 |
# -------------------------
|
| 47 |
-
#
|
| 48 |
# -------------------------
|
| 49 |
COUNTER_FILE = "counters.json"
|
| 50 |
|
|
@@ -53,7 +53,7 @@ def load_counters():
|
|
| 53 |
with open(COUNTER_FILE, "r") as f:
|
| 54 |
return json.load(f)
|
| 55 |
else:
|
| 56 |
-
counters = {"visitors": 0
|
| 57 |
with open(COUNTER_FILE, "w") as f:
|
| 58 |
json.dump(counters, f)
|
| 59 |
return counters
|
|
@@ -68,62 +68,38 @@ counters["visitors"] += 1
|
|
| 68 |
save_counters(counters)
|
| 69 |
|
| 70 |
# -------------------------
|
| 71 |
-
#
|
| 72 |
# -------------------------
|
| 73 |
-
def
|
| 74 |
-
|
| 75 |
-
answered_indices.append(index)
|
| 76 |
-
|
| 77 |
-
# Show explanation
|
| 78 |
-
question = quiz_questions[index]
|
| 79 |
if selected_option == question["answer"]:
|
| 80 |
-
|
| 81 |
else:
|
| 82 |
-
|
| 83 |
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
counters = load_counters()
|
| 87 |
-
counters["completions"] += 1
|
| 88 |
-
save_counters(counters)
|
| 89 |
-
next_question_text = "π You have completed the quiz!"
|
| 90 |
-
next_options = []
|
| 91 |
-
else:
|
| 92 |
-
next_index = len(answered_indices)
|
| 93 |
-
next_question = quiz_questions[next_index]
|
| 94 |
-
next_question_text = f"### Question {next_index+1}: {next_question['question']}"
|
| 95 |
-
next_options = next_question["options"]
|
| 96 |
-
|
| 97 |
-
return next_question_text, next_options, explanation, answered_indices, counters["visitors"], counters["completions"]
|
| 98 |
-
|
| 99 |
-
def show_hint(index, explanation):
|
| 100 |
-
return f"π‘ Hint: {quiz_questions[index]['hint']}"
|
| 101 |
|
| 102 |
# -------------------------
|
| 103 |
# Build interface
|
| 104 |
# -------------------------
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
with gr.Row():
|
| 118 |
-
submit_btn = gr.Button("Submit Answer")
|
| 119 |
-
hint_btn = gr.Button("Show Hint")
|
| 120 |
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
hint_btn.click(fn=show_hint, inputs=[gr.State(0), explanation_md], outputs=explanation_md)
|
| 128 |
|
| 129 |
-
|
|
|
|
|
|
| 44 |
]
|
| 45 |
|
| 46 |
# -------------------------
|
| 47 |
+
# Visitor counter
|
| 48 |
# -------------------------
|
| 49 |
COUNTER_FILE = "counters.json"
|
| 50 |
|
|
|
|
| 53 |
with open(COUNTER_FILE, "r") as f:
|
| 54 |
return json.load(f)
|
| 55 |
else:
|
| 56 |
+
counters = {"visitors": 0}
|
| 57 |
with open(COUNTER_FILE, "w") as f:
|
| 58 |
json.dump(counters, f)
|
| 59 |
return counters
|
|
|
|
| 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 |
+
gr.Markdown(f"π₯ Visitors: {counters['visitors']}")
|
| 92 |
+
|
| 93 |
+
with gr.Row():
|
| 94 |
+
gr.Button("Submit Answer").click(fn=lambda x: check_answer(i, x), inputs=options_dropdown, outputs=output)
|
| 95 |
+
gr.Button("Show Hint").click(fn=lambda: show_hint(i), inputs=None, outputs=output)
|
| 96 |
+
return block
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
+
# -------------------------
|
| 99 |
+
# Create quiz interface
|
| 100 |
+
# -------------------------
|
| 101 |
+
question_blocks = [create_question_block(i, q) for i, q in enumerate(quiz_questions)]
|
| 102 |
+
quiz_interface = gr.TabbedInterface(question_blocks, ["Q1", "Q2", "Q3", "Q4", "Q5"])
|
|
|
|
|
|
|
| 103 |
|
| 104 |
+
# Launch
|
| 105 |
+
quiz_interface.launch()
|