Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
CHANGED
|
@@ -62,15 +62,16 @@ def save_counters(counters):
|
|
| 62 |
with open(COUNTER_FILE, "w") as f:
|
| 63 |
json.dump(counters, f)
|
| 64 |
|
| 65 |
-
|
| 66 |
-
counters = load_counters()
|
| 67 |
-
counters["visitors"] += 1
|
| 68 |
-
save_counters(counters)
|
|
|
|
| 69 |
|
| 70 |
# -------------------------
|
| 71 |
# Score tracking
|
| 72 |
# -------------------------
|
| 73 |
-
score = [0]
|
| 74 |
|
| 75 |
def check_answer(question_index, selected_option):
|
| 76 |
question = quiz_questions[question_index]
|
|
@@ -92,25 +93,25 @@ def show_hint(question_index):
|
|
| 92 |
return quiz_questions[question_index]["hint"]
|
| 93 |
|
| 94 |
# -------------------------
|
| 95 |
-
# Build
|
| 96 |
# -------------------------
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
gr.
|
| 106 |
-
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
# -------------------------
|
| 110 |
-
#
|
| 111 |
# -------------------------
|
| 112 |
-
|
| 113 |
-
quiz_interface = gr.TabbedInterface(question_blocks, ["Q1", "Q2", "Q3", "Q4", "Q5"])
|
| 114 |
-
|
| 115 |
-
# Launch
|
| 116 |
-
quiz_interface.launch()
|
|
|
|
| 62 |
with open(COUNTER_FILE, "w") as f:
|
| 63 |
json.dump(counters, f)
|
| 64 |
|
| 65 |
+
def increment_visitor():
|
| 66 |
+
counters = load_counters()
|
| 67 |
+
counters["visitors"] += 1
|
| 68 |
+
save_counters(counters)
|
| 69 |
+
return f"👥 Visitors: {counters['visitors']}"
|
| 70 |
|
| 71 |
# -------------------------
|
| 72 |
# Score tracking
|
| 73 |
# -------------------------
|
| 74 |
+
score = [0]
|
| 75 |
|
| 76 |
def check_answer(question_index, selected_option):
|
| 77 |
question = quiz_questions[question_index]
|
|
|
|
| 93 |
return quiz_questions[question_index]["hint"]
|
| 94 |
|
| 95 |
# -------------------------
|
| 96 |
+
# Build Interface
|
| 97 |
# -------------------------
|
| 98 |
+
with gr.Blocks() as demo:
|
| 99 |
+
# Visitor counter
|
| 100 |
+
visitor_md = gr.Markdown("👥 Visitors: 0")
|
| 101 |
+
visitor_md.load(fn=increment_visitor, inputs=None, outputs=visitor_md)
|
| 102 |
+
|
| 103 |
+
# Tabs for each question
|
| 104 |
+
with gr.Tabs():
|
| 105 |
+
for i, q in enumerate(quiz_questions):
|
| 106 |
+
with gr.Tab(f"Q{i+1}"):
|
| 107 |
+
gr.Markdown(f"### Question {i+1}: {q['question']}")
|
| 108 |
+
options_dropdown = gr.Radio(q["options"], label="Select your answer")
|
| 109 |
+
output = gr.Markdown("")
|
| 110 |
+
with gr.Row():
|
| 111 |
+
gr.Button("Submit Answer").click(fn=lambda x, idx=i: check_answer(idx, x), inputs=options_dropdown, outputs=output)
|
| 112 |
+
gr.Button("Show Hint").click(fn=lambda idx=i: show_hint(idx), inputs=None, outputs=output)
|
| 113 |
|
| 114 |
# -------------------------
|
| 115 |
+
# Launch App
|
| 116 |
# -------------------------
|
| 117 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|