Arvind-Science123 commited on
Commit
78a9928
·
verified ·
1 Parent(s): 7c0e41d

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -23
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
- # Increment visitor count
66
- counters = load_counters()
67
- counters["visitors"] += 1
68
- save_counters(counters)
 
69
 
70
  # -------------------------
71
  # Score tracking
72
  # -------------------------
73
- score = [0] # Use list to keep mutable score across lambdas
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 interface
96
  # -------------------------
97
- def create_question_block(i, q):
98
- with gr.Blocks() as block:
99
- gr.Markdown(f"### Question {i+1}: {q['question']}")
100
- options_dropdown = gr.Radio(q["options"], label="Select your answer")
101
- output = gr.Markdown("")
102
- gr.Markdown(f"👥 Visitors: {counters['visitors']}")
103
-
104
- with gr.Row():
105
- gr.Button("Submit Answer").click(fn=lambda x: check_answer(i, x), inputs=options_dropdown, outputs=output)
106
- gr.Button("Show Hint").click(fn=lambda: show_hint(i), inputs=None, outputs=output)
107
- return block
 
 
 
 
108
 
109
  # -------------------------
110
- # Quiz interface
111
  # -------------------------
112
- question_blocks = [create_question_block(i, q) for i, q in enumerate(quiz_questions)]
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()