File size: 1,285 Bytes
efa45a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aceaeff
5e0d30d
efa45a8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { pipeline, env } from "@xenova/transformers";

async function main() {
  const inputEl = document.getElementById("input-text");
  const btnEl   = document.getElementById("generate-btn");
  const outEl   = document.getElementById("output-text");

  // (Optional) force ONNX CPU backend
  env.backends.onnx.wasm.wasmPaths = "https://cdn.jsdelivr.net/npm/@xenova/transformers@2.17.2/dist/";
  env.useWasmCPU();

  // Load the text2text-generation pipeline with the ONNX weights
  // hosted at onnx-community/orpheus-3b-0.1-ft-ONNX :contentReference[oaicite:0]{index=0}
  const generator = await pipeline(
    "text2text-generation",
    "onnx-community/orpheus-3b-0.1-ft-ONNX"
  );

  // Enable button once ready
  btnEl.disabled = false;

  btnEl.addEventListener("click", async () => {
    const prompt = inputEl.value.trim();
    if (!prompt) return;

    btnEl.disabled = true;
    outEl.textContent = "Generating…";

    try {
      // Generate up to 256 tokens; adjust max_length as needed :contentReference[oaicite:1]{index=1}
      const [res] = await generator(prompt, { max_length: 256 });
      outEl.textContent = res.generated_text;
    } catch (err) {
      outEl.textContent = `Error: ${err}`;
    } finally {
      btnEl.disabled = false;
    }
  });
}

main();