Spaces:
Sleeping
Sleeping
Commit ·
ff1e5bf
1
Parent(s): 4589db1
Add rendering of tokens
Browse files- frontend/script.js +35 -18
frontend/script.js
CHANGED
|
@@ -35,29 +35,46 @@ function displayOutput(data) {
|
|
| 35 |
<div id="tokenContainer"></div>
|
| 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 |
// Only consider preceding tokens.
|
| 62 |
const weights = row.slice(0, index);
|
| 63 |
|
|
|
|
| 35 |
<div id="tokenContainer"></div>
|
| 36 |
`;
|
| 37 |
}
|
| 38 |
+
function renderTokens(tokens, attentionData, layer_idx, head_idx) {
|
| 39 |
+
const container = document.getElementById('tokenContainer');
|
| 40 |
+
container.innerHTML = "";
|
| 41 |
|
| 42 |
+
tokens.forEach((token, index) => {
|
| 43 |
+
const span = document.createElement('span');
|
| 44 |
+
span.textContent = token + " ";
|
| 45 |
|
| 46 |
+
span.addEventListener('mouseenter', () => {
|
| 47 |
+
highlightAttention(index, attentionData, layer_idx, head_idx);
|
| 48 |
+
});
|
| 49 |
+
|
| 50 |
+
span.addEventListener('mouseleave', () => {
|
| 51 |
+
resetTokenSizes();
|
| 52 |
+
});
|
| 53 |
+
container.appendChild(span);
|
| 54 |
+
});
|
| 55 |
+
}
|
| 56 |
+
function displayHoverTokens(data, layer_idx, head_idx) {
|
| 57 |
+
const tokens = data.tokens;
|
| 58 |
+
const attentionMatrix = data.attention;
|
| 59 |
|
| 60 |
+
renderTokens(tokens, attentionMatrix, layer_idx, head_idx);
|
| 61 |
+
}
|
| 62 |
+
function resetTokenSizes() {
|
| 63 |
+
const container = document.getElementById("tokenContainer");
|
| 64 |
+
Array.from(container.children).forEach((span) => {
|
| 65 |
+
span.style.fontSize = "16px";
|
| 66 |
+
});
|
| 67 |
}
|
| 68 |
|
| 69 |
+
function highlightAttention(index, attentionData, layer_idx, head_idx){
|
| 70 |
+
const container = document.getElementById('tokenContainer');
|
| 71 |
+
// Retrieve the row corresponding to the hovered token.
|
| 72 |
+
const row = attentionData[layer_idx][head_idx][index];
|
| 73 |
+
if (!row) {
|
| 74 |
+
console.warn(`No attention data for token index ${index}`);
|
| 75 |
+
return;
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
// Only consider preceding tokens.
|
| 79 |
const weights = row.slice(0, index);
|
| 80 |
|