Paar, F. (Ferdinand) commited on
Commit
2e082b4
·
1 Parent(s): 42370b7

thubnail filled and layer head box next to each other

Browse files
Files changed (1) hide show
  1. frontend/script.js +61 -47
frontend/script.js CHANGED
@@ -56,65 +56,91 @@ function createLargeThumbnail(data) {
56
  const tokens = data.tokens;
57
  const attention = data.attention;
58
 
59
- // Dimensions for the thumbnail
60
- const width = 300;
61
- const height = 300;
62
- const textHeight = 30; // Space for token text (top and bottom)
63
- const graphHeight = height - 2 * textHeight; // Remaining space for the graph
64
 
65
- // Create the main container and append it to #thumbnailContainer
 
 
 
66
  const container = d3.select("#thumbnailContainer")
67
  .append("div")
68
  .attr("id", "largeThumbnail")
69
- .style("width", width + "px")
70
- .style("height", height + "px")
71
  .style("border", "2px solid #ddd")
72
  .style("border-radius", "6px")
73
  .style("background", "#fff")
74
- .style("padding", "10px")
 
 
 
 
 
 
 
 
75
  .style("display", "flex")
76
  .style("flex-direction", "column")
77
- .style("align-items", "center");
78
-
79
- // Add token text above the graph
80
- container.append("div")
81
- .classed("thumbnail-token-text", true)
82
- .style("width", "100%")
83
- .style("text-align", "center")
84
- .style("font-size", "16px")
85
- .style("margin-bottom", "5px")
86
- .text(tokens.join(" "));
87
-
88
- // Create the SVG container for the graph
89
  const svg = container.append("svg")
90
- .attr("width", width)
91
  .attr("height", graphHeight)
92
- .attr("viewBox", `0 0 ${width} ${graphHeight}`)
93
  .attr("preserveAspectRatio", "none");
94
 
95
- // Add header text inside the SVG
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96
  svg.append("text")
97
- .attr("x", width / 2)
98
- .attr("y", 20) // adjust as needed
99
  .attr("text-anchor", "middle")
100
  .attr("font-size", "14px")
101
  .text("Layer 1 - Head 1");
102
 
103
- // Calculate spacing for tokens inside the SVG
104
  const tokenCount = tokens.length;
105
  const tokenSpacing = graphHeight / (tokenCount + 1);
106
 
107
- // Draw attention lines for the first layer, first head
108
- // (Assuming attention[0][0] is a matrix: tokens x tokens)
109
  attention[0][0].forEach((sourceWeights, sourceIdx) => {
110
  const rowMax = Math.max(...sourceWeights) || 1;
111
  sourceWeights.forEach((weight, targetIdx) => {
112
  if (weight > 0.01 && sourceIdx !== targetIdx) {
113
  const normalizedWeight = weight / rowMax;
114
  svg.append("line")
115
- .attr("x1", 20) // left margin inside the SVG
116
  .attr("y1", tokenSpacing * (sourceIdx + 1))
117
- .attr("x2", width - 20) // right margin inside the SVG
118
  .attr("y2", tokenSpacing * (targetIdx + 1))
119
  .attr("stroke", "#800000")
120
  .attr("stroke-width", Math.max(0.5, normalizedWeight * 4))
@@ -124,15 +150,6 @@ function createLargeThumbnail(data) {
124
  });
125
  });
126
 
127
- // Add token text below the graph
128
- container.append("div")
129
- .classed("thumbnail-token-text", true)
130
- .style("width", "100%")
131
- .style("text-align", "center")
132
- .style("font-size", "16px")
133
- .style("margin-top", "5px")
134
- .text(tokens.join(" "));
135
-
136
  return container.node();
137
  }
138
 
@@ -201,17 +218,14 @@ function createAttentionThumbnail(data, layerIdx, headIdx) {
201
  thumbnail.on("click", function() {
202
  console.log(`Clicked: Layer ${layerIdx + 1}, Head ${headIdx + 1}`);
203
 
204
- // Remove background from all thumbnails
205
  d3.selectAll(".thumbnail").style("background", "#fff");
206
-
207
- // Set background for clicked thumbnail
208
  d3.select(this).style("background", "#ddd");
209
 
210
- // Update displayed head/layer numbers
211
- d3.select("#display_head .number-box").text(headIdx + 1);
212
- d3.select("#display_layer .number-box").text(layerIdx + 1);
213
 
214
- // Display updated hover tokens
215
  displayHoverTokens(data, layerIdx, headIdx);
216
  });
217
 
 
56
  const tokens = data.tokens;
57
  const attention = data.attention;
58
 
59
+ // Dimensions
60
+ const graphWidth = 300;
61
+ const tokenAreaWidth = 60;
62
+ const totalWidth = tokenAreaWidth * 2 + graphWidth;
63
+ const graphHeight = 300;
64
 
65
+ // Remove any existing large thumbnail so only the latest shows
66
+ d3.select("#largeThumbnail").remove();
67
+
68
+ // Create main container as a flex row
69
  const container = d3.select("#thumbnailContainer")
70
  .append("div")
71
  .attr("id", "largeThumbnail")
72
+ .style("width", totalWidth + "px")
73
+ .style("height", graphHeight + "px")
74
  .style("border", "2px solid #ddd")
75
  .style("border-radius", "6px")
76
  .style("background", "#fff")
77
+ .style("display", "flex")
78
+ .style("align-items", "center")
79
+ .style("justify-content", "space-between");
80
+
81
+ // Left token container
82
+ const leftTokens = container.append("div")
83
+ .attr("class", "tokens-left")
84
+ .style("width", tokenAreaWidth + "px")
85
+ .style("height", graphHeight + "px")
86
  .style("display", "flex")
87
  .style("flex-direction", "column")
88
+ .style("justify-content", "space-around")
89
+ .style("align-items", "flex-end");
90
+
91
+ tokens.forEach(token => {
92
+ leftTokens.append("div")
93
+ .style("font-size", "14px")
94
+ .style("text-align", "right")
95
+ .text(token);
96
+ });
97
+
98
+ // Center SVG for the graph
 
99
  const svg = container.append("svg")
100
+ .attr("width", graphWidth)
101
  .attr("height", graphHeight)
102
+ .attr("viewBox", `0 0 ${graphWidth} ${graphHeight}`)
103
  .attr("preserveAspectRatio", "none");
104
 
105
+ // Right token container
106
+ const rightTokens = container.append("div")
107
+ .attr("class", "tokens-right")
108
+ .style("width", tokenAreaWidth + "px")
109
+ .style("height", graphHeight + "px")
110
+ .style("display", "flex")
111
+ .style("flex-direction", "column")
112
+ .style("justify-content", "space-around")
113
+ .style("align-items", "flex-start");
114
+
115
+ tokens.forEach(token => {
116
+ rightTokens.append("div")
117
+ .style("font-size", "14px")
118
+ .style("text-align", "left")
119
+ .text(token);
120
+ });
121
+
122
+ // Optional: Add header text inside the SVG
123
  svg.append("text")
124
+ .attr("x", graphWidth / 2)
125
+ .attr("y", 20)
126
  .attr("text-anchor", "middle")
127
  .attr("font-size", "14px")
128
  .text("Layer 1 - Head 1");
129
 
130
+ // Compute vertical spacing (each token aligns with a node)
131
  const tokenCount = tokens.length;
132
  const tokenSpacing = graphHeight / (tokenCount + 1);
133
 
134
+ // Draw attention lines for the first layer, first head (attention[0][0])
 
135
  attention[0][0].forEach((sourceWeights, sourceIdx) => {
136
  const rowMax = Math.max(...sourceWeights) || 1;
137
  sourceWeights.forEach((weight, targetIdx) => {
138
  if (weight > 0.01 && sourceIdx !== targetIdx) {
139
  const normalizedWeight = weight / rowMax;
140
  svg.append("line")
141
+ .attr("x1", 20) // left margin within the SVG
142
  .attr("y1", tokenSpacing * (sourceIdx + 1))
143
+ .attr("x2", graphWidth - 20) // right margin within the SVG
144
  .attr("y2", tokenSpacing * (targetIdx + 1))
145
  .attr("stroke", "#800000")
146
  .attr("stroke-width", Math.max(0.5, normalizedWeight * 4))
 
150
  });
151
  });
152
 
 
 
 
 
 
 
 
 
 
153
  return container.node();
154
  }
155
 
 
218
  thumbnail.on("click", function() {
219
  console.log(`Clicked: Layer ${layerIdx + 1}, Head ${headIdx + 1}`);
220
 
 
221
  d3.selectAll(".thumbnail").style("background", "#fff");
 
 
222
  d3.select(this).style("background", "#ddd");
223
 
224
+ // Update displayed head and layer numbers
225
+ d3.select("#display_head").text(headIdx + 1);
226
+ d3.select("#display_layer").text(layerIdx + 1);
227
 
228
+ // Update hover tokens
229
  displayHoverTokens(data, layerIdx, headIdx);
230
  });
231