Spaces:
Running
Running
ping98k commited on
Commit ·
7376f34
1
Parent(s): c67d118
Update K-Means clustering implementation to utilize new tokenizer and model for enhanced text generation and reasoning capabilities
Browse files- index.html +44 -11
index.html
CHANGED
|
@@ -65,7 +65,7 @@
|
|
| 65 |
</div>
|
| 66 |
<script src="https://cdn.plot.ly/plotly-2.32.0.min.js"></script>
|
| 67 |
<script type="module">
|
| 68 |
-
import { pipeline, TextStreamer } from
|
| 69 |
import { UMAP } from "https://cdn.jsdelivr.net/npm/umap-js@1.4.0/+esm";
|
| 70 |
|
| 71 |
const embed = await pipeline(
|
|
@@ -73,11 +73,8 @@
|
|
| 73 |
"onnx-community/Qwen3-Embedding-0.6B-ONNX",
|
| 74 |
{ device: "webgpu", dtype: "q4f16" },
|
| 75 |
);
|
| 76 |
-
const
|
| 77 |
-
|
| 78 |
-
"onnx-community/Qwen3-0.6B-ONNX",
|
| 79 |
-
{ device: "webgpu", dtype: "q4f16" },
|
| 80 |
-
);
|
| 81 |
|
| 82 |
const task = "Given a textual input sentence, retrieve relevant categories that best describe it.";
|
| 83 |
document.getElementById("run").onclick = async () => {
|
|
@@ -170,15 +167,51 @@
|
|
| 170 |
const joined = clustered[c].join("\n");
|
| 171 |
const messages = [
|
| 172 |
{ role: "system", content: "You are a helpful assistant." },
|
| 173 |
-
{ role: "user", content: `
|
| 174 |
];
|
| 175 |
-
const
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
max_new_tokens: 32,
|
| 177 |
do_sample: false,
|
| 178 |
-
streamer
|
| 179 |
});
|
| 180 |
-
let name =
|
| 181 |
-
name = name.replace(/^[\s\n]+|[\s\n]+$/g, "");
|
| 182 |
clusterNames.push(name.length > 0 ? name : `Cluster ${c + 1}`);
|
| 183 |
}
|
| 184 |
progressBarInner.style.width = "100%";
|
|
|
|
| 65 |
</div>
|
| 66 |
<script src="https://cdn.plot.ly/plotly-2.32.0.min.js"></script>
|
| 67 |
<script type="module">
|
| 68 |
+
import { pipeline, TextStreamer, AutoTokenizer, AutoModelForCausalLM } from 'https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.6.0';
|
| 69 |
import { UMAP } from "https://cdn.jsdelivr.net/npm/umap-js@1.4.0/+esm";
|
| 70 |
|
| 71 |
const embed = await pipeline(
|
|
|
|
| 73 |
"onnx-community/Qwen3-Embedding-0.6B-ONNX",
|
| 74 |
{ device: "webgpu", dtype: "q4f16" },
|
| 75 |
);
|
| 76 |
+
const tokenizer = await AutoTokenizer.from_pretrained("onnx-community/Qwen3-0.6B-ONNX");
|
| 77 |
+
const model = await AutoModelForCausalLM.from_pretrained("onnx-community/Qwen3-0.6B-ONNX", { device: "webgpu", dtype: "q4f16" });
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
const task = "Given a textual input sentence, retrieve relevant categories that best describe it.";
|
| 80 |
document.getElementById("run").onclick = async () => {
|
|
|
|
| 167 |
const joined = clustered[c].join("\n");
|
| 168 |
const messages = [
|
| 169 |
{ role: "system", content: "You are a helpful assistant." },
|
| 170 |
+
{ role: "user", content: `Given the following texts, provide a short, descriptive name for this group:\n\n${joined}` }
|
| 171 |
];
|
| 172 |
+
const reasonEnabled = false;
|
| 173 |
+
const inputs = tokenizer.apply_chat_template(messages, {
|
| 174 |
+
add_generation_prompt: true,
|
| 175 |
+
return_dict: true,
|
| 176 |
+
enable_thinking: reasonEnabled,
|
| 177 |
+
});
|
| 178 |
+
const [START_THINKING_TOKEN_ID, END_THINKING_TOKEN_ID] = tokenizer.encode("<think></think>", { add_special_tokens: false });
|
| 179 |
+
let state = "answering";
|
| 180 |
+
let startTime;
|
| 181 |
+
let numTokens = 0;
|
| 182 |
+
let tps;
|
| 183 |
+
const token_callback_function = (tokens) => {
|
| 184 |
+
startTime ??= performance.now();
|
| 185 |
+
if (numTokens++ > 0) {
|
| 186 |
+
tps = (numTokens / (performance.now() - startTime)) * 1000;
|
| 187 |
+
}
|
| 188 |
+
switch (Number(tokens[0])) {
|
| 189 |
+
case START_THINKING_TOKEN_ID:
|
| 190 |
+
state = "thinking";
|
| 191 |
+
break;
|
| 192 |
+
case END_THINKING_TOKEN_ID:
|
| 193 |
+
state = "answering";
|
| 194 |
+
break;
|
| 195 |
+
}
|
| 196 |
+
console.log(state, tokens, tokenizer.decode(tokens));
|
| 197 |
+
};
|
| 198 |
+
const callback_function = (output) => {
|
| 199 |
+
// You can update UI here if desired
|
| 200 |
+
console.log({ output, tps, numTokens, state });
|
| 201 |
+
};
|
| 202 |
+
const streamer = new TextStreamer(tokenizer, {
|
| 203 |
+
skip_prompt: true,
|
| 204 |
+
skip_special_tokens: true,
|
| 205 |
+
callback_function,
|
| 206 |
+
token_callback_function,
|
| 207 |
+
});
|
| 208 |
+
const outputTokens = await model.generate({
|
| 209 |
+
...inputs,
|
| 210 |
max_new_tokens: 32,
|
| 211 |
do_sample: false,
|
| 212 |
+
streamer,
|
| 213 |
});
|
| 214 |
+
let name = tokenizer.decode(outputTokens[0], { skip_special_tokens: false }).trim();
|
|
|
|
| 215 |
clusterNames.push(name.length > 0 ? name : `Cluster ${c + 1}`);
|
| 216 |
}
|
| 217 |
progressBarInner.style.width = "100%";
|