Spaces:
Running
Running
fix transformers js issue
Browse files
app.py
CHANGED
@@ -1186,16 +1186,49 @@ def build_transformers_inline_html(files: dict) -> str:
|
|
1186 |
css = files.get('style.css') or ''
|
1187 |
|
1188 |
# Normalize JS imports to CDN (handle both @huggingface/transformers and legacy @xenova/transformers)
|
1189 |
-
cdn_url = "https://cdn.jsdelivr.net/npm/@huggingface/[email protected].
|
1190 |
-
|
1191 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1192 |
|
1193 |
# Prepend a small prelude to reduce persistent caching during preview
|
|
|
1194 |
# Note: importing env alongside user's own imports is fine in ESM
|
1195 |
if js.strip():
|
1196 |
prelude = (
|
1197 |
f"import {{ env }} from '{cdn_url}';\n"
|
1198 |
"try { env.useBrowserCache = false; } catch (e) {}\n"
|
|
|
|
|
1199 |
)
|
1200 |
js = prelude + js
|
1201 |
|
@@ -1233,6 +1266,26 @@ def build_transformers_inline_html(files: dict) -> str:
|
|
1233 |
|
1234 |
# Inline JS: insert before </body>
|
1235 |
script_tag = f"<script type=\"module\">\n{js}\n</script>" if js else ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1236 |
# Cleanup script to clear Cache Storage and IndexedDB on unload to free model weights
|
1237 |
cleanup_tag = (
|
1238 |
"<script>\n"
|
@@ -1250,10 +1303,10 @@ def build_transformers_inline_html(files: dict) -> str:
|
|
1250 |
match = _re.search(r"</body>", doc, flags=_re.IGNORECASE)
|
1251 |
if match:
|
1252 |
idx = match.start()
|
1253 |
-
doc = doc[:idx] + script_tag + cleanup_tag + doc[idx:]
|
1254 |
else:
|
1255 |
# Append at end
|
1256 |
-
doc = doc + script_tag + cleanup_tag
|
1257 |
|
1258 |
return doc
|
1259 |
|
|
|
1186 |
css = files.get('style.css') or ''
|
1187 |
|
1188 |
# Normalize JS imports to CDN (handle both @huggingface/transformers and legacy @xenova/transformers)
|
1189 |
+
cdn_url = "https://cdn.jsdelivr.net/npm/@huggingface/[email protected].2"
|
1190 |
+
|
1191 |
+
def _normalize_imports(_code: str) -> str:
|
1192 |
+
if not _code:
|
1193 |
+
return _code or ""
|
1194 |
+
_code = _re.sub(r"from\s+['\"]@huggingface/transformers['\"]", f"from '{cdn_url}'", _code)
|
1195 |
+
_code = _re.sub(r"from\s+['\"]@xenova/transformers['\"]", f"from '{cdn_url}'", _code)
|
1196 |
+
_code = _re.sub(r"from\s+['\"]https://cdn.jsdelivr.net/npm/@huggingface/transformers@[^'\"]+['\"]", f"from '{cdn_url}'", _code)
|
1197 |
+
_code = _re.sub(r"from\s+['\"]https://cdn.jsdelivr.net/npm/@xenova/transformers@[^'\"]+['\"]", f"from '{cdn_url}'", _code)
|
1198 |
+
return _code
|
1199 |
+
|
1200 |
+
# Extract inline module scripts from index.html, then merge into JS so we control imports
|
1201 |
+
inline_modules = []
|
1202 |
+
try:
|
1203 |
+
for _m in _re.finditer(r"<script\\b[^>]*type=[\"\']module[\"\'][^>]*>([\s\S]*?)</script>", html, flags=_re.IGNORECASE):
|
1204 |
+
inline_modules.append(_m.group(1))
|
1205 |
+
if inline_modules:
|
1206 |
+
html = _re.sub(r"<script\\b[^>]*type=[\"\']module[\"\'][^>]*>[\s\S]*?</script>\\s*", "", html, flags=_re.IGNORECASE)
|
1207 |
+
# Normalize any external module script URLs that load transformers to a single CDN version (keep the tag)
|
1208 |
+
html = _re.sub(r"https://cdn\.jsdelivr\.net/npm/@huggingface/transformers@[^'\"<>\s]+", cdn_url, html)
|
1209 |
+
html = _re.sub(r"https://cdn\.jsdelivr\.net/npm/@xenova/transformers@[^'\"<>\s]+", cdn_url, html)
|
1210 |
+
except Exception:
|
1211 |
+
# Best-effort; continue
|
1212 |
+
pass
|
1213 |
+
|
1214 |
+
# Merge inline module code with provided index.js, then normalize imports
|
1215 |
+
combined_js_parts = []
|
1216 |
+
if inline_modules:
|
1217 |
+
combined_js_parts.append("\n\n".join(inline_modules))
|
1218 |
+
if js:
|
1219 |
+
combined_js_parts.append(js)
|
1220 |
+
js = "\n\n".join([p for p in combined_js_parts if (p and p.strip())])
|
1221 |
+
js = _normalize_imports(js)
|
1222 |
|
1223 |
# Prepend a small prelude to reduce persistent caching during preview
|
1224 |
+
# Also ensure a global `transformers` namespace exists for apps relying on it
|
1225 |
# Note: importing env alongside user's own imports is fine in ESM
|
1226 |
if js.strip():
|
1227 |
prelude = (
|
1228 |
f"import {{ env }} from '{cdn_url}';\n"
|
1229 |
"try { env.useBrowserCache = false; } catch (e) {}\n"
|
1230 |
+
"try { if (env && env.backends && env.backends.onnx && env.backends.onnx.wasm) { env.backends.onnx.wasm.numThreads = 1; env.backends.onnx.wasm.proxy = false; } } catch (e) {}\n"
|
1231 |
+
f"(async () => {{ try {{ if (typeof globalThis.transformers === 'undefined') {{ const m = await import('{cdn_url}'); globalThis.transformers = m; }} }} catch (e) {{}} }})();\n"
|
1232 |
)
|
1233 |
js = prelude + js
|
1234 |
|
|
|
1266 |
|
1267 |
# Inline JS: insert before </body>
|
1268 |
script_tag = f"<script type=\"module\">\n{js}\n</script>" if js else ""
|
1269 |
+
# Lightweight debug console overlay to surface runtime errors inside the iframe
|
1270 |
+
debug_overlay = (
|
1271 |
+
"<style>\n"
|
1272 |
+
"#anycoder-debug{position:fixed;left:0;right:0;bottom:0;max-height:45%;overflow:auto;"
|
1273 |
+
"background:rgba(0,0,0,.85);color:#9eff9e;padding:.5em;font:12px/1.4 monospace;z-index:2147483647;display:none}"
|
1274 |
+
"#anycoder-debug pre{margin:0;white-space:pre-wrap;word-break:break-word}"
|
1275 |
+
"</style>\n"
|
1276 |
+
"<div id=\"anycoder-debug\"></div>\n"
|
1277 |
+
"<script>\n"
|
1278 |
+
"(function(){\n"
|
1279 |
+
" const el = document.getElementById('anycoder-debug');\n"
|
1280 |
+
" function show(){ if(el && el.style.display!=='block'){ el.style.display='block'; } }\n"
|
1281 |
+
" function log(msg){ try{ show(); const pre=document.createElement('pre'); pre.textContent=msg; el.appendChild(pre);}catch(e){} }\n"
|
1282 |
+
" const origError = console.error.bind(console);\n"
|
1283 |
+
" console.error = function(){ origError.apply(console, arguments); try{ log('console.error: ' + Array.from(arguments).map(a=>{try{return (typeof a==='string')?a:JSON.stringify(a);}catch(e){return String(a);}}).join(' ')); }catch(e){} };\n"
|
1284 |
+
" window.addEventListener('error', e => { log('window.onerror: ' + (e && e.message ? e.message : 'Unknown error')); });\n"
|
1285 |
+
" window.addEventListener('unhandledrejection', e => { try{ const r=e && e.reason; log('unhandledrejection: ' + (r && (r.message || JSON.stringify(r)))); }catch(err){ log('unhandledrejection'); } });\n"
|
1286 |
+
"})();\n"
|
1287 |
+
"</script>"
|
1288 |
+
)
|
1289 |
# Cleanup script to clear Cache Storage and IndexedDB on unload to free model weights
|
1290 |
cleanup_tag = (
|
1291 |
"<script>\n"
|
|
|
1303 |
match = _re.search(r"</body>", doc, flags=_re.IGNORECASE)
|
1304 |
if match:
|
1305 |
idx = match.start()
|
1306 |
+
doc = doc[:idx] + debug_overlay + script_tag + cleanup_tag + doc[idx:]
|
1307 |
else:
|
1308 |
# Append at end
|
1309 |
+
doc = doc + debug_overlay + script_tag + cleanup_tag
|
1310 |
|
1311 |
return doc
|
1312 |
|