Skip to content

Commit c537cf6

Browse files
committed
vendor: include shim for transformers in web/demo/vendor and prefer local import in embedding worker
1 parent d1e0b66 commit c537cf6

2 files changed

Lines changed: 41 additions & 1 deletion

File tree

web/demo/js/embedding-service.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,22 @@
1717
// Inline worker script as a Blob to avoid extra files/paths.
1818
// Loads transformers.js from CDN and caches the feature-extraction pipeline.
1919
const WORKER_SOURCE = `
20-
self.importScripts('https://cdn.jsdelivr.net/npm/@xenova/transformers@2.15.0/dist/transformers.min.js');
20+
// Prefer a locally vendored transformers build (served under /demo/vendor/) to
21+
// avoid CDN/network issues in restricted environments. Fall back to the
22+
// CDN when the local file is not present or fails to load.
23+
try {
24+
self.importScripts('/demo/vendor/transformers.min.js');
25+
} catch (e) {
26+
try {
27+
self.importScripts('https://cdn.jsdelivr.net/npm/@xenova/transformers@2.15.0/dist/transformers.min.js');
28+
} catch (err) {
29+
// Propagate import failure; worker will post errors on attempts
30+
// to use the extractor.
31+
// Note: we avoid throwing here to ensure the worker can post a
32+
// meaningful error back to the main thread when used.
33+
console && console.error && console.error('Failed to import transformers:', err && err.message);
34+
}
35+
}
2136
let extractorPromise = null;
2237
async function getExtractor() {
2338
if (!extractorPromise) {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Minimal shim of transformers for offline demo tests.
2+
// Provides a `transformers.pipeline('feature-extraction', model)` API that
3+
// returns a fake extractor producing deterministic embeddings for testing.
4+
(function (global) {
5+
function fakeExtractor(model) {
6+
return async function (text, opts) {
7+
// produce a fixed-length embedding deterministically from text
8+
var seed = 0;
9+
for (var i = 0; i < text.length; i++) seed += text.charCodeAt(i);
10+
var len = 384;
11+
var arr = new Float32Array(len);
12+
for (var j = 0; j < len; j++) {
13+
arr[j] = ((seed + j * 997) % 1000) / 1000;
14+
}
15+
return { data: arr };
16+
};
17+
}
18+
19+
global.transformers = {
20+
pipeline: function (type, model) {
21+
if (type !== 'feature-extraction') throw new Error('Unsupported pipeline type in shim');
22+
return fakeExtractor(model);
23+
}
24+
};
25+
})(self);

0 commit comments

Comments
 (0)