xh365 commited on
Commit
d827699
·
1 Parent(s): a8549bf

fix counter bugs

Browse files
Files changed (1) hide show
  1. app.py +18 -19
app.py CHANGED
@@ -35,12 +35,14 @@ VERBAL_MSG = "Please verbally describe why you are satisfied or unsatisfied at t
35
  DEFAULT_SCENARIO = "Product advertisement"
36
  METHODS = ["Baseline", "Experimental"]
37
  MAX_ROUND = 5
 
38
  # intermittent memory
39
  counter1, counter2 = 1, 1
40
  responses_memory = {}
41
  assigned_scenarios = list(SCENARIOS.keys())[:2]
42
  current_task1, current_task2 = METHODS # current task 1 (tab 1)
43
  task1_success, task2_success = False, False
 
44
 
45
  ########################################################################################################
46
  # Generating images with two methods
@@ -126,21 +128,16 @@ def display_info_message(msg, duration=5):
126
  gr.Info(msg, duration=duration)
127
 
128
  def switch_tab(active_tab):
129
- print("switching tab")
130
  if active_tab == "Task A":
131
  return gr.Tabs(selected="Task B")
132
  else:
133
  return gr.Tabs(selected="Task A")
134
 
135
  def check_satisfaction(sim_radio, active_tab):
136
- global counter1, counter2, current_task1, current_task2
137
- method = current_task1 if active_tab == "Task A" else current_task2
138
- counter = counter1 if method == METHODS[0] else counter2
139
-
140
  fully_satisfied_option = ["Satisfied", "Very Satisfied"] # The value to trigger submit
141
- enable_submit = sim_radio in fully_satisfied_option
142
-
143
- return gr.update(interactive=enable_submit)
144
 
145
  def check_participant(participant):
146
  if participant == "":
@@ -180,9 +177,10 @@ def set_user(participant):
180
 
181
  def display_scenario(participant, choice):
182
  # reset intermittent storage when scenario change
183
- global counter1, counter2, responses_memory, current_task1, current_task2, task1_success, task2_success
184
 
185
  task1_success, task2_success = False, False
 
186
  counter1, counter2 = 1, 1
187
 
188
  if check_participant(participant):
