zoharcozmox commited on
Commit
00054da
·
verified ·
1 Parent(s): fb351a4

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +65 -37
index.js CHANGED
@@ -1,41 +1,69 @@
1
  import { pipeline, env } from "@xenova/transformers";
2
 
3
- async function main() {
4
- const inputEl = document.getElementById("input-text");
5
- const btnEl = document.getElementById("generate-btn");
6
- const outEl = document.getElementById("output-text");
7
-
8
- // (Optional) force ONNX CPU backend
9
- env.backends.onnx.wasm.wasmPaths = "https://cdn.jsdelivr.net/npm/@xenova/transformers@2.17.2/dist/";
10
- env.useWasmCPU();
11
-
12
- // Load the text2text-generation pipeline with the ONNX weights
13
- // hosted at onnx-community/orpheus-3b-0.1-ft-ONNX :contentReference[oaicite:0]{index=0}
14
- const generator = await pipeline(
15
- "text2text-generation",
16
- "onnx-community/orpheus-3b-0.1-ft-ONNX"
17
- );
18
-
19
- // Enable button once ready
20
- btnEl.disabled = false;
21
-
22
- btnEl.addEventListener("click", async () => {
23
- const prompt = inputEl.value.trim();
24
- if (!prompt) return;
25
-
26
- btnEl.disabled = true;
27
- outEl.textContent = "Generating…";
28
-
29
- try {
30
- // Generate up to 256 tokens; adjust max_length as needed :contentReference[oaicite:1]{index=1}
31
- const [res] = await generator(prompt, { max_length: 256 });
32
- outEl.textContent = res.generated_text;
33
- } catch (err) {
34
- outEl.textContent = `Error: ${err}`;
35
- } finally {
36
- btnEl.disabled = false;
37
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  });
39
- }
40
 
41
- main();
 
 
 
 
 
 
1
  import { pipeline, env } from "@xenova/transformers";
2
 
3
+ const MODEL_ID = "onnx-community/orpheus-3b-0.1-ft-ONNX";
4
+
5
+ const modal = document.getElementById("modal");
6
+ const modalText = document.getElementById("modal-text");
7
+ const progressBar = document.getElementById("progress-fill");
8
+ const inputEl = document.getElementById("input-text");
9
+ const btnEl = document.getElementById("generate-btn");
10
+ const loadingEl = document.getElementById("loading-text");
11
+ const outputEl = document.getElementById("output-text");
12
+
13
+ let generator = null;
14
+
15
+ // Set up ONNX WASM paths (optional)
16
+ env.backends.onnx.wasm.wasmPaths = "https://cdn.jsdelivr.net/npm/@xenova/transformers@2.17.2/dist/";
17
+ env.useWasmCPU();
18
+
19
+ btnEl.addEventListener("click", async () => {
20
+ const prompt = inputEl.value.trim();
21
+ if (!prompt) return;
22
+
23
+ btnEl.disabled = true;
24
+ outputEl.textContent = "";
25
+ loadingEl.textContent = "";
26
+
27
+ // Show downloading modal
28
+ modal.classList.remove("hidden");
29
+ progressBar.style.width = "0%";
30
+ modalText.textContent = "Loading model... 0%";
31
+
32
+ // Load (or reuse) pipeline with progress callback :contentReference[oaicite:0]{index=0}
33
+ if (!generator) {
34
+ generator = await pipeline("text2text-generation", MODEL_ID, {
35
+ progress_callback: (args) => {
36
+ if (args.status === "progress") {
37
+ const pct = Math.floor(args.progress * 100);
38
+ progressBar.style.width = pct + "%";
39
+ modalText.textContent = `Loading model... ${pct}%`;
40
+ }
41
+ },
42
+ });
43
+ }
44
+
45
+ // Hide modal
46
+ modalText.textContent = "Model ready!";
47
+ await new Promise((r) => setTimeout(r, 300));
48
+ modal.classList.add("hidden");
49
+
50
+ // Stream tokens as they generate :contentReference[oaicite:1]{index=1}
51
+ loadingEl.classList.remove("hidden");
52
+ await generator(prompt, {
53
+ max_length: 256,
54
+ callback_function: (beams) => {
55
+ // beams may be an array of { token, text, ... }
56
+ // append the latest token or text
57
+ const tok = typeof beams === "string"
58
+ ? beams
59
+ : (beams[0]?.text || beams[0]?.token || "");
60
+ loadingEl.textContent += tok;
61
+ },
62
  });
 
63
 
64
+ // Final output from the pipeline
65
+ const [res] = await generator(prompt, { max_length: 0 });
66
+ loadingEl.classList.add("hidden");
67
+ outputEl.textContent = res.generated_text;
68
+
69
+ btnEl.disabled