Spaces:
Running
Running
Update index.html
Browse files- index.html +57 -88
index.html
CHANGED
|
@@ -1,132 +1,101 @@
|
|
| 1 |
<!DOCTYPE html>
|
| 2 |
<html lang="en">
|
| 3 |
<head>
|
| 4 |
-
<meta charset="
|
| 5 |
<title>🎵 Strudel.cc Code Generator</title>
|
| 6 |
<style>
|
| 7 |
-
body { font-family: sans-serif;
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 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 |
-
<
|
| 20 |
-
<textarea id="styleInput" rows="3" placeholder="e.g., electronic, ambient…">electronic</textarea>
|
| 21 |
|
| 22 |
-
<
|
|
|
|
| 23 |
<div id="status">Initializing…</div>
|
| 24 |
|
| 25 |
-
<h2>Generated
|
| 26 |
<pre id="output"></pre>
|
| 27 |
|
| 28 |
<script type="module">
|
| 29 |
-
|
| 30 |
-
|
| 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 |
-
|
| 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 |
-
|
| 79 |
-
|
| 80 |
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
-
|
| 84 |
-
async function loadModel() {
|
| 85 |
-
statusEl.textContent = 'Loading model…';
|
| 86 |
try {
|
| 87 |
-
|
|
|
|
| 88 |
'text-generation',
|
| 89 |
-
|
| 90 |
-
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
);
|
| 92 |
-
statusEl.textContent = 'Model
|
| 93 |
btn.disabled = false;
|
| 94 |
-
btn.textContent = '🎵 Generate
|
| 95 |
-
} catch (
|
| 96 |
-
statusEl.textContent =
|
| 97 |
}
|
| 98 |
}
|
| 99 |
|
| 100 |
-
// ── HANDLE GENERATION ──
|
| 101 |
btn.addEventListener('click', async () => {
|
| 102 |
-
const style =
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
statusEl.textContent = 'Generating…';
|
| 107 |
-
|
| 108 |
|
| 109 |
try {
|
| 110 |
-
const
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
max_length: 256,
|
| 112 |
temperature: 0.7,
|
| 113 |
top_p: 0.9,
|
| 114 |
do_sample: true
|
| 115 |
});
|
| 116 |
|
| 117 |
-
let
|
| 118 |
-
if (
|
| 119 |
-
|
| 120 |
}
|
| 121 |
-
|
| 122 |
-
statusEl.textContent = '
|
| 123 |
-
} catch (
|
| 124 |
-
statusEl.textContent =
|
|
|
|
|
|
|
| 125 |
}
|
| 126 |
});
|
| 127 |
|
| 128 |
-
//
|
| 129 |
-
|
| 130 |
</script>
|
| 131 |
</body>
|
| 132 |
</html>
|
|
|
|
| 1 |
<!DOCTYPE html>
|
| 2 |
<html lang="en">
|
| 3 |
<head>
|
| 4 |
+
<meta charset="UTF-8">
|
| 5 |
<title>🎵 Strudel.cc Code Generator</title>
|
| 6 |
<style>
|
| 7 |
+
body { font-family: sans-serif; margin: 2em; max-width: 800px; }
|
| 8 |
+
textarea, button, pre { width: 100%; box-sizing: border-box; margin: .5em 0; }
|
| 9 |
+
textarea { height: 3em; padding: .5em; font-family: monospace; }
|
| 10 |
+
button { padding: .75em; font-size: 1em; }
|
|
|
|
|
|
|
| 11 |
#status { font-style: italic; color: #555; }
|
| 12 |
+
pre { background: #f7f7f7; padding: 1em; white-space: pre-wrap; word-break: break-word; }
|
| 13 |
</style>
|
| 14 |
</head>
|
| 15 |
<body>
|
|
|
|
| 16 |
|
| 17 |
+
<h1>🎵 Strudel.cc Code Generator</h1>
|
|
|
|
| 18 |
|
| 19 |
+
<textarea id="prompt" placeholder="e.g. electronic"></textarea>
|
| 20 |
+
<button id="generate" disabled>Loading model…</button>
|
| 21 |
<div id="status">Initializing…</div>
|
| 22 |
|
| 23 |
+
<h2>Generated Code</h2>
|
| 24 |
<pre id="output"></pre>
|
| 25 |
|
| 26 |
<script type="module">
|
| 27 |
+
import { pipeline, env }
|
| 28 |
+
from 'https://cdn.jsdelivr.net/npm/@xenova/transformers@3.7.0/dist/transformers.esm.js';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
// Allow Hub downloads, point to onnx wasm files:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
env.allowRemoteModels = true;
|
| 32 |
+
env.backends.onnx.wasm.wasmPaths =
|
| 33 |
+
'https://cdn.jsdelivr.net/npm/@xenova/transformers@3.7.0/dist/onnx/';
|
| 34 |
|
| 35 |
+
const MODEL_ID = 'robertnetwork/strudel-small-onnx'; // ONNX version
|
| 36 |
+
const statusEl = document.getElementById('status');
|
| 37 |
+
const btn = document.getElementById('generate');
|
| 38 |
+
const promptEl = document.getElementById('prompt');
|
| 39 |
+
const outputEl = document.getElementById('output');
|
| 40 |
+
let generator;
|
| 41 |
|
| 42 |
+
async function init() {
|
|
|
|
|
|
|
| 43 |
try {
|
| 44 |
+
statusEl.textContent = 'Loading tokenizer and model…';
|
| 45 |
+
generator = await pipeline(
|
| 46 |
'text-generation',
|
| 47 |
+
MODEL_ID,
|
| 48 |
+
{
|
| 49 |
+
device: 'webgpu',
|
| 50 |
+
dtype: 'fp32',
|
| 51 |
+
// progress_callback will receive messages like:
|
| 52 |
+
// "Downloading tokenizer_config.json…", "Downloading pytorch_model.bin…", "Compiling WebGPU shaders…"
|
| 53 |
+
progress_callback: msg => statusEl.textContent = msg
|
| 54 |
+
}
|
| 55 |
);
|
| 56 |
+
statusEl.textContent = 'Model ready! Enter a prompt and click Generate.';
|
| 57 |
btn.disabled = false;
|
| 58 |
+
btn.textContent = '🎵 Generate';
|
| 59 |
+
} catch (e) {
|
| 60 |
+
statusEl.textContent = '❌ Error loading model: ' + e;
|
| 61 |
}
|
| 62 |
}
|
| 63 |
|
|
|
|
| 64 |
btn.addEventListener('click', async () => {
|
| 65 |
+
const style = promptEl.value.trim();
|
| 66 |
+
if (!style) return;
|
| 67 |
+
outputEl.textContent = '';
|
|
|
|
| 68 |
statusEl.textContent = 'Generating…';
|
| 69 |
+
btn.disabled = true;
|
| 70 |
|
| 71 |
try {
|
| 72 |
+
const instruction =
|
| 73 |
+
'Generate Strudel.cc JavaScript code given style tags and a prompt, without commentary or markdown.';
|
| 74 |
+
const prompt = `Instruction: ${instruction}\nInput: ${style}\nOutput:`;
|
| 75 |
+
|
| 76 |
+
// Run the model
|
| 77 |
+
const [ result ] = await generator(prompt, {
|
| 78 |
max_length: 256,
|
| 79 |
temperature: 0.7,
|
| 80 |
top_p: 0.9,
|
| 81 |
do_sample: true
|
| 82 |
});
|
| 83 |
|
| 84 |
+
let text = result.generated_text || result.text || '';
|
| 85 |
+
if (text.includes('Output:')) {
|
| 86 |
+
text = text.split('Output:').pop();
|
| 87 |
}
|
| 88 |
+
outputEl.textContent = text.trim();
|
| 89 |
+
statusEl.textContent = 'Done!';
|
| 90 |
+
} catch (e) {
|
| 91 |
+
statusEl.textContent = '❌ Generation error: ' + e;
|
| 92 |
+
} finally {
|
| 93 |
+
btn.disabled = false;
|
| 94 |
}
|
| 95 |
});
|
| 96 |
|
| 97 |
+
// Start loading immediately
|
| 98 |
+
init();
|
| 99 |
</script>
|
| 100 |
</body>
|
| 101 |
</html>
|