@huggingface/transformers 3.4.2 → 3.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/README.md +2 -2
  2. package/dist/ort-wasm-simd-threaded.jsep.mjs +105 -124
  3. package/dist/ort-wasm-simd-threaded.jsep.wasm +0 -0
  4. package/dist/transformers.js +1374 -1462
  5. package/dist/transformers.js.map +1 -1
  6. package/dist/transformers.min.js +1 -1
  7. package/dist/transformers.min.js.map +1 -1
  8. package/dist/transformers.node.cjs +177 -67
  9. package/dist/transformers.node.cjs.map +1 -1
  10. package/dist/transformers.node.min.cjs +1 -1
  11. package/dist/transformers.node.min.cjs.map +1 -1
  12. package/dist/transformers.node.min.mjs +1 -1
  13. package/dist/transformers.node.min.mjs.map +1 -1
  14. package/dist/transformers.node.mjs +177 -67
  15. package/dist/transformers.node.mjs.map +1 -1
  16. package/dist/transformers.web.js +177 -67
  17. package/dist/transformers.web.js.map +1 -1
  18. package/dist/transformers.web.min.js +1 -1
  19. package/dist/transformers.web.min.js.map +1 -1
  20. package/package.json +6 -6
  21. package/src/configs.js +6 -0
  22. package/src/env.js +3 -2
  23. package/src/generation/logits_process.js +4 -2
  24. package/src/models/audio_spectrogram_transformer/feature_extraction_audio_spectrogram_transformer.js +1 -6
  25. package/src/models/seamless_m4t/feature_extraction_seamless_m4t.js +1 -6
  26. package/src/models/wespeaker/feature_extraction_wespeaker.js +1 -6
  27. package/src/models.js +26 -13
  28. package/src/tokenizers.js +23 -4
  29. package/src/utils/audio.js +11 -2
  30. package/src/utils/data-structures.js +53 -0
  31. package/src/utils/hub.js +30 -22
  32. package/types/configs.d.ts +8 -0
  33. package/types/configs.d.ts.map +1 -1
  34. package/types/env.d.ts +4 -2
  35. package/types/env.d.ts.map +1 -1
  36. package/types/generation/logits_process.d.ts.map +1 -1
  37. package/types/models/audio_spectrogram_transformer/feature_extraction_audio_spectrogram_transformer.d.ts.map +1 -1
  38. package/types/models/seamless_m4t/feature_extraction_seamless_m4t.d.ts.map +1 -1
  39. package/types/models/wespeaker/feature_extraction_wespeaker.d.ts.map +1 -1
  40. package/types/models.d.ts.map +1 -1
  41. package/types/tokenizers.d.ts.map +1 -1
  42. package/types/utils/audio.d.ts +2 -1
  43. package/types/utils/audio.d.ts.map +1 -1
  44. package/types/utils/data-structures.d.ts +32 -0
  45. package/types/utils/data-structures.d.ts.map +1 -1
  46. package/types/utils/hub.d.ts.map +1 -1
  47. package/types/utils/tensor.d.ts +1 -1
