Update index.html
Browse files- index.html +0 -67
index.html
CHANGED
@@ -78,12 +78,6 @@
|
|
78 |
id="text-input"
|
79 |
placeholder="Each line is treated as one sentence"
|
80 |
></textarea>
|
81 |
-
<h3>Labels:</h3>
|
82 |
-
<textarea
|
83 |
-
id="labels-input"
|
84 |
-
placeholder="Each line is treated as one label"
|
85 |
-
style="max-height: 200px"
|
86 |
-
></textarea>
|
87 |
<button id="embed-btn">Embed & Plot</button>
|
88 |
<p
|
89 |
id="status"
|
@@ -231,17 +225,6 @@
|
|
231 |
|
232 |
document.getElementById("text-input").value = sentences.join("\n");
|
233 |
|
234 |
-
const labels = [
|
235 |
-
"Weather",
|
236 |
-
"Technology",
|
237 |
-
"Cooking",
|
238 |
-
"Sports",
|
239 |
-
"Finance",
|
240 |
-
"Music",
|
241 |
-
"History",
|
242 |
-
];
|
243 |
-
document.getElementById("labels-input").value = labels.join("\n");
|
244 |
-
|
245 |
const statusEl = document.getElementById("status");
|
246 |
const embed = await pipeline(
|
247 |
"feature-extraction",
|
@@ -284,56 +267,6 @@
|
|
284 |
|
285 |
statusEl.textContent = "Plotting projection...";
|
286 |
|
287 |
-
const labelInput = document
|
288 |
-
.getElementById("labels-input")
|
289 |
-
.value.trim();
|
290 |
-
const labelTexts = labelInput
|
291 |
-
.split("\n")
|
292 |
-
.map((s) => s.trim())
|
293 |
-
.filter((s) => s);
|
294 |
-
|
295 |
-
let labelEmbeddings = [];
|
296 |
-
if (labelTexts.length) {
|
297 |
-
statusEl.textContent = "Embedding labels...";
|
298 |
-
const lblOut = await embed(
|
299 |
-
labelTexts.map((x) => `Category: ${x}`),
|
300 |
-
{ pooling: "cls", normalize: true },
|
301 |
-
);
|
302 |
-
labelEmbeddings = lblOut.tolist();
|
303 |
-
}
|
304 |
-
const threshold = 0.1;
|
305 |
-
const assigned = embeddings.map((sentEmb) => {
|
306 |
-
if (!labelEmbeddings.length) return "Other";
|
307 |
-
const sims = labelEmbeddings.map((lblEmb) =>
|
308 |
-
sentEmb.reduce((sum, v, i) => sum + v * lblEmb[i], 0),
|
309 |
-
);
|
310 |
-
let maxSim = -Infinity,
|
311 |
-
idx = -1;
|
312 |
-
for (let i = 0; i < sims.length; i++) {
|
313 |
-
if (sims[i] > maxSim) {
|
314 |
-
maxSim = sims[i];
|
315 |
-
idx = i;
|
316 |
-
}
|
317 |
-
}
|
318 |
-
return maxSim >= threshold ? labelTexts[idx] : "Other";
|
319 |
-
});
|
320 |
-
|
321 |
-
// Build one trace per label category
|
322 |
-
const uniq = Array.from(new Set(assigned));
|
323 |
-
const data = uniq.map((lbl) => {
|
324 |
-
const inds = assigned
|
325 |
-
.map((a, i) => (a === lbl ? i : -1))
|
326 |
-
.filter((i) => i >= 0);
|
327 |
-
return {
|
328 |
-
x: inds.map((i) => coords[i][0]),
|
329 |
-
y: inds.map((i) => coords[i][1]),
|
330 |
-
mode: "markers",
|
331 |
-
type: "scatter",
|
332 |
-
name: lbl,
|
333 |
-
text: inds.map((i) => sentences[i]),
|
334 |
-
marker: { size: 12 },
|
335 |
-
};
|
336 |
-
});
|
337 |
const layout = {
|
338 |
title: { text: "Labels" },
|
339 |
margin: { l: 0, r: 0, t: 0, b: 0 },
|
|
|
78 |
id="text-input"
|
79 |
placeholder="Each line is treated as one sentence"
|
80 |
></textarea>
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
<button id="embed-btn">Embed & Plot</button>
|
82 |
<p
|
83 |
id="status"
|
|
|
225 |
|
226 |
document.getElementById("text-input").value = sentences.join("\n");
|
227 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
228 |
const statusEl = document.getElementById("status");
|
229 |
const embed = await pipeline(
|
230 |
"feature-extraction",
|
|
|
267 |
|
268 |
statusEl.textContent = "Plotting projection...";
|
269 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
270 |
const layout = {
|
271 |
title: { text: "Labels" },
|
272 |
margin: { l: 0, r: 0, t: 0, b: 0 },
|