File size: 8,107 Bytes
912d2b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import { $, el, api, ApiError, toast, store } from "/static/common.js";

const S = { token: store.get("admin.token"), project: null, groupBy: "gender" };

async function boot() {
  $("#signin-form").onsubmit = (e) => { e.preventDefault(); S.token = $("#a-token").value; store.set("admin.token", S.token); enter(); };
  $("#new-project").onclick = () => showEditor();
  $("#m-file").onchange = async (e) => { const f = e.target.files[0]; if (f) $("#m-text").value = await f.text(); };
  $("#m-check").onclick = () => sendManifest(true);
  $("#m-publish").onclick = () => sendManifest(false);
  try {
    const h = await api("/api/health");
    $("#health").textContent = `ASR: ${h.asr.backend === "none" ? "off" : h.asr.model_id}${h.default_tokens ? ". Default tokens are in use; set your own before sharing this server." : ""}`;
  } catch { /* offline */ }
  if (S.token) enter();
}

async function enter() {
  try {
    await api("/api/projects/validate", { method: "POST", token: S.token, body: {} });
  } catch (e) {
    if (e instanceof ApiError && e.status === 401) {
      $("#signin-error").textContent = "That token isn't valid for admin access.";
      $("#signin").classList.remove("hidden"); $("#workspace").classList.add("hidden");
      return;
    }
  }
  $("#signin").classList.add("hidden"); $("#workspace").classList.remove("hidden");
  loadProjects();
}

async function loadProjects() {
  const list = await api("/api/projects");
  $("#projects").replaceChildren(...list.map((p) => el("li", {}, el("button", {
    "aria-current": String(p.project_id === S.project), onclick: () => openReport(p.project_id),
  }, el("span", { class: "t", text: p.title || p.project_id }),
     el("span", { class: "small muted", text: `${p.project_id}, ${p.n_tasks} tasks, ${p.languages.join(", ")}` })))));
  if (!list.length) showEditor();
}

function showEditor(json = "") {
  $("#report").classList.add("hidden");
  $("#editor").classList.remove("hidden");
  if (json) $("#m-text").value = json;
  $("#m-result").replaceChildren();
}

async function sendManifest(dryRun) {
  const out = $("#m-result");
  let body;
  try { body = JSON.parse($("#m-text").value); }
  catch (e) { out.replaceChildren(el("ul", { class: "errors" }, el("li", { text: `Not valid JSON: ${e.message}` }))); return; }
  try {
    const r = dryRun
      ? await api("/api/projects/validate", { method: "POST", token: S.token, body })
      : await api("/api/projects?replace=true", { method: "POST", token: S.token, body });
    if (dryRun) {
      const types = Object.entries(r.task_types).map(([k, v]) => `${v} ${k.replaceAll("_", " ")}`).join(", ");
      out.replaceChildren(el("p", { class: "pill ok", text: "Manifest is valid" }),
        el("p", { text: `${r.project_id}: ${r.n_tasks} tasks (${types}).${r.exists ? " Publishing will update the existing project." : ""}` }));
    } else {
      toast(`Published ${r.project_id}.`);
      await loadProjects();
      openReport(r.project_id);
    }
  } catch (e) {
    const errs = e.body?.errors || [{ loc: [], msg: e.message }];
    out.replaceChildren(el("p", { class: "pill stop", text: `${errs.length} problem${errs.length > 1 ? "s" : ""} to fix` }),
      el("ul", { class: "errors" }, errs.map((x) => el("li", { text: `${x.loc.join(".") || "manifest"}: ${x.msg}` }))));
  }
}

async function download(path, filename) {
  try {
    const res = await api(path, { token: S.token, raw: true });
    const url = URL.createObjectURL(await res.blob());
    const a = el("a", { href: url, download: filename });
    document.body.append(a); a.click(); a.remove();
    setTimeout(() => URL.revokeObjectURL(url), 2000);
  } catch (e) { toast(e.message); }
}

const pct = (v) => (v == null ? "n/a" : `${(100 * v).toFixed(1)}%`);

