zoharcozmox commited on
Commit
efa45a8
·
verified ·
1 Parent(s): fb52811

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +39 -52
index.js CHANGED
@@ -1,54 +1,41 @@
1
- // Replace with the exact repo ID on HF Hub
2
- const MODEL_ID = 'onnx-community/orpheus-3b-0.1-ft-ONNX';
3
-
4
- let generator;
5
-
6
- async function init() {
7
- // Show a loading message
8
- const btn = document.getElementById('generate');
9
- btn.disabled = true;
10
- btn.textContent = 'Loading model…';
11
-
12
- // Initialize the pipeline. This will download the tokenizer and ONNX files.
13
- generator = await window.xenova.pipeline('text-generation', MODEL_ID);
14
-
15
- btn.disabled = false;
16
- btn.textContent = 'Generate';
17
- }
18
-
19
- async function generate() {
20
- const promptEl = document.getElementById('prompt');
21
- const outputEl = document.getElementById('output');
22
- const btn = document.getElementById('generate');
23
-
24
- const prompt = promptEl.value.trim();
25
- if (!prompt) {
26
- outputEl.textContent = 'Please enter a prompt.';
27
- return;
28
- }
29
-
30
- btn.disabled = true;
31
- btn.textContent = 'Generating…';
32
- outputEl.textContent = '';
33
-
34
- try {
35
- // Call the ONNX‑powered transformer
36
- const results = await generator(prompt, {
37
- max_length: 200,
38
- num_return_sequences: 1,
39
- temperature: 0.7
40
- });
41
-
42
- // The library returns an array of sequences
43
- outputEl.textContent = results[0].generated_text;
44
- } catch (err) {
45
- console.error(err);
46
- outputEl.textContent = 'Error during generation. See console for details.';
47
- } finally {
48
- btn.disabled = false;
49
- btn.textContent = 'Generate';
50
- }
51
  }
52
 
53
- document.getElementById('generate').addEventListener('click', generate);
54
- window.addEventListener('load', init);
 
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();