Tristan Thrush commited on
Commit
28cc200
1 Parent(s): df77487
Files changed (1) hide show
  1. app.py +41 -30
app.py CHANGED
@@ -60,21 +60,25 @@ with demo:
60
  state["cnt"] += 1
61
 
62
  done = state["cnt"] == total_cnt
63
- toggle_final_submit = gr.update(visible=done)
64
  toggle_example_submit = gr.update(visible=not done)
65
  new_state_md = f"State: {state['cnt']}/{total_cnt} ({state['cnt_fooled']} fooled)"
66
 
67
  state["data"].append({"cnt": state["cnt"], "text": txt, "target": tgt, "model_pred": pred["label"], "fooled": fooled})
68
 
69
  query = parse_qs(dummy[1:])
70
- if "assignmentId" in query:
71
  # It seems that someone is using this app on mturk. We need to
72
  # store the assignmentId in the state before submit_hit_button
73
  # is clicked. We can do this here in _predict. We need to save the
74
  # assignmentId so that the turker can get credit for their HIT.
75
  state["assignmentId"] = query["assignmentId"][0]
 
 
 
 
 
76
 
77
- return pred_confidences, ret, state, toggle_example_submit, toggle_final_submit, new_state_md, dummy
78
 
79
  # Input fields
80
  text_input = gr.Textbox(placeholder="Enter model-fooling statement", show_label=False)
@@ -87,6 +91,8 @@ with demo:
87
  submit_ex_button = gr.Button("Submit")
88
  with gr.Column(visible=False) as final_submit:
89
  submit_hit_button = gr.Button("Submit HIT")
 
 
90
 
91
  # Store the HIT data into a Hugging Face dataset.
92
  # The HIT is also stored and logged on mturk when post_hit_js is run below.
@@ -115,38 +121,27 @@ with demo:
115
  submit_ex_button.click(
116
  _predict,
117
  inputs=[text_input, label_input, state, dummy],
118
- outputs=[label_output, text_output, state, example_submit, final_submit, state_display, dummy],
119
  _js=get_window_location_search_js,
120
  )
121
 
122
  post_hit_js = """
123
  function(state) {
124
- if (state["assignmentId"] !== "" && state["assignmentId"] !== "ASSIGNMENT_ID_NOT_AVAILABLE"){
125
- // If there is an assignmentId, then the submitter is on mturk
126
- // and has accepted the HIT. So, we need to submit their HIT.
127
- const form = document.createElement('form');
128
- form.action = 'https://workersandbox.mturk.com/mturk/externalSubmit';
129
- form.method = 'post';
130
- for (const key in state) {
131
- const hiddenField = document.createElement('input');
132
- hiddenField.type = 'hidden';
133
- hiddenField.name = key;
134
- hiddenField.value = state[key];
135
- form.appendChild(hiddenField);
136
- };
137
- document.body.appendChild(form);
138
- form.submit();
139
- return state;
140
- } else {
141
- // If there is no assignmentId, then we assume that the submitter is
142
- // on huggingface.co or there is an mturker doing the preview.
143
- // This means that we can't log a HIT in mturk, but
144
- // _store_in_huggingface_dataset will still store the example in
145
- // our dataset without an assignmentId. The following line here
146
- // loads the app again so the user can enter in another "fake" HIT.
147
- window.location.href = window.location.href;
148
- return state;
149
- }
150
  }
151
  """
152
 
@@ -157,4 +152,20 @@ with demo:
157
  _js=post_hit_js,
158
  )
159
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
160
  demo.launch()
 
60
  state["cnt"] += 1
61
 
62
  done = state["cnt"] == total_cnt
 
63
  toggle_example_submit = gr.update(visible=not done)
64
  new_state_md = f"State: {state['cnt']}/{total_cnt} ({state['cnt_fooled']} fooled)"
65
 
66
  state["data"].append({"cnt": state["cnt"], "text": txt, "target": tgt, "model_pred": pred["label"], "fooled": fooled})
67
 
68
  query = parse_qs(dummy[1:])
69
+ if "assignmentId" in query and query["assignmentId"][0] != "ASSIGNMENT_ID_NOT_AVAILABLE":
70
  # It seems that someone is using this app on mturk. We need to
71
  # store the assignmentId in the state before submit_hit_button
72
  # is clicked. We can do this here in _predict. We need to save the
73
  # assignmentId so that the turker can get credit for their HIT.
74
  state["assignmentId"] = query["assignmentId"][0]
75
+ toggle_final_submit = gr.update(visible=done)
76
+ toggle_final_submit_preview = gr.update(visible=False)
77
+ else:
78
+ toggle_final_submit_preview = gr.update(visible=done)
79
+ toggle_final_submit = gr.update(visible=False)
80
 
81
+ return pred_confidences, ret, state, toggle_example_submit, toggle_final_submit, toggle_final_submit_preview, new_state_md, dummy
82
 
83
  # Input fields
84
  text_input = gr.Textbox(placeholder="Enter model-fooling statement", show_label=False)
 
91
  submit_ex_button = gr.Button("Submit")
92
  with gr.Column(visible=False) as final_submit:
93
  submit_hit_button = gr.Button("Submit HIT")
94
+ with gr.Column(visible=False) as final_submit_preview:
95
+ submit_hit_button_preview = gr.Button("Submit Work (preview mode; no mturk HIT credit)")
96
 
97
  # Store the HIT data into a Hugging Face dataset.
98
  # The HIT is also stored and logged on mturk when post_hit_js is run below.
 
121
  submit_ex_button.click(
122
  _predict,
123
  inputs=[text_input, label_input, state, dummy],
124
+ outputs=[label_output, text_output, state, example_submit, final_submit, final_submit_preview, state_display, dummy],
125
  _js=get_window_location_search_js,
126
  )
127
 
128
  post_hit_js = """
129
  function(state) {
130
+ // If there is an assignmentId, then the submitter is on mturk
131
+ // and has accepted the HIT. So, we need to submit their HIT.
132
+ const form = document.createElement('form');
133
+ form.action = 'https://workersandbox.mturk.com/mturk/externalSubmit';
134
+ form.method = 'post';
135
+ for (const key in state) {
136
+ const hiddenField = document.createElement('input');
137
+ hiddenField.type = 'hidden';
138
+ hiddenField.name = key;
139
+ hiddenField.value = state[key];
140
+ form.appendChild(hiddenField);
141
+ };
142
+ document.body.appendChild(form);
143
+ form.submit();
144
+ return state;
 
 
 
 
 
 
 
 
 
 
 
145
  }
146
  """
147
 
 
152
  _js=post_hit_js,
153
  )
154
 
155
+ refresh_app_js = """
156
+ function(state) {
157
+ // The following line here loads the app again so the user can
158
+ // enter in another preview-mode "HIT".
159
+ window.location.href = window.location.href;
160
+ return state;
161
+ }
162
+ """
163
+
164
+ submit_hit_button_preview.click(
165
+ _store_in_huggingface_dataset,
166
+ inputs=[state],
167
+ outputs=[state],
168
+ _js=refresh_app_js,
169
+ )
170
+
171
  demo.launch()