Tristan Thrush commited on
Commit
40bc8d5
1 Parent(s): a4b2b7a

changes to make it work on huggingface.co in addition to mturk

Browse files
Files changed (1) hide show
  1. app.py +39 -23
app.py CHANGED
@@ -63,11 +63,13 @@ with demo:
63
  toggle_example_submit = gr.update(visible=not done)
64
  new_state_md = f"State: {state['cnt']}/{total_cnt} ({state['fooled']} fooled)"
65
 
66
- # We need to store the assignmentId in the state before submit_hit_button
67
- # is clicked. We can do this here in _predict, which is called before
68
- # submit_hit_button is clicked
69
- query = parse_qs(dummy[1:])
70
- state["assignmentId"] = query["assignmentId"][0]
 
 
71
 
72
  return pred_confidences, ret, state, toggle_example_submit, toggle_final_submit, new_state_md, dummy
73
 
@@ -87,11 +89,22 @@ with demo:
87
  # The HIT is also stored and logged on mturk when post_hit_js is run below.
88
  # This _store_in_huggingface_dataset function just demonstrates how easy it is
89
  # to automatically create a Hugging Face dataset from mturk.
90
- def _store_in_huggingface_dataset(state):
91
  with open(DATA_FILE, "a") as jsonlfile:
92
- jsonlfile.write(json.dumps(state))
93
  repo.push_to_hub()
94
 
 
 
 
 
 
 
 
 
 
 
 
95
  # Button event handlers
96
  get_window_location_search_js = """
97
  function(text_input, label_input, state, dummy) {
@@ -107,27 +120,30 @@ with demo:
107
  )
108
 
109
  post_hit_js = """
110
- function(state) {
111
- const form = document.createElement('form');
112
- form.action = 'https://workersandbox.mturk.com/mturk/externalSubmit';
113
- form.method = 'post';
114
- for (const key in state) {
115
- const hiddenField = document.createElement('input');
116
- hiddenField.type = 'hidden';
117
- hiddenField.name = key;
118
- hiddenField.value = state[key];
119
- form.appendChild(hiddenField)
120
- };
121
- document.body.appendChild(form);
122
- form.submit();
123
- return [state];
 
 
 
124
  }
125
  """
126
 
127
  submit_hit_button.click(
128
  _store_in_huggingface_dataset,
129
- inputs=[state],
130
- outputs=None,
131
  _js=post_hit_js,
132
  )
133
 
 
63
  toggle_example_submit = gr.update(visible=not done)
64
  new_state_md = f"State: {state['cnt']}/{total_cnt} ({state['fooled']} fooled)"
65
 
66
+ if "assignmentId" in query:
67
+ # It seems that someone is using this app on mturk. We need to
68
+ # store the assignmentId in the state before submit_hit_button
69
+ # is clicked. We can do this here in _predict. We need to save the
70
+ # assignmentId so that the turker can get credit for their hit.
71
+ query = parse_qs(dummy[1:])
72
+ state["assignmentId"] = query["assignmentId"][0]
73
 
74
  return pred_confidences, ret, state, toggle_example_submit, toggle_final_submit, new_state_md, dummy
75
 
 
89
  # The HIT is also stored and logged on mturk when post_hit_js is run below.
90
  # This _store_in_huggingface_dataset function just demonstrates how easy it is
91
  # to automatically create a Hugging Face dataset from mturk.
92
+ def _store_in_huggingface_dataset(state, toggle_final_submit, toggle_example_submit, new_state_md):
93
  with open(DATA_FILE, "a") as jsonlfile:
94
+ jsonlfile.write(json.dumps(state) + "\n")
95
  repo.push_to_hub()
96
 
97
+ if state["assignmentId"] == "":
98
+ # If assignmentId is not set, then someone is using this app on
99
+ # huggingface.co, and we can reset the app to it's initial state
100
+ # after they submit their fake "HIT".
101
+ state = {"assignmentId": "", "cnt": 0, "fooled": 0, "data": [], "metadata": {}}
102
+ toggle_final_submit = gr.update(visible=False)
103
+ toggle_example_submit = gr.update(visible=True)
104
+ new_state_md = gr.Markdown(f"State: 0/{total_cnt} (0 fooled)")
105
+
106
+ return state, toggle_final_submit, toggle_example_submit, new_state_md
107
+
108
  # Button event handlers
109
  get_window_location_search_js = """
110
  function(text_input, label_input, state, dummy) {
 
120
  )
121
 
122
  post_hit_js = """
123
+ function(state, toggle_final_submit, toggle_example_submit, new_state_md) {
124
+ if (state["assignmentId"] !== "") {
125
+ //It seems that someone is using this app on mturk, so submit the HIT.
126
+ const form = document.createElement('form');
127
+ form.action = 'https://workersandbox.mturk.com/mturk/externalSubmit';
128
+ form.method = 'post';
129
+ for (const key in state) {
130
+ const hiddenField = document.createElement('input');
131
+ hiddenField.type = 'hidden';
132
+ hiddenField.name = key;
133
+ hiddenField.value = state[key];
134
+ form.appendChild(hiddenField)
135
+ };
136
+ document.body.appendChild(form);
137
+ form.submit();
138
+ }
139
+ return [state, toggle_final_submit, toggle_example_submit, new_state_md];
140
  }
141
  """
142
 
143
  submit_hit_button.click(
144
  _store_in_huggingface_dataset,
145
+ inputs=[state, toggle_final_submit, toggle_example_submit, new_state_md],
146
+ outputs=[state, toggle_final_submit, toggle_example_submit, new_state_md],
147
  _js=post_hit_js,
148
  )
149