Tristan Thrush commited on
Commit
9a12ee6
1 Parent(s): 3f9ceca

minor changes per some of douwe's feedback

Browse files
Files changed (1) hide show
  1. app.py +10 -8
app.py CHANGED
@@ -26,8 +26,11 @@ repo = Repository(
26
  local_dir="data", clone_from=DATASET_REPO_URL, use_auth_token=HF_TOKEN
27
  )
28
 
 
 
29
  # This function pushes the HIT data written in data.jsonl to our Hugging Face
30
  # dataset every minute. Adjust the frequency to suit your needs.
 
31
  def asynchronous_push(f_stop):
32
  if repo.is_repo_clean():
33
  print("Repo currently clean. Ignoring push_to_hub")
@@ -40,7 +43,7 @@ def asynchronous_push(f_stop):
40
  repo.git_push()
41
  if not f_stop.is_set():
42
  # call again in 60 seconds
43
- threading.Timer(60, asynchronous_push, [f_stop]).start()
44
 
45
  f_stop = threading.Event()
46
  asynchronous_push(f_stop)
@@ -51,7 +54,6 @@ pipe = pipeline("sentiment-analysis")
51
  demo = gr.Blocks()
52
 
53
  with demo:
54
- total_cnt = 2 # How many examples per HIT
55
  dummy = gr.Textbox(visible=False) # dummy for passing assignmentId
56
 
57
  # We keep track of state as a JSON
@@ -61,7 +63,7 @@ with demo:
61
  gr.Markdown("# DADC in Gradio example")
62
  gr.Markdown("Try to fool the model and find an example where it predicts the wrong label!")
63
 
64
- state_display = gr.Markdown(f"State: 0/{total_cnt} (0 fooled)")
65
 
66
  # Generate model prediction
67
  # Default model: distilbert-base-uncased-finetuned-sst-2-english
@@ -80,9 +82,9 @@ with demo:
80
  ret += " You did not fool the model! Too bad, try again!"
81
  state["cnt"] += 1
82
 
83
- done = state["cnt"] == total_cnt
84
  toggle_example_submit = gr.update(visible=not done)
85
- new_state_md = f"State: {state['cnt']}/{total_cnt} ({state['cnt_fooled']} fooled)"
86
 
87
  state["data"].append({"cnt": state["cnt"], "text": txt, "target": tgt.lower(), "model_pred": pred["label"].lower(), "fooled": fooled})
88
 
@@ -99,8 +101,8 @@ with demo:
99
  toggle_final_submit_preview = gr.update(visible=done)
100
  toggle_final_submit = gr.update(visible=False)
101
 
102
- if state["cnt"] == total_cnt:
103
- # Write the HIT data to our local dataset because the person has
104
  # submitted everything now.
105
  with open(DATA_FILE, "a") as jsonlfile:
106
  json_data_with_assignment_id =\
@@ -121,7 +123,7 @@ with demo:
121
  with gr.Column(visible=False) as final_submit:
122
  submit_hit_button = gr.Button("Submit HIT")
123
  with gr.Column(visible=False) as final_submit_preview:
124
- submit_hit_button_preview = gr.Button("Submit Work (preview mode; no mturk HIT credit)")
125
 
126
  # Button event handlers
127
  get_window_location_search_js = """
 
26
  local_dir="data", clone_from=DATASET_REPO_URL, use_auth_token=HF_TOKEN
27
  )
28
 
29
+ TOTAL_CNT = 2 # How many examples per HIT
30
+
31
  # This function pushes the HIT data written in data.jsonl to our Hugging Face
32
  # dataset every minute. Adjust the frequency to suit your needs.
33
+ PUSH_FREQUENCY = 60
34
  def asynchronous_push(f_stop):
35
  if repo.is_repo_clean():
36
  print("Repo currently clean. Ignoring push_to_hub")
 
43
  repo.git_push()
44
  if not f_stop.is_set():
45
  # call again in 60 seconds
46
+ threading.Timer(PUSH_FREQUENCY, asynchronous_push, [f_stop]).start()
47
 
48
  f_stop = threading.Event()
49
  asynchronous_push(f_stop)
 
54
  demo = gr.Blocks()
55
 
56
  with demo:
 
57
  dummy = gr.Textbox(visible=False) # dummy for passing assignmentId
58
 
59
  # We keep track of state as a JSON
 
63
  gr.Markdown("# DADC in Gradio example")
64
  gr.Markdown("Try to fool the model and find an example where it predicts the wrong label!")
65
 
66
+ state_display = gr.Markdown(f"State: 0/{TOTAL_CNT} (0 fooled)")
67
 
68
  # Generate model prediction
69
  # Default model: distilbert-base-uncased-finetuned-sst-2-english
 
82
  ret += " You did not fool the model! Too bad, try again!"
83
  state["cnt"] += 1
84
 
85
+ done = state["cnt"] == TOTAL_CNT
86
  toggle_example_submit = gr.update(visible=not done)
87
+ new_state_md = f"State: {state['cnt']}/{TOTAL_CNT} ({state['cnt_fooled']} fooled)"
88
 
89
  state["data"].append({"cnt": state["cnt"], "text": txt, "target": tgt.lower(), "model_pred": pred["label"].lower(), "fooled": fooled})
90
 
 
101
  toggle_final_submit_preview = gr.update(visible=done)
102
  toggle_final_submit = gr.update(visible=False)
103
 
104
+ if state["cnt"] == TOTAL_CNT:
105
+ # Write the HIT data to our local dataset because the worker has
106
  # submitted everything now.
107
  with open(DATA_FILE, "a") as jsonlfile:
108
  json_data_with_assignment_id =\
 
123
  with gr.Column(visible=False) as final_submit:
124
  submit_hit_button = gr.Button("Submit HIT")
125
  with gr.Column(visible=False) as final_submit_preview:
126
+ submit_hit_button_preview = gr.Button("Submit Work (preview mode; no mturk HIT credit, but your examples will still be stored)")
127
 
128
  # Button event handlers
129
  get_window_location_search_js = """