Update index.html
Browse files- index.html +74 -96
index.html
CHANGED
|
@@ -3,25 +3,23 @@
|
|
| 3 |
<head>
|
| 4 |
<meta charset="utf-8" />
|
| 5 |
<meta name="viewport" content="width=device-width, initial-scale=1"/>
|
| 6 |
-
<title>
|
| 7 |
<style>
|
| 8 |
-
body { font:
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
#
|
| 14 |
-
|
|
|
|
| 15 |
.ok { color:#0a0 } .err{ color:#b00 } .warn{ color:#b66 }
|
| 16 |
-
pre { background:#f6f6f6; padding:8px; overflow:auto }
|
| 17 |
</style>
|
| 18 |
</head>
|
| 19 |
<body>
|
| 20 |
<h1>NLLB translation in browser</h1>
|
| 21 |
|
| 22 |
-
<div id="
|
| 23 |
-
|
| 24 |
-
<div id="row" style="margin-top:12px">
|
| 25 |
<div>
|
| 26 |
<label for="src_lang">Source language</label>
|
| 27 |
<select id="src_lang">
|
|
@@ -45,9 +43,8 @@
|
|
| 45 |
<label for="from">Input text</label>
|
| 46 |
<textarea id="from" placeholder="Type text to translate"></textarea>
|
| 47 |
|
| 48 |
-
<!-- Progress UI -->
|
| 49 |
<div id="progress-wrap">
|
| 50 |
-
<progress id="loadProgress" value="0" max="1" style="width:100
|
| 51 |
<div id="progressText" class="muted">Initializing…</div>
|
| 52 |
</div>
|
| 53 |
|
|
@@ -56,115 +53,95 @@
|
|
| 56 |
<label for="to" style="margin-top:12px">Output</label>
|
| 57 |
<textarea id="to" readonly>Output will be here...</textarea>
|
| 58 |
|
| 59 |
-
<
|
| 60 |
-
<details>
|
| 61 |
-
<summary>Debug</summary>
|
| 62 |
-
<div id="debug"></div>
|
| 63 |
-
</details>
|
| 64 |
|
| 65 |
-
<!-- Loader: try ESM import first; if that fails, try UMD global by injecting a classic <script> -->
|
| 66 |
<script>
|
| 67 |
-
const
|
| 68 |
-
const debug = document.getElementById('debug');
|
| 69 |
-
|
| 70 |
-
function log(line, cls='') {
|
| 71 |
-
const p = document.createElement('div');
|
| 72 |
-
if (cls) p.className = cls;
|
| 73 |
-
p.textContent = line;
|
| 74 |
-
debug.appendChild(p);
|
| 75 |
-
}
|
| 76 |
|
| 77 |
async function loadTransformers() {
|
| 78 |
-
//
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
}
|
| 94 |
|
| 95 |
-
//
|
|
|
|
| 96 |
await new Promise((resolve) => {
|
| 97 |
const s = document.createElement('script');
|
| 98 |
-
s.src = './transformers.min.js';
|
| 99 |
-
s.onload =
|
| 100 |
-
s.onerror = () => resolve();
|
| 101 |
document.head.appendChild(s);
|
| 102 |
});
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
log('Loaded via UMD (global window.transformers exists)', 'ok');
|
| 107 |
-
} else {
|
| 108 |
-
status.innerHTML = '<span class="err">❌ Could not load Transformers.js as ESM or UMD.</span>';
|
| 109 |
-
log('window.transformers is ' + typeof window.transformers, 'err');
|
| 110 |
}
|
|
|
|
| 111 |
}
|
| 112 |
|
| 113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
-
function
|
| 116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
const btn = document.getElementById("submit");
|
| 119 |
const out = document.getElementById("to");
|
| 120 |
const fromEl = document.getElementById("from");
|
| 121 |
const srcEl = document.getElementById("src_lang");
|
| 122 |
const tgtEl = document.getElementById("tgt_lang");
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
const
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
btn.disabled = false;
|
| 128 |
btn.value = "Translate";
|
| 129 |
|
| 130 |
-
function showProgressUI(show, message) {
|
| 131 |
-
wrap.style.display = show ? "" : "none";
|
| 132 |
-
if (message) txt.textContent = message;
|
| 133 |
-
bar.value = 0; bar.max = 1;
|
| 134 |
-
}
|
| 135 |
-
function progressCallback(p) {
|
| 136 |
-
if (p.status) txt.textContent = p.status;
|
| 137 |
-
if (typeof p.loaded === "number" && typeof p.total === "number" && p.total > 0) {
|
| 138 |
-
bar.max = p.total;
|
| 139 |
-
bar.value = p.loaded;
|
| 140 |
-
const pct = Math.round((p.loaded / p.total) * 100);
|
| 141 |
-
txt.textContent = `Downloading ${p.file || "model"}… ${pct}%`;
|
| 142 |
-
}
|
| 143 |
-
}
|
| 144 |
-
|
| 145 |
let translator = null;
|
| 146 |
async function getTranslator() {
|
| 147 |
if (translator) return translator;
|
| 148 |
showProgressUI(true, "Loading model…");
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
device: (navigator.gpu ? "webgpu" : "wasm"),
|
| 157 |
-
progress_callback: progressCallback
|
| 158 |
-
});
|
| 159 |
-
|
| 160 |
-
txt.textContent = "✅ Model ready";
|
| 161 |
-
bar.max = 1; bar.value = 1;
|
| 162 |
-
return translator;
|
| 163 |
-
} catch (e) {
|
| 164 |
-
console.error(e);
|
| 165 |
-
txt.textContent = "❌ Error loading model — see console.";
|
| 166 |
-
throw e;
|
| 167 |
-
}
|
| 168 |
}
|
| 169 |
|
| 170 |
btn.addEventListener("click", async () => {
|
|
@@ -177,12 +154,13 @@
|
|
| 177 |
const res = await t(text, { src_lang: srcEl.value, tgt_lang: tgtEl.value, max_length: 128 });
|
| 178 |
out.value = res?.[0]?.translation_text || "(no output)";
|
| 179 |
} catch (e) {
|
|
|
|
| 180 |
out.value = "❌ Translation failed. See console.";
|
| 181 |
} finally {
|
| 182 |
btn.disabled = false; btn.value = prev;
|
| 183 |
}
|
| 184 |
});
|
| 185 |
-
}
|
| 186 |
</script>
|
| 187 |
</body>
|
| 188 |
</html>
|
|
|
|
| 3 |
<head>
|
| 4 |
<meta charset="utf-8" />
|
| 5 |
<meta name="viewport" content="width=device-width, initial-scale=1"/>
|
| 6 |
+
<title>NLLB translation in browser</title>
|
| 7 |
<style>
|
| 8 |
+
body { font:14px/1.5 system-ui, -apple-system, Segoe UI, Roboto, sans-serif; margin:16px; }
|
| 9 |
+
h1 { margin:0 0 12px }
|
| 10 |
+
label { display:block; margin:8px 0 4px }
|
| 11 |
+
textarea { width:100%; min-height:120px }
|
| 12 |
+
select, input[type="submit"] { padding:8px; font:inherit }
|
| 13 |
+
#row { display:grid; grid-template-columns:1fr 1fr; gap:12px }
|
| 14 |
+
#progress-wrap { margin:10px 0; display:none }
|
| 15 |
+
.muted { opacity:.8 }
|
| 16 |
.ok { color:#0a0 } .err{ color:#b00 } .warn{ color:#b66 }
|
|
|
|
| 17 |
</style>
|
| 18 |
</head>
|
| 19 |
<body>
|
| 20 |
<h1>NLLB translation in browser</h1>
|
| 21 |
|
| 22 |
+
<div id="row">
|
|
|
|
|
|
|
| 23 |
<div>
|
| 24 |
<label for="src_lang">Source language</label>
|
| 25 |
<select id="src_lang">
|
|
|
|
| 43 |
<label for="from">Input text</label>
|
| 44 |
<textarea id="from" placeholder="Type text to translate"></textarea>
|
| 45 |
|
|
|
|
| 46 |
<div id="progress-wrap">
|
| 47 |
+
<progress id="loadProgress" value="0" max="1" style="width:100%"></progress>
|
| 48 |
<div id="progressText" class="muted">Initializing…</div>
|
| 49 |
</div>
|
| 50 |
|
|
|
|
| 53 |
<label for="to" style="margin-top:12px">Output</label>
|
| 54 |
<textarea id="to" readonly>Output will be here...</textarea>
|
| 55 |
|
| 56 |
+
<div id="status" class="muted" style="margin-top:8px">Loading library…</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
|
|
|
| 58 |
<script>
|
| 59 |
+
const statusEl = document.getElementById('status');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
async function loadTransformers() {
|
| 62 |
+
// Try ESM from CDNs (like julien-c’s page)
|
| 63 |
+
const cdns = [
|
| 64 |
+
'https://cdn.jsdelivr.net/npm/@xenova/[email protected]',
|
| 65 |
+
'https://unpkg.com/@xenova/transformers@3.0.0'
|
| 66 |
+
];
|
| 67 |
+
for (const url of cdns) {
|
| 68 |
+
try {
|
| 69 |
+
statusEl.textContent = `Trying ESM: ${url}`;
|
| 70 |
+
const mod = await import(/* @vite-ignore */ url);
|
| 71 |
+
if (mod?.pipeline) {
|
| 72 |
+
window.transformers = mod; // expose
|
| 73 |
+
statusEl.textContent = `✅ Loaded via ESM: ${url}`;
|
| 74 |
+
return;
|
| 75 |
+
}
|
| 76 |
+
} catch (_) {}
|
| 77 |
}
|
| 78 |
|
| 79 |
+
// Fallback: local UMD (needs real ~3–4 MB bundle in repo root)
|
| 80 |
+
statusEl.textContent = 'Trying local UMD ./transformers.min.js…';
|
| 81 |
await new Promise((resolve) => {
|
| 82 |
const s = document.createElement('script');
|
| 83 |
+
s.src = './transformers.min.js?v=' + Math.random();
|
| 84 |
+
s.onload = resolve; s.onerror = resolve;
|
|
|
|
| 85 |
document.head.appendChild(s);
|
| 86 |
});
|
| 87 |
+
if (window.transformers?.pipeline) {
|
| 88 |
+
statusEl.textContent = '✅ Loaded via local UMD (./transformers.min.js)';
|
| 89 |
+
return;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
}
|
| 91 |
+
statusEl.textContent = '❌ Could not load Transformers.js (ESM or UMD).';
|
| 92 |
}
|
| 93 |
|
| 94 |
+
function showProgressUI(show, message) {
|
| 95 |
+
const wrap = document.getElementById("progress-wrap");
|
| 96 |
+
const txt = document.getElementById("progressText");
|
| 97 |
+
const bar = document.getElementById("loadProgress");
|
| 98 |
+
wrap.style.display = show ? "" : "none";
|
| 99 |
+
if (message) txt.textContent = message;
|
| 100 |
+
bar.value = 0; bar.max = 1;
|
| 101 |
+
}
|
| 102 |
|
| 103 |
+
function progressCallback(p) {
|
| 104 |
+
const txt = document.getElementById("progressText");
|
| 105 |
+
const bar = document.getElementById("loadProgress");
|
| 106 |
+
if (p.status) txt.textContent = p.status;
|
| 107 |
+
if (typeof p.loaded === "number" && typeof p.total === "number" && p.total > 0) {
|
| 108 |
+
bar.max = p.total; bar.value = p.loaded;
|
| 109 |
+
const pct = Math.round((p.loaded / p.total) * 100);
|
| 110 |
+
txt.textContent = `Downloading ${p.file || "model"}… ${pct}%`;
|
| 111 |
+
}
|
| 112 |
+
}
|
| 113 |
+
|
| 114 |
+
(async () => {
|
| 115 |
+
await loadTransformers();
|
| 116 |
+
const tf = window.transformers;
|
| 117 |
+
if (!(tf && tf.pipeline)) return;
|
| 118 |
|
| 119 |
const btn = document.getElementById("submit");
|
| 120 |
const out = document.getElementById("to");
|
| 121 |
const fromEl = document.getElementById("from");
|
| 122 |
const srcEl = document.getElementById("src_lang");
|
| 123 |
const tgtEl = document.getElementById("tgt_lang");
|
| 124 |
+
|
| 125 |
+
// env setup
|
| 126 |
+
const { env } = tf;
|
| 127 |
+
env.useBrowserCache = true;
|
| 128 |
+
env.allowLocalModels = false;
|
| 129 |
+
env.backends.onnx.wasm.numThreads = Math.max(4, Math.min(8, navigator.hardwareConcurrency || 4));
|
| 130 |
|
| 131 |
btn.disabled = false;
|
| 132 |
btn.value = "Translate";
|
| 133 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
let translator = null;
|
| 135 |
async function getTranslator() {
|
| 136 |
if (translator) return translator;
|
| 137 |
showProgressUI(true, "Loading model…");
|
| 138 |
+
translator = await tf.pipeline("translation", "Xenova/nllb-200-distilled-600M", {
|
| 139 |
+
device: (navigator.gpu ? "webgpu" : "wasm"),
|
| 140 |
+
progress_callback: progressCallback
|
| 141 |
+
});
|
| 142 |
+
document.getElementById("progressText").textContent = "✅ Model ready";
|
| 143 |
+
const bar = document.getElementById("loadProgress"); bar.max = 1; bar.value = 1;
|
| 144 |
+
return translator;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
}
|
| 146 |
|
| 147 |
btn.addEventListener("click", async () => {
|
|
|
|
| 154 |
const res = await t(text, { src_lang: srcEl.value, tgt_lang: tgtEl.value, max_length: 128 });
|
| 155 |
out.value = res?.[0]?.translation_text || "(no output)";
|
| 156 |
} catch (e) {
|
| 157 |
+
console.error(e);
|
| 158 |
out.value = "❌ Translation failed. See console.";
|
| 159 |
} finally {
|
| 160 |
btn.disabled = false; btn.value = prev;
|
| 161 |
}
|
| 162 |
});
|
| 163 |
+
})();
|
| 164 |
</script>
|
| 165 |
</body>
|
| 166 |
</html>
|