Update index.html
Browse files- index.html +181 -27
index.html
CHANGED
|
@@ -1,34 +1,188 @@
|
|
| 1 |
<!doctype html>
|
| 2 |
-
<html>
|
| 3 |
-
|
| 4 |
-
<
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
try {
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
| 21 |
} else {
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
// quick smoke test
|
| 25 |
-
if (!window.transformers?.pipeline) {
|
| 26 |
-
s.textContent = '⚠️ Library present but pipeline() missing.';
|
| 27 |
}
|
| 28 |
} catch (e) {
|
| 29 |
-
|
| 30 |
-
console.error(e);
|
| 31 |
}
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
</html>
|
|
|
|
| 1 |
<!doctype html>
|
| 2 |
+
<html lang="en">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="utf-8" />
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1"/>
|
| 6 |
+
<title>Transformers.js load test + NLLB demo</title>
|
| 7 |
+
<style>
|
| 8 |
+
body { font: 14px/1.5 system-ui, -apple-system, Segoe UI, Roboto, sans-serif; margin: 16px; }
|
| 9 |
+
label { display:block; margin: 8px 0 4px; }
|
| 10 |
+
textarea { width: 100%; min-height: 120px; }
|
| 11 |
+
select, input[type="submit"] { padding: 8px; font: inherit; }
|
| 12 |
+
#row { display:grid; grid-template-columns:1fr 1fr; gap:12px; }
|
| 13 |
+
#progress-wrap { margin:10px 0; display:none; }
|
| 14 |
+
.muted { opacity:.75 }
|
| 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="status" class="muted">Loading library…</div>
|
| 23 |
+
|
| 24 |
+
<div id="row" style="margin-top:12px">
|
| 25 |
+
<div>
|
| 26 |
+
<label for="src_lang">Source language</label>
|
| 27 |
+
<select id="src_lang">
|
| 28 |
+
<option value="eng_Latn" selected>English (eng_Latn)</option>
|
| 29 |
+
<option value="spa_Latn">Spanish (spa_Latn)</option>
|
| 30 |
+
<option value="fra_Latn">French (fra_Latn)</option>
|
| 31 |
+
<option value="hin_Deva">Hindi (hin_Deva)</option>
|
| 32 |
+
</select>
|
| 33 |
+
</div>
|
| 34 |
+
<div>
|
| 35 |
+
<label for="tgt_lang">Target language</label>
|
| 36 |
+
<select id="tgt_lang">
|
| 37 |
+
<option value="spa_Latn" selected>Spanish (spa_Latn)</option>
|
| 38 |
+
<option value="eng_Latn">English (eng_Latn)</option>
|
| 39 |
+
<option value="fra_Latn">French (fra_Latn)</option>
|
| 40 |
+
<option value="hin_Deva">Hindi (hin_Deva)</option>
|
| 41 |
+
</select>
|
| 42 |
+
</div>
|
| 43 |
+
</div>
|
| 44 |
+
|
| 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%;"></progress>
|
| 51 |
+
<div id="progressText" class="muted">Initializing…</div>
|
| 52 |
+
</div>
|
| 53 |
+
|
| 54 |
+
<input type="submit" id="submit" value="Loading..." disabled />
|
| 55 |
+
|
| 56 |
+
<label for="to" style="margin-top:12px">Output</label>
|
| 57 |
+
<textarea id="to" readonly>Output will be here...</textarea>
|
| 58 |
+
|
| 59 |
+
<hr/>
|
| 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 status = document.getElementById('status');
|
| 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 |
+
// 1) Try as ESM module
|
| 79 |
try {
|
| 80 |
+
// IMPORTANT: only attempt module import; no prior classic <script> tag present.
|
| 81 |
+
const mod = await import('./transformers.min.js');
|
| 82 |
+
if (mod && (typeof mod.pipeline === 'function')) {
|
| 83 |
+
window.transformers = mod; // expose for app code
|
| 84 |
+
status.textContent = '✅ Loaded Transformers.js via ESM import.';
|
| 85 |
+
log('Loaded via ESM', 'ok');
|
| 86 |
+
return;
|
| 87 |
} else {
|
| 88 |
+
log('ESM import succeeded but pipeline() missing.', 'warn');
|
| 89 |
+
// fall through to UMD
|
|
|
|
|
|
|
|
|
|
| 90 |
}
|
| 91 |
} catch (e) {
|
| 92 |
+
log('ESM import threw: ' + (e && e.message ? e.message : String(e)), 'err');
|
|
|
|
| 93 |
}
|
| 94 |
+
|
| 95 |
+
// 2) Try as UMD global by injecting classic script
|
| 96 |
+
await new Promise((resolve) => {
|
| 97 |
+
const s = document.createElement('script');
|
| 98 |
+
s.src = './transformers.min.js';
|
| 99 |
+
s.onload = () => resolve();
|
| 100 |
+
s.onerror = () => resolve();
|
| 101 |
+
document.head.appendChild(s);
|
| 102 |
+
});
|
| 103 |
+
|
| 104 |
+
if (window.transformers && typeof window.transformers.pipeline === 'function') {
|
| 105 |
+
status.textContent = '✅ Loaded Transformers.js via UMD global.';
|
| 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 |
+
loadTransformers().then(setupApp);
|
| 114 |
+
|
| 115 |
+
function setupApp() {
|
| 116 |
+
if (!(window.transformers && window.transformers.pipeline)) return;
|
| 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 |
+
const wrap = document.getElementById("progress-wrap");
|
| 124 |
+
const bar = document.getElementById("loadProgress");
|
| 125 |
+
const txt = document.getElementById("progressText");
|
| 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 |
+
try {
|
| 150 |
+
const { pipeline, env } = window.transformers;
|
| 151 |
+
env.useBrowserCache = true;
|
| 152 |
+
env.allowLocalModels = false;
|
| 153 |
+
env.backends.onnx.wasm.numThreads = Math.max(4, Math.min(8, navigator.hardwareConcurrency || 4));
|
| 154 |
+
|
| 155 |
+
translator = await pipeline("translation", "Xenova/nllb-200-distilled-600M", {
|
| 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 () => {
|
| 171 |
+
const prev = btn.value;
|
| 172 |
+
btn.disabled = true; btn.value = "Working…";
|
| 173 |
+
try {
|
| 174 |
+
const t = await getTranslator();
|
| 175 |
+
const text = (fromEl.value || "").trim();
|
| 176 |
+
if (!text) { out.value = "Please type some text."; return; }
|
| 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>
|