HotshotGoku commited on
Commit
e5bb8a3
·
1 Parent(s): 8ff7133

Start button timer added

Browse files
Files changed (1) hide show
  1. app.py +47 -79
app.py CHANGED
@@ -20,24 +20,6 @@ gc = service_account_from_dict(info)
20
  ws = gc.open_by_key("1lfhomrFnhmRIxQvdJZr-7gASA3sONBT_mblHNreMKvc").sheet1
21
 
22
 
23
- # no start timer function needed here, since we start it on first Fake? click
24
- # (or Retry, which resets it)
25
- # def start_timer(state):
26
- # # only set once
27
- # if state is None:
28
- # state = {}
29
- # if "timer_start" not in state:
30
- # state["timer_start"] = datetime.now().isoformat()
31
- # return state
32
-
33
-
34
- def click_timer(is_checked, state):
35
- # if they check any Fake? box and timer isn't running yet, start it
36
- if state is None:
37
- state = {}
38
- if is_checked and "timer_start" not in state:
39
- state["timer_start"] = datetime.now().isoformat()
40
- return state
41
 
42
  def record_run(name, session_start, attempt_num, elapsed_sec, familiarity, correct_count, wrong_list):
43
  """
@@ -62,18 +44,6 @@ def get_new_grid(state=None):
62
  - Leave timer_start unset on initial load (so they must click Start or first Fake?)
63
  """
64
 
65
- # # 1) Initialize or preserve only attempts/wrongs and any existing timer
66
- # if state is None or "attempts" not in state:
67
- # attempts = []
68
- # wrongs = []
69
- # # do NOT set timer_start here—that only comes from the checkbox
70
- # else:
71
- # attempts = state["attempts"]
72
- # wrongs = state["wrongs"]
73
- # # preserve timer_start if previously set
74
- # timer_start = state.get("timer_start") if state else None
75
-
76
- # 1) Preserve attempt_count or start at 0
77
  attempt_count = (state or {}).get("attempt_count", 0)
78
 
79
 
@@ -91,23 +61,14 @@ def get_new_grid(state=None):
91
  thumbs = [np.array(Image.open(p).convert("RGB")) for p in paths]
92
  resets = [False] * 16
93
 
94
- # # 3) Build the updated state
95
- # new_state = {
96
- # "session_start": session_start,
97
- # "attempts": attempts,
98
- # "wrongs": wrongs,
99
- # "paths": paths,
100
- # "truth": truth
101
- # }
102
 
103
  new_state = {
104
  "paths": paths,
105
  "truth": truth,
106
- "attempt_count": attempt_count , # increment attempt count
 
107
  }
108
- # Reset timer on Retry (state exists); leave unset on first load (state is None)
109
- if state is not None:
110
- new_state["timer_start"] = datetime.now().isoformat()
111
 
112
  return (*thumbs, *resets, new_state)
113
 
