File size: 52,091 Bytes
e0d876f 912d2b5 e0d876f 912d2b5 e0d876f 912d2b5 e0d876f ea934f7 e0d876f 86b459c e0d876f 912d2b5 e0d876f ea934f7 e0d876f 912d2b5 e0d876f 86b459c e0d876f 86b459c e0d876f 86b459c e0d876f 86b459c e0d876f 86b459c e0d876f 86b459c e0d876f 86b459c e0d876f 912d2b5 e0d876f ea934f7 e0d876f a075fab 5080a18 e0d876f 5080a18 e0d876f 5080a18 e0d876f a075fab e0d876f a075fab e0d876f a075fab e0d876f 912d2b5 e0d876f a075fab e0d876f 912d2b5 e0d876f 5080a18 e0d876f a075fab e0d876f a075fab e0d876f 912d2b5 e0d876f 912d2b5 e0d876f 912d2b5 e0d876f 912d2b5 e0d876f 912d2b5 e0d876f 912d2b5 e0d876f 912d2b5 e0d876f 912d2b5 e0d876f 912d2b5 e0d876f 912d2b5 e0d876f 912d2b5 e0d876f 912d2b5 e0d876f 912d2b5 e0d876f 912d2b5 e0d876f 912d2b5 e0d876f 912d2b5 e0d876f 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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 | import { $, el, api, ApiError, toast, store, LANG_NAMES } from "/static/common.js";
const S = { token: store.get("admin.session"), user: null, projects: [], pid: store.get("admin.project"), tab: null,
pendingLetter: null, queue: [], idx: -1, filter: "all", openedAt: 0, scores: {}, perceived: null,
groupBy: "gender", days: 7, setup: false };
const LETTER_ID = "ethics_approval_letter";
const TASK_NAMES = { read_speech: "Read speech", image_description: "Image description", command: "Command",
prosody_pair: "Prosody pair", emotion: "Expressive speech", tts_prosody: "TTS reading" };
const pct = (v) => (v == null ? "n/a" : `${(100 * v).toFixed(1)}%`);
const rupees = (v) => "₹" + Number(v || 0).toLocaleString("en-IN", { maximumFractionDigits: 2 });
const call = (path, opts = {}) => api(path, { token: S.token, ...opts });
const isAdmin = () => S.user && S.user.role === "admin";
// ================================================================ auth
async function boot() {
$("#login-form").onsubmit = submitLogin;
$("#logout").onclick = logout;
$("#project-select").onchange = (e) => { S.pid = e.target.value; store.set("admin.project", S.pid); openTab(S.tab); };
document.querySelectorAll("#tabs button").forEach((b) => (b.onclick = () => openTab(b.dataset.tab)));
window.addEventListener("hashchange", () => S.user && openTab(location.hash.slice(1)));
document.addEventListener("keydown", reviewKeys);
initBuilder();
if (S.token) {
try { S.user = await call("/api/auth/me"); if (S.user.role === "participant") { location.replace("/participant"); return; } return enter(); } catch { store.del("admin.session"); S.token = null; }
}
showLogin();
}
async function showLogin() {
const st = await api("/api/auth/status").catch(() => ({ has_users: true }));
S.setup = !st.has_users;
$("#login-title").textContent = S.setup ? "Create the first admin account" : "Log in";
$("#l-submit").textContent = S.setup ? "Create account" : "Log in";
$("#l-token-row").classList.toggle("hidden", !S.setup);
$("#l-name-row").classList.toggle("hidden", !S.setup);
$("#l-pass").autocomplete = S.setup ? "new-password" : "current-password";
$("#login").classList.remove("hidden");
for (const id of ["tabs", "me", "logout"]) $("#" + id).classList.add("hidden");
document.querySelectorAll("main > section[id^=tab-]").forEach((s) => s.classList.add("hidden"));
}
async function submitLogin(e) {
e.preventDefault();
$("#login-error").textContent = "";
const username = $("#l-user").value.trim(), password = $("#l-pass").value;
try {
const r = S.setup
? await api("/api/auth/setup", { method: "POST", token: $("#l-token").value, body: { username, password, role: "admin", display_name: $("#l-name").value.trim() || username } })
: await api("/api/auth/login", { method: "POST", body: { username, password } });
S.token = r.token;
store.set("admin.session", r.token);
S.user = { username: r.username, role: r.role, display_name: r.display_name };
if (r.role === "participant") { location.replace("/participant"); return; }
$("#l-pass").value = "";
enter();
} catch (err) {
const d = err.body?.detail;
$("#login-error").textContent = Array.isArray(d) ? d.map((x) => x.msg).join("; ") : err.message;
}
}
async function logout() {
await call("/api/auth/logout", { method: "POST" }).catch(() => {});
store.del("admin.session");
S.token = null; S.user = null;
showLogin();
}
async function enter() {
$("#login").classList.add("hidden");
$("#tabs").classList.remove("hidden");
const name = S.user.display_name || S.user.username || (S.user.role === "admin" ? "Admin" : "Reviewer");
$("#me-name").textContent = `${name} (${S.user.role})`;
$("#me-avatar").textContent = name.split(/\s+/).map((w) => w[0]).join("").slice(0, 2).toUpperCase();
for (const id of ["me", "logout"]) $("#" + id).classList.remove("hidden");
document.querySelectorAll("#tabs button").forEach((b) => b.classList.toggle("hidden", b.dataset.role === "admin" && !isAdmin()));
await loadProjects();
const want = location.hash.slice(1) || (location.pathname === "/review" ? "review" : isAdmin() ? "projects" : "review");
openTab(want);
}
async function loadProjects() {
S.projects = await api("/api/projects");
const sel = $("#project-select");
if (!S.projects.find((p) => p.project_id === S.pid)) S.pid = S.projects.find(p => !["SCQC_DEMO_001", "SCQC_PILOT_HI", "SCQC_PROSODY_EMOTION", "TTS_DEMO_HI"].includes(p.project_id))?.project_id || null;
if (S.pid) store.set("admin.project", S.pid);
}
function openTab(tab) {
const allowed = isAdmin() ? ["projects", "dashboard", "builder", "review", "analytics", "payments", "team"] : ["review"];
if (!allowed.includes(tab)) tab = allowed[0];
S.tab = tab;
if (location.hash.slice(1) !== tab) history.replaceState(null, "", "#" + tab);
document.querySelectorAll("#tabs button").forEach((b) => b.setAttribute("aria-current", b.dataset.tab === tab ? "page" : "false"));
document.querySelectorAll("main > section[id^=tab-]").forEach((s) => s.classList.toggle("hidden", s.id !== "tab-" + tab));
({ projects: renderProjects, dashboard: renderDashboard, builder: () => {}, review: loadQueue, analytics: renderAnalytics,
payments: renderPayments, team: renderTeam })[tab]();
}
function needProject(box) {
if (S.pid) return false;
box.replaceChildren(el("div", { class: "panel" }, el("p", { text: "No projects yet. Create one from Projects → New project." })));
return true;
}
// Project selection belongs in the Projects workspace, not the global header.
// Existing example projects are hidden by default but never deleted.
const EXAMPLE_IDS = new Set(["SCQC_DEMO_001", "SCQC_PILOT_HI", "SCQC_PROSODY_EMOTION", "TTS_DEMO_HI"]);
let showExamples = false;
let projectSearch = "";
function selectProject(id, nextTab = "dashboard") {
S.pid = id;
store.set("admin.project", id);
openTab(nextTab);
}
function renderProjects() {
const box = $("tab-projects");
const shown = S.projects.filter(p => (showExamples || !EXAMPLE_IDS.has(p.project_id)) &&
`${p.title || ""} ${p.project_id}`.toLowerCase().includes(projectSearch.toLowerCase()));
const search = el("input", { type: "search", placeholder: "Search projects", value: projectSearch, "aria-label": "Search projects" });
search.oninput = () => { projectSearch = search.value; renderProjects(); };
const examples = el("input", { type: "checkbox", checked: showExamples });
examples.onchange = () => { showExamples = examples.checked; renderProjects(); };
const cards = shown.map(p => el("div", { class: "panel stack", style: "min-width:0" },
el("div", { class: "small muted", text: EXAMPLE_IDS.has(p.project_id) ? "Example project" : "Research project" }),
el("h3", { style: "margin:0", text: p.title || p.project_id }),
el("p", { class: "small muted", text: p.project_id }),
el("div", { class: "actions" },
el("button", { onclick: () => selectProject(p.project_id), text: "Open workspace" }),
el("button", { onclick: () => selectProject(p.project_id, "builder"), text: "Edit tasks" }))));
box.replaceChildren(
el("div", { class: "actions", style: "justify-content:space-between;align-items:center" },
el("div", {}, el("h2", { style: "margin:0", text: "Projects" }), el("p", { class: "muted small", text: "Choose a research project to open its workspace." })),
el("button", { onclick: () => { S.pid = null; store.del("admin.project"); openTab("builder"); }, text: "+ New project" })),
el("div", { class: "panel actions", style: "align-items:center;flex-wrap:wrap" }, search,
el("label", { class: "small", style: "display:flex;align-items:center;gap:8px" }, examples, "Show example projects")),
el("div", { class: "project-grid" }, cards.length ? cards : [el("div", { class: "panel muted", text: "No matching research projects. Create a project, or show example projects." })]));
// Keep search focus and cursor while filtering.
if (document.activeElement !== search && projectSearch) { search.focus(); search.setSelectionRange(search.value.length, search.value.length); }
}
// ================================================================ charts
function barChart(rows, key, label) {
const W = 640, H = 220, pad = 28, n = rows.length, max = Math.max(1, ...rows.map((r) => r[key] || 0));
const bw = (W - pad * 2) / n;
const svg = [`<svg class="chart" viewBox="0 0 ${W} ${H}" role="img" aria-label="${label}">`];
for (let g = 0; g <= 4; g++) {
const y = H - pad - (g / 4) * (H - pad * 2);
svg.push(`<line class="grid" x1="${pad}" x2="${W - pad}" y1="${y}" y2="${y}"/><text x="2" y="${y + 4}">${Math.round((g / 4) * max)}</text>`);
}
rows.forEach((r, i) => {
const h = ((r[key] || 0) / max) * (H - pad * 2);
svg.push(`<rect class="bar" rx="4" x="${pad + i * bw + bw * 0.18}" y="${H - pad - h}" width="${bw * 0.64}" height="${h}"><title>${r.date}: ${r[key]}</title></rect>`);
svg.push(`<text x="${pad + i * bw + bw / 2}" y="${H - 8}" text-anchor="middle">${r.date.slice(8)}</text>`);
});
svg.push("</svg>");
const d = el("div");
d.innerHTML = svg.join("");
return d.firstChild;
}
function lineChart(rows, key, label) {
const W = 640, H = 220, pad = 34;
const pts = rows.map((r, i) => [i, r[key]]).filter(([, v]) => v != null);
const svg = [`<svg class="chart" viewBox="0 0 ${W} ${H}" role="img" aria-label="${label}">`];
for (let g = 0; g <= 4; g++) {
const y = H - pad - (g / 4) * (H - pad * 2);
svg.push(`<line class="grid" x1="${pad}" x2="${W - 10}" y1="${y}" y2="${y}"/><text x="0" y="${y + 4}">${g * 25}%</text>`);
}
const x = (i) => pad + (i / Math.max(1, rows.length - 1)) * (W - pad - 10);
const y = (v) => H - pad - v * (H - pad * 2);
if (pts.length) svg.push(`<polyline class="line" points="${pts.map(([i, v]) => `${x(i)},${y(v)}`).join(" ")}"/>`);
pts.forEach(([i, v]) => svg.push(`<circle cx="${x(i)}" cy="${y(v)}" r="3.5" fill="var(--accent)"><title>${rows[i].date}: ${(v * 100).toFixed(1)}%</title></circle>`));
rows.forEach((r, i) => { if (i % Math.ceil(rows.length / 7) === 0) svg.push(`<text x="${x(i)}" y="${H - 8}" text-anchor="middle">${r.date.slice(5)}</text>`); });
svg.push("</svg>");
const d = el("div");
d.innerHTML = svg.join("");
return d.firstChild;
}
const hbar = (name, frac, text) => el("div", { class: "hbar" }, el("span", { class: "small", text: name }),
el("div", { class: "track" }, el("i", { style: `width:${Math.round(100 * (frac || 0))}%` })), el("span", { class: "small num", text }));
function table(headers, rows) {
return el("div", { class: "scroll-x" }, el("table", {},
el("thead", {}, el("tr", {}, headers.map(([t, num]) => el("th", { class: num ? "num" : null, text: t })))),
el("tbody", {}, rows.length ? rows.map((r) => el("tr", {}, r.map((c, i) => el("td", { class: headers[i][1] ? "num" : null }, c ?? "n/a"))))
: el("tr", {}, el("td", { colspan: headers.length, class: "muted", text: "No data yet." })))));
}
// ================================================================ dashboard
async function renderDashboard() {
const box = $("#tab-dashboard");
if (needProject(box)) return;
box.replaceChildren(el("p", { class: "muted", text: "Loading…" }));
let d, r;
try { [d, r] = await Promise.all([call(`/api/projects/${S.pid}/dashboard?days=${S.days}`), call(`/api/projects/${S.pid}/report`)]); }
catch (e) { box.replaceChildren(el("p", { class: "errors", text: e.message })); return; }
const e = r.ethics || {};
const stat = (label, value, sub) => el("div", { class: "stat" }, el("div", { class: "label", text: label }), el("div", { class: "value", text: value }), sub ? el("div", { class: "sub", text: sub }) : null);
const link = `${location.origin}/?project=${encodeURIComponent(S.pid)}`;
const daysSel = el("div", { class: "chips" }, [7, 14, 30].map((n) => el("button", { "aria-pressed": String(S.days === n), onclick: () => { S.days = n; renderDashboard(); } }, `Last ${n} days`)));
box.replaceChildren(
el("div", { class: "actions", style: "justify-content:space-between;align-items:center" }, el("h2", { style: "margin:0", text: d.title || S.pid }), daysSel),
ethicsBanner(e),
el("div", { class: "stats" },
stat("Total contributors", d.contributors.toLocaleString("en-IN")),
stat("Approved recordings", d.approved.toLocaleString("en-IN"), d.approval_rate != null ? `${pct(d.approval_rate)} of decided` : null),
stat("Pending review", d.pending_review.toLocaleString("en-IN"), d.checking ? `${d.checking} in final check` : null),
stat("Total payout", rupees(d.total_payout), "simulated payments")),
el("div", { class: "panel" }, el("h3", { style: "margin-top:0", text: "Collection progress" }), barChart(d.daily, "recordings", "Recordings per day")),
el("p", { class: "small" }, "Participant link: ", el("a", { href: link, target: "_blank", rel: "noopener", text: link })));
}
function ethicsBanner(e) {
let text;
if (!e.required) text = `Ethics review not required. Reason given: ${e.not_required_reason}`;
else if (e.status === "approved") text = `Approved by ${e.committee}, ${e.protocol_id}${e.amendment_id ? ` (amendment ${e.amendment_id})` : ""}. Valid until ${e.expiry_date} (${e.days_to_expiry} days). Participants ${e.participants}${e.max_participants ? ` of ${e.max_participants}` : ""}.`;
else if (e.status === "pending") text = "Waiting for ethics approval: closed to participants.";
else text = `Internal test mode: ${e.test_participants} test participant(s); excluded from exports.`;
return el("div", { class: `verdict ${e.open ? (e.status === "internal_test" ? "attn" : "ok") : "neutral"}` },
el("h3", { text: e.open ? "Open to participants" : "Closed to participants" }), el("p", { text }),
e.closed_reason ? el("p", { class: "small", text: e.closed_reason }) : null);
}
// ================================================================ analytics
async function renderAnalytics() {
const box = $("#tab-analytics");
if (needProject(box)) return;
box.replaceChildren(el("p", { class: "muted", text: "Loading…" }));
let d, r, st = null;
try {
[d, r] = await Promise.all([call(`/api/projects/${S.pid}/dashboard?days=30`), call(`/api/projects/${S.pid}/report?group_by=${S.groupBy}`)]);
st = await call(`/api/projects/${S.pid}/study`).catch(() => null);
} catch (e) { box.replaceChildren(el("p", { class: "errors", text: e.message })); return; }
const langs = Object.entries(d.by_language).sort((a, b) => b[1].recordings - a[1].recordings);
const groupSel = el("select", { "aria-label": "Group by", style: "width:auto", onchange: (e) => { S.groupBy = e.target.value; renderAnalytics(); } },
["gender", "age_group", "region", "language", "condition"].map((g) => el("option", { value: g, selected: g === S.groupBy, text: g.replace("_", " ") })));
const kids = [
el("div", { class: "stats" },
el("div", { class: "stat" }, el("div", { class: "label", text: "Approval rate" }), el("div", { class: "value", text: pct(d.approval_rate) })),
el("div", { class: "stat" }, el("div", { class: "label", text: "Usable yield" }), el("div", { class: "value", text: pct(r.usable_yield) })),
el("div", { class: "stat" }, el("div", { class: "label", text: "Reviewer minutes per approved" }), el("div", { class: "value", text: r.reviewer_minutes_per_accepted ?? "n/a" }))),
el("div", { class: "panel" }, el("h3", { style: "margin-top:0", text: "Approval rate, last 30 days" }), lineChart(d.daily, "approval_rate", "Approval rate per day")),
el("div", { class: "panel" }, el("h3", { style: "margin-top:0", text: "By language" }),
...langs.map(([l, v]) => hbar(LANG_NAMES[l] || l, v.approval_rate, `${pct(v.approval_rate)} of ${v.recordings}`))),
el("div", { class: "panel" }, el("h3", { style: "margin-top:0", text: "By task type" }),
table([["Task type"], ["Recordings", 1], ["Approved", 1], ["In review", 1], ["Approval rate", 1]],
Object.entries(d.by_task_type).map(([k, v]) => [TASK_NAMES[k] || k, v.recordings, v.approved, v.review, pct(v.approval_rate)]))),
el("div", { class: "panel stack" },
el("div", { class: "actions", style: "justify-content:space-between;align-items:center" }, el("h3", { style: "margin:0", text: "Fairness: automatic flags by speaker group" }), groupSel),
el("p", { class: "small muted", text: "Flags overturned = flagged recordings a reviewer then approved. A gap between groups suggests the automatic checks treat them differently; confirm with the blind audit before drawing conclusions." }),
table([["Group"], ["Recordings", 1], ["Auto-flagged", 1], ["Flagged, human-decided", 1], ["Flags overturned", 1]],
Object.entries(r.groups).map(([g, v]) => [g, v.n, pct(v.auto_flag_rate), v.human_decided, pct(v.flag_overturn_rate)]))),
];
if (st && st.cells.length) {
kids.push(el("div", { class: "panel stack" }, el("h3", { style: "margin:0", text: "User study" }),
el("p", { class: "small muted", text: `Listener agreement (Fleiss' kappa): ${st.listener_fleiss_kappa ?? "n/a"}. ${st.notes}` }),
table([["Condition"], ["Language"], ["Participants", 1], ["Completed", 1], ["Usable yield", 1], ["Attempts per kept", 1], ["Listeners heard intended meaning", 1], ["Auto prosody agrees with listeners", 1], ["SUS (mean)", 1]],
st.cells.map((c) => [c.condition.replaceAll("_", " "), LANG_NAMES[c.language] || c.language, c.participants, c.completed, pct(c.usable_yield),
c.attempts_per_kept, pct(c.listener_accuracy), c.auto_prosody_n ? `${pct(c.auto_prosody_agreement)} (n=${c.auto_prosody_n})` : "n/a",
c.sus_mean != null ? `${c.sus_mean}${c.sus_ci95 ? ` [${c.sus_ci95.join(", ")}]` : ""} (n=${c.sus_n})` : "n/a"]))));
}
kids.push(el("div", { class: "actions" },
el("button", { onclick: () => download(`/api/projects/${S.pid}/export.zip`, `${S.pid}_export.zip`) }, "Download approved data"),
el("button", { onclick: () => download(`/api/projects/${S.pid}/export.jsonl`, `${S.pid}.jsonl`) }, "Download metadata only"),
el("button", { onclick: () => download(`/api/projects/${S.pid}/ethics-pack.zip`, `${S.pid}_ethics_drafts.zip`) }, "Download ethics drafts")));
box.replaceChildren(...kids);
}
async function download(path, filename) {
try {
const res = await call(path, { 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); }
}
// ================================================================ payments (simulated)
async function renderPayments() {
const box = $("#tab-payments");
if (needProject(box)) return;
let p;
try { p = await call(`/api/projects/${S.pid}/payouts/batches`); } catch (e) { box.replaceChildren(el("p", { class: "errors", text: e.message })); return; }
box.replaceChildren(
el("div", { class: "verdict attn" }, el("h3", { text: "Simulated payments" }),
el("p", { text: "Payout batches and transaction references here are simulated to show how payments would work. No money is transferred." })),
el("div", { class: "stats" },
el("div", { class: "stat" }, el("div", { class: "label", text: "Earned, not yet batched" }), el("div", { class: "value", text: rupees(p.unbatched.total) }),
el("div", { class: "sub", text: `${p.unbatched.n} approved recording(s)` }))),
el("div", { class: "actions" },
el("button", { class: "primary", disabled: !p.unbatched.n, onclick: async () => {
try { const b = await call(`/api/projects/${S.pid}/payouts/batches`, { method: "POST" }); toast(`Batch ${b.batch_id} created.`); renderPayments(); } catch (e) { toast(e.message); }
} }, "Create payout batch"),
el("button", { onclick: () => download(`/api/projects/${S.pid}/payouts.csv`, `${S.pid}_payouts.csv`) }, "Download totals per contributor")),
table([["Batch"], ["Created"], ["Recordings", 1], ["Total", 1], ["Status"], [""]],
p.batches.map((b) => [b.batch_id, new Date(b.created_at * 1000).toLocaleString(), b.n, rupees(b.total),
el("span", { class: `pill ${b.status === "paid" ? "ok" : "attn"}`, text: b.status === "paid" ? "Paid (simulated)" : "Pending" }),
b.status === "paid" ? "" : el("button", { onclick: async () => {
try { await call(`/api/payouts/batches/${b.batch_id}/settle`, { method: "POST" }); toast("Marked as paid (simulated)."); renderPayments(); } catch (e) { toast(e.message); }
} }, "Mark paid")])));
}
// ================================================================ team
async function renderTeam() {
const box = $("#tab-team");
let users = [];
try { users = await call("/api/users"); } catch (e) { box.replaceChildren(el("p", { class: "errors", text: e.message })); return; }
const form = el("form", { class: "panel stack" },
el("h3", { style: "margin-top:0", text: "Add a team member" }),
el("div", { class: "row" },
el("div", {}, el("label", { for: "u-name", text: "User name" }), el("input", { type: "text", id: "u-name", required: true })),
el("div", {}, el("label", { for: "u-display", text: "Display name" }), el("input", { type: "text", id: "u-display" }))),
el("div", { class: "row" },
el("div", {}, el("label", { for: "u-role", text: "Role" }), el("select", { id: "u-role" }, el("option", { value: "reviewer", text: "Reviewer: review queue only" }), el("option", { value: "admin", text: "Admin: everything" }), el("option", { value: "participant", text: "Participant: collection workspace" }))),
el("div", {}, el("label", { for: "u-pass", text: "Temporary password (10+ characters)" }), el("input", { type: "text", id: "u-pass", required: true, minlength: 10 }))),
el("div", { class: "actions" }, el("button", { class: "primary", type: "submit" }, "Add")));
form.onsubmit = async (e) => {
e.preventDefault();
try {
await call("/api/users", { method: "POST", body: { username: $("#u-name").value.trim(), display_name: $("#u-display").value.trim() || null, role: $("#u-role").value, password: $("#u-pass").value } });
toast("Added. Share the user name and temporary password with them."); renderTeam();
} catch (err) { toast(err.message); }
};
box.replaceChildren(el("h2", { text: "Team" }),
table([["User"], ["Name"], ["Role"], ["Status"], [""]], users.map((u) => [u.username, u.display_name, u.role,
u.disabled ? el("span", { class: "pill stop", text: "Disabled" }) : el("span", { class: "pill ok", text: "Active" }),
u.username === S.user.username ? "You" : el("span", { class: "actions" },
el("button", { onclick: async () => { try { await call(`/api/users/${u.username}`, { method: "PATCH", body: { disabled: !u.disabled } }); renderTeam(); } catch (e) { toast(e.message); } } }, u.disabled ? "Enable" : "Disable"),
el("button", { onclick: async () => {
const pw = prompt(`New password for ${u.username} (10+ characters):`);
if (!pw) return;
try { await call(`/api/users/${u.username}`, { method: "PATCH", body: { password: pw } }); toast("Password changed."); } catch (e) { toast(e.message); }
} }, "Reset password"))])), form);
}
// ================================================================ task builder
const ALL_LANGS = ["as", "bn", "brx", "doi", "gu", "hi", "kn", "ks", "kok", "mai", "ml", "mni", "mr", "ne", "or", "pa", "sa", "sat", "sd", "ta", "te", "ur", "en"];
function initBuilder() {
$("#b-langs").replaceChildren(...ALL_LANGS.map((l) => el("label", {}, el("input", { type: "checkbox", value: l, name: "b-lang", onchange: syncLangSelect }), LANG_NAMES[l] || l)));
$("#t-type").onchange = taskFields;
for (const id of ["workflow-type", "collection-focus", "prosody-enabled", "prosody-focus", "validation-mode"]) $("#" + id).addEventListener("change", syncWorkflow);
syncWorkflow();
$("#t-add").onclick = addTask;
$("#t-csv").onchange = importCsv;
$("#b-new").onclick = () => setManifest(blankManifest());
$("#b-load").onclick = loadSelected;
$("#b-apply-project").onclick = applyProject;
$("#m-check").onclick = () => sendManifest(true);
$("#m-publish").onclick = () => sendManifest(false);
$("#m-file").onchange = async (e) => { const f = e.target.files[0]; if (f) setManifest(JSON.parse(await f.text())); };
$("#m-text").addEventListener("change", () => { const m = manifestJson(true); if (m) fillFromManifest(m); });
document.querySelectorAll("input[name=e-req]").forEach((r) => (r.onchange = ethicsToggle));
$("#e-apply").onclick = ethicsApply;
$("#e-draft").onclick = ethicsDraft;
taskFields();
setManifest(blankManifest());
syncWorkflow();
}
function blankManifest() {
return { project_id: "", title: "", languages: ["hi"], consent_version: "v1",
defaults: { validation: { preset: "L2", human_review: "uncertain" }, payment: { mode: "none", amount: 0, currency: "INR" } },
tasks: [] };
}
function manifestJson(quiet = false) {
try { return JSON.parse($("#m-text").value || "{}"); }
catch (e) { if (!quiet) toast(`The manifest JSON has an error: ${e.message}`); return null; }
}
function setManifest(m) {
$("#m-text").value = JSON.stringify(m, null, 2);
fillFromManifest(m);
}
function fillFromManifest(m) {
$("#b-pid").value = m.project_id || "";
$("#b-title").value = m.title || "";
$("#b-consent-version").value = m.consent_version || "v1";
$("#b-reward").value = m.defaults?.payment?.amount ?? 0;
document.querySelectorAll("input[name=b-lang]").forEach((c) => (c.checked = (m.languages || []).includes(c.value)));
syncLangSelect();
renderTaskList(m);
ethicsLoad(m);
}
async function loadSelected() {
if (!S.pid) return toast("No project selected.");
try { setManifest(await call(`/api/projects/${S.pid}/manifest`)); toast(`Loaded ${S.pid}.`); } catch (e) { toast(e.message); }
}
function syncLangSelect() {
const langs = [...document.querySelectorAll("input[name=b-lang]:checked")].map((c) => c.value);
$("#t-lang").replaceChildren(...langs.map((l) => el("option", { value: l, text: LANG_NAMES[l] || l })));
}
function applyProject() {
const m = manifestJson(); if (!m) return;
m.project_id = $("#b-pid").value.trim();
m.title = $("#b-title").value.trim() || null;
m.languages = [...document.querySelectorAll("input[name=b-lang]:checked")].map((c) => c.value);
m.consent_version = $("#b-consent-version").value.trim() || "v1";
const amt = Number($("#b-reward").value || 0);
m.defaults = m.defaults || {};
m.defaults.payment = amt > 0 ? { mode: "per_approved_recording", amount: amt, currency: "INR" } : { mode: "none", amount: 0, currency: "INR" };
setManifest(JSON.parse(JSON.stringify(m)));
toast("Project settings applied.");
}
function syncWorkflow() {
const auditing = $("#workflow-type").value === "auditing";
const prosody = $("#prosody-enabled").checked;
$("#collection-options").classList.toggle("hidden", auditing);
$("#audit-notice").classList.toggle("hidden", !auditing);
$("#prosody-options").classList.toggle("hidden", !prosody);
$("#workflow-help").textContent = auditing
? "Audit existing speech datasets using reference comparison and human review."
: "Collect new recordings; enable prosody only when required.";
$("#t-add").disabled = auditing;
$("#t-csv").disabled = auditing;
$("#t-type").value = prosody ? $("#prosody-focus").value : $("#collection-focus").value;
taskFields();
}
function taskFields() {
const t = $("#t-type").value;
document.querySelectorAll(".t-field").forEach((f) => f.classList.toggle("hidden", !f.dataset.for.split(" ").includes(t)));
const semantic = t === "read_speech" && $("#validation-mode").value === "semantic";
$("#validation-mode-wrap").classList.toggle("hidden", t !== "read_speech");
$("#intent-fields").classList.toggle("hidden", !(semantic || t === "command"));
}
function buildTask(row, m) {
const type = row.task_type, lang = row.language;
const n = (m.tasks || []).filter((t) => t.task_type === type && t.language === lang).length + 1;
const id = row.task_id || `${lang}_${type}_${String(n).padStart(3, "0")}`;
const prompt = {};
if (row.text) prompt.text = row.text;
if (type === "image_description") {
if (!row.image_url) throw new Error("image tasks need an image link");
const aid = `img_${id}`;
m.assets = m.assets || {};
m.assets[aid] = { url: row.image_url, alt: row.scenario || "Picture to describe" };
prompt.image_asset_id = aid;
if (row.scenario) prompt.scenario = row.scenario;
if (row.suggested_text) prompt.suggested_text = row.suggested_text;
}
if (row.emphasis) prompt.emphasis = row.emphasis;
if (type === "prosody_pair") { prompt.pair_id = row.pair_id; prompt.interpretation = row.interpretation; }
if (type === "command" || (type === "read_speech" && row.validation_mode === "semantic")) {
prompt.intent = row.intent;
m.intents = m.intents || {};
const conf = (row.confusable || "").split(",").map((x) => x.trim()).filter(Boolean);
if (!row.intent) throw new Error("Semantic validation requires an expected intent ID");
m.intents[row.intent] = m.intents[row.intent] || { phrases: [], confusable_with: [] };
for (const phrase of [row.text, ...(row.equivalents || "").split("\n").map(x => x.trim()).filter(Boolean)]) {
if (!m.intents[row.intent].phrases.includes(phrase)) m.intents[row.intent].phrases.push(phrase);
}
if (!m.intents[row.intent].phrases.includes(row.text)) m.intents[row.intent].phrases.push(row.text);
for (const c of conf) {
if (!m.intents[row.intent].confusable_with.includes(c)) m.intents[row.intent].confusable_with.push(c);
m.intents[c] = m.intents[c] || { phrases: [c.replaceAll("_", " ")], confusable_with: [] };
}
}
if (type === "emotion") { prompt.scenario = row.scenario; if (row.emotion) prompt.emotion = row.emotion; }
const task = { task_id: id, task_type: type, language: lang, prompt };
const v = {};
if (row.level) v.preset = row.level;
if (row.accept_confidence !== "" && row.accept_confidence != null) v.accept_confidence = Number(row.accept_confidence);
if (row.validation_mode === "semantic" && type === "read_speech") v.disable = ["read_speech_match"];
if (Object.keys(v).length) task.validation = { human_review: ["image_description", "prosody_pair", "emotion"].includes(type) ? "all" : "uncertain", ...v };
if (row.reward !== "" && row.reward != null) task.payment = { mode: "per_approved_recording", amount: Number(row.reward), currency: "INR" };
return task;
}
function addTask() {
const m = manifestJson(); if (!m) return;
if ($("#workflow-type").value === "auditing") return toast("Use the dataset auditing import workflow; recording tasks are not applicable.");
const type = $("#t-type").value;
const row = { task_type: type, language: $("#t-lang").value, task_id: $("#t-id").value.trim(),
text: $("#t-text").value.trim(), image_url: $("#t-image").value.trim(), scenario: type === "emotion" ? $("#t-situation").value.trim() : $("#t-scenario").value.trim(),
suggested_text: $("#t-suggested").value.trim(), emphasis: $("#t-emphasis").value.trim(), pair_id: $("#t-pair").value.trim(),
interpretation: $("#t-interp").value.trim(), intent: $("#t-intent").value.trim(), confusable: $("#t-confusable").value, equivalents: $("#t-equivalents").value, validation_mode: $("#validation-mode").value,
emotion: $("#t-emotion").value.trim(), level: $("#t-level").value, accept_confidence: $("#t-conf").value.trim(), reward: $("#t-reward").value.trim() };
if (!row.language) return toast("Choose the project's languages first.");
try {
m.tasks = m.tasks || [];
m.tasks.push(buildTask(row, m));
setManifest(m);
for (const id of ["t-id", "t-text", "t-emphasis", "t-interp", "t-suggested", "t-image", "t-equivalents"]) $("#" + id).value = "";
toast("Task added. Publish when you're ready.");
} catch (e) { toast(e.message); }
}
function parseCsv(text) {
const rows = []; let row = [], cell = "", q = false;
for (let i = 0; i < text.length; i++) {
const ch = text[i];
if (q) { if (ch === '"' && text[i + 1] === '"') { cell += '"'; i++; } else if (ch === '"') q = false; else cell += ch; }
else if (ch === '"') q = true;
else if (ch === ",") { row.push(cell); cell = ""; }
else if (ch === "\n" || ch === "\r") { if (ch === "\r" && text[i + 1] === "\n") i++; row.push(cell); rows.push(row); row = []; cell = ""; }
else cell += ch;
}
if (cell || row.length) { row.push(cell); rows.push(row); }
const head = (rows.shift() || []).map((h) => h.trim().toLowerCase());
return rows.filter((r) => r.some((c) => c.trim())).map((r) => Object.fromEntries(head.map((h, i) => [h, (r[i] || "").trim()])));
}
async function importCsv(e) {
const f = e.target.files[0]; if (!f) return;
const m = manifestJson(); if (!m) return;
const rows = parseCsv(await f.text());
const errors = [];
m.tasks = m.tasks || [];
rows.forEach((r, i) => {
try {
if (!r.task_type || !r.language) throw new Error("task_type and language are required");
if (!(m.languages || []).includes(r.language)) m.languages = [...(m.languages || []), r.language];
m.tasks.push(buildTask(r, m));
} catch (err) { errors.push(`row ${i + 2}: ${err.message}`); }
});
setManifest(m);
e.target.value = "";
toast(`${rows.length - errors.length} task(s) imported${errors.length ? `, ${errors.length} skipped` : ""}.`);
if (errors.length) $("#m-result").replaceChildren(el("ul", { class: "errors" }, errors.map((x) => el("li", { text: x }))));
}
function renderTaskList(m) {
const tasks = m.tasks || [];
$("#t-list").replaceChildren(tasks.length ? table([["Task"], ["Type"], ["Language"], ["Prompt"], ["Level"], [""]],
tasks.map((t, i) => [t.task_id, TASK_NAMES[t.task_type] || t.task_type, LANG_NAMES[t.language] || t.language,
el("span", { lang: t.language, text: (t.prompt.text || t.prompt.scenario || t.prompt.image_asset_id || "").slice(0, 60) }),
t.validation?.preset || m.defaults?.validation?.preset || "L1",
el("button", { onclick: () => { const mm = manifestJson(); mm.tasks.splice(i, 1); setManifest(mm); } }, "Remove")]))
: el("p", { class: "muted small", text: "No tasks yet." }));
}
async function sendManifest(dryRun) {
const out = $("#m-result");
const body = manifestJson(); if (!body) return;
try {
const r = await call(dryRun ? "/api/projects/validate" : "/api/projects?replace=true", { method: "POST", body });
if (dryRun) {
const types = Object.entries(r.task_types).map(([k, v]) => `${v} ${TASK_NAMES[k] || k}`).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." : ""}` }));
return;
}
if (S.pendingLetter) {
const form = new FormData();
form.append("file", S.pendingLetter, S.pendingLetter.name);
try { await call(`/api/projects/${r.project_id}/assets/${LETTER_ID}`, { method: "PUT", form }); S.pendingLetter = null; }
catch (e) { toast(`Published, but the approval letter didn't upload: ${e.message}`); }
}
toast(`Published ${r.project_id}.`);
S.pid = r.project_id; store.set("admin.project", S.pid);
await loadProjects();
out.replaceChildren(el("p", { class: "pill ok", text: "Published" }));
} 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}` }))));
}
}
// ---------------------------------------------------------------- ethics form
function ethicsToggle() {
const v = document.querySelector("input[name=e-req]:checked")?.value;
$("#e-yes").classList.toggle("hidden", v !== "yes");
$("#e-no").classList.toggle("hidden", v !== "no");
}
function ethicsLoad(m) {
const e = m.ethics || {};
document.querySelectorAll("input[name=e-req]").forEach((r) => { r.checked = e.required !== undefined && (r.value === "yes") === Boolean(e.required); });
const set = (id, v) => { $(id).value = v ?? ""; };
set("#e-reason", e.not_required_reason); set("#e-status", e.status || "pending");
set("#e-committee", e.committee); set("#e-institution", e.institution);
set("#e-protocol", e.protocol_id); set("#e-amendment", e.amendment_id);
set("#e-approved", e.approval_date); set("#e-expiry", e.expiry_date);
set("#e-max", e.max_participants); set("#e-age", e.minimum_age ?? 18);
set("#e-retention", e.retention_days); set("#e-hosting", e.data_hosting);
set("#e-uses", (e.approved_uses || []).join("\n"));
set("#e-researcher", e.contact?.researcher); set("#e-committee-contact", e.contact?.committee);
const box = $("#e-consent");
box.replaceChildren(el("p", { class: "small muted", text: "Approved consent text, in every project language. Empty: SCQC's default text (not allowed once approved)." }));
for (const l of m.languages || []) {
const c = (e.consent || {})[l] || {};
box.append(el("div", { class: "stack" },
el("label", { for: `e-ct-${l}`, text: `Consent title: ${LANG_NAMES[l] || l}` }), el("input", { type: "text", id: `e-ct-${l}`, lang: l, value: c.title || "" }),
el("label", { for: `e-cx-${l}`, text: `Consent text: ${LANG_NAMES[l] || l}` }),
el("textarea", { id: `e-cx-${l}`, lang: l, style: "min-height:110px;font-family:inherit;font-size:1rem" }, c.text || "")));
}
ethicsToggle();
}
function ethicsApply() {
const m = manifestJson(); if (!m) return;
const req = document.querySelector("input[name=e-req]:checked")?.value;
if (!req) return toast("Choose whether ethics committee approval is required.");
const val = (id) => $(id).value.trim();
const num = (id) => (val(id) ? Number(val(id)) : undefined);
let e;
if (req === "no") e = { required: false, not_required_reason: val("#e-reason") };
else {
e = { required: true, status: val("#e-status"), committee: val("#e-committee") || undefined, institution: val("#e-institution") || undefined,
protocol_id: val("#e-protocol") || undefined, amendment_id: val("#e-amendment") || undefined, approval_date: val("#e-approved") || undefined,
expiry_date: val("#e-expiry") || undefined, max_participants: num("#e-max"), minimum_age: num("#e-age") ?? 18,
retention_days: num("#e-retention"), data_hosting: val("#e-hosting") || undefined,
approved_uses: val("#e-uses").split("\n").map((x) => x.trim()).filter(Boolean),
contact: val("#e-researcher") ? { researcher: val("#e-researcher"), committee: val("#e-committee-contact") || undefined } : undefined, consent: {} };
for (const l of m.languages || []) {
const t = $(`#e-ct-${l}`)?.value.trim(), x = $(`#e-cx-${l}`)?.value.trim();
if (t || x) e.consent[l] = { title: t, text: x };
}
const file = $("#e-letter").files[0];
if (file) {
S.pendingLetter = file;
m.assets = m.assets || {};
m.assets[LETTER_ID] = { url: "uploaded:" + file.name, mime: "application/pdf", alt: "Ethics approval letter" };
e.approval_document = LETTER_ID;
} else if (m.ethics?.approval_document) e.approval_document = m.ethics.approval_document;
}
m.ethics = JSON.parse(JSON.stringify(e));
$("#m-text").value = JSON.stringify(m, null, 2);
toast("Ethics details applied. Check, then publish.");
sendManifest(true);
}
async function ethicsDraft() {
const m = manifestJson(); if (!m) return;
try {
const res = await fetch("/api/ethics/draft", { method: "POST", headers: { "Content-Type": "application/json", "X-SCQC-Token": S.token }, body: JSON.stringify(m) });
if (!res.ok) {
const b = await res.json().catch(() => ({}));
const errs = b.errors || [{ loc: [], msg: b.detail || `HTTP ${res.status}` }];
$("#e-result").replaceChildren(el("p", { class: "pill stop", text: "Fix the manifest first" }),
el("ul", { class: "errors" }, errs.map((x) => el("li", { text: `${(x.loc || []).join(".") || "manifest"}: ${x.msg}` }))));
return;
}
const url = URL.createObjectURL(await res.blob());
const a = el("a", { href: url, download: `${m.project_id || "project"}_ethics_drafts.zip` });
document.body.append(a); a.click(); a.remove();
setTimeout(() => URL.revokeObjectURL(url), 2000);
$("#e-result").replaceChildren();
} catch (err) { toast(err.message); }
}
// ================================================================ review queue
const ISSUES = [["wrong_words", "Words differ from the prompt"], ["missing_words", "Words missing"], ["noise", "Background noise"],
["distortion", "Distortion or clipping"], ["cut_off", "Cut off"], ["off_task", "Off-task or irrelevant"],
["wrong_language", "Not the task language"], ["multiple_speakers", "More than one speaker"], ["personal_info", "Personal information spoken"],
["prosody_wrong", "Intonation doesn't follow the punctuation"]];
const LABELS = { image_relevance: "Relevant to the picture", naturalness_completeness: "Natural and complete", human_meaning: "Intended meaning comes across",
prosodic_realization: "Prosody marks the meaning", pair_integrity: "Pair recorded consistently", perceived_expression: "Expression comes across",
listener_agreement: "Expression is unambiguous", transcript_fidelity: "Matches the prompt", target_command_correctness: "Command said correctly",
completeness: "Complete", intent_label: "Intent is clear", prompt_completion: "Prompt completed", lexical_fidelity: "Words match the prompt",
punctuation_prosody: "Follows the punctuation", consistency: "Consistent with the speaker's other takes" };
const PRIO = { high: ["High", "stop"], medium: ["Med", "attn"], low: ["Low", "ok"] };
async function loadQueue() {
if (!S.pid) return needProject($("#detail"));
try { S.queue = await call(`/api/review/queue?project_id=${encodeURIComponent(S.pid)}&limit=500&reviewer_id=${encodeURIComponent(S.user.username || "admin")}`); }
catch (e) { toast(e.message); S.queue = []; }
$("#reload").onclick = loadQueue;
renderQueue();
openItem(visibleQueue().length ? 0 : -1);
}
function visibleQueue() {
return S.queue.filter((i) => S.filter === "all" || (S.filter === "high" ? i.priority === "high" : i.categories.includes(S.filter)));
}
function renderQueue() {
const count = (f) => S.queue.filter((i) => f === "all" || (f === "high" ? i.priority === "high" : i.categories.includes(f))).length;
$("#queue-count").textContent = `Review queue (${S.queue.length})`;
$("#filters").replaceChildren(...[["all", "All"], ["flagged", "Flagged"], ["uncertain", "Uncertain"], ["high", "High priority"]].map(([f, t]) =>
el("button", { "aria-pressed": String(S.filter === f), onclick: () => { S.filter = f; renderQueue(); openItem(visibleQueue().length ? 0 : -1); } }, `${t} (${count(f)})`)));
const items = visibleQueue();
$("#queue").replaceChildren(...items.map((it, i) => {
const [pt, pc] = PRIO[it.priority];
const why = it.blind ? "Blind listening" : ([...it.reasons, ...it.route_notes].map((x) => x.replaceAll("_", " ")).join(", ") || "Routine check");
return el("li", {}, el("button", { "aria-current": String(i === S.idx), onclick: () => openItem(i) },
el("span", { class: "list-row" }, el("span", { class: "grow" }, el("span", { class: "t", text: `${TASK_NAMES[it.task_type] || it.task_type}, ${LANG_NAMES[it.language] || it.language}` }),
el("span", { class: "small muted line-clamp", style: "display:block", text: why })), el("span", { class: `pill ${pc}`, text: pt }))));
}));
}
function openItem(i) {
S.idx = i; S.scores = {}; S.perceived = null; S.openedAt = performance.now();
renderQueue();
const d = $("#detail"), it = visibleQueue()[i];
if (!it) { d.replaceChildren(el("p", { class: "muted", text: "Nothing to review here. New recordings appear after they upload." })); return; }
const kids = [el("div", { class: "actions", style: "justify-content:space-between;align-items:center" },
el("h2", { style: "margin:0", text: TASK_NAMES[it.task_type] || it.task_type }), el("span", { class: `pill ${PRIO[it.priority][1]}`, text: `${PRIO[it.priority][0]} priority` }))];
if (it.asset_url) kids.push(el("img", { class: "prompt-image", src: `${it.asset_url}?token=${encodeURIComponent(S.token)}`, alt: "Picture shown to the speaker" }));
const promptText = it.prompt.text || it.prompt.scenario;
if (promptText) kids.push(el("p", { class: "align-line", lang: it.language, text: promptText }));
if (it.prompt.emphasis && !it.blind) kids.push(el("p", { class: "small muted" }, "Stress expected on ", el("strong", { lang: it.language, text: it.prompt.emphasis })));
if (it.prompt.suggested_text) kids.push(el("p", { class: "small muted", lang: it.language, text: `Suggested: ${it.prompt.suggested_text}` }));
const audio = el("audio", { controls: true, preload: "auto", src: `${it.audio_url}?token=${encodeURIComponent(S.token)}` });
kids.push(audio);
if (it.blind) {
kids.push(el("p", { class: "muted small", text: "Automatic results and the intended meaning are hidden so your judgement is independent." }));
if (it.perceived_options?.length) {
kids.push(el("h3", { text: it.task_type === "prosody_pair" ? "Which meaning did you hear?" : "Which expression did you hear?" }));
const seg = el("div", { class: "actions" });
for (const o of it.perceived_options) {
const b = el("button", { "aria-pressed": "false", onclick: () => { S.perceived = o; seg.querySelectorAll("button").forEach((x) => { x.setAttribute("aria-pressed", String(x === b)); x.classList.toggle("primary", x === b); }); } }, o);
seg.append(b);
}
kids.push(seg);
}
} else {
if (it.confidence) kids.push(el("p", { class: "small" }, el("strong", { text: `Confidence ${Math.round(it.confidence.score * 100)}%` }),
` (${it.confidence.label}${it.confidence.calibrated ? ", calibrated" : ", uncalibrated"})`));
if (it.reasons.length || it.route_notes.length)
kids.push(el("div", { class: "actions" }, [...it.reasons, ...it.route_notes].map((r) => el("span", { class: "pill attn", text: r.replaceAll("_", " ") }))));
if (it.match?.alignment) kids.push(alignmentView(it.match, it.language));
for (const v of it.validators || []) {
const dd = v.data || {};
if (v.name === "prosody" && Object.keys(dd).length) kids.push(el("p", { class: "small muted", text:
`Prosody: ${dd.pauses_found ?? 0}/${dd.targets?.pauses ?? 0} pauses at punctuation, final slope ${dd.final_slope_st_per_s ?? "n/a"} st/s, pitch range ${dd.pitch_range_st ?? "n/a"} st.` }));
if (v.name === "image_relevance" && dd.suggested_coverage != null) kids.push(el("p", { class: "small muted", text: `Suggested words heard: ${pct(dd.suggested_coverage)}.` }));
if (v.name === "mfa_alignment" && dd.aligned !== undefined) kids.push(el("p", { class: "small muted", text: `Forced alignment: ${dd.aligned ? "aligned" : "failed"}${dd.ll_z != null ? `, fit z = ${dd.ll_z}` : ""}.` }));
}
if (it.signal) kids.push(signalView(it.signal));
}
const pending = Object.entries(it.rubric.criteria).filter(([, c]) => c.score === null);
if (pending.length) {
kids.push(el("h3", { text: "Score what the system couldn't" }));
const box = el("div", { class: "criteria" });
for (const [name, c] of pending) {
const seg = el("div", { class: "seg", role: "group", "aria-label": LABELS[name] || name });
for (const [v, t] of [[0, "No"], [0.5, "Partly"], [1, "Yes"]]) {
const b = el("button", { "aria-pressed": "false", onclick: () => { S.scores[name] = v; seg.querySelectorAll("button").forEach((x) => x.setAttribute("aria-pressed", String(x === b))); } }, t);
seg.append(b);
}
box.append(el("div", { class: "criterion" }, el("span", {}, LABELS[name] || name, el("span", { class: "muted small", text: ` ${c.weight} pts` })), seg));
}
kids.push(box);
}
kids.push(el("h3", { text: "Issues you heard" }));
kids.push(el("div", { class: "checks" }, ISSUES.map(([k, t]) => el("label", {}, el("input", { type: "checkbox", value: k, name: "issue" }), t))));
if (it.required_reviewers > 1) kids.push(el("p", { class: "small muted", text: `Needs ${it.required_reviewers} reviewers; ${it.reviews_so_far} so far.` }));
kids.push(el("div", { class: "actions" }, el("button", { class: "primary", onclick: () => submitReview("accept") }, "Accept"),
el("button", { class: "danger", onclick: () => submitReview("reject") }, "Reject")));
d.replaceChildren(...kids);
audio.play().catch(() => {});
}
function alignmentView(m, lang) {
const line = el("p", { class: "align-line", lang });
for (const o of m.alignment) {
if (o.op === "match") line.append(el("span", { text: o.ref }), " ");
else if (o.op === "del") line.append(el("span", { class: "del", title: "Not heard by any model", text: o.ref }), " ");
else if (o.op === "sub") line.append(el("span", { class: "sub", title: `Heard as “${o.hyp}”`, text: o.ref }), el("small", { text: ` (${o.hyp})` }), " ");
else line.append(el("span", { class: "ins", text: `+${o.hyp}` }), " ");
}
const models = m.per_model ? Object.entries(m.per_model).map(([k, v]) => el("li", { class: "small" }, el("strong", { text: `${k}: ` }), el("span", { lang, text: v.asr_text }), ` (CER ${v.cer})`)) : [];
return el("div", {}, el("h3", { text: "Prompt against the ASR models" }), line, models.length ? el("ul", {}, models) : null,
el("p", { class: "small muted", text: `Best CER ${m.cer}${m.cer_between != null ? `, models disagree with each other by ${m.cer_between}` : ""}. The models can be wrong; trust what you hear.` }));
}
function signalView(s) {
const kv = el("dl", { class: "kv" });
const add = (k, v) => kv.append(el("dt", { text: k }), el("dd", { text: v }));
add("Duration", `${s.duration_s} s`); add("Speech", `${s.speech_s} s`);
add("SNR estimate", s.snr_db == null ? "n/a" : `${s.snr_db} dB`); add("Clipped samples", `${(100 * s.clip_ratio).toFixed(3)}%`);
add("Leading / trailing silence", `${s.leading_silence_s} s / ${s.trailing_silence_s} s`);
return el("div", {}, el("h3", { text: "Signal" }), kv);
}
async function submitReview(verdict) {
const it = visibleQueue()[S.idx];
if (!it) return;
if (it.blind && it.perceived_options?.length && !S.perceived) return toast("Choose what you heard first.");
const issues = [...document.querySelectorAll("input[name=issue]:checked")].map((x) => x.value);
try {
const r = await call(`/api/review/${it.recording_id}`, { method: "POST", body: { verdict, issues, scores: S.scores, perceived: S.perceived,
reviewer_id: S.user.username || "admin", seconds_spent: Math.round((performance.now() - S.openedAt) / 100) / 10 } });
toast(r.needs_more_reviews ? "Saved. Other reviewers will also listen." : `Saved. Recording ${r.status}.`);
} catch (e) { toast(e.message); if (!(e instanceof ApiError && e.status === 409)) return; }
S.queue = S.queue.filter((x) => x !== it);
renderQueue();
openItem(Math.min(S.idx, visibleQueue().length - 1));
}
function reviewKeys(e) {
if (S.tab !== "review" || e.target.matches("input, select, textarea") || S.idx < 0) return;
const audio = $("#detail audio");
if (e.key === " " && audio && !e.target.matches("button")) { e.preventDefault(); audio.paused ? audio.play() : audio.pause(); }
else if (e.key === "a" || e.key === "A") submitReview("accept");
else if (e.key === "r" || e.key === "R") submitReview("reject");
else if (e.key === "j" || e.key === "J") openItem(Math.min(S.idx + 1, visibleQueue().length - 1));
}
boot();
|