File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1717// Inline worker script as a Blob to avoid extra files/paths.
1818// Loads transformers.js from CDN and caches the feature-extraction pipeline.
1919const 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) {
Original file line number Diff line number Diff line change 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 ) ;
You can’t perform that action at this time.
0 commit comments