@@ -163,7 +124,6 @@ def evaluate(
163
 
164
  return (*bordered, state, summary)
165
 
166
-
167
  with gr.Blocks(fill_width=True) as demo:
168
  # Terms & Conditions gate
169
  terms_cb = gr.Checkbox(
@@ -181,50 +141,58 @@ with gr.Blocks(fill_width=True) as demo:
181
  # Familiarity slider (1=first time, 10=expert)
182
  familiarity = gr.Slider(
183
  minimum=1, maximum=10, step=1, value=1,
184
- label="How familiar are you with P. aeruginosa patterns? 1: first time seeing these 10: Have worked with these patterns before",
185
  interactive=True
186
  )
187
  state = gr.State(None)
188
 
189
- # start_cb = gr.Checkbox(label="Timer starts now", interactive=True)
190
- # start_cb.change(fn=start_timer, inputs=[state], outputs=[state])
191
-
192
-
193
-
194
- image_components = []
195
- checkbox_components = []
196
- for row in range(4):
197
- with gr.Row():
198
- for col in range(4):
199
- idx = row * 4 + col
200
- with gr.Column(min_width=200, scale=1):
201
- img = gr.Image(type="numpy", interactive=False, show_label=False)
202
- cb = gr.Checkbox(label="Fake?", elem_id=f"cb_{idx}")
203
- image_components.append(img)
204
- checkbox_components.append(cb)
205
-
206
- # if they forget the box, start timer on first Fake? checkbox click
207
- for cb in checkbox_components:
208
- cb.change(fn=click_timer, inputs=[cb, state], outputs=[state])
209
-
210
- submit_btn = gr.Button("Submit")
211
- retry_btn = gr.Button("Retry")
212
- score_out = gr.Textbox(label="Score & Time", interactive=False)
213
-
214
- demo.load(
215
- fn=get_new_grid,
216
- inputs=[state],
217
- outputs=[*image_components, *checkbox_components, state]
218
- )
219
- retry_btn.click(
 
 
 
 
 
 
 
 
220
  fn=get_new_grid,
221
  inputs=[state],
222
  outputs=[*image_components, *checkbox_components, state]
223
  )
224
- submit_btn.click(
225
- fn=evaluate,
226
- inputs=[*checkbox_components, state,user_name, familiarity],
227
- outputs=[*image_components, state,score_out]
228
  )
229
 
230
 
 
20
  ws = gc.open_by_key("1lfhomrFnhmRIxQvdJZr-7gASA3sONBT_mblHNreMKvc").sheet1
21
 
22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
  def record_run(name, session_start, attempt_num, elapsed_sec, familiarity, correct_count, wrong_list):
25
  """
 
44
  - Leave timer_start unset on initial load (so they must click Start or first Fake?)
45
  """
46
 
 
 
 
 
 
 
 
 
 
 
 
 
47
  attempt_count = (state or {}).get("attempt_count", 0)
48
 
49
 
 
61
  thumbs = [np.array(Image.open(p).convert("RGB")) for p in paths]
62
  resets = [False] * 16
63
 
 
 
 
 
 
 
 
 
64
 
65
  new_state = {
66
  "paths": paths,
67
  "truth": truth,
68
+ "attempt_count": attempt_count , # increment attempt count,
69
+ "timer_start": datetime.now().isoformat()
70
  }
71
+
 
 
72
 
73
  return (*thumbs, *resets, new_state)
74
 
 
124
 
125
  return (*bordered, state, summary)
126
 
 
127
  with gr.Blocks(fill_width=True) as demo:
128
  # Terms & Conditions gate
129
  terms_cb = gr.Checkbox(
 
141
  # Familiarity slider (1=first time, 10=expert)
142
  familiarity = gr.Slider(
143
  minimum=1, maximum=10, step=1, value=1,
144
+ label="How familiar are you with P. aeruginosa patterns? 1: First time seeing these 10: Have worked with these patterns before",
145
  interactive=True
146
  )
147
  state = gr.State(None)
148
 
149
+ # Add a Start button that WILL load images and start the timer
150
+ start_btn = gr.Button("Start")
151
+
152
+ # Now wrap the grid + controls in a hidden Column
153
+ with gr.Column(visible=False) as grid_ui:
154
+
155
+ image_components = []
156
+ checkbox_components = []
157
+ for row in range(4):
158
+ with gr.Row():
159
+ for col in range(4):
160
+ idx = row * 4 + col
161
+ with gr.Column(min_width=200, scale=1):
162
+ img = gr.Image(type="numpy", interactive=False, show_label=False)
163
+ cb = gr.Checkbox(label="Fake?", elem_id=f"cb_{idx}")
164
+ image_components.append(img)
165
+ checkbox_components.append(cb)
166
+
167
+
168
+
169
+
170
+ submit_btn = gr.Button("Submit")
171
+ retry_btn = gr.Button("Retry")
172
+ score_out = gr.Textbox(label="Score & Time", interactive=False)
173
+
174
+
175
+
176
+ retry_btn.click(
177
+ fn=get_new_grid,
178
+ inputs=[state],
179
+ outputs=[*image_components, *checkbox_components, state]
180
+ )
181
+ submit_btn.click(
182
+ fn=evaluate,
183
+ inputs=[*checkbox_components, state,user_name, familiarity],
184
+ outputs=[*image_components, state,score_out]
185
+ )
186
+ # Wire start_btn to both load the grid _and_ reveal it
187
+ start_btn.click(
188
  fn=get_new_grid,
189
  inputs=[state],
190
  outputs=[*image_components, *checkbox_components, state]
191
  )
192
+ start_btn.click(
193
+ fn=lambda _: gr.update(visible=True),
194
+ inputs=[],
195
+ outputs=[grid_ui]
196
  )
197
 
198