@@ -168,6 +168,7 @@ var TOKEN_TYPES = Object.freeze({
168
168
  Is: "Is",
169
169
  NotIn: "NotIn",
170
170
  Else: "Else",
171
+ EndSet: "EndSet",
171
172
  EndIf: "EndIf",
172
173
  ElseIf: "ElseIf",
173
174
  EndFor: "EndFor",
@@ -184,6 +185,7 @@ var KEYWORDS = Object.freeze({
184
185
  is: TOKEN_TYPES.Is,
185
186
  if: TOKEN_TYPES.If,
186
187
  else: TOKEN_TYPES.Else,
188
+ endset: TOKEN_TYPES.EndSet,
187
189
  endif: TOKEN_TYPES.EndIf,
188
190
  elif: TOKEN_TYPES.ElseIf,
189
191
  endfor: TOKEN_TYPES.EndFor,
@@ -420,10 +422,11 @@ var For = class extends Statement {
420
422
  type = "For";
421
423
  };
422
424
  var SetStatement = class extends Statement {
423
- constructor(assignee, value) {
425
+ constructor(assignee, value, body) {
424
426
  super();
425
427
  this.assignee = assignee;
426
428
  this.value = value;
429
+ this.body = body;
427
430
  }
428
431
  type = "Set";
429
432
  };
@@ -631,10 +634,19 @@ function parse(tokens) {
631
634
  const left = parseExpression();
632
635
  if (is(TOKEN_TYPES.Equals)) {
633
636
  ++current;
634
- const value = parseSetStatement();
635
- return new SetStatement(left, value);
637
+ const value = parseExpression();
638
+ return new SetStatement(left, value, []);
639
+ } else {
640
+ const body = [];
641
+ expect(TOKEN_TYPES.CloseStatement, "Expected %} token");
642
+ while (!(tokens[current]?.type === TOKEN_TYPES.OpenStatement && tokens[current + 1]?.type === TOKEN_TYPES.EndSet)) {
643
+ const another = parseAny();
644
+ body.push(another);
645
+ }
646
+ expect(TOKEN_TYPES.OpenStatement, "Expected {% token");
647
+ expect(TOKEN_TYPES.EndSet, "Expected endset token");
648
+ return new SetStatement(left, null, body);
636
649
  }
637
- return left;
638
650
  }
639
651
  function parseIfStatement() {
640
652
  const test = parseExpression();
@@ -1441,6 +1453,8 @@ var Interpreter = class {
1441
1453
  );
1442
1454
  case "join":
1443
1455
  return new StringValue(operand.value.map((x) => x.value).join(""));
1456
+ case "string":
1457
+ return new StringValue(toJSON(operand));
1444
1458
  default:
1445
1459
  throw new Error(`Unknown ArrayValue filter: ${filter.value}`);
1446
1460
  }
@@ -1708,7 +1722,7 @@ var Interpreter = class {
1708
1722
  return value instanceof RuntimeValue ? value : new UndefinedValue();
1709
1723
  }
1710
1724
  evaluateSet(node, environment) {
1711
- const rhs = this.evaluate(node.value, environment);
1725
+ const rhs = node.value ? this.evaluate(node.value, environment) : this.evaluateBlock(node.body, environment);
1712
1726
  if (node.assignee.type === "Identifier") {
1713
1727
  const variableName = node.assignee.value;
1714
1728
  environment.setVariable(variableName, rhs);
@@ -4029,6 +4043,7 @@ class AutoConfig {
4029
4043
  /**
4030
4044
  * Transformers.js-specific configuration, possibly present in config.json under the key `transformers.js_config`.
4031
4045
  * @typedef {Object} TransformersJSConfig
4046
+ * @property {Record<import('./utils/devices.js').DeviceType, DeviceConfig>} [device_config] Device-specific configurations.
4032
4047
  * @property {import('./utils/tensor.js').DataType|Record<import('./utils/dtypes.js').DataType, import('./utils/tensor.js').DataType>} [kv_cache_dtype] The data type of the key-value cache.
4033
4048
  * @property {Record<string, number>} [free_dimension_overrides] Override the free dimensions of the model.
4034
4049
  * See https://onnxruntime.ai/docs/tutorials/web/env-flags-and-session-options.html#freedimensionoverrides
@@ -4038,6 +4053,11 @@ class AutoConfig {
4038
4053
  * @property {import('./utils/hub.js').ExternalData|Record<string, import('./utils/hub.js').ExternalData>} [use_external_data_format=false] Whether to load the model using the external data format (used for models >= 2GB in size).
4039
4054
  */
4040
4055
 
4056
+ /**
4057
+ * Device-specific configuration options.
4058
+ * @typedef {Omit<TransformersJSConfig, "device" | "device_config">} DeviceConfig
4059
+ */
4060
+
4041
4061
 
4042
4062
  /***/ }),
4043
4063
 
@@ -4083,7 +4103,7 @@ __webpack_require__.r(__webpack_exports__);
4083
4103
 
4084
4104
 
4085
4105
 
4086
- const VERSION = '3.4.2';
4106
+ const VERSION = '3.5.0';
4087
4107
 
4088
4108
  // Check if various APIs are available (depends on environment)
4089
4109
  const IS_BROWSER_ENV = typeof window !== "undefined" && typeof window.document !== "undefined";
@@ -4175,7 +4195,8 @@ const localModelPath = RUNNING_LOCALLY
4175
4195
  * @property {string} cacheDir The directory to use for caching files with the file system. By default, it is `./.cache`.
4176
4196
  * @property {boolean} useCustomCache Whether to use a custom cache system (defined by `customCache`), defaults to `false`.
4177
4197
  * @property {Object} customCache The custom cache to use. Defaults to `null`. Note: this must be an object which
4178
- * implements the `match` and `put` functions of the Web Cache API. For more information, see https://developer.mozilla.org/en-US/docs/Web/API/Cache
4198
+ * implements the `match` and `put` functions of the Web Cache API. For more information, see https://developer.mozilla.org/en-US/docs/Web/API/Cache.
4199
+ * If you wish, you may also return a `Promise<string>` from the `match` function if you'd like to use a file path instead of `Promise<Response>`.
4179
4200
  */
4180
4201
 
4181
4202
  /** @type {TransformersEnvironment} */
@@ -5202,13 +5223,15 @@ class NoBadWordsLogitsProcessor extends LogitsProcessor {
5202
5223
  const batch_logits_data = /** @type {Float32Array} */(logits[i].data);
5203
5224
  const ids = input_ids[i];
5204
5225
  for (const bad_word_ids of this.bad_words_ids) {
5226
+ // There aren't enough tokens to match the banned sequence
5227
+ if (ids.length < bad_word_ids.length - 1) continue;
5228
+
5205
5229
  // Whether to modify the logits of the last token in the bad word id sequence
5206
5230
  let mark = true;
5207
5231
 
5208
5232
  // For each bad word in the list, if the current sequence of input ids ends with this sequence (excluding the last),
5209
5233
  // then we set the logits of the last bad word id to -Infinity.
5210
- for (let j = 1; j <= bad_word_ids.length - 1 && bad_word_ids.length < ids.length; ++j) {
5211
-
5234
+ for (let j = 1; j <= bad_word_ids.length - 1; ++j) {
5212
5235
  // NOTE: We use != instead of !== to compare bigint and number
5213
5236
  // @ts-ignore
5214
5237
  if (bad_word_ids.at(-j - 1) != ids.at(-j)) {
@@ -6680,7 +6703,8 @@ const MODEL_CLASS_TO_NAME_MAPPING = new Map();
6680
6703
  * @private
6681
6704
  */
6682
6705
  async function getSession(pretrained_model_name_or_path, fileName, options) {
6683
- const custom_config = options.config?.['transformers.js_config'] ?? {};
6706
+ let custom_config = options.config?.['transformers.js_config'] ?? {};
6707
+
6684
6708
  let device = options.device ?? custom_config.device;
6685
6709
  if (device && typeof device !== 'string') {
6686
6710
  if (device.hasOwnProperty(fileName)) {
@@ -6695,8 +6719,18 @@ async function getSession(pretrained_model_name_or_path, fileName, options) {
6695
6719
  const selectedDevice = /** @type {import("./utils/devices.js").DeviceType} */(
6696
6720
  device ?? (_env_js__WEBPACK_IMPORTED_MODULE_14__.apis.IS_NODE_ENV ? 'cpu' : 'wasm')
6697
6721
  );
6722
+
6698
6723
  const executionProviders = (0,_backends_onnx_js__WEBPACK_IMPORTED_MODULE_1__.deviceToExecutionProviders)(selectedDevice);
6699
6724
 
6725
+ // Update custom config with the selected device's config, if it exists
6726
+ const device_config = custom_config.device_config ?? {};
6727
+ if (device_config.hasOwnProperty(selectedDevice)) {
6728
+ custom_config = {
6729
+ ...custom_config,
6730
+ ...device_config[selectedDevice],
6731
+ };
6732
+ }
6733
+
6700
6734
  // If options.dtype is specified, we use it to choose the suffix for the model file.
6701
6735
  // Otherwise, we use the default dtype for the device.
6702
6736
  let dtype = options.dtype ?? custom_config.dtype;
@@ -6713,11 +6747,11 @@ async function getSession(pretrained_model_name_or_path, fileName, options) {
6713
6747
  // Try to choose the auto dtype based on the custom config
6714
6748
  let config_dtype = custom_config.dtype;
6715
6749
  if (typeof config_dtype !== 'string') {
6716
- config_dtype = config_dtype[fileName];
6750
+ config_dtype = config_dtype?.[fileName];
6717
6751
  }
6718
6752
 
6719
6753
  if (config_dtype && config_dtype !== _utils_dtypes_js__WEBPACK_IMPORTED_MODULE_2__.DATA_TYPES.auto && _utils_dtypes_js__WEBPACK_IMPORTED_MODULE_2__.DATA_TYPES.hasOwnProperty(config_dtype)) {
6720
- // Defined by the custom config, and is not "auto"
6754
+ // Defined by the config, and is not "auto"
6721
6755
  dtype = config_dtype;
6722
6756
  } else {
6723
6757
  // Choose default dtype based on device, falling back to fp32
@@ -6734,10 +6768,11 @@ async function getSession(pretrained_model_name_or_path, fileName, options) {
6734
6768
  }
6735
6769
 
6736
6770
  // Only valid for models with a decoder
6737
- const kv_cache_dtype = custom_config.kv_cache_dtype
6738
- ? (typeof custom_config.kv_cache_dtype === 'string'
6739
- ? custom_config.kv_cache_dtype
6740
- : custom_config.kv_cache_dtype[selectedDtype] ?? 'float32')
6771
+ const kv_cache_dtype_config = custom_config.kv_cache_dtype;
6772
+ const kv_cache_dtype = kv_cache_dtype_config
6773
+ ? (typeof kv_cache_dtype_config === 'string'
6774
+ ? kv_cache_dtype_config
6775
+ : kv_cache_dtype_config[selectedDtype] ?? 'float32')
6741
6776
  : undefined;
6742
6777
 
6743
6778
  if (kv_cache_dtype && !['float32', 'float16'].includes(kv_cache_dtype)) {
@@ -6765,14 +6800,15 @@ async function getSession(pretrained_model_name_or_path, fileName, options) {
6765
6800
  session_options.freeDimensionOverrides ??= free_dimension_overrides;
6766
6801
  } else if (selectedDevice.startsWith('webnn') && !session_options.freeDimensionOverrides) {
6767
6802
  console.warn(
6768
- 'WebNN does not currently support dynamic shapes and requires `free_dimension_overrides` to be set in config.json as a field within "transformers.js_config". ' +
6769
- 'When `free_dimension_overrides` is not set, you may experience significant performance degradation.'
6803
+ `WebNN does not currently support dynamic shapes and requires 'free_dimension_overrides' to be set in config.json, preferably as a field within config["transformers.js_config"]["device_config"]["${selectedDevice}"]. ` +
6804
+ `When 'free_dimension_overrides' is not set, you may experience significant performance degradation.`
6770
6805
  );
6771
6806
  }
6772
6807
 
6773
- const bufferOrPathPromise = (0,_utils_hub_js__WEBPACK_IMPORTED_MODULE_5__.getModelFile)(pretrained_model_name_or_path, modelFileName, true, options, _env_js__WEBPACK_IMPORTED_MODULE_14__.apis.IS_NODE_ENV);
6808
+ const return_path = _env_js__WEBPACK_IMPORTED_MODULE_14__.apis.IS_NODE_ENV && _env_js__WEBPACK_IMPORTED_MODULE_14__.env.useFSCache;
6809
+ const bufferOrPathPromise = (0,_utils_hub_js__WEBPACK_IMPORTED_MODULE_5__.getModelFile)(pretrained_model_name_or_path, modelFileName, true, options, return_path);
6774
6810
 
6775
- // handle onnx external data files
6811
+ // Handle onnx external data files
6776
6812
  const use_external_data_format = options.use_external_data_format ?? custom_config.use_external_data_format;
6777
6813
  /** @type {Promise<string|{path: string, data: Uint8Array}>[]} */
6778
6814
  let externalDataPromises = [];
@@ -6798,7 +6834,7 @@ async function getSession(pretrained_model_name_or_path, fileName, options) {
6798
6834
  const path = `${baseName}_data${i === 0 ? '' : '_' + i}`;
6799
6835
  const fullPath = `${options.subfolder ?? ''}/${path}`;
6800
6836
  externalDataPromises.push(new Promise(async (resolve, reject) => {
6801
- const data = await (0,_utils_hub_js__WEBPACK_IMPORTED_MODULE_5__.getModelFile)(pretrained_model_name_or_path, fullPath, true, options, _env_js__WEBPACK_IMPORTED_MODULE_14__.apis.IS_NODE_ENV);
6837
+ const data = await (0,_utils_hub_js__WEBPACK_IMPORTED_MODULE_5__.getModelFile)(pretrained_model_name_or_path, fullPath, true, options, return_path);
6802
6838
  resolve(data instanceof Uint8Array ? { path, data } : path);
6803
6839
  }));
6804
6840
  }
@@ -14977,7 +15013,7 @@ class ASTFeatureExtractor extends _base_feature_extraction_utils_js__WEBPACK_IMP
14977
15013
 
14978
15014
  const sampling_rate = this.config.sampling_rate;
14979
15015
  const mel_filters = (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_2__.mel_filter_bank)(
14980
- 256, // num_frequency_bins
15016
+ 257, // num_frequency_bins
14981
15017
  this.config.num_mel_bins, // num_mel_filters
14982
15018
  20, // min_frequency
14983
15019
  Math.floor(sampling_rate / 2), // max_frequency
@@ -14986,11 +15022,6 @@ class ASTFeatureExtractor extends _base_feature_extraction_utils_js__WEBPACK_IMP
14986
15022
  "kaldi", // mel_scale
14987
15023
  true, // triangularize_in_mel_space
14988
15024
  );
14989
-
14990
- // Do padding:
14991
- for (let i = 0; i < mel_filters.length; ++i) {
14992
- mel_filters[i].push(0);
14993
- }
14994
15025
  this.mel_filters = mel_filters;
14995
15026
 
14996
15027
  this.window = (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_2__.window_function)(400, 'hann', {
@@ -18580,7 +18611,7 @@ class SeamlessM4TFeatureExtractor extends _base_feature_extraction_utils_js__WEB
18580
18611
 
18581
18612
  const sampling_rate = this.config.sampling_rate;
18582
18613
  const mel_filters = (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_2__.mel_filter_bank)(
18583
- 256, // num_frequency_bins
18614
+ 257, // num_frequency_bins
18584
18615
  this.config.num_mel_bins, // num_mel_filters
18585
18616
  20, // min_frequency
18586
18617
  Math.floor(sampling_rate / 2), // max_frequency
@@ -18589,11 +18620,6 @@ class SeamlessM4TFeatureExtractor extends _base_feature_extraction_utils_js__WEB
18589
18620
  "kaldi", // mel_scale
18590
18621
  true, // triangularize_in_mel_space
18591
18622
  );
18592
-
18593
- // Do padding:
18594
- for (let i = 0; i < mel_filters.length; ++i) {
18595
- mel_filters[i].push(0);
18596
- }
18597
18623
  this.mel_filters = mel_filters;
18598
18624
 
18599
18625
  this.window = (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_2__.window_function)(400, 'povey', {
@@ -19346,7 +19372,7 @@ class WeSpeakerFeatureExtractor extends _base_feature_extraction_utils_js__WEBPA
19346
19372
 
19347
19373
  const sampling_rate = this.config.sampling_rate;
19348
19374
  const mel_filters = (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_2__.mel_filter_bank)(
19349
- 256, // num_frequency_bins
19375
+ 257, // num_frequency_bins
19350
19376
  this.config.num_mel_bins, // num_mel_filters
19351
19377
  20, // min_frequency
19352
19378
  Math.floor(sampling_rate / 2), // max_frequency
@@ -19355,11 +19381,6 @@ class WeSpeakerFeatureExtractor extends _base_feature_extraction_utils_js__WEBPA
19355
19381
  "kaldi", // mel_scale
19356
19382
  true, // triangularize_in_mel_space
19357
19383
  );
19358
-
19359
- // Do padding:
19360
- for (let i = 0; i < mel_filters.length; ++i) {
19361
- mel_filters[i].push(0);
19362
- }
19363
19384
  this.mel_filters = mel_filters;
19364
19385
 
19365
19386
  this.window = (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_2__.window_function)(400, 'hamming', {
@@ -24346,8 +24367,24 @@ class BPE extends TokenizerModel {
24346
24367
 
24347
24368
  this.ignore_merges = this.config.ignore_merges ?? false;
24348
24369
 
24349
- /** @type {Map<string, string[]>} */
24350
- this.cache = new Map();
24370
+ /**
24371
+ * The maximum length we should cache in a model.
24372
+ * Strings that are too long have minimal chances to cache hit anyway
24373
+ */
24374
+ this.max_length_to_cache = 256;
24375
+
24376
+ /**
24377
+ * The default capacity for a `BPE`'s internal cache.
24378
+ */
24379
+ this.cache_capacity = 10000;
24380
+ this.cache = new _utils_data_structures_js__WEBPACK_IMPORTED_MODULE_5__.LRUCache(this.cache_capacity);
24381
+ }
24382
+
24383
+ /**
24384
+ * Clears the cache.
24385
+ */
24386
+ clear_cache() {
24387
+ this.cache.clear();
24351
24388
  }
24352
24389
 
24353
24390
  /**
@@ -24474,8 +24511,10 @@ class BPE extends TokenizerModel {
24474
24511
  }
24475
24512
  }
24476
24513
 
24477
- // Save the result to the cache
24478
- this.cache.set(token, result);
24514
+ if (token.length < this.max_length_to_cache) {
24515
+ // Save the result to the cache
24516
+ this.cache.put(token, result);
24517
+ }
24479
24518
 
24480
24519
  return result;
24481
24520
  }
@@ -28275,7 +28314,8 @@ function linspace(start, end, num) {
28275
28314
  * various implementation exist, which differ in the number of filters, the shape of the filters, the way the filters
28276
28315
  * are spaced, the bandwidth of the filters, and the manner in which the spectrum is warped. The goal of these
28277
28316
  * features is to approximate the non-linear human perception of the variation in pitch with respect to the frequency.
28278
- * @param {number} num_frequency_bins Number of frequencies used to compute the spectrogram (should be the same as in `stft`).
28317
+ * @param {number} num_frequency_bins Number of frequency bins (should be the same as `n_fft // 2 + 1`
28318
+ * where `n_fft` is the size of the Fourier Transform used to compute the spectrogram).
28279
28319
  * @param {number} num_mel_filters Number of mel filters to generate.
28280
28320
  * @param {number} min_frequency Lowest frequency of interest in Hz.
28281
28321
  * @param {number} max_frequency Highest frequency of interest in Hz. This should not exceed `sampling_rate / 2`.
@@ -28301,6 +28341,14 @@ function mel_filter_bank(
28301
28341
  throw new Error('norm must be one of null or "slaney"');
28302
28342
  }
28303
28343
 
28344
+ if (num_frequency_bins < 2) {
28345
+ throw new Error(`Require num_frequency_bins: ${num_frequency_bins} >= 2`);
28346
+ }
28347
+
28348
+ if (min_frequency > max_frequency) {
28349
+ throw new Error(`Require min_frequency: ${min_frequency} <= max_frequency: ${max_frequency}`);
28350
+ }
28351
+
28304
28352
  const mel_min = hertz_to_mel(min_frequency, mel_scale);
28305
28353
  const mel_max = hertz_to_mel(max_frequency, mel_scale);
28306
28354
  const mel_freqs = linspace(mel_min, mel_max, num_mel_filters + 2);
@@ -28309,7 +28357,7 @@ function mel_filter_bank(
28309
28357
  let fft_freqs; // frequencies of FFT bins in Hz
28310
28358
 
28311
28359
  if (triangularize_in_mel_space) {
28312
- const fft_bin_width = sampling_rate / (num_frequency_bins * 2);
28360
+ const fft_bin_width = sampling_rate / ((num_frequency_bins - 1) * 2);
28313
28361
  fft_freqs = hertz_to_mel(Float64Array.from({ length: num_frequency_bins }, (_, i) => i * fft_bin_width), mel_scale);
28314
28362
  filter_freqs = mel_freqs;
28315
28363
  } else {
@@ -29186,6 +29234,7 @@ __webpack_require__.r(__webpack_exports__);
29186
29234
  /* harmony export */ __webpack_require__.d(__webpack_exports__, {
29187
29235
  /* harmony export */ CharTrie: () => (/* binding */ CharTrie),
29188
29236
  /* harmony export */ DictionarySplitter: () => (/* binding */ DictionarySplitter),
29237
+ /* harmony export */ LRUCache: () => (/* binding */ LRUCache),
29189
29238
  /* harmony export */ PriorityQueue: () => (/* binding */ PriorityQueue),
29190
29239
  /* harmony export */ TokenLattice: () => (/* binding */ TokenLattice)
29191
29240
  /* harmony export */ });
@@ -29711,6 +29760,59 @@ class DictionarySplitter {
29711
29760
  }
29712
29761
  }
29713
29762
 
29763
+ /**
29764
+ * A simple Least Recently Used (LRU) cache implementation in JavaScript.
29765
+ * This cache stores key-value pairs and evicts the least recently used item
29766
+ * when the capacity is exceeded.
29767
+ */
29768
+ class LRUCache {
29769
+ /**
29770
+ * Creates an LRUCache instance.
29771
+ * @param {number} capacity The maximum number of items the cache can hold.
29772
+ */
29773
+ constructor(capacity) {
29774
+ this.capacity = capacity;
29775
+ this.cache = new Map();
29776
+ }
29777
+
29778
+ /**
29779
+ * Retrieves the value associated with the given key and marks the key as recently used.
29780
+ * @param {any} key The key to retrieve.
29781
+ * @returns {any} The value associated with the key, or undefined if the key does not exist.
29782
+ */
29783
+ get(key) {
29784
+ if (!this.cache.has(key)) return undefined;
29785
+ const value = this.cache.get(key);
29786
+ this.cache.delete(key);
29787
+ this.cache.set(key, value);
29788
+ return value;
29789
+ }
29790
+
29791
+ /**
29792
+ * Inserts or updates the key-value pair in the cache.
29793
+ * If the key already exists, it is updated and marked as recently used.
29794
+ * If the cache exceeds its capacity, the least recently used item is evicted.
29795
+ * @param {any} key The key to add or update.
29796
+ * @param {any} value The value to associate with the key.
29797
+ */
29798
+ put(key, value) {
29799
+ if (this.cache.has(key)) {
29800
+ this.cache.delete(key);
29801
+ }
29802
+ this.cache.set(key, value);
29803
+ if (this.cache.size > this.capacity) {
29804
+ this.cache.delete(this.cache.keys().next().value);
29805
+ }
29806
+ }
29807
+
29808
+ /**
29809
+ * Clears the cache.
29810
+ */
29811
+ clear() {
29812
+ this.cache.clear();
29813
+ }
29814
+ }
29815
+
29714
29816
 
29715
29817
  /***/ }),
29716
29818
 
@@ -30318,6 +30420,22 @@ async function getModelFile(path_or_repo_id, filename, fatal = true, options = {
30318
30420
  // First, check if the a caching backend is available
30319
30421
  // If no caching mechanism available, will download the file every time
30320
30422
  let cache;
30423
+ if (!cache && _env_js__WEBPACK_IMPORTED_MODULE_2__.env.useCustomCache) {
30424
+ // Allow the user to specify a custom cache system.
30425
+ if (!_env_js__WEBPACK_IMPORTED_MODULE_2__.env.customCache) {
30426
+ throw Error('`env.useCustomCache=true`, but `env.customCache` is not defined.')
30427
+ }
30428
+
30429
+ // Check that the required methods are defined:
30430
+ if (!_env_js__WEBPACK_IMPORTED_MODULE_2__.env.customCache.match || !_env_js__WEBPACK_IMPORTED_MODULE_2__.env.customCache.put) {
30431
+ throw new Error(
30432
+ "`env.customCache` must be an object which implements the `match` and `put` functions of the Web Cache API. " +
30433
+ "For more information, see https://developer.mozilla.org/en-US/docs/Web/API/Cache"
30434
+ )
30435
+ }
30436
+ cache = _env_js__WEBPACK_IMPORTED_MODULE_2__.env.customCache;
30437
+ }
30438
+
30321
30439
  if (!cache && _env_js__WEBPACK_IMPORTED_MODULE_2__.env.useBrowserCache) {
30322
30440
  if (typeof caches === 'undefined') {
30323
30441
  throw Error('Browser cache is not available in this environment.')
@@ -30335,28 +30453,14 @@ async function getModelFile(path_or_repo_id, filename, fatal = true, options = {
30335
30453
  }
30336
30454
 
30337
30455
  if (!cache && _env_js__WEBPACK_IMPORTED_MODULE_2__.env.useFSCache) {
30338
- // TODO throw error if not available
30456
+ if (!_env_js__WEBPACK_IMPORTED_MODULE_2__.apis.IS_FS_AVAILABLE) {
30457
+ throw Error('File System Cache is not available in this environment.');
30458
+ }
30339
30459
 
30340
30460
  // If `cache_dir` is not specified, use the default cache directory
30341
30461
  cache = new FileCache(options.cache_dir ?? _env_js__WEBPACK_IMPORTED_MODULE_2__.env.cacheDir);
30342
30462
  }
30343
30463
 
30344
- if (!cache && _env_js__WEBPACK_IMPORTED_MODULE_2__.env.useCustomCache) {
30345
- // Allow the user to specify a custom cache system.
30346
- if (!_env_js__WEBPACK_IMPORTED_MODULE_2__.env.customCache) {
30347
- throw Error('`env.useCustomCache=true`, but `env.customCache` is not defined.')
30348
- }
30349
-
30350
- // Check that the required methods are defined:
30351
- if (!_env_js__WEBPACK_IMPORTED_MODULE_2__.env.customCache.match || !_env_js__WEBPACK_IMPORTED_MODULE_2__.env.customCache.put) {
30352
- throw new Error(
30353
- "`env.customCache` must be an object which implements the `match` and `put` functions of the Web Cache API. " +
30354
- "For more information, see https://developer.mozilla.org/en-US/docs/Web/API/Cache"
30355
- )
30356
- }
30357
- cache = _env_js__WEBPACK_IMPORTED_MODULE_2__.env.customCache;
30358
- }
30359
-
30360
30464
  const revision = options.revision ?? 'main';
30361
30465
  const requestURL = pathJoin(path_or_repo_id, filename);
30362
30466
 
@@ -30538,7 +30642,7 @@ async function getModelFile(path_or_repo_id, filename, fatal = true, options = {
30538
30642
  });
30539
30643
 
30540
30644
  if (result) {
30541
- if (return_path) {
30645
+ if (!_env_js__WEBPACK_IMPORTED_MODULE_2__.apis.IS_NODE_ENV && return_path) {
30542
30646
  throw new Error("Cannot return path in a browser environment.")
30543
30647
  }
30544
30648
  return result;
@@ -30547,12 +30651,18 @@ async function getModelFile(path_or_repo_id, filename, fatal = true, options = {
30547
30651
  return response.filePath;
30548
30652
  }
30549
30653
 
30550
- const path = await cache.match(cacheKey);
30551
- if (path instanceof FileResponse) {
30552
- return path.filePath;
30654
+ // Otherwise, return the cached response (most likely a `FileResponse`).
30655
+ // NOTE: A custom cache may return a Response, or a string (file path)
30656
+ const cachedResponse = await cache?.match(cacheKey);
30657
+ if (cachedResponse instanceof FileResponse) {
30658
+ return cachedResponse.filePath;
30659
+ } else if (cachedResponse instanceof Response) {
30660
+ return new Uint8Array(await cachedResponse.arrayBuffer());
30661
+ } else if (typeof cachedResponse === 'string') {
30662
+ return cachedResponse;
30553
30663
  }
30554
- throw new Error("Unable to return path for response.");
30555
30664
 
30665
+ throw new Error("Unable to get model file path or buffer.");
30556
30666
  }
30557
30667
 
30558
30668
  /**