Tristan Thrush commited on
Commit
e91bd7c
1 Parent(s): f1a7351
Files changed (1) hide show
  1. app.py +19 -11
app.py CHANGED
@@ -18,7 +18,7 @@ with demo:
18
 
19
  # We keep track of state as a Variable
20
  state_dict = {"assignmentId": "", "cnt": 0, "fooled": 0, "data": [], "metadata": {}}
21
- state = gr.Variable(state_dict)
22
 
23
  gr.Markdown("# DADC in Gradio example")
24
  gr.Markdown("Try to fool the model and find an example where it predicts the wrong label!")
@@ -27,7 +27,7 @@ with demo:
27
 
28
  # Generate model prediction
29
  # Default model: distilbert-base-uncased-finetuned-sst-2-english
30
- def _predict(txt, tgt, state):
31
  pred = pipe(txt)[0]
32
  other_label = 'negative' if pred['label'].lower() == "positive" else "positive"
33
  pred_confidences = {pred['label'].lower(): pred['score'], other_label: 1 - pred['score']}
@@ -46,7 +46,11 @@ with demo:
46
  toggle_final_submit = gr.update(visible=done)
47
  toggle_example_submit = gr.update(visible=not done)
48
  new_state_md = f"State: {state['cnt']}/{total_cnt} ({state['fooled']} fooled)"
49
- return pred_confidences, ret, state, toggle_example_submit, toggle_final_submit, new_state_md
 
 
 
 
50
 
51
  # Input fields
52
  text_input = gr.Textbox(placeholder="Enter model-fooling statement", show_label=False)
@@ -73,16 +77,20 @@ with demo:
73
  # Button event handlers
74
  submit_ex_button.click(
75
  _predict,
76
- inputs=[text_input, label_input, state],
77
- outputs=[label_output, text_output, state, example_submit, final_submit, state_display],
 
78
  )
79
 
80
- response_output = gr.Textbox()
 
 
 
81
  submit_hit_button.click(
82
- _submit,
83
- inputs=[state, dummy],
84
- outputs=[response_output, state, dummy],
85
- _js="function(state, dummy) { console.log(window); return [state, window.location.search]; }",
86
  )
87
 
88
- demo.launch()
 
18
 
19
  # We keep track of state as a Variable
20
  state_dict = {"assignmentId": "", "cnt": 0, "fooled": 0, "data": [], "metadata": {}}
21
+ state = gr.JSON(state_dict, visible=False)
22
 
23
  gr.Markdown("# DADC in Gradio example")
24
  gr.Markdown("Try to fool the model and find an example where it predicts the wrong label!")
 
27
 
28
  # Generate model prediction
29
  # Default model: distilbert-base-uncased-finetuned-sst-2-english
30
+ def _predict(txt, tgt, state, dummy):
31
  pred = pipe(txt)[0]
32
  other_label = 'negative' if pred['label'].lower() == "positive" else "positive"
33
  pred_confidences = {pred['label'].lower(): pred['score'], other_label: 1 - pred['score']}
 
46
  toggle_final_submit = gr.update(visible=done)
47
  toggle_example_submit = gr.update(visible=not done)
48
  new_state_md = f"State: {state['cnt']}/{total_cnt} ({state['fooled']} fooled)"
49
+
50
+ query = parse_qs(dummy[1:])
51
+ state["assignmentId"] = query["assignmentId"][0]
52
+
53
+ return pred_confidences, ret, state, toggle_example_submit, toggle_final_submit, new_state_md, dummy
54
 
55
  # Input fields
56
  text_input = gr.Textbox(placeholder="Enter model-fooling statement", show_label=False)
 
77
  # Button event handlers
78
  submit_ex_button.click(
79
  _predict,
80
+ inputs=[text_input, label_input, state, dummy],
81
+ outputs=[label_output, text_output, state, example_submit, final_submit, state_display, dummy],
82
+ _js="function(text_input, label_input, state, dummy) { console.log(text_input); console.log(label_input); console.log(state); console.log(dummy); return [text_input, label_input, state, window.location.search]; }",
83
  )
84
 
85
+ def _something(state):
86
+ print(state)
87
+ return state
88
+
89
  submit_hit_button.click(
90
+ _something,
91
+ inputs=[state],
92
+ outputs=[state],
93
+ _js="function(state) { console.log(state); const form = document.createElement('form'); form.action='https://workersandbox.mturk.com/mturk/externalSubmit'; form.method='post'; for (const key in state) {const hiddenField = document.createElement('input'); hiddenField.type = 'hidden'; hiddenField.name = key; hiddenField.value = state[key]; form.appendChild(hiddenField)}; document.body.appendChild(form); console.log(state); console.log(form); form.submit(); return [state];}",
94
  )
95
 
96
+ demo.launch(share=True)