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

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -25
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import gradio as gr
2
  import json
3
  import os
 
4
 
5
  # -------------------------
6
  # Quiz data
@@ -46,12 +47,15 @@ quiz_questions = [
46
  # -------------------------
47
  # Visitor counter
48
  # -------------------------
49
- COUNTER_FILE = "counters.json"
50
 
51
  def load_counters():
52
  if os.path.exists(COUNTER_FILE):
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:
@@ -62,11 +66,15 @@ def save_counters(counters):
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
@@ -76,16 +84,15 @@ score = [0]
76
  def check_answer(question_index, selected_option):
77
  question = quiz_questions[question_index]
78
 
79
- # Update score
80
  if selected_option == question["answer"]:
81
  score[0] += 1
82
  feedback = f"✅ Correct!\n\n{question['explanation']}"
83
  else:
84
  feedback = f"❌ Incorrect.\n\n{question['explanation']}"
85
 
86
- # If last question, show total score
87
  if question_index == len(quiz_questions) - 1:
88
  feedback += f"\n\n🎯 Your total score: {score[0]}/{len(quiz_questions)}"
 
89
 
90
  return feedback
91
 
@@ -93,25 +100,25 @@ def show_hint(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()
 
 
 
 
 
1
  import gradio as gr
2
  import json
3
  import os
4
+ import random
5
 
6
  # -------------------------
7
  # Quiz data
 
47
  # -------------------------
48
  # Visitor counter
49
  # -------------------------
50
+ COUNTER_FILE = "/tmp/counters.json" # Works both locally and in Hugging Face
51
 
52
  def load_counters():
53
  if os.path.exists(COUNTER_FILE):
54
  with open(COUNTER_FILE, "r") as f:
55
+ try:
56
+ return json.load(f)
57
+ except json.JSONDecodeError:
58
+ return {"visitors": 0}
59
  else:
60
  counters = {"visitors": 0}
61
  with open(COUNTER_FILE, "w") as f:
 
66
  with open(COUNTER_FILE, "w") as f:
67
  json.dump(counters, f)
68
 
69
+ # Increment visitor count on each new user session
70
+ session_id = str(random.randint(100000, 999999))
71
+ counters = load_counters()
72
+
73
+ # To ensure it increments once per user, simulate a new visitor each reload
74
+ if session_id not in counters:
75
  counters["visitors"] += 1
76
+ counters[session_id] = True
77
  save_counters(counters)
 
78
 
79
  # -------------------------
80
  # Score tracking
 
84
  def check_answer(question_index, selected_option):
85
  question = quiz_questions[question_index]
86
 
 
87
  if selected_option == question["answer"]:
88
  score[0] += 1
89
  feedback = f"✅ Correct!\n\n{question['explanation']}"
90
  else:
91
  feedback = f"❌ Incorrect.\n\n{question['explanation']}"
92
 
 
93
  if question_index == len(quiz_questions) - 1:
94
  feedback += f"\n\n🎯 Your total score: {score[0]}/{len(quiz_questions)}"
95
+ feedback += "\n\n📘 Reference: A. A. Dev, G. Bagheri, E. Bodenschatz, T. M. Hermans, and B. Doudin, “Suppression of Interfacial Instability of Immiscible Liquid-in-Liquid Flow Using Magnetic Forces.” *Adv. Sci.* (2025): e10327. https://doi.org/10.1002/advs.202510327"
96
 
97
  return feedback
98
 
 
100
  return quiz_questions[question_index]["hint"]
101
 
102
  # -------------------------
103
+ # Build interface
104
  # -------------------------
105
+ def create_question_block(i, q):
106
+ with gr.Blocks() as block:
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
+ gr.Markdown(f"👥 Visitors: {counters['visitors']}")
111
+
112
+ with gr.Row():
113
+ gr.Button("Submit Answer").click(fn=lambda x: check_answer(i, x), inputs=options_dropdown, outputs=output)
114
+ gr.Button("Show Hint").click(fn=lambda: show_hint(i), inputs=None, outputs=output)
115
+ return block
 
 
 
 
116
 
117
  # -------------------------
118
+ # Quiz interface
119
  # -------------------------
120
+ question_blocks = [create_question_block(i, q) for i, q in enumerate(quiz_questions)]
121
+ quiz_interface = gr.TabbedInterface(question_blocks, ["Q1", "Q2", "Q3", "Q4", "Q5"])
122
+
123
+ # Launch
124
+ quiz_interface.launch()