@@ -254,8 +252,8 @@ def redesign(participant, scenario, prompt, sim_radio, current_images, history_i
254
  method = current_task1 if active_tab == "Task A" else current_task2
255
 
256
  if check_evaluation(sim_radio) and check_participant(participant):
257
-
258
-
259
  responses_memory[participant][method][counter] = {}
260
  responses_memory[participant][method][counter]["prompt"] = prompt
261
  responses_memory[participant][method][counter]["sim_radio"] = sim_radio
@@ -267,18 +265,18 @@ def redesign(participant, scenario, prompt, sim_radio, current_images, history_i
267
  elif current_images:
268
  history_images.extend(current_images)
269
  current_images = []
 
270
  examples_state = gr.update(samples=history_prompts, visible=True)
271
  prompt_state = gr.update(interactive=True)
272
- next_state = gr.update(interactive=False) if counter >= MAX_ROUND else gr.update(visible=True, interactive=True)
273
  redesign_state = gr.update(interactive=False) if counter >= MAX_ROUND else gr.update(interactive=True)
274
- submit_state = gr.update(interactive=True) if counter >= MAX_ROUND else gr.update(interactive=False)
275
 
 
276
  if method == METHODS[0]:
277
  counter1 += 1
278
- counter = counter1
279
  else:
280
  counter2 += 1
281
- counter = counter2
282
 
283
  return None, None, None, current_images, history_images, examples_state, prompt_state, next_state, redesign_state, submit_state
284
  else:
@@ -287,7 +285,7 @@ def redesign(participant, scenario, prompt, sim_radio, current_images, history_i
287
 
288
  def save_response(participant, scenario, prompt, sim_radio, active_tab):
289
  global current_task1, current_task2 # not change
290
- global task1_success, task2_success, counter1, counter2, responses_memory, assigned_scenarios # will change
291
 
292
  method = current_task1 if active_tab == "Task A" else current_task2
293
  if check_evaluation(sim_radio) and check_participant(participant):
@@ -316,7 +314,8 @@ def save_response(participant, scenario, prompt, sim_radio, active_tab):
316
  task1_success = True
317
  else:
318
  task2_success = True
319
-
 
320
  # decide if change scenario
321
  next_scenario = assigned_scenarios[1] if task1_success and task2_success else assigned_scenarios[0]
322
 
@@ -512,8 +511,8 @@ with gr.Blocks(theme=gr.themes.Soft(font=[gr.themes.GoogleFont("Inconsolata"), "
512
  # prompt2.change(fn=reset_gallery, inputs=[], outputs=[gallery_state2])
513
  next_btn1.click(fn=generate_image, inputs=[participant, scenario, prompt1, active_tab, like_image1, dislike_image1], outputs=[images_method1])
514
  next_btn2.click(fn=generate_image, inputs=[participant, scenario, prompt2, active_tab, like_image2, dislike_image2], outputs=[images_method2])
515
- sim_radio1.change(fn=check_satisfaction, inputs=[sim_radio1, active_tab], outputs=[submit_btn1])
516
- sim_radio2.change(fn=check_satisfaction, inputs=[sim_radio2, active_tab], outputs=[submit_btn2])
517
  dislike_radio1.select(fn=select_image, inputs=[dislike_radio1, images_method1], outputs=[dislike_image1])
518
  like_radio1.select(fn=select_image, inputs=[like_radio1, images_method1], outputs=[like_image1])
519
  dislike_radio2.select(fn=select_image, inputs=[dislike_radio2, images_method2], outputs=[dislike_image2])
 
35
  DEFAULT_SCENARIO = "Product advertisement"
36
  METHODS = ["Baseline", "Experimental"]
37
  MAX_ROUND = 5
38
+
39
  # intermittent memory
40
  counter1, counter2 = 1, 1
41
  responses_memory = {}
42
  assigned_scenarios = list(SCENARIOS.keys())[:2]
43
  current_task1, current_task2 = METHODS # current task 1 (tab 1)
44
  task1_success, task2_success = False, False
45
+ enable_submit = False
46
 
47
  ########################################################################################################
48
  # Generating images with two methods
 
128
  gr.Info(msg, duration=duration)
129
 
130
  def switch_tab(active_tab):
 
131
  if active_tab == "Task A":
132
  return gr.Tabs(selected="Task B")
133
  else:
134
  return gr.Tabs(selected="Task A")
135
 
136
  def check_satisfaction(sim_radio, active_tab):
137
+ global enable_submit
 
 
 
138
  fully_satisfied_option = ["Satisfied", "Very Satisfied"] # The value to trigger submit
139
+ if sim_radio in fully_satisfied_option:
140
+ enable_submit = True
 
141
 
142
  def check_participant(participant):
143
  if participant == "":
 
177
 
178
  def display_scenario(participant, choice):
179
  # reset intermittent storage when scenario change
180
+ global counter1, counter2, responses_memory, current_task1, current_task2, task1_success, task2_success, enable_submit
181
 
182
  task1_success, task2_success = False, False
183
+ enable_submit = False
184
  counter1, counter2 = 1, 1
185
 
186
  if check_participant(participant):
 
252
  method = current_task1 if active_tab == "Task A" else current_task2
253
 
254
  if check_evaluation(sim_radio) and check_participant(participant):
255
+ counter = counter1 if method == METHODS[0] else counter2
256
+
257
  responses_memory[participant][method][counter] = {}
258
  responses_memory[participant][method][counter]["prompt"] = prompt
259
  responses_memory[participant][method][counter]["sim_radio"] = sim_radio
 
265
  elif current_images:
266
  history_images.extend(current_images)
267
  current_images = []
268
+
269
  examples_state = gr.update(samples=history_prompts, visible=True)
270
  prompt_state = gr.update(interactive=True)
271
+ next_state = gr.update(visible=True, interactive=True)
272
  redesign_state = gr.update(interactive=False) if counter >= MAX_ROUND else gr.update(interactive=True)
273
+ submit_state = gr.update(interactive=True) if counter >= MAX_ROUND or enable_submit else gr.update(interactive=False)
274
 
275
+ # update counter
276
  if method == METHODS[0]:
277
  counter1 += 1
 
278
  else:
279
  counter2 += 1
 
280
 
281
  return None, None, None, current_images, history_images, examples_state, prompt_state, next_state, redesign_state, submit_state
282
  else:
 
285
 
286
  def save_response(participant, scenario, prompt, sim_radio, active_tab):
287
  global current_task1, current_task2 # not change
288
+ global task1_success, task2_success, counter1, counter2, enable_submit, responses_memory, assigned_scenarios # will change
289
 
290
  method = current_task1 if active_tab == "Task A" else current_task2
291
  if check_evaluation(sim_radio) and check_participant(participant):
 
314
  task1_success = True
315
  else:
316
  task2_success = True
317
+ enable_submit = False
318
+
319
  # decide if change scenario
320
  next_scenario = assigned_scenarios[1] if task1_success and task2_success else assigned_scenarios[0]
321
 
 
511
  # prompt2.change(fn=reset_gallery, inputs=[], outputs=[gallery_state2])
512
  next_btn1.click(fn=generate_image, inputs=[participant, scenario, prompt1, active_tab, like_image1, dislike_image1], outputs=[images_method1])
513
  next_btn2.click(fn=generate_image, inputs=[participant, scenario, prompt2, active_tab, like_image2, dislike_image2], outputs=[images_method2])
514
+ sim_radio1.change(fn=check_satisfaction, inputs=[sim_radio1, active_tab])
515
+ sim_radio2.change(fn=check_satisfaction, inputs=[sim_radio2, active_tab])
516
  dislike_radio1.select(fn=select_image, inputs=[dislike_radio1, images_method1], outputs=[dislike_image1])
517
  like_radio1.select(fn=select_image, inputs=[like_radio1, images_method1], outputs=[like_image1])
518
  dislike_radio2.select(fn=select_image, inputs=[dislike_radio2, images_method2], outputs=[dislike_image2])