Spaces:
Runtime error
Runtime error
Update index.html
Browse files- index.html +16 -25
index.html
CHANGED
|
@@ -108,40 +108,31 @@
|
|
| 108 |
<!-- Single module: loads transformers UMD safely, then runs the app -->
|
| 109 |
<script type="module">
|
| 110 |
// ---------- Safe vendor loader (fixes text/plain/CORS issues) ----------
|
| 111 |
-
async function
|
| 112 |
-
const path = "./assets/vendor/transformers-3.0.0.js"; // your vendored file
|
| 113 |
const res = await fetch(path);
|
| 114 |
if (!res.ok) throw new Error(`Failed to fetch ${path}: ${res.status}`);
|
| 115 |
const code = await res.text();
|
| 116 |
-
const blob = new Blob([code], { type: "
|
| 117 |
const url = URL.createObjectURL(blob);
|
| 118 |
try {
|
| 119 |
-
await
|
| 120 |
-
const s = document.createElement("script");
|
| 121 |
-
s.src = url;
|
| 122 |
-
s.onload = () => resolve();
|
| 123 |
-
s.onerror = (e) => reject(e);
|
| 124 |
-
// classic script so the UMD attaches to globalThis.transformers
|
| 125 |
-
s.type = "text/javascript";
|
| 126 |
-
document.head.appendChild(s);
|
| 127 |
-
});
|
| 128 |
} finally {
|
| 129 |
URL.revokeObjectURL(url);
|
| 130 |
}
|
| 131 |
-
const T = globalThis.transformers;
|
| 132 |
-
if (!T) throw new Error("transformers UMD not found on window");
|
| 133 |
-
// Expose a stable handle for the rest of the file
|
| 134 |
-
window.HF = { env: T.env, AutoTokenizer: T.AutoTokenizer, AutoModelForCausalLM: T.AutoModelForCausalLM };
|
| 135 |
}
|
| 136 |
-
|
| 137 |
-
//
|
| 138 |
-
await
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
env.useBrowserCache = true;
|
| 146 |
env.backends.onnx.webgpu = { enabled: false };
|
| 147 |
env.backends.onnx.preferredBackend = "wasm";
|
|
|
|
| 108 |
<!-- Single module: loads transformers UMD safely, then runs the app -->
|
| 109 |
<script type="module">
|
| 110 |
// ---------- Safe vendor loader (fixes text/plain/CORS issues) ----------
|
| 111 |
+
async function importVendoredModule(path) {
|
|
|
|
| 112 |
const res = await fetch(path);
|
| 113 |
if (!res.ok) throw new Error(`Failed to fetch ${path}: ${res.status}`);
|
| 114 |
const code = await res.text();
|
| 115 |
+
const blob = new Blob([code], { type: "text/javascript" }); // ESM ok
|
| 116 |
const url = URL.createObjectURL(blob);
|
| 117 |
try {
|
| 118 |
+
return await import(url); // <-- dynamic import of ESM
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
} finally {
|
| 120 |
URL.revokeObjectURL(url);
|
| 121 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
}
|
| 123 |
+
|
| 124 |
+
// Use your local vendored file
|
| 125 |
+
const T = await importVendoredModule("./assets/vendor/transformers-3.0.0.js");
|
| 126 |
+
|
| 127 |
+
// Pull named exports from the ESM module
|
| 128 |
+
const { env, AutoTokenizer, AutoModelForCausalLM } = T;
|
| 129 |
+
|
| 130 |
+
// (Optional) expose for other inline modules that expect window.HF
|
| 131 |
+
window.HF = { env, AutoTokenizer, AutoModelForCausalLM };
|
| 132 |
+
|
| 133 |
+
// ----- Backend (no cross-origin isolation) -----
|
| 134 |
+
env.localModelPath = null;
|
| 135 |
+
env.allowRemoteModels = false; // toggle to true only when using distilgpt2
|
| 136 |
env.useBrowserCache = true;
|
| 137 |
env.backends.onnx.webgpu = { enabled: false };
|
| 138 |
env.backends.onnx.preferredBackend = "wasm";
|