Spaces:
Sleeping
Sleeping
annotation app code-36022ba6f085+git-449290e-dirty
Browse files123 unmodified lines124125126127128129130120 unmodified lines2512522532542552562576 unmodified lines2642652662672682692706 unmodified lines277278279280281282283123 unmodified lineswith gr.Column(visible=True) as g_start:key_in = gr.Textbox(label="Access key", type="password", placeholder="the key you were sent",visible=bool(config.get("access_key") or config.get("expert_access_key")),elem_id="key_in")gr.Markdown("**First time?** Choose an ID (not your name) and **write it down**: it is the only ""way to come back to where you stopped. \n"120 unmodified linesdef on_code(code, key, st):"""A returning participant: the ID must exist (signed up before, or reserved in the config)."""if C.key_cohort(key, config) is None:return {start_err: _err("Wrong access key.")}norm, err = C.validate_id(code)if err:6 unmodified linesdef on_new(code, key, st):"""A new participant: sign the ID up (cohort from the key, next order in rotation)."""cohort = C.key_cohort(key, config)if cohort is None:return {start_err: _err("Wrong access key.")}norm, err = C.validate_id(code)6 unmodified linesexcept Exception as e: # noqa: BLE001 -- cannot check the repo: do not risk a duplicateprint(f"[answer-sync] could not check ID {norm} in the repo: {e}", flush=True)return {start_err: _err("Could not check this ID right now. Please try again in a minute.")}if taken or not C.register(out_dir, norm, cohort, C.assign_order(out_dir, config, cohort)):return {start_err: _err(f"The ID “{norm}” is already taken. If it is yours, click “I'm coming "f"back”; otherwise choose another.")}if on_saved:123 unmodified lines124125126127128129130131120 unmodified lines2522532542552562572586 unmodified lines2652662672682692702712722732742752762772786 unmodified lines285286287288289290291123 unmodified lineswith gr.Column(visible=True) as g_start:key_in = gr.Textbox(label="Access key", type="password", placeholder="the key you were sent",visible=bool(config.get("access_key") or config.get("expert_access_key")or config.get("block_keys")),elem_id="key_in")gr.Markdown("**First time?** Choose an ID (not your name) and **write it down**: it is the only ""way to come back to where you stopped. \n"120 unmodified linesdef on_code(code, key, st):"""A returning participant: the ID must exist (signed up before, or reserved in the config)."""if C.key_cohort(key, config) is None and C.block_key(key, config) is None:return {start_err: _err("Wrong access key.")}norm, err = C.validate_id(code)if err:6 unmodified linesdef on_new(code, key, st):"""A new participant: sign the ID up (cohort from the key, next order in rotation)."""block = Noneif split and config["block_keys"]: # this Space hands out blocks by personal key onlyinfo = C.block_key(key, config)if info is None:return {start_err: _err("Wrong access key. Use the personal key you were sent for this part.")}cohort, block = info["cohort"], info["block"]else:cohort = C.key_cohort(key, config)if cohort is None:return {start_err: _err("Wrong access key.")}norm, err = C.validate_id(code)6 unmodified linesexcept Exception as e: # noqa: BLE001 -- cannot check the repo: do not risk a duplicateprint(f"[answer-sync] could not check ID {norm} in the repo: {e}", flush=True)return {start_err: _err("Could not check this ID right now. Please try again in a minute.")}if taken or not C.register(out_dir, norm, cohort, C.assign_order(out_dir, config, cohort), block):return {start_err: _err(f"The ID “{norm}” is already taken. If it is yours, click “I'm coming "f"back”; otherwise choose another.")}if on_saved:
140 unmodified lines1411421431441451461471481491 unmodified line15115215315415515664 unmodified lines22122222322422522622722822913 unmodified lines2432442452462472482493 unmodified lines253254255256257258259260146 unmodified lines407408409410411412413140 unmodified linesf"(study.access_key in the config, or ANNOTATION_ACCESS_KEY)")if expert_key and (len(expert_key) < 6 and public or expert_key == key):raise ValueError(f"{path}: study.expert_access_key must differ from access_key (and be 6+ characters)")study = {k: v for k, v in study.items() if k not in ("access_key", "expert_access_key")} # never shownreturn _freeze({"study": study, "orders": orders, "annotators": annotators, "access_key": key,"expert_access_key": expert_key})def check_access_key(given: str | None, config) -> bool:1 unmodified linereturn key_cohort(given, config) is not Nonedef key_cohort(given: str | None, config) -> str | None:"""The cohort a key signs people up into: "expert" for the expert key, "standard" for the shared key (or whenno key is configured at all); None for a wrong key."""64 unmodified lines"""Who this ID is: its sign-up line, or its entry in the config's reserved IDs; None if unknown."""for r in read_records(out_dir, code):if r.get("type") == "registration":return {"cohort": r["cohort"], "order_id": r["order_id"]}if code in config["annotators"]:return {"cohort": cohort_of(code), "order_id": config["annotators"][code]["order"]}return None13 unmodified linesreturn rows[n % len(rows)]def register(out_dir: str | Path, code: str, cohort: str, order_id: int) -> bool:"""Create this ID's file with its sign-up line. False (writes nothing) if the ID already exists."""p = output_path(out_dir, code)p.parent.mkdir(parents=True, exist_ok=True)3 unmodified linesf.seek(0)if f.read().strip():return Falsef.write(json.dumps({"type": "registration", "annotator": code, "cohort": cohort, "order_id": order_id,"registered_at": now_iso()}, ensure_ascii=False) + "\n")f.flush()os.fsync(f.fileno())return True146 unmodified linesreg = registration(out_dir, code, config)if reg is None:raise KeyError(f"unknown ID {code!r}")queue = queue_for_order(reg["order_id"], config, docs, split)return {"code": code, "cohort": reg["cohort"], "order_id": reg["order_id"],"queue": queue, "pos": next_index(queue, done_ids(out_dir, code)), "screen": "consent","consent_at": None, "view": None, "positions": None, "s1": None, "times": {}}140 unmodified lines1411421431441451461471481491501511521531541551561571 unmodified line15916016116216316416516616716816917017117217317417517664 unmodified lines24124224324424524624724824913 unmodified lines2632642652662672682693 unmodified lines273274275276277278279280281282283146 unmodified lines430431432433434435436437438439140 unmodified linesf"(study.access_key in the config, or ANNOTATION_ACCESS_KEY)")if expert_key and (len(expert_key) < 6 and public or expert_key == key):raise ValueError(f"{path}: study.expert_access_key must differ from access_key (and be 6+ characters)")block_keys = {}for spec in study.get("block_keys") or []: # personal keys for a split Space: key -> its block and groupk, b, c = str(spec["key"]).strip(), int(spec["block"]), spec.get("cohort", "standard")if b not in range(N_BLOCKS) or c not in ("standard", "expert") or len(k) < 6 or k in block_keys \or k in (key, expert_key):raise ValueError(f"{path}: bad block_keys entry for block {spec.get('block')!r}: blocks 0-3, cohort "f"standard/expert, keys of 6+ characters, all different from each other and the shared keys")block_keys[k] = {"block": b, "cohort": c}study = {k: v for k, v in study.items() if k not in ("access_key", "expert_access_key", "block_keys")}return _freeze({"study": study, "orders": orders, "annotators": annotators, "access_key": key,"expert_access_key": expert_key, "block_keys": block_keys})def check_access_key(given: str | None, config) -> bool:1 unmodified linereturn key_cohort(given, config) is not Nonedef block_key(given: str | None, config) -> dict | None:"""{"block", "cohort"} for a personal block key (constant-time comparison against each), else None."""import hmacg = (given or "").strip().encode()hit = Nonefor k, info in config.get("block_keys", {}).items():if hmac.compare_digest(g, k.encode()):hit = dict(info)return hitdef key_cohort(given: str | None, config) -> str | None:"""The cohort a key signs people up into: "expert" for the expert key, "standard" for the shared key (or whenno key is configured at all); None for a wrong key."""64 unmodified lines"""Who this ID is: its sign-up line, or its entry in the config's reserved IDs; None if unknown."""for r in read_records(out_dir, code):if r.get("type") == "registration":return {"cohort": r["cohort"], "order_id": r["order_id"], "block": r.get("block")}if code in config["annotators"]:return {"cohort": cohort_of(code), "order_id": config["annotators"][code]["order"], "block": None}return None13 unmodified linesreturn rows[n % len(rows)]def register(out_dir: str | Path, code: str, cohort: str, order_id: int, block: int | None = None) -> bool:"""Create this ID's file with its sign-up line. False (writes nothing) if the ID already exists."""p = output_path(out_dir, code)p.parent.mkdir(parents=True, exist_ok=True)3 unmodified linesf.seek(0)if f.read().strip():return Falsereg = {"type": "registration", "annotator": code, "cohort": cohort, "order_id": order_id,"registered_at": now_iso()}if block is not None:reg["block"] = block # signed up with a personal block keyf.write(json.dumps(reg, ensure_ascii=False) + "\n")f.flush()os.fsync(f.fileno())return True146 unmodified linesreg = registration(out_dir, code, config)if reg is None:raise KeyError(f"unknown ID {code!r}")if split and reg.get("block") is not None: # a personal block key: exactly that block, in file orderqueue = [docs[i]["doc_id"] for i in blocks(len(docs))[reg["block"]]]else:queue = queue_for_order(reg["order_id"], config, docs, split)return {"code": code, "cohort": reg["cohort"], "order_id": reg["order_id"],"queue": queue, "pos": next_index(queue, done_ids(out_dir, code)), "screen": "consent","consent_at": None, "view": None, "positions": None, "s1": None, "times": {}}
app.py+12-4
core.py+34-8