async function openReport(pid) {
  S.project = pid;
  loadProjects();
  $("#editor").classList.add("hidden");
  const box = $("#report");
  box.classList.remove("hidden");
  box.replaceChildren(el("p", { class: "muted", text: "Loading report…" }));
  let r;
  try { r = await api(`/api/projects/${pid}/report?group_by=${S.groupBy}`, { token: S.token }); }
  catch (e) { box.replaceChildren(el("p", { class: "errors", text: e.message })); return; }

  const link = `${location.origin}/?project=${encodeURIComponent(pid)}`;
  const kv = el("dl", { class: "kv" });
  const add = (k, v) => kv.append(el("dt", { text: k }), el("dd", { text: v }));
  add("Recordings", r.recordings);
  add("Usable yield", pct(r.usable_yield));
  add("Retries", r.retry_chains ? `${r.retry_chains} prompts re-recorded, ${pct(r.retry_success_rate)} ended with a kept recording` : "none yet");
  add("Reviews", `${r.reviews}, ${r.reviewer_minutes} reviewer minutes`);
  add("Reviewer minutes per approved", r.reviewer_minutes_per_accepted ?? "n/a");
  add("Status", Object.entries(r.status_counts).map(([k, v]) => `${k} ${v}`).join(", ") || "none yet");

  const sel = el("select", { "aria-label": "Group by", style: "width:auto", onchange: (e) => { S.groupBy = e.target.value; openReport(pid); } },
    ["gender", "age_group", "region", "language", "condition"].map((g) => el("option", { value: g, selected: g === S.groupBy, text: g.replace("_", " ") })));

  const rows = Object.entries(r.groups).map(([g, v]) => el("tr", {},
    el("td", { text: g }), el("td", { class: "num", text: v.n }), el("td", { class: "num", text: pct(v.auto_flag_rate) }),
    el("td", { class: "num", text: v.human_decided }), el("td", { class: "num", text: pct(v.flag_overturn_rate) })));
  const table = el("div", { class: "scroll-x" }, el("table", {},
    el("thead", {}, el("tr", {}, el("th", { text: "Group" }), el("th", { class: "num", text: "Recordings" }),
      el("th", { class: "num", text: "Auto-flagged" }), el("th", { class: "num", text: "Flagged, human-decided" }),
      el("th", { class: "num", text: "Flags overturned by humans" }))),
    el("tbody", {}, rows.length ? rows : el("tr", {}, el("td", { colspan: 5, class: "muted", text: "No recordings yet." })))));

  const cond = Object.entries(r.by_condition || {}).filter(([k]) => k !== "none");
  const condTable = cond.length ? el("div", { class: "scroll-x" }, el("h3", { text: "By experiment condition" }), el("table", {},
    el("thead", {}, el("tr", {}, el("th", { text: "Condition" }), el("th", { class: "num", text: "Kept recordings" }),
      el("th", { class: "num", text: "Approved" }), el("th", { class: "num", text: "Usable yield" }), el("th", { class: "num", text: "Attempts per kept" }))),
    el("tbody", {}, cond.map(([k, v]) => el("tr", {}, el("td", { text: k.replaceAll("_", " ") }), el("td", { class: "num", text: v.recordings }),
      el("td", { class: "num", text: v.accepted }), el("td", { class: "num", text: pct(v.usable_yield) }),
      el("td", { class: "num", text: v.attempts_per_kept_recording ?? "n/a" })))))) : null;

  box.replaceChildren(...[
    el("h2", { style: "margin-top:0", text: pid }),
    el("p", {}, "Participant link: ", el("a", { href: link, target: "_blank", rel: "noopener", text: link })),
    kv,
    el("div", { class: "actions", style: "align-items:center" }, el("h3", { style: "margin:0", text: "Automatic flags by group" }), sel),
    el("p", { class: "small muted", text: "“Flags overturned” is the share of flagged recordings that a reviewer then approved. A gap between groups is a warning sign of unfair automatic checks; confirm it with the retrospective audit before drawing conclusions." }),
    table, condTable,
    el("div", { class: "actions" },
      el("button", { onclick: () => download(`/api/projects/${pid}/export.zip`, `${pid}_export.zip`) }, "Download approved data"),
      el("button", { onclick: () => download(`/api/projects/${pid}/export.jsonl`, `${pid}.jsonl`) }, "Download metadata only"),
      el("button", { onclick: () => download(`/api/projects/${pid}/payouts.csv`, `${pid}_payouts.csv`) }, "Download payout totals"),
      el("button", { onclick: async () => { const m = await api(`/api/projects/${pid}/manifest`, { token: S.token }); showEditor(JSON.stringify(m, null, 2)); } }, "Edit manifest")),
  ].filter(Boolean));
}

boot();