Spaces:
Sleeping
Sleeping
annotation app code-24665847839a
Browse files
app.py
CHANGED
|
@@ -77,17 +77,32 @@ def theme():
|
|
| 77 |
# reporting after a few successive grows, so long screens got cut off (submit button out of reach). This keeps the
|
| 78 |
# frame as tall as the content: it measures the bottom of the last element (not the document, which can stretch to
|
| 79 |
# the frame and would loop) and asks iframe-resizer for that height whenever it changes.
|
|
|
|
|
|
|
|
|
|
| 80 |
FRAME_FIT_JS = """
|
| 81 |
<script>
|
| 82 |
(function () {
|
| 83 |
-
let last = 0;
|
| 84 |
function fit() {
|
| 85 |
const pif = window.parentIFrame, end = document.getElementById('bottom_spacer');
|
| 86 |
if (!pif || !end) return;
|
| 87 |
const h = Math.ceil(end.getBoundingClientRect().bottom + window.scrollY + 24);
|
| 88 |
if (Math.abs(h - last) > 2) { last = h; pif.size(h); }
|
| 89 |
}
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
window.addEventListener('resize', fit);
|
| 92 |
setInterval(fit, 1000);
|
| 93 |
})();
|
|
@@ -375,9 +390,19 @@ def build_demo(docs, config, out_dir: str | Path, meta: dict, layout: str = "hor
|
|
| 375 |
return {**show("consent"), state: st, start_err: "", consent_err: "", id_box: _id_box(norm),
|
| 376 |
id_saved_cb: gr.update(value=False)}
|
| 377 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 378 |
def on_consent(agreed, saved, st):
|
| 379 |
-
if not st
|
| 380 |
-
return
|
|
|
|
|
|
|
| 381 |
if not saved:
|
| 382 |
return {consent_err: _err("Please save your ID first, then tick the box: without it you cannot "
|
| 383 |
"come back to where you stopped.")}
|
|
@@ -401,8 +426,10 @@ def build_demo(docs, config, out_dir: str | Path, meta: dict, layout: str = "hor
|
|
| 401 |
gap_mw_q: gr.update(value=f"**How much easier** is {m} than {h}?", visible=True), s1_err: ""}
|
| 402 |
|
| 403 |
def on_s1(e, h, g1, g2, st):
|
| 404 |
-
if not st
|
| 405 |
-
return
|
|
|
|
|
|
|
| 406 |
err = C.check_screen1(e, h, g1, g2)
|
| 407 |
if err:
|
| 408 |
return {s1_err: _err(err)}
|
|
@@ -413,8 +440,10 @@ def build_demo(docs, config, out_dir: str | Path, meta: dict, layout: str = "hor
|
|
| 413 |
return enter_s2(st, C.doc_by_id(docs, st["view"]["doc_id"]))
|
| 414 |
|
| 415 |
def on_s2(om, co, ov, st):
|
| 416 |
-
if not st
|
| 417 |
-
return
|
|
|
|
|
|
|
| 418 |
return save(st, C.screen2_answers(st["cohort"], om, co, ov), s2_err, "screen2_submitted")
|
| 419 |
|
| 420 |
code_btn.click(on_code, [code_in, key_in, state], ALL, **EVENT)
|
|
|
|
| 77 |
# reporting after a few successive grows, so long screens got cut off (submit button out of reach). This keeps the
|
| 78 |
# frame as tall as the content: it measures the bottom of the last element (not the document, which can stretch to
|
| 79 |
# the frame and would loop) and asks iframe-resizer for that height whenever it changes.
|
| 80 |
+
# It also scrolls back to the top whenever a new screen is shown (the progress line changes): the submit button is at
|
| 81 |
+
# the bottom, so otherwise the next document opened on its last text and the questions, with "Document N" and the
|
| 82 |
+
# first texts out of view.
|
| 83 |
FRAME_FIT_JS = """
|
| 84 |
<script>
|
| 85 |
(function () {
|
| 86 |
+
let last = 0, shown = '';
|
| 87 |
function fit() {
|
| 88 |
const pif = window.parentIFrame, end = document.getElementById('bottom_spacer');
|
| 89 |
if (!pif || !end) return;
|
| 90 |
const h = Math.ceil(end.getBoundingClientRect().bottom + window.scrollY + 24);
|
| 91 |
if (Math.abs(h - last) > 2) { last = h; pif.size(h); }
|
| 92 |
}
|
| 93 |
+
function toTop() {
|
| 94 |
+
// only the "Document N of M" part: while an event runs, gradio's status timer adds text to the element
|
| 95 |
+
const p = [...document.querySelectorAll('#s1_progress, #s2_progress')].find(e => e.offsetParent !== null);
|
| 96 |
+
const m = p ? p.textContent.match(/Document \\d+ of \\d+/) : null;
|
| 97 |
+
const key = m ? p.id + '|' + m[0] : null; // the id too: screens 1 and 2 of one document share the label
|
| 98 |
+
if (!key || key === shown) return;
|
| 99 |
+
shown = key;
|
| 100 |
+
window.scrollTo(0, 0);
|
| 101 |
+
const pif = window.parentIFrame;
|
| 102 |
+
if (pif && pif.scrollToOffset) pif.scrollToOffset(0, 0);
|
| 103 |
+
}
|
| 104 |
+
new MutationObserver(() => { fit(); toTop(); }).observe(document.documentElement,
|
| 105 |
+
{subtree: true, childList: true, attributes: true});
|
| 106 |
window.addEventListener('resize', fit);
|
| 107 |
setInterval(fit, 1000);
|
| 108 |
})();
|
|
|
|
| 390 |
return {**show("consent"), state: st, start_err: "", consent_err: "", id_box: _id_box(norm),
|
| 391 |
id_saved_cb: gr.update(value=False)}
|
| 392 |
|
| 393 |
+
def lost() -> dict:
|
| 394 |
+
"""No session: the server restarted (a Space restart) or gradio dropped an idle closed tab's state. An empty
|
| 395 |
+
dict would make gradio paint an 'Error' badge on every component, with no hint of what to do."""
|
| 396 |
+
msg = _err("This page lost its connection to the study (the page was restarted or left idle for a long "
|
| 397 |
+
"time). Your answers to earlier documents are saved, this one is not. Please reload this page and click "
|
| 398 |
+
"“I'm coming back” with your ID.")
|
| 399 |
+
return {consent_err: msg, s1_err: msg, s2_err: msg}
|
| 400 |
+
|
| 401 |
def on_consent(agreed, saved, st):
|
| 402 |
+
if not st:
|
| 403 |
+
return lost()
|
| 404 |
+
if st["screen"] != "consent":
|
| 405 |
+
return {state: st} # a stale click: nothing to do
|
| 406 |
if not saved:
|
| 407 |
return {consent_err: _err("Please save your ID first, then tick the box: without it you cannot "
|
| 408 |
"come back to where you stopped.")}
|
|
|
|
| 426 |
gap_mw_q: gr.update(value=f"**How much easier** is {m} than {h}?", visible=True), s1_err: ""}
|
| 427 |
|
| 428 |
def on_s1(e, h, g1, g2, st):
|
| 429 |
+
if not st:
|
| 430 |
+
return lost()
|
| 431 |
+
if st["screen"] != "s1":
|
| 432 |
+
return {state: st}
|
| 433 |
err = C.check_screen1(e, h, g1, g2)
|
| 434 |
if err:
|
| 435 |
return {s1_err: _err(err)}
|
|
|
|
| 440 |
return enter_s2(st, C.doc_by_id(docs, st["view"]["doc_id"]))
|
| 441 |
|
| 442 |
def on_s2(om, co, ov, st):
|
| 443 |
+
if not st:
|
| 444 |
+
return lost()
|
| 445 |
+
if st["screen"] != "s2":
|
| 446 |
+
return {state: st}
|
| 447 |
return save(st, C.screen2_answers(st["cohort"], om, co, ov), s2_err, "screen2_submitted")
|
| 448 |
|
| 449 |
code_btn.click(on_code, [code_in, key_in, state], ALL, **EVENT)
|
core.py
CHANGED
|
@@ -248,7 +248,9 @@ def registration(out_dir: str | Path, code: str, config) -> dict | None:
|
|
| 248 |
|
| 249 |
|
| 250 |
def id_taken(out_dir: str | Path, code: str, config) -> bool:
|
| 251 |
-
|
|
|
|
|
|
|
| 252 |
|
| 253 |
|
| 254 |
def assign_order(out_dir: str | Path, config, cohort: str) -> int:
|
|
|
|
| 248 |
|
| 249 |
|
| 250 |
def id_taken(out_dir: str | Path, code: str, config) -> bool:
|
| 251 |
+
"""Reserved, or a file with something in it (an empty file is not a sign-up: register() accepts it)."""
|
| 252 |
+
p = output_path(out_dir, code)
|
| 253 |
+
return code in config["annotators"] or (p.exists() and bool(p.read_text(encoding="utf-8").strip()))
|
| 254 |
|
| 255 |
|
| 256 |
def assign_order(out_dir: str | Path, config, cohort: str) -> int:
|
hub.py
CHANGED
|
@@ -7,11 +7,13 @@ answers cannot live only there either. With ``ANNOTATION_HUB_REPO`` set, the app
|
|
| 7 |
1. downloads the config and the documents from that repo at startup (``ANNOTATION_CONFIG``, ``ANNOTATION_DATA``
|
| 8 |
are then paths inside the repo);
|
| 9 |
2. restores the existing answer files from it before serving, so resume works after any restart;
|
| 10 |
-
3. after every completed document, pushes that annotator's whole answer file back to it, from a background thread
|
|
|
|
| 11 |
|
| 12 |
The local write (append + fsync, core.append_record) still happens first and is what the annotator waits for. The
|
| 13 |
-
push follows within
|
| 14 |
-
shutdown. Repo layout: ``config.yaml``, ``data/<name>.json``,
|
|
|
|
| 15 |
|
| 16 |
The ``Store`` interface is small so tests can swap in ``LocalStore`` (a directory standing in for the repo).
|
| 17 |
"""
|
|
@@ -57,6 +59,10 @@ class LocalStore:
|
|
| 57 |
tmp.write_bytes(data)
|
| 58 |
os.replace(tmp, dest)
|
| 59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
class HubStore:
|
| 62 |
"""The private dataset repo on the Hugging Face Hub."""
|
|
@@ -105,6 +111,13 @@ class HubStore:
|
|
| 105 |
self.api.upload_file(path_or_fileobj=data, path_in_repo=path_in_repo, repo_id=self.repo_id,
|
| 106 |
repo_type=REPO_TYPE, commit_message=message)
|
| 107 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
def open_store(repo: str):
|
| 110 |
"""``file:///some/dir`` = a local directory standing in for the repo (dry runs, tests); else a Hub repo id."""
|
|
@@ -135,10 +148,13 @@ class AnswerSync:
|
|
| 135 |
"""
|
| 136 |
|
| 137 |
def __init__(self, store, out_dir: str | Path, prefix: str, push_local_only: bool = False, retries: int = 5,
|
| 138 |
-
backoff: float = 2.0):
|
| 139 |
self.store, self.out_dir, self.prefix = store, Path(out_dir), prefix.rstrip("/")
|
| 140 |
self.push_local_only = push_local_only # True only for an explicit, persistent output dir
|
| 141 |
self.retries, self.backoff = retries, backoff
|
|
|
|
|
|
|
|
|
|
| 142 |
self._q: queue.Queue[str] = queue.Queue()
|
| 143 |
self._pending: set[str] = set()
|
| 144 |
self._lock = threading.Lock()
|
|
@@ -162,6 +178,8 @@ class AnswerSync:
|
|
| 162 |
import json
|
| 163 |
|
| 164 |
p = C.output_path(self.out_dir, code)
|
|
|
|
|
|
|
| 165 |
p.parent.mkdir(parents=True, exist_ok=True)
|
| 166 |
with open(p, "a+", encoding="utf-8") as f:
|
| 167 |
fcntl.flock(f, fcntl.LOCK_EX)
|
|
@@ -238,34 +256,53 @@ class AnswerSync:
|
|
| 238 |
def _run(self) -> None:
|
| 239 |
while True:
|
| 240 |
try:
|
| 241 |
-
|
| 242 |
except queue.Empty: # idle: retry anything that failed earlier
|
| 243 |
self._requeue_failed()
|
| 244 |
continue
|
| 245 |
-
|
| 246 |
-
self._pending.discard(code) # a write after this point re-queues the code
|
| 247 |
try:
|
| 248 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 249 |
try:
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
self.store.upload(data, f"{self.prefix}/{code}.jsonl", f"answers: {code} ({n} lines)")
|
| 253 |
-
self.pushed += 1
|
| 254 |
-
self.failed.discard(code)
|
| 255 |
break
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
time.sleep(self.backoff * 2 ** attempt)
|
| 260 |
-
else:
|
| 261 |
-
self.failed.add(code)
|
| 262 |
-
print(f"[answer-sync] giving up on {code} for now; it is safe on local disk and will be "
|
| 263 |
-
f"retried within a minute", flush=True)
|
| 264 |
finally:
|
| 265 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 266 |
|
| 267 |
def flush(self, timeout: float = 30.0) -> bool:
|
| 268 |
-
"""Wait until every queued push is done (or timeout). Also retries codes that failed earlier.
|
|
|
|
|
|
|
| 269 |
self._requeue_failed()
|
| 270 |
t0 = time.time()
|
| 271 |
while self._q.unfinished_tasks:
|
|
|
|
| 7 |
1. downloads the config and the documents from that repo at startup (``ANNOTATION_CONFIG``, ``ANNOTATION_DATA``
|
| 8 |
are then paths inside the repo);
|
| 9 |
2. restores the existing answer files from it before serving, so resume works after any restart;
|
| 10 |
+
3. after every completed document, pushes that annotator's whole answer file back to it, from a background thread;
|
| 11 |
+
files queued within a minute go up together in one commit (the Hub limits commits per hour).
|
| 12 |
|
| 13 |
The local write (append + fsync, core.append_record) still happens first and is what the annotator waits for. The
|
| 14 |
+
push follows within a minute; failures are retried with backoff and re-queued, and pending pushes are flushed on
|
| 15 |
+
shutdown without waiting for the minute. Repo layout: ``config.yaml``, ``data/<name>.json``,
|
| 16 |
+
``annotations/<data stem>/<code>.jsonl``.
|
| 17 |
|
| 18 |
The ``Store`` interface is small so tests can swap in ``LocalStore`` (a directory standing in for the repo).
|
| 19 |
"""
|
|
|
|
| 59 |
tmp.write_bytes(data)
|
| 60 |
os.replace(tmp, dest)
|
| 61 |
|
| 62 |
+
def upload_many(self, files: dict[str, bytes], message: str) -> None:
|
| 63 |
+
for path_in_repo, data in files.items():
|
| 64 |
+
self.upload(data, path_in_repo, message)
|
| 65 |
+
|
| 66 |
|
| 67 |
class HubStore:
|
| 68 |
"""The private dataset repo on the Hugging Face Hub."""
|
|
|
|
| 111 |
self.api.upload_file(path_or_fileobj=data, path_in_repo=path_in_repo, repo_id=self.repo_id,
|
| 112 |
repo_type=REPO_TYPE, commit_message=message)
|
| 113 |
|
| 114 |
+
def upload_many(self, files: dict[str, bytes], message: str) -> None:
|
| 115 |
+
"""Several files in ONE commit."""
|
| 116 |
+
from huggingface_hub import CommitOperationAdd
|
| 117 |
+
|
| 118 |
+
self.api.create_commit(self.repo_id, repo_type=REPO_TYPE, commit_message=message,
|
| 119 |
+
operations=[CommitOperationAdd(path_in_repo=k, path_or_fileobj=v) for k, v in files.items()])
|
| 120 |
+
|
| 121 |
|
| 122 |
def open_store(repo: str):
|
| 123 |
"""``file:///some/dir`` = a local directory standing in for the repo (dry runs, tests); else a Hub repo id."""
|
|
|
|
| 148 |
"""
|
| 149 |
|
| 150 |
def __init__(self, store, out_dir: str | Path, prefix: str, push_local_only: bool = False, retries: int = 5,
|
| 151 |
+
backoff: float = 2.0, min_interval: float = 60.0):
|
| 152 |
self.store, self.out_dir, self.prefix = store, Path(out_dir), prefix.rstrip("/")
|
| 153 |
self.push_local_only = push_local_only # True only for an explicit, persistent output dir
|
| 154 |
self.retries, self.backoff = retries, backoff
|
| 155 |
+
self.min_interval = min_interval # seconds between two commits (both Spaces share one account's quota)
|
| 156 |
+
self._last_commit = float("-inf") # time.monotonic() of the last commit attempt
|
| 157 |
+
self._wake = threading.Event()
|
| 158 |
self._q: queue.Queue[str] = queue.Queue()
|
| 159 |
self._pending: set[str] = set()
|
| 160 |
self._lock = threading.Lock()
|
|
|
|
| 178 |
import json
|
| 179 |
|
| 180 |
p = C.output_path(self.out_dir, code)
|
| 181 |
+
if not remote and not p.exists():
|
| 182 |
+
return b"" # e.g. an unknown ID typed at "I'm coming back": do not create an empty file for it
|
| 183 |
p.parent.mkdir(parents=True, exist_ok=True)
|
| 184 |
with open(p, "a+", encoding="utf-8") as f:
|
| 185 |
fcntl.flock(f, fcntl.LOCK_EX)
|
|
|
|
| 256 |
def _run(self) -> None:
|
| 257 |
while True:
|
| 258 |
try:
|
| 259 |
+
first = self._q.get(timeout=60)
|
| 260 |
except queue.Empty: # idle: retry anything that failed earlier
|
| 261 |
self._requeue_failed()
|
| 262 |
continue
|
| 263 |
+
codes = [first]
|
|
|
|
| 264 |
try:
|
| 265 |
+
# One commit per interval, carrying every file queued meanwhile: the Hub has an hourly commit quota
|
| 266 |
+
# (not documented; "exceeded our hourly quotas for action: commit"), and one commit per finished
|
| 267 |
+
# document reaches it when several people work at once. flush() cuts the wait short.
|
| 268 |
+
self._wake.wait(max(0.0, self._last_commit + self.min_interval - time.monotonic()))
|
| 269 |
+
while True:
|
| 270 |
try:
|
| 271 |
+
codes.append(self._q.get_nowait())
|
| 272 |
+
except queue.Empty:
|
|
|
|
|
|
|
|
|
|
| 273 |
break
|
| 274 |
+
with self._lock:
|
| 275 |
+
self._pending.difference_update(codes) # a write after this point re-queues the code
|
| 276 |
+
self._push_batch(sorted(set(codes) | self.failed)) # earlier failures ride along
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 277 |
finally:
|
| 278 |
+
for _ in codes:
|
| 279 |
+
self._q.task_done()
|
| 280 |
+
|
| 281 |
+
def _push_batch(self, codes: list[str]) -> None:
|
| 282 |
+
for attempt in range(self.retries):
|
| 283 |
+
try:
|
| 284 |
+
files = {f"{self.prefix}/{c}.jsonl": self._merge_into_local(c, self._remote(c)) # never shrink
|
| 285 |
+
for c in codes}
|
| 286 |
+
files = {k: v for k, v in files.items() if v}
|
| 287 |
+
if files:
|
| 288 |
+
self.store.upload_many(files, f"answers: {', '.join(codes)}")
|
| 289 |
+
self._last_commit = time.monotonic()
|
| 290 |
+
self.pushed += len(files)
|
| 291 |
+
self.failed.difference_update(codes)
|
| 292 |
+
return
|
| 293 |
+
except Exception as e: # noqa: BLE001 -- network, rate limit, hub outage: retry, never crash
|
| 294 |
+
print(f"[answer-sync] push of {', '.join(codes)} failed (attempt {attempt + 1}/{self.retries}): {e}",
|
| 295 |
+
flush=True)
|
| 296 |
+
time.sleep(self.backoff * 2 ** attempt)
|
| 297 |
+
self._last_commit = time.monotonic()
|
| 298 |
+
self.failed.update(codes)
|
| 299 |
+
print(f"[answer-sync] giving up on {', '.join(codes)} for now; safe on local disk, retried with the next push "
|
| 300 |
+
f"or within a minute", flush=True)
|
| 301 |
|
| 302 |
def flush(self, timeout: float = 30.0) -> bool:
|
| 303 |
+
"""Wait until every queued push is done (or timeout). Also retries codes that failed earlier. For shutdown:
|
| 304 |
+
from here on pushes no longer wait for the commit interval."""
|
| 305 |
+
self._wake.set()
|
| 306 |
self._requeue_failed()
|
| 307 |
t0 = time.time()
|
| 308 |
while self._q.unfinished_tasks:
|