robertnetwork commited on
Commit
91e7c3f
·
verified ·
1 Parent(s): c4ee964

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +89 -30
index.html CHANGED
@@ -6,68 +6,127 @@
6
  <style>
7
  body { font-family: sans-serif; max-width: 800px; margin: 2em auto; padding: 0 1em; }
8
  h1 { text-align: center; }
9
- textarea { width: 100%; box-sizing: border-box; padding: 0.5em; margin-bottom: 1em; font-family: monospace; }
10
- button { display: block; margin: 0 auto 1em; padding: 0.75em 1.5em; font-size: 1em; }
 
11
  pre { background: #f7f7f7; padding: 1em; overflow-x: auto; white-space: pre-wrap; word-break: break-word; }
 
12
  </style>
13
  </head>
14
  <body>
15
  <h1>🎵 Strudel.cc Code Generator</h1>
16
 
17
  <label for="styleInput">Style/Prompt:</label>
18
- <textarea id="styleInput" rows="3" placeholder="e.g. electronic, ambient, techno…">electronic</textarea>
19
 
20
- <button id="generateBtn">🎵 Generate Code</button>
 
21
 
22
  <h2>Generated Strudel.cc Code</h2>
23
- <pre id="output">Waiting for input…</pre>
24
 
25
  <script type="module">
26
- // Load Transformers.js from CDN
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  import { pipeline, env } from 'https://cdn.jsdelivr.net/npm/@xenova/transformers@3.7.0/dist/transformers.esm.js';
28
 
29
- // Allow remote models from the Hub (default)
30
  env.allowRemoteModels = true;
31
- // Point to the ONNX/WebAssembly backend files
32
  env.backends.onnx.wasm.wasmPaths = 'https://cdn.jsdelivr.net/npm/@xenova/transformers@3.7.0/dist/onnx/';
33
 
34
- async function init() {
35
- // Create a text-generation pipeline on WebGPU
36
- const gen = await pipeline(
37
- 'text-generation',
38
- 'robertnetwork/strudel-small-onnx',
39
- {
40
- device: 'webgpu',
41
- dtype: 'fp32', // fp32 for WebGPU
42
- }
43
- );
 
 
 
 
 
 
 
 
44
 
45
- document.getElementById('generateBtn').onclick = async () => {
46
- const style = document.getElementById('styleInput').value.trim();
47
- const instruction = "Generate Strudel.cc JavaScript code given style tags and a prompt, without commentary or markdown.";
48
- const prompt = `Instruction: ${instruction}\nInput: ${style}\nOutput:`;
 
49
 
50
- const outEl = document.getElementById('output');
51
- outEl.textContent = 'Generating…';
52
 
53
- // Run the model
54
- const result = await gen(prompt, {
55
  max_length: 256,
56
  temperature: 0.7,
57
  top_p: 0.9,
58
  do_sample: true
59
  });
60
 
61
- // Extract the generated text
62
  let code = result[0].generated_text || result[0].text || '';
63
  if (code.includes('Output:')) {
64
  code = code.split('Output:').pop();
65
  }
66
  outEl.textContent = code.trim();
67
- };
68
- }
 
 
 
69
 
70
- init();
 
71
  </script>
72
  </body>
73
  </html>
 
6
  <style>
7
  body { font-family: sans-serif; max-width: 800px; margin: 2em auto; padding: 0 1em; }
8
  h1 { text-align: center; }
9
+ label, textarea, button, pre, #status { display: block; width: 100%; box-sizing: border-box; margin-bottom: 1em; }
10
+ textarea { padding: 0.5em; font-family: monospace; }
11
+ button { padding: 0.75em; font-size: 1em; cursor: pointer; }
12
  pre { background: #f7f7f7; padding: 1em; overflow-x: auto; white-space: pre-wrap; word-break: break-word; }
13
+ #status { font-style: italic; color: #555; }
14
  </style>
15
  </head>
16
  <body>
17
  <h1>🎵 Strudel.cc Code Generator</h1>
18
 
19
  <label for="styleInput">Style/Prompt:</label>
20
+ <textarea id="styleInput" rows="3" placeholder="e.g., electronic, ambient…">electronic</textarea>
21
 
22
+ <button id="generateBtn" disabled>Loading model…</button>
23
+ <div id="status">Initializing…</div>
24
 
25
  <h2>Generated Strudel.cc Code</h2>
26
+ <pre id="output"></pre>
27
 
28
  <script type="module">
29
+ // Grab our status and button elements up front
30
+ const statusEl = document.getElementById('status');
31
+ const btn = document.getElementById('generateBtn');
32
+ const outEl = document.getElementById('output');
33
+
34
+ // ── FETCH OVERRIDE ──
35
+ // Intercept downloads of .onnx files to report progress
36
+ const originalFetch = window.fetch.bind(window);
37
+ window.fetch = async (input, init) => {
38
+ const url = typeof input === 'string' ? input : input.url;
39
+ const response = await originalFetch(input, init);
40
+
41
+ if (url.endsWith('.onnx')) {
42
+ const lengthHeader = response.headers.get('Content-Length');
43
+ if (lengthHeader) {
44
+ const total = parseInt(lengthHeader, 10);
45
+ let loaded = 0;
46
+ const reader = response.body.getReader();
47
+ const stream = new ReadableStream({
48
+ start(ctrl) {
49
+ function read() {
50
+ reader.read().then(({ done, value }) => {
51
+ if (done) {
52
+ ctrl.close();
53
+ return;
54
+ }
55
+ loaded += value.byteLength;
56
+ statusEl.textContent = `Downloading model weights: ${(loaded/total*100).toFixed(1)}%`;
57
+ ctrl.enqueue(value);
58
+ read();
59
+ });
60
+ }
61
+ read();
62
+ }
63
+ });
64
+ return new Response(stream, { headers: response.headers });
65
+ } else {
66
+ statusEl.textContent = 'Downloading model weights…';
67
+ }
68
+ }
69
+
70
+ return response;
71
+ };
72
+
73
+ // ── IMPORT & CONFIGURE TRANSFORMERS.JS ──
74
  import { pipeline, env } from 'https://cdn.jsdelivr.net/npm/@xenova/transformers@3.7.0/dist/transformers.esm.js';
75
 
76
+ // Allow remote ONNX model downloads
77
  env.allowRemoteModels = true;
78
+ // Point to the ONNX runtime WASM files
79
  env.backends.onnx.wasm.wasmPaths = 'https://cdn.jsdelivr.net/npm/@xenova/transformers@3.7.0/dist/onnx/';
80
 
81
+ let generatorPipeline;
82
+
83
+ // ── INITIALIZE MODEL ──
84
+ async function loadModel() {
85
+ statusEl.textContent = 'Loading model…';
86
+ try {
87
+ generatorPipeline = await pipeline(
88
+ 'text-generation',
89
+ 'robertnetwork/strudel-small-onnx',
90
+ { device: 'webgpu', dtype: 'fp32' }
91
+ );
92
+ statusEl.textContent = 'Model loaded successfully!';
93
+ btn.disabled = false;
94
+ btn.textContent = '🎵 Generate Code';
95
+ } catch (err) {
96
+ statusEl.textContent = `Error loading model: ${err}`;
97
+ }
98
+ }
99
 
100
+ // ── HANDLE GENERATION ──
101
+ btn.addEventListener('click', async () => {
102
+ const style = document.getElementById('styleInput').value.trim();
103
+ const instruction = 'Generate Strudel.cc JavaScript code given style tags and a prompt, without commentary or markdown.';
104
+ const prompt = `Instruction: ${instruction}\nInput: ${style}\nOutput:`;
105
 
106
+ statusEl.textContent = 'Generating…';
107
+ outEl.textContent = '';
108
 
109
+ try {
110
+ const result = await generatorPipeline(prompt, {
111
  max_length: 256,
112
  temperature: 0.7,
113
  top_p: 0.9,
114
  do_sample: true
115
  });
116
 
 
117
  let code = result[0].generated_text || result[0].text || '';
118
  if (code.includes('Output:')) {
119
  code = code.split('Output:').pop();
120
  }
121
  outEl.textContent = code.trim();
122
+ statusEl.textContent = 'Generation succeeded!';
123
+ } catch (err) {
124
+ statusEl.textContent = `Error during generation: ${err}`;
125
+ }
126
+ });
127
 
128
+ // Kick off model load on page load
129
+ loadModel();
130
  </script>
131
  </body>
132
  </html>