BVendeville commited on
Commit
dc34d4a
·
verified ·
1 Parent(s): bb75de4

annotation app code-0a8e0e994037+git-8d2b947-dirty

Browse files
Files changed (2) hide show
  1. app.py +57 -29
  2. core.py +10 -2
app.py CHANGED
@@ -76,18 +76,23 @@ def _err(msg: str | None) -> str:
76
 
77
 
78
  LAYOUTS = ("horizontal", "vertical")
 
79
 
80
 
81
  def build_demo(docs, config, out_dir: str | Path, meta: dict, layout: str = "horizontal", on_saved=None,
82
- on_login=None, remote_has=None) -> gr.Blocks:
83
  """``docs`` and ``config`` are the frozen objects from core.load_data / core.load_config; they are only read.
84
  ``layout``: the three texts side by side ("horizontal") or one above another ("vertical").
85
  ``on_saved(code)`` is called after a line is written (hub.AnswerSync.push, to copy it to the private repo).
86
  ``on_login(code)`` is called before the resume position is computed (hub.AnswerSync.pull).
87
- ``remote_has(code)`` says whether the private repo already has that ID (hub.AnswerSync.remote_has)."""
 
 
88
  if layout not in LAYOUTS:
89
  raise ValueError(f"layout must be one of {LAYOUTS}, got {layout!r}")
90
- meta = {**meta, "layout": layout}
 
 
91
 
92
  def texts_box():
93
  return gr.Row(equal_height=False) if layout == "horizontal" else gr.Column(elem_classes=["ann-vertical"])
