Spaces:
Running
Running
Create app.js
Browse files
app.js
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const METHODS = {
|
| 2 |
+
hybrid: { label: "Hybrid E5 + rerank", color: "#4f46e5" },
|
| 3 |
+
graph: { label: "Retained E5 + selected graph", color: "#0f766e" },
|
| 4 |
+
paths: { label: "Retained E5 + graph paths", color: "#c25b16" },
|
| 5 |
+
both: { label: "Retained E5 + graph + paths", color: "#9a6700" }
|
| 6 |
+
};
|
| 7 |
+
|
| 8 |
+
const state = { benchmark: [], byId: {}, replay: null, sources: {}, corpus: {}, filtered: [] };
|
| 9 |
+
const $ = id => document.getElementById(id);
|
| 10 |
+
const esc = value => String(value ?? "").replace(/[&<>"']/g, c => ({"&":"&","<":"<",">":">",'"':""","'":"'"}[c]));
|
| 11 |
+
const parseJsonl = text => text.split(/\r?\n/).filter(Boolean).map(line => JSON.parse(line));
|
| 12 |
+
|
| 13 |
+
function parseCsv(text) {
|
| 14 |
+
const lines = text.trim().split(/\r?\n/);
|
| 15 |
+
const headers = lines.shift().split(",");
|
| 16 |
+
return lines.map(line => {
|
| 17 |
+
const cells = line.match(/("(?:[^"]|"")*"|[^,]*)(?:,|$)/g).map(v => v.replace(/,$/, "").replace(/^"|"$/g, "").replace(/""/g, '"'));
|
| 18 |
+
return Object.fromEntries(headers.map((h, i) => [h, cells[i] || ""]));
|
| 19 |
+
});
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
async function loadCorpus(year) {
|
| 23 |
+
if (state.corpus[year]) return state.corpus[year];
|
| 24 |
+
const response = await fetch(`corpus/eval_corpus_${year}.jsonl`);
|
| 25 |
+
if (!response.ok) throw new Error(`Could not load the ${year} evidence corpus.`);
|
| 26 |
+
const rows = parseJsonl(await response.text());
|
| 27 |
+
state.corpus[year] = Object.fromEntries(rows.map(row => [row.object_id, row]));
|
| 28 |
+
return state.corpus[year];
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
function goldStatus(question, candidate) {
|
| 32 |
+
const evidence = question.evidence || [];
|
| 33 |
+
if (evidence.some(e => e.object_id === candidate.object_id)) return { css: "gold", label: "Exact gold evidence" };
|
| 34 |
+
if (evidence.some(e => String(e.page) === String(candidate.page))) return { css: "page", label: "Gold page, different object" };
|
| 35 |
+
return { css: "miss", label: "Not annotated gold" };
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
function provenance(reasons = []) {
|
| 39 |
+
const labels = [];
|
| 40 |
+
for (const reason of reasons) {
|
| 41 |
+
if (reason === "base" || reason.startsWith("source:hybrid")) labels.push("Hybrid");
|
| 42 |
+
else if (reason.startsWith("source:without_adjacent_page")) labels.push("Selected graph");
|
| 43 |
+
else if (reason.startsWith("query_metric:")) labels.push(`Query metric: ${reason.split(":")[1]}`);
|
| 44 |
+
else if (reason.startsWith("query_entity:")) labels.push(`Query entity: ${reason.split(":")[1]}`);
|
| 45 |
+
else if (reason.startsWith("path:same_page:")) labels.push("Same page");
|
| 46 |
+
else if (reason.startsWith("path:same_metric:")) labels.push(`Same metric: ${reason.split(":").at(-1)}`);
|
| 47 |
+
else if (reason.startsWith("path:same_entity:")) labels.push(`Same entity: ${reason.split(":").at(-1)}`);
|
| 48 |
+
}
|
| 49 |
+
return [...new Set(labels.length ? labels : ["Saved ranking"])];
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
function applyFilters() {
|
| 53 |
+
const year = $("year-filter").value;
|
| 54 |
+
const type = $("type-filter").value;
|
| 55 |
+
const keyword = $("keyword-filter").value.trim().toLowerCase();
|
| 56 |
+
state.filtered = state.benchmark.filter(q => state.replay.results[q.question_id])
|
| 57 |
+
.filter(q => year === "All years" || String(q.report_year) === year)
|
| 58 |
+
.filter(q => type === "All types" || q.question_type === type)
|
| 59 |
+
.filter(q => !keyword || q.question.toLowerCase().includes(keyword));
|
| 60 |
+
$("question-count").textContent = `${state.filtered.length} questions`;
|
| 61 |
+
$("question-select").innerHTML = state.filtered.map(q => `<option value="${esc(q.question_id)}">${esc(q.report_year)} | ${esc(q.question)}</option>`).join("");
|
| 62 |
+
render();
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
async function render() {
|
| 66 |
+
const qid = $("question-select").value;
|
| 67 |
+
if (!qid) {
|
| 68 |
+
$("question-summary").innerHTML = "<p>No questions match these filters.</p>";
|
| 69 |
+
$("comparison").innerHTML = "";
|
| 70 |
+
$("ranking").innerHTML = "";
|
| 71 |
+
return;
|
| 72 |
+
}
|
| 73 |
+
const question = state.byId[qid];
|
| 74 |
+
$("question-summary").innerHTML = `<h3>${esc(question.question)}</h3><p><strong>Gold answer:</strong> ${esc(question.answer || "No annotated answer")}</p><p>${esc(question.report_year)} annual report · ${esc(question.question_type.replaceAll("_", " "))}</p>`;
|
| 75 |
+
$("comparison").innerHTML = Object.entries(METHODS).map(([key, method]) => {
|
| 76 |
+
const top = state.replay.results[qid][key].top_k;
|
| 77 |
+
const exact = top.filter(c => goldStatus(question, c).css === "gold");
|
| 78 |
+
const page = top.filter(c => ["gold", "page"].includes(goldStatus(question, c).css));
|
| 79 |
+
const first = exact.length ? exact[0].rank : "-";
|
| 80 |
+
return `<article class="metric-block" style="--accent:${method.color}"><h3>${esc(method.label)}</h3><div class="metric-row"><span>Exact objects</span><strong>${exact.length}</strong></div><div class="metric-row"><span>Gold-page objects</span><strong>${page.length}</strong></div><div class="metric-row"><span>First exact rank</span><strong>${first}</strong></div></article>`;
|
| 81 |
+
}).join("");
|
| 82 |
+
await renderRanking(question);
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
async function renderRanking(question) {
|
| 86 |
+
const key = $("method-select").value || "both";
|
| 87 |
+
const method = METHODS[key];
|
| 88 |
+
const result = state.replay.results[question.question_id][key];
|
| 89 |
+
$("ranking").innerHTML = '<div class="loading">Loading evidence text...</div>';
|
| 90 |
+
try {
|
| 91 |
+
const corpus = await loadCorpus(question.report_year);
|
| 92 |
+
const trace = result.trace || {};
|
| 93 |
+
const cues = [];
|
| 94 |
+
if (trace.years?.length) cues.push(`Year: ${trace.years.join(", ")}`);
|
| 95 |
+
if (trace.entities?.length) cues.push(`Entities: ${trace.entities.join(", ")}`);
|
| 96 |
+
if (trace.metrics?.length) cues.push(`Metrics: ${trace.metrics.join(", ")}`);
|
| 97 |
+
const allLabels = [...new Set(result.top_k.flatMap(c => provenance(c.reasons)))];
|
| 98 |
+
$("route-summary").style.setProperty("--accent", method.color);
|
| 99 |
+
$("route-summary").innerHTML = `<strong>${esc(method.label)}</strong><span>${esc(cues.join(" · ") || "Saved ranking without path cues")}</span><div class="chips">${allLabels.map(x => `<span class="chip">${esc(x)}</span>`).join("")}</div>`;
|
| 100 |
+
$("ranking").innerHTML = result.top_k.map(candidate => {
|
| 101 |
+
const obj = corpus[candidate.object_id] || {};
|
| 102 |
+
const status = goldStatus(question, candidate);
|
| 103 |
+
const base = state.sources[question.document_id] || "";
|
| 104 |
+
const link = base ? `${base}#page=${candidate.page}` : "#";
|
| 105 |
+
return `<article class="evidence ${status.css}"><div class="rank">#${candidate.rank}</div><div><div class="match">${esc(status.label)}</div><div class="chips">${provenance(candidate.reasons).map(x => `<span class="chip">${esc(x)}</span>`).join("")}</div></div><div class="meta">Page ${esc(candidate.page)}<br>${esc(candidate.type || obj.object_type || "")}</div><div class="preview"><strong>${esc(candidate.object_id)}</strong><p>${esc((obj.text || "Evidence text unavailable").replace(/\s+/g, " ").slice(0, 520))}</p></div><a class="source" href="${esc(link)}" target="_blank" rel="noopener">PDF</a></article>`;
|
| 106 |
+
}).join("");
|
| 107 |
+
} catch (error) {
|
| 108 |
+
$("ranking").innerHTML = `<div class="error">${esc(error.message)}</div>`;
|
| 109 |
+
}
|
| 110 |
+
}
|
| 111 |
+
|
| 112 |
+
async function init() {
|
| 113 |
+
try {
|
| 114 |
+
const [benchmarkRes, replayRes, sourceRes] = await Promise.all([
|
| 115 |
+
fetch("finrag_equinor_v1.0.jsonl"), fetch("graph_replay.json"), fetch("source_manifest.csv")
|
| 116 |
+
]);
|
| 117 |
+
if (!benchmarkRes.ok || !replayRes.ok || !sourceRes.ok) throw new Error("One or more demo data files could not be loaded.");
|
| 118 |
+
state.benchmark = parseJsonl(await benchmarkRes.text()).filter(q => q.is_answerable);
|
| 119 |
+
state.byId = Object.fromEntries(state.benchmark.map(q => [q.question_id, q]));
|
| 120 |
+
state.replay = await replayRes.json();
|
| 121 |
+
state.sources = Object.fromEntries(parseCsv(await sourceRes.text()).map(row => [row.document_id, row.download_url]));
|
| 122 |
+
const years = [...new Set(state.benchmark.map(q => q.report_year))].sort();
|
| 123 |
+
$("year-filter").innerHTML += years.map(y => `<option>${y}</option>`).join("");
|
| 124 |
+
const types = [...new Set(state.benchmark.map(q => q.question_type))].sort();
|
| 125 |
+
$("type-filter").innerHTML += types.map(t => `<option value="${esc(t)}">${esc(t.replaceAll("_", " "))}</option>`).join("");
|
| 126 |
+
$("method-select").innerHTML = Object.entries(METHODS).map(([k, v]) => `<option value="${k}" ${k === "both" ? "selected" : ""}>${esc(v.label)}</option>`).join("");
|
| 127 |
+
["year-filter", "type-filter"].forEach(id => $(id).addEventListener("change", applyFilters));
|
| 128 |
+
$("keyword-filter").addEventListener("input", applyFilters);
|
| 129 |
+
$("question-select").addEventListener("change", render);
|
| 130 |
+
$("method-select").addEventListener("change", () => renderRanking(state.byId[$("question-select").value]));
|
| 131 |
+
applyFilters();
|
| 132 |
+
} catch (error) {
|
| 133 |
+
$("question-count").textContent = "Load failed";
|
| 134 |
+
$("question-summary").innerHTML = `<div class="error">${esc(error.message)}</div>`;
|
| 135 |
+
}
|
| 136 |
+
}
|
| 137 |
+
|
| 138 |
+
init();
|