Arvind-Science123 commited on
Commit
011d04c
Β·
verified Β·
1 Parent(s): 871b739

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -51
app.py CHANGED
@@ -44,7 +44,7 @@ quiz_questions = [
44
  ]
45
 
46
  # -------------------------
47
- # Counters
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, "completions": 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
- # Quiz logic
72
  # -------------------------
73
- def submit_answer(index, selected_option, answered_indices):
74
- # Add current question to answered list
75
- answered_indices.append(index)
76
-
77
- # Show explanation
78
- question = quiz_questions[index]
79
  if selected_option == question["answer"]:
80
- explanation = f"βœ… Correct!\n\n{question['explanation']}"
81
  else:
82
- explanation = f"❌ Incorrect.\n\n{question['explanation']}"
83
 
84
- # If all questions answered, increment completion
85
- if len(answered_indices) == len(quiz_questions):
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
- with gr.Blocks() as demo:
106
- visited_counters = load_counters()
107
- question_index = 0
108
- question_text = f"### Question 1: {quiz_questions[0]['question']}"
109
-
110
- question_md = gr.Markdown(question_text)
111
- options_radio = gr.Radio(quiz_questions[0]['options'], label="Select your answer")
112
- explanation_md = gr.Markdown("")
113
- visitor_md = gr.Markdown(f"πŸ‘₯ Visitors: {visited_counters['visitors']}")
114
- completion_md = gr.Markdown(f"βœ… Completions: {visited_counters['completions']}")
115
- answered_state = gr.State([])
116
-
117
- with gr.Row():
118
- submit_btn = gr.Button("Submit Answer")
119
- hint_btn = gr.Button("Show Hint")
120
 
121
- submit_btn.click(
122
- fn=submit_answer,
123
- inputs=[gr.State(0), options_radio, answered_state],
124
- outputs=[question_md, options_radio, explanation_md, answered_state, visitor_md, completion_md]
125
- )
126
-
127
- hint_btn.click(fn=show_hint, inputs=[gr.State(0), explanation_md], outputs=explanation_md)
128
 
129
- demo.launch()
 
 
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()