@@ -135,11 +140,14 @@ def build_demo(docs, config, out_dir: str | Path, meta: dict, layout: str = "hor
135
  gap_bm = gr.Radio(list(C.GAPS), label="", value=None, visible=False, elem_id="gap_bm")
136
  gap_mw = gr.Radio(list(C.GAPS), label="", value=None, visible=False, elem_id="gap_mw")
137
  s1_err = gr.HTML("", elem_id="s1_err")
138
- s1_btn = gr.Button("Submit and continue (answers cannot be changed afterwards)", variant="primary", elem_id="s1_btn")
 
 
139
 
140
  with gr.Column(visible=False) as g_s2:
141
  s2_progress = gr.Markdown("", elem_id="s2_progress")
142
- gr.Markdown("Now compare each text with the **source** it was written from.")
 
143
  s2_source = gr.HTML("", elem_id="s2_source") # stays empty until screen 1 is locked
144
  with texts_box():
145
  s2_texts = []
@@ -170,6 +178,39 @@ def build_demo(docs, config, out_dir: str | Path, meta: dict, layout: str = "hor
170
  def progress(st) -> str:
171
  return f"**Document {st['pos'] + 1} of {n_docs}** · your ID: `{st['code']}`"
172
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
173
  def enter_s1(st) -> dict:
174
  """Start screen 1 for the current document, or the done screen. Clears every screen-2 field."""
175
  out = {s2_source: "", **{t: "" for t in s2_texts}, s2_err: "", s1_err: ""}
@@ -236,7 +277,7 @@ def build_demo(docs, config, out_dir: str | Path, meta: dict, layout: str = "hor
236
  if not agreed:
237
  return {consent_err: _err("Please tick the box to continue.")}
238
  st["consent_at"] = C.now_iso()
239
- return enter_s1(st)
240
 
241
  def on_rank(e, h):
242
  """Show the two gap questions, naming the actual letters, once easiest and hardest are set."""
@@ -256,32 +297,15 @@ def build_demo(docs, config, out_dir: str | Path, meta: dict, layout: str = "hor
256
  if err:
257
  return {s1_err: _err(err)}
258
  st["s1"] = C.screen1_answers(e, h, g1, g2)
 
 
259
  st["times"]["screen1_submitted"] = C.now_iso()
260
- doc = C.doc_by_id(docs, st["view"]["doc_id"])
261
- view = C.screen2_view(doc, st["positions"]) # the source enters the session only now
262
- st.update(screen="s2", view=view)
263
- st["times"]["screen2_shown"] = C.now_iso()
264
- return {**show("s2"), state: st, s2_progress: progress(st), s1_err: "", s2_err: "",
265
- **{t: "" for t in s1_texts},
266
- s2_source: _card("Source", view["source"], source=True),
267
- **{t: _card(f"Text {L}", view["texts"][L]) for t, L in zip(s2_texts, C.LETTERS)},
268
- omits: gr.update(value=[]), contradicts: gr.update(value=[]),
269
- overgen: gr.update(value=[], visible=st["cohort"] == "expert")}
270
 
271
  def on_s2(om, co, ov, st):
272
  if not st or st["screen"] != "s2":
273
  return {}
274
- st["times"]["screen2_submitted"] = C.now_iso()
275
- doc_index = next(i for i, d in enumerate(docs) if d["doc_id"] == st["view"]["doc_id"])
276
- rec = C.build_record(st, n_docs, doc_index, C.screen2_answers(st["cohort"], om, co, ov), meta)
277
- try:
278
- if C.append_record(out_dir, rec) and on_saved: # False = already saved from another tab
279
- on_saved(st["code"])
280
- except OSError as e:
281
- st["times"].pop("screen2_submitted")
282
- return {s2_err: _err(f"Could not save your answers ({e}). Please try again or contact the organiser.")}
283
- st["pos"] = C.next_index(st["queue"], C.done_ids(out_dir, st["code"]))
284
- return enter_s1(st)
285
 
286
  code_btn.click(on_code, [code_in, key_in, state], ALL, **EVENT)
287
  new_btn.click(on_new, [code_in, key_in, state], ALL, **EVENT)
@@ -306,6 +330,9 @@ def main(argv=None):
306
  help="annotation output directory (env ANNOTATION_OUTPUT_DIR; default annotations/<data file stem>)")
307
  ap.add_argument("--layout", default=os.environ.get("ANNOTATION_LAYOUT", "horizontal"), choices=LAYOUTS,
308
  help="texts side by side or one above another (env ANNOTATION_LAYOUT)")
 
 
 
309
  ap.add_argument("--hub-repo", default=os.environ.get("ANNOTATION_HUB_REPO"),
310
  help="private HF dataset repo holding config, documents and answers; --data/--config are then "
311
  "paths inside it (env ANNOTATION_HUB_REPO; token from HF_TOKEN)")
@@ -325,7 +352,7 @@ def main(argv=None):
325
  if not args.data:
326
  ap.error("--data (or ANNOTATION_DATA) is required")
327
 
328
- stem = Path(args.data).stem
329
  explicit_out = bool(args.out)
330
  if args.hub_repo:
331
  # always one folder per item set; without an explicit (persistent) dir, a fresh private one, so local test
@@ -378,7 +405,8 @@ def main(argv=None):
378
  print(f"{len(docs)} documents, {len(config['annotators'])} reserved IDs, writing to {Path(args.out).resolve()}")
379
 
380
  demo = build_demo(docs, config, args.out, meta, layout=args.layout, on_saved=sync.push if sync else None,
381
- on_login=sync.pull if sync else None, remote_has=sync.remote_has if sync else None)
 
382
  demo.queue().launch(**launch_kwargs(args.host, args.port))
383
 
384
 
 
76
 
77
 
78
  LAYOUTS = ("horizontal", "vertical")
79
+ TASKS = ("both", "simplicity", "validity") # both = screen 1 then screen 2; or one screen per Space
80
 
81
 
82
  def build_demo(docs, config, out_dir: str | Path, meta: dict, layout: str = "horizontal", on_saved=None,
83
+ on_login=None, remote_has=None, task: str = "both") -> gr.Blocks:
84
  """``docs`` and ``config`` are the frozen objects from core.load_data / core.load_config; they are only read.
85
  ``layout``: the three texts side by side ("horizontal") or one above another ("vertical").
86
  ``on_saved(code)`` is called after a line is written (hub.AnswerSync.push, to copy it to the private repo).
87
  ``on_login(code)`` is called before the resume position is computed (hub.AnswerSync.pull).
88
+ ``remote_has(code)`` says whether the private repo already has that ID (hub.AnswerSync.remote_has).
89
+ ``task``: "both" (simplicity without the source, locked, then validity with it), "simplicity" (screen 1 only;
90
+ the source is never shown) or "validity" (screen 2 only: source and texts together)."""
91
  if layout not in LAYOUTS:
92
  raise ValueError(f"layout must be one of {LAYOUTS}, got {layout!r}")
93
+ if task not in TASKS:
94
+ raise ValueError(f"task must be one of {TASKS}, got {task!r}")
95
+ meta = {**meta, "layout": layout, "task": task}
96
 
97
  def texts_box():
98
  return gr.Row(equal_height=False) if layout == "horizontal" else gr.Column(elem_classes=["ann-vertical"])
 
140
  gap_bm = gr.Radio(list(C.GAPS), label="", value=None, visible=False, elem_id="gap_bm")
141
  gap_mw = gr.Radio(list(C.GAPS), label="", value=None, visible=False, elem_id="gap_mw")
142
  s1_err = gr.HTML("", elem_id="s1_err")
143
+ s1_btn = gr.Button("Submit and go to the next document" if task == "simplicity" else
144
+ "Submit and continue (answers cannot be changed afterwards)", variant="primary",
145
+ elem_id="s1_btn")
146
 
147
  with gr.Column(visible=False) as g_s2:
148
  s2_progress = gr.Markdown("", elem_id="s2_progress")
149
+ gr.Markdown("Compare each text with the **source** it was written from." if task == "validity" else
150
+ "Now compare each text with the **source** it was written from.")
151
  s2_source = gr.HTML("", elem_id="s2_source") # stays empty until screen 1 is locked
152
  with texts_box():
153
  s2_texts = []
 
178
  def progress(st) -> str:
179
  return f"**Document {st['pos'] + 1} of {n_docs}** · your ID: `{st['code']}`"
180
 
181
+ def enter_next(st) -> dict:
182
+ """The current document's first screen (screen 2 directly for the validity task), or the done screen."""
183
+ if task == "validity" and st["pos"] < n_docs:
184
+ doc = C.doc_by_id(docs, st["queue"][st["pos"]])
185
+ st.update(positions=C.positions(st["code"], doc["doc_id"], systems, salt), s1=None, times={})
186
+ return enter_s2(st, doc)
187
+ return enter_s1(st)
188
+
189
+ def enter_s2(st, doc) -> dict:
190
+ view = C.screen2_view(doc, st["positions"]) # the source enters the session only now
191
+ st.update(screen="s2", view=view)
192
+ st["times"]["screen2_shown"] = C.now_iso()
193
+ return {**show("s2"), state: st, s2_progress: progress(st), s1_err: "", s2_err: "",
194
+ **{t: "" for t in s1_texts},
195
+ s2_source: _card("Source", view["source"], source=True),
196
+ **{t: _card(f"Text {L}", view["texts"][L]) for t, L in zip(s2_texts, C.LETTERS)},
197
+ omits: gr.update(value=[]), contradicts: gr.update(value=[]),
198
+ overgen: gr.update(value=[], visible=st["cohort"] == "expert")}
199
+
200
+ def save(st, s2_answers, err_box, submitted_key) -> dict:
201
+ """Write this document's line, push it, and move on (or show the error on the current screen)."""
202
+ st["times"][submitted_key] = C.now_iso()
203
+ doc_index = next(i for i, d in enumerate(docs) if d["doc_id"] == st["view"]["doc_id"])
204
+ rec = C.build_record(st, n_docs, doc_index, s2_answers, meta)
205
+ try:
206
+ if C.append_record(out_dir, rec) and on_saved: # False = already saved from another tab
207
+ on_saved(st["code"])
208
+ except OSError as e:
209
+ st["times"].pop(submitted_key)
210
+ return {err_box: _err(f"Could not save your answers ({e}). Please try again or contact the organiser.")}
211
+ st["pos"] = C.next_index(st["queue"], C.done_ids(out_dir, st["code"]))
212
+ return enter_next(st)
213
+
214
  def enter_s1(st) -> dict:
215
  """Start screen 1 for the current document, or the done screen. Clears every screen-2 field."""
216
  out = {s2_source: "", **{t: "" for t in s2_texts}, s2_err: "", s1_err: ""}
 
277
  if not agreed:
278
  return {consent_err: _err("Please tick the box to continue.")}
279
  st["consent_at"] = C.now_iso()
280
+ return enter_next(st)
281
 
282
  def on_rank(e, h):
283
  """Show the two gap questions, naming the actual letters, once easiest and hardest are set."""
 
297
  if err:
298
  return {s1_err: _err(err)}
299
  st["s1"] = C.screen1_answers(e, h, g1, g2)
300
+ if task == "simplicity":
301
+ return save(st, None, s1_err, "screen1_submitted")
302
  st["times"]["screen1_submitted"] = C.now_iso()
303
+ return enter_s2(st, C.doc_by_id(docs, st["view"]["doc_id"]))
 
 
 
 
 
 
 
 
 
304
 
305
  def on_s2(om, co, ov, st):
306
  if not st or st["screen"] != "s2":
307
  return {}
308
+ return save(st, C.screen2_answers(st["cohort"], om, co, ov), s2_err, "screen2_submitted")
 
 
 
 
 
 
 
 
 
 
309
 
310
  code_btn.click(on_code, [code_in, key_in, state], ALL, **EVENT)
311
  new_btn.click(on_new, [code_in, key_in, state], ALL, **EVENT)
 
330
  help="annotation output directory (env ANNOTATION_OUTPUT_DIR; default annotations/<data file stem>)")
331
  ap.add_argument("--layout", default=os.environ.get("ANNOTATION_LAYOUT", "horizontal"), choices=LAYOUTS,
332
  help="texts side by side or one above another (env ANNOTATION_LAYOUT)")
333
+ ap.add_argument("--task", default=os.environ.get("ANNOTATION_TASK", "both"), choices=TASKS,
334
+ help="both screens, or only simplicity / only validity (env ANNOTATION_TASK); answers of a "
335
+ "single-task app go to a folder <data stem>-<task>")
336
  ap.add_argument("--hub-repo", default=os.environ.get("ANNOTATION_HUB_REPO"),
337
  help="private HF dataset repo holding config, documents and answers; --data/--config are then "
338
  "paths inside it (env ANNOTATION_HUB_REPO; token from HF_TOKEN)")
 
352
  if not args.data:
353
  ap.error("--data (or ANNOTATION_DATA) is required")
354
 
355
+ stem = Path(args.data).stem + ("" if args.task == "both" else f"-{args.task}")
356
  explicit_out = bool(args.out)
357
  if args.hub_repo:
358
  # always one folder per item set; without an explicit (persistent) dir, a fresh private one, so local test
 
405
  print(f"{len(docs)} documents, {len(config['annotators'])} reserved IDs, writing to {Path(args.out).resolve()}")
406
 
407
  demo = build_demo(docs, config, args.out, meta, layout=args.layout, on_saved=sync.push if sync else None,
408
+ on_login=sync.pull if sync else None, remote_has=sync.remote_has if sync else None,
409
+ task=args.task)
410
  demo.queue().launch(**launch_kwargs(args.host, args.port))
411
 
412
 
core.py CHANGED
@@ -452,9 +452,12 @@ def screen2_answers(cohort: str, omits, contradicts, overgen) -> dict:
452
  return out
453
 
454
 
455
- def build_record(state: dict, n_docs: int, doc_index: int, s2: dict, meta: dict) -> dict:
456
- return {
 
 
457
  "schema_version": SCHEMA_VERSION,
 
458
  "annotator": state["code"],
459
  "cohort": state["cohort"],
460
  "order_id": state["order_id"],
@@ -471,3 +474,8 @@ def build_record(state: dict, n_docs: int, doc_index: int, s2: dict, meta: dict)
471
  "app_version": meta["app_version"],
472
  "layout": meta.get("layout", "horizontal"),
473
  }
 
 
 
 
 
 
452
  return out
453
 
454
 
455
+ def build_record(state: dict, n_docs: int, doc_index: int, s2: dict | None, meta: dict) -> dict:
456
+ """One answer line. A single-task app (``meta["task"]``) leaves out the other screen's answers and times."""
457
+ task = meta.get("task", "both")
458
+ rec = {
459
  "schema_version": SCHEMA_VERSION,
460
+ "task": task,
461
  "annotator": state["code"],
462
  "cohort": state["cohort"],
463
  "order_id": state["order_id"],
 
474
  "app_version": meta["app_version"],
475
  "layout": meta.get("layout", "horizontal"),
476
  }
477
+ if task == "simplicity":
478
+ rec.pop("screen2")
479
+ elif task == "validity":
480
+ rec.pop("screen1")
481
+ return rec