Javedalam commited on
Commit
01723a1
·
verified ·
1 Parent(s): 94560fa

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +113 -78
index.html CHANGED
@@ -15,6 +15,8 @@
15
  #progress-wrap { margin: 10px 0; display:none; }
16
  #footer { opacity:.7; margin-top:12px; font-size:12px; }
17
  .muted { opacity:.8 }
 
 
18
  </style>
19
  </head>
20
  <body>
@@ -59,89 +61,122 @@
59
  Tip: first load downloads the model; later loads are much faster thanks to browser cache.
60
  </div>
61
 
62
- <!-- Use unpkg CDN (jsDelivr failed in your Space) -->
63
- <script src="https://unpkg.com/@xenova/transformers@3.0.0/dist/transformers.min.js"></script>
64
 
 
65
  <script>
66
- // Grab DOM elements
67
- const btn = document.getElementById("submit");
68
- const out = document.getElementById("to");
69
- const fromEl = document.getElementById("from");
70
- const srcEl = document.getElementById("src_lang");
71
- const tgtEl = document.getElementById("tgt_lang");
72
- const wrap = document.getElementById("progress-wrap");
73
- const bar = document.getElementById("loadProgress");
74
- const txt = document.getElementById("progressText");
75
-
76
- // Enable button right away
77
- btn.disabled = false;
78
- btn.value = "Translate";
79
-
80
- // Transformers.js UMD bundle
81
- const tf = window.transformers;
82
- if (!tf) {
83
- document.body.insertAdjacentHTML('beforeend',
84
- '<p style="color:#b00">❌ Failed to load Transformers.js. Check CDN/network.</p>');
85
- }
86
-
87
- let translator = null;
88
-
89
- function showProgressUI(show, message) {
90
- wrap.style.display = show ? "" : "none";
91
- if (message) txt.textContent = message;
92
- bar.value = 0; bar.max = 1;
93
- }
94
-
95
- function progressCallback(p) {
96
- if (p.status) txt.textContent = p.status;
97
- if (typeof p.loaded === "number" && typeof p.total === "number" && p.total > 0) {
98
- bar.max = p.total;
99
- bar.value = p.loaded;
100
- const pct = Math.round((p.loaded / p.total) * 100);
101
- txt.textContent = `Downloading ${p.file || "model"}… ${pct}%`;
102
  }
103
- }
104
-
105
- async function getTranslator() {
106
- if (translator) return translator;
107
-
108
- showProgressUI(true, "Loading model…");
109
- try {
110
- const { pipeline, env } = tf;
111
- env.useBrowserCache = true;
112
- env.allowLocalModels = false;
113
- env.backends.onnx.wasm.numThreads = Math.max(4, Math.min(8, navigator.hardwareConcurrency || 4));
114
-
115
- translator = await pipeline("translation", "Xenova/nllb-200-distilled-600M", {
116
- device: (navigator.gpu ? "webgpu" : "wasm"),
117
- progress_callback: progressCallback
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
118
  });
119
-
120
- txt.textContent = "✅ Model ready";
121
- bar.max = 1; bar.value = 1;
122
- return translator;
123
- } catch (e) {
124
- console.error(e);
125
- txt.textContent = "❌ Error loading model — see console.";
126
- throw e;
127
- }
128
- }
129
-
130
- btn.addEventListener("click", async () => {
131
- const prev = btn.value;
132
- btn.disabled = true; btn.value = "Working…";
133
- try {
134
- const t = await getTranslator();
135
- const text = (fromEl.value || "").trim();
136
- if (!text) { out.value = "Please type some text."; return; }
137
- const res = await t(text, { src_lang: srcEl.value, tgt_lang: tgtEl.value, max_length: 128 });
138
- out.value = res?.[0]?.translation_text || "(no output)";
139
- } catch (e) {
140
- out.value = "❌ Translation failed. See console.";
141
- } finally {
142
- btn.disabled = false; btn.value = prev;
143
  }
144
- });
145
  </script>
146
  </body>
147
  </html>
 
15
  #progress-wrap { margin: 10px 0; display:none; }
16
  #footer { opacity:.7; margin-top:12px; font-size:12px; }
17
  .muted { opacity:.8 }
18
+ .error { color:#b00 }
19
+ .ok { color:#0a0 }
20
  </style>
21
  </head>
22
  <body>
 
61
  Tip: first load downloads the model; later loads are much faster thanks to browser cache.
62
  </div>
63
 
64
+ <div id="libStatus" class="error"></div>
 
65
 
66
+ <!-- Loader that tries multiple CDNs, then local file -->
67
  <script>
68
+ (function loadTransformersUMD() {
69
+ const tries = [
70
+ "https://cdn.jsdelivr.net/npm/@xenova/transformers@3.0.0/dist/transformers.min.js",
71
+ "https://unpkg.com/@xenova/transformers@3.0.0/dist/transformers.min.js",
72
+ "https://huggingface.co/datasets/Xenova/transformers.js/resolve/main/3.0.0/transformers.min.js",
73
+ "./transformers.min.js" // <- upload this file as a final fallback
74
+ ];
75
+ const statusEl = document.getElementById("libStatus");
76
+
77
+ function tryNext(i) {
78
+ if (i >= tries.length) {
79
+ statusEl.textContent = "❌ Failed to load Transformers.js from all sources.";
80
+ return;
81
+ }
82
+ const src = tries[i];
83
+ const s = document.createElement("script");
84
+ s.src = src;
85
+ s.async = true;
86
+ s.onload = () => {
87
+ if (window.transformers) {
88
+ statusEl.className = "ok";
89
+ statusEl.textContent = "✅ Transformers.js loaded from: " + src;
90
+ initApp();
91
+ } else {
92
+ statusEl.textContent = "⚠️ Loaded but window.transformers missing: " + src;
93
+ tryNext(i + 1);
94
+ }
95
+ };
96
+ s.onerror = () => {
97
+ statusEl.textContent = "⚠️ Failed: " + src + " — trying next…";
98
+ tryNext(i + 1);
99
+ };
100
+ document.head.appendChild(s);
 
 
 
101
  }
102
+ tryNext(0);
103
+
104
+ // initApp runs only after the library is present
105
+ function initApp() {
106
+ const btn = document.getElementById("submit");
107
+ const out = document.getElementById("to");
108
+ const fromEl = document.getElementById("from");
109
+ const srcEl = document.getElementById("src_lang");
110
+ const tgtEl = document.getElementById("tgt_lang");
111
+ const wrap = document.getElementById("progress-wrap");
112
+ const bar = document.getElementById("loadProgress");
113
+ const txt = document.getElementById("progressText");
114
+
115
+ btn.disabled = false;
116
+ btn.value = "Translate";
117
+
118
+ const tf = window.transformers;
119
+ let translator = null;
120
+
121
+ function showProgressUI(show, message) {
122
+ wrap.style.display = show ? "" : "none";
123
+ if (message) txt.textContent = message;
124
+ bar.value = 0; bar.max = 1;
125
+ }
126
+
127
+ function progressCallback(p) {
128
+ if (p.status) txt.textContent = p.status;
129
+ if (typeof p.loaded === "number" && typeof p.total === "number" && p.total > 0) {
130
+ bar.max = p.total;
131
+ bar.value = p.loaded;
132
+ const pct = Math.round((p.loaded / p.total) * 100);
133
+ txt.textContent = `Downloading ${p.file || "model"}… ${pct}%`;
134
+ }
135
+ }
136
+
137
+ async function getTranslator() {
138
+ if (translator) return translator;
139
+
140
+ showProgressUI(true, "Loading model…");
141
+ try {
142
+ const { pipeline, env } = tf;
143
+ env.useBrowserCache = true;
144
+ env.allowLocalModels = false;
145
+ // If WASM fallback happens, more threads help a bit
146
+ env.backends.onnx.wasm.numThreads = Math.max(4, Math.min(8, navigator.hardwareConcurrency || 4));
147
+
148
+ translator = await pipeline("translation", "Xenova/nllb-200-distilled-600M", {
149
+ device: (navigator.gpu ? "webgpu" : "wasm"),
150
+ progress_callback: progressCallback
151
+ });
152
+
153
+ txt.textContent = "✅ Model ready";
154
+ bar.max = 1; bar.value = 1;
155
+ return translator;
156
+ } catch (e) {
157
+ console.error(e);
158
+ txt.textContent = "❌ Error loading model — see console.";
159
+ throw e;
160
+ }
161
+ }
162
+
163
+ btn.addEventListener("click", async () => {
164
+ const prev = btn.value;
165
+ btn.disabled = true; btn.value = "Working…";
166
+ try {
167
+ const t = await getTranslator();
168
+ const text = (fromEl.value || "").trim();
169
+ if (!text) { out.value = "Please type some text."; return; }
170
+ const res = await t(text, { src_lang: srcEl.value, tgt_lang: tgtEl.value, max_length: 128 });
171
+ out.value = res?.[0]?.translation_text || "(no output)";
172
+ } catch (e) {
173
+ out.value = "❌ Translation failed. See console.";
174
+ } finally {
175
+ btn.disabled = false; btn.value = prev;
176
+ }
177
  });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
178
  }
179
+ })();
180
  </script>
181
  </body>
182
  </html>