@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
@@ -154,6 +154,7 @@ var TOKEN_TYPES = Object.freeze({
154
154
  Is: "Is",
155
155
  NotIn: "NotIn",
156
156
  Else: "Else",
157
+ EndSet: "EndSet",
157
158
  EndIf: "EndIf",
158
159
  ElseIf: "ElseIf",
159
160
  EndFor: "EndFor",
@@ -170,6 +171,7 @@ var KEYWORDS = Object.freeze({
170
171
  is: TOKEN_TYPES.Is,
171
172
  if: TOKEN_TYPES.If,
172
173
  else: TOKEN_TYPES.Else,
174
+ endset: TOKEN_TYPES.EndSet,
173
175
  endif: TOKEN_TYPES.EndIf,
174
176
  elif: TOKEN_TYPES.ElseIf,
175
177
  endfor: TOKEN_TYPES.EndFor,
@@ -406,10 +408,11 @@ var For = class extends Statement {
406
408
  type = "For";
407
409
  };
408
410
  var SetStatement = class extends Statement {
409
- constructor(assignee, value) {
411
+ constructor(assignee, value, body) {
410
412
  super();
411
413
  this.assignee = assignee;
412
414
  this.value = value;
415
+ this.body = body;
413
416
  }
414
417
  type = "Set";
415
418
  };
@@ -617,10 +620,19 @@ function parse(tokens) {
617
620
  const left = parseExpression();
618
621
  if (is(TOKEN_TYPES.Equals)) {
619
622
  ++current;
620
- const value = parseSetStatement();
621
- return new SetStatement(left, value);
623
+ const value = parseExpression();
624
+ return new SetStatement(left, value, []);
625
+ } else {
626
+ const body = [];
627
+ expect(TOKEN_TYPES.CloseStatement, "Expected %} token");
628
+ while (!(tokens[current]?.type === TOKEN_TYPES.OpenStatement && tokens[current + 1]?.type === TOKEN_TYPES.EndSet)) {
629
+ const another = parseAny();
630
+ body.push(another);
631
+ }
632
+ expect(TOKEN_TYPES.OpenStatement, "Expected {% token");
633
+ expect(TOKEN_TYPES.EndSet, "Expected endset token");
634
+ return new SetStatement(left, null, body);
622
635
  }
623
- return left;
624
636
  }
625
637
  function parseIfStatement() {
626
638
  const test = parseExpression();
@@ -1427,6 +1439,8 @@ var Interpreter = class {
1427
1439
  );
1428
1440
  case "join":
1429
1441
  return new StringValue(operand.value.map((x) => x.value).join(""));
1442
+ case "string":
1443
+ return new StringValue(toJSON(operand));
1430
1444
  default:
1431
1445
  throw new Error(`Unknown ArrayValue filter: ${filter.value}`);
1432
1446
  }
@@ -1694,7 +1708,7 @@ var Interpreter = class {
1694
1708
  return value instanceof RuntimeValue ? value : new UndefinedValue();
1695
1709
  }
1696
1710
  evaluateSet(node, environment) {
1697
- const rhs = this.evaluate(node.value, environment);
1711
+ const rhs = node.value ? this.evaluate(node.value, environment) : this.evaluateBlock(node.body, environment);
1698
1712
  if (node.assignee.type === "Identifier") {
1699
1713
  const variableName = node.assignee.value;
1700
1714
  environment.setVariable(variableName, rhs);
@@ -4021,6 +4035,7 @@ class AutoConfig {
4021
4035
  /**
4022
4036
  * Transformers.js-specific configuration, possibly present in config.json under the key `transformers.js_config`.
4023
4037
  * @typedef {Object} TransformersJSConfig
4038
+ * @property {Record<import('./utils/devices.js').DeviceType, DeviceConfig>} [device_config] Device-specific configurations.
4024
4039
  * @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.
4025
4040
  * @property {Record<string, number>} [free_dimension_overrides] Override the free dimensions of the model.
4026
4041
  * See https://onnxruntime.ai/docs/tutorials/web/env-flags-and-session-options.html#freedimensionoverrides
@@ -4030,6 +4045,11 @@ class AutoConfig {
4030
4045
  * @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).
4031
4046
  */
4032
4047
 
4048
+ /**
4049
+ * Device-specific configuration options.
4050
+ * @typedef {Omit<TransformersJSConfig, "device" | "device_config">} DeviceConfig
4051
+ */
4052
+
4033
4053
 
4034
4054
  /***/ }),
4035
4055
 
@@ -4076,7 +4096,7 @@ __webpack_require__.r(__webpack_exports__);
4076
4096
 
4077
4097
 
4078
4098
 
4079
- const VERSION = '3.4.2';
4099
+ const VERSION = '3.5.0';
4080
4100
 
4081
4101
  // Check if various APIs are available (depends on environment)
4082
4102
  const IS_BROWSER_ENV = typeof window !== "undefined" && typeof window.document !== "undefined";
@@ -4168,7 +4188,8 @@ const localModelPath = RUNNING_LOCALLY
4168
4188
  * @property {string} cacheDir The directory to use for caching files with the file system. By default, it is `./.cache`.
4169
4189
  * @property {boolean} useCustomCache Whether to use a custom cache system (defined by `customCache`), defaults to `false`.
4170
4190
  * @property {Object} customCache The custom cache to use. Defaults to `null`. Note: this must be an object which
4171
- * 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
4191
+ * 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.
4192
+ * 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>`.
4172
4193
  */
4173
4194
 
4174
4195
  /** @type {TransformersEnvironment} */
@@ -5197,13 +5218,15 @@ class NoBadWordsLogitsProcessor extends LogitsProcessor {
5197
5218
  const batch_logits_data = /** @type {Float32Array} */(logits[i].data);
5198
5219
  const ids = input_ids[i];
5199
5220
  for (const bad_word_ids of this.bad_words_ids) {
5221
+ // There aren't enough tokens to match the banned sequence
5222
+ if (ids.length < bad_word_ids.length - 1) continue;
5223
+
5200
5224
  // Whether to modify the logits of the last token in the bad word id sequence
5201
5225
  let mark = true;
5202
5226
 
5203
5227
  // For each bad word in the list, if the current sequence of input ids ends with this sequence (excluding the last),
5204
5228
  // then we set the logits of the last bad word id to -Infinity.
5205
- for (let j = 1; j <= bad_word_ids.length - 1 && bad_word_ids.length < ids.length; ++j) {
5206
-
5229
+ for (let j = 1; j <= bad_word_ids.length - 1; ++j) {
5207
5230
  // NOTE: We use != instead of !== to compare bigint and number
5208
5231
  // @ts-ignore
5209
5232
  if (bad_word_ids.at(-j - 1) != ids.at(-j)) {
@@ -6679,7 +6702,8 @@ const MODEL_CLASS_TO_NAME_MAPPING = new Map();
6679
6702
  * @private
6680
6703
  */
6681
6704
  async function getSession(pretrained_model_name_or_path, fileName, options) {
6682
- const custom_config = options.config?.['transformers.js_config'] ?? {};
6705
+ let custom_config = options.config?.['transformers.js_config'] ?? {};
6706
+
6683
6707
  let device = options.device ?? custom_config.device;
6684
6708
  if (device && typeof device !== 'string') {
6685
6709
  if (device.hasOwnProperty(fileName)) {
@@ -6694,8 +6718,18 @@ async function getSession(pretrained_model_name_or_path, fileName, options) {
6694
6718
  const selectedDevice = /** @type {import("./utils/devices.js").DeviceType} */(
6695
6719
  device ?? (_env_js__WEBPACK_IMPORTED_MODULE_14__.apis.IS_NODE_ENV ? 'cpu' : 'wasm')
6696
6720
  );
6721
+
6697
6722
  const executionProviders = (0,_backends_onnx_js__WEBPACK_IMPORTED_MODULE_1__.deviceToExecutionProviders)(selectedDevice);
6698
6723
 
6724
+ // Update custom config with the selected device's config, if it exists
6725
+ const device_config = custom_config.device_config ?? {};
6726
+ if (device_config.hasOwnProperty(selectedDevice)) {
6727
+ custom_config = {
6728
+ ...custom_config,
6729
+ ...device_config[selectedDevice],
6730
+ };
6731
+ }
6732
+
6699
6733
  // If options.dtype is specified, we use it to choose the suffix for the model file.
6700
6734
  // Otherwise, we use the default dtype for the device.
6701
6735
  let dtype = options.dtype ?? custom_config.dtype;
@@ -6712,11 +6746,11 @@ async function getSession(pretrained_model_name_or_path, fileName, options) {
6712
6746
  // Try to choose the auto dtype based on the custom config
6713
6747
  let config_dtype = custom_config.dtype;
6714
6748
  if (typeof config_dtype !== 'string') {
6715
- config_dtype = config_dtype[fileName];
6749
+ config_dtype = config_dtype?.[fileName];
6716
6750
  }
6717
6751
 
6718
6752
  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)) {
6719
- // Defined by the custom config, and is not "auto"
6753
+ // Defined by the config, and is not "auto"
6720
6754
  dtype = config_dtype;
6721
6755
  } else {
6722
6756
  // Choose default dtype based on device, falling back to fp32
@@ -6733,10 +6767,11 @@ async function getSession(pretrained_model_name_or_path, fileName, options) {
6733
6767
  }
6734
6768
 
6735
6769
  // Only valid for models with a decoder
6736
- const kv_cache_dtype = custom_config.kv_cache_dtype
6737
- ? (typeof custom_config.kv_cache_dtype === 'string'
6738
- ? custom_config.kv_cache_dtype
6739
- : custom_config.kv_cache_dtype[selectedDtype] ?? 'float32')
6770
+ const kv_cache_dtype_config = custom_config.kv_cache_dtype;
6771
+ const kv_cache_dtype = kv_cache_dtype_config
6772
+ ? (typeof kv_cache_dtype_config === 'string'
6773
+ ? kv_cache_dtype_config
6774
+ : kv_cache_dtype_config[selectedDtype] ?? 'float32')
6740
6775
  : undefined;
6741
6776
 
6742
6777
  if (kv_cache_dtype && !['float32', 'float16'].includes(kv_cache_dtype)) {
@@ -6764,14 +6799,15 @@ async function getSession(pretrained_model_name_or_path, fileName, options) {
6764
6799
  session_options.freeDimensionOverrides ??= free_dimension_overrides;
6765
6800
  } else if (selectedDevice.startsWith('webnn') && !session_options.freeDimensionOverrides) {
6766
6801
  console.warn(
6767
- '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". ' +
6768
- 'When `free_dimension_overrides` is not set, you may experience significant performance degradation.'
6802
+ `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}"]. ` +
6803
+ `When 'free_dimension_overrides' is not set, you may experience significant performance degradation.`
6769
6804
  );
6770
6805
  }
6771
6806
 
6772
- 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);
6807
+ const return_path = _env_js__WEBPACK_IMPORTED_MODULE_14__.apis.IS_NODE_ENV && _env_js__WEBPACK_IMPORTED_MODULE_14__.env.useFSCache;
6808
+ const bufferOrPathPromise = (0,_utils_hub_js__WEBPACK_IMPORTED_MODULE_5__.getModelFile)(pretrained_model_name_or_path, modelFileName, true, options, return_path);
6773
6809
 
6774
- // handle onnx external data files
6810
+ // Handle onnx external data files
6775
6811
  const use_external_data_format = options.use_external_data_format ?? custom_config.use_external_data_format;
6776
6812
  /** @type {Promise<string|{path: string, data: Uint8Array}>[]} */
6777
6813
  let externalDataPromises = [];
@@ -6797,7 +6833,7 @@ async function getSession(pretrained_model_name_or_path, fileName, options) {
6797
6833
  const path = `${baseName}_data${i === 0 ? '' : '_' + i}`;
6798
6834
  const fullPath = `${options.subfolder ?? ''}/${path}`;
6799
6835
  externalDataPromises.push(new Promise(async (resolve, reject) => {
6800
- 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);
6836
+ const data = await (0,_utils_hub_js__WEBPACK_IMPORTED_MODULE_5__.getModelFile)(pretrained_model_name_or_path, fullPath, true, options, return_path);
6801
6837
  resolve(data instanceof Uint8Array ? { path, data } : path);
6802
6838
  }));
6803
6839
  }
@@ -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', {
@@ -18636,7 +18667,7 @@ class SeamlessM4TFeatureExtractor extends _base_feature_extraction_utils_js__WEB
18636
18667
 
18637
18668
  const sampling_rate = this.config.sampling_rate;
18638
18669
  const mel_filters = (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_2__.mel_filter_bank)(
18639
- 256, // num_frequency_bins
18670
+ 257, // num_frequency_bins
18640
18671
  this.config.num_mel_bins, // num_mel_filters
18641
18672
  20, // min_frequency
18642
18673
  Math.floor(sampling_rate / 2), // max_frequency
@@ -18645,11 +18676,6 @@ class SeamlessM4TFeatureExtractor extends _base_feature_extraction_utils_js__WEB
18645
18676
  "kaldi", // mel_scale
18646
18677
  true, // triangularize_in_mel_space
18647
18678
  );
18648
-
18649
- // Do padding:
18650
- for (let i = 0; i < mel_filters.length; ++i) {
18651
- mel_filters[i].push(0);
18652
- }
18653
18679
  this.mel_filters = mel_filters;
18654
18680
 
18655
18681
  this.window = (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_2__.window_function)(400, 'povey', {
@@ -19418,7 +19444,7 @@ class WeSpeakerFeatureExtractor extends _base_feature_extraction_utils_js__WEBPA
19418
19444
 
19419
19445
  const sampling_rate = this.config.sampling_rate;
19420
19446
  const mel_filters = (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_2__.mel_filter_bank)(
19421
- 256, // num_frequency_bins
19447
+ 257, // num_frequency_bins
19422
19448
  this.config.num_mel_bins, // num_mel_filters
19423
19449
  20, // min_frequency
19424
19450
  Math.floor(sampling_rate / 2), // max_frequency
@@ -19427,11 +19453,6 @@ class WeSpeakerFeatureExtractor extends _base_feature_extraction_utils_js__WEBPA
19427
19453
  "kaldi", // mel_scale
19428
19454
  true, // triangularize_in_mel_space
19429
19455
  );
19430
-
19431
- // Do padding:
19432
- for (let i = 0; i < mel_filters.length; ++i) {
19433
- mel_filters[i].push(0);
19434
- }
19435
19456
  this.mel_filters = mel_filters;
19436
19457
 
19437
19458
  this.window = (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_2__.window_function)(400, 'hamming', {
@@ -24426,8 +24447,24 @@ class BPE extends TokenizerModel {
24426
24447
 
24427
24448
  this.ignore_merges = this.config.ignore_merges ?? false;
24428
24449
 
24429
- /** @type {Map<string, string[]>} */
24430
- this.cache = new Map();
24450
+ /**
24451
+ * The maximum length we should cache in a model.
24452
+ * Strings that are too long have minimal chances to cache hit anyway
24453
+ */
24454
+ this.max_length_to_cache = 256;
24455
+
24456
+ /**
24457
+ * The default capacity for a `BPE`'s internal cache.
24458
+ */
24459
+ this.cache_capacity = 10000;
24460
+ this.cache = new _utils_data_structures_js__WEBPACK_IMPORTED_MODULE_5__.LRUCache(this.cache_capacity);
24461
+ }
24462
+
24463
+ /**
24464
+ * Clears the cache.
24465
+ */
24466
+ clear_cache() {
24467
+ this.cache.clear();
24431
24468
  }
24432
24469
 
24433
24470
  /**
@@ -24554,8 +24591,10 @@ class BPE extends TokenizerModel {
24554
24591
  }
24555
24592
  }
24556
24593
 
24557
- // Save the result to the cache
24558
- this.cache.set(token, result);
24594
+ if (token.length < this.max_length_to_cache) {
24595
+ // Save the result to the cache
24596
+ this.cache.put(token, result);
24597
+ }
24559
24598
 
24560
24599
  return result;
24561
24600
  }
@@ -28356,7 +28395,8 @@ function linspace(start, end, num) {
28356
28395
  * various implementation exist, which differ in the number of filters, the shape of the filters, the way the filters
28357
28396
  * are spaced, the bandwidth of the filters, and the manner in which the spectrum is warped. The goal of these
28358
28397
  * features is to approximate the non-linear human perception of the variation in pitch with respect to the frequency.
28359
- * @param {number} num_frequency_bins Number of frequencies used to compute the spectrogram (should be the same as in `stft`).
28398
+ * @param {number} num_frequency_bins Number of frequency bins (should be the same as `n_fft // 2 + 1`
28399
+ * where `n_fft` is the size of the Fourier Transform used to compute the spectrogram).
28360
28400
  * @param {number} num_mel_filters Number of mel filters to generate.
28361
28401
  * @param {number} min_frequency Lowest frequency of interest in Hz.
28362
28402
  * @param {number} max_frequency Highest frequency of interest in Hz. This should not exceed `sampling_rate / 2`.
@@ -28382,6 +28422,14 @@ function mel_filter_bank(
28382
28422
  throw new Error('norm must be one of null or "slaney"');
28383
28423
  }
28384
28424
 
28425
+ if (num_frequency_bins < 2) {
28426
+ throw new Error(`Require num_frequency_bins: ${num_frequency_bins} >= 2`);
28427
+ }
28428
+
28429
+ if (min_frequency > max_frequency) {
28430
+ throw new Error(`Require min_frequency: ${min_frequency} <= max_frequency: ${max_frequency}`);
28431
+ }
28432
+
28385
28433
  const mel_min = hertz_to_mel(min_frequency, mel_scale);
28386
28434
  const mel_max = hertz_to_mel(max_frequency, mel_scale);
28387
28435
  const mel_freqs = linspace(mel_min, mel_max, num_mel_filters + 2);
@@ -28390,7 +28438,7 @@ function mel_filter_bank(
28390
28438
  let fft_freqs; // frequencies of FFT bins in Hz
28391
28439
 
28392
28440
  if (triangularize_in_mel_space) {
28393
- const fft_bin_width = sampling_rate / (num_frequency_bins * 2);
28441
+ const fft_bin_width = sampling_rate / ((num_frequency_bins - 1) * 2);
28394
28442
  fft_freqs = hertz_to_mel(Float64Array.from({ length: num_frequency_bins }, (_, i) => i * fft_bin_width), mel_scale);
28395
28443
  filter_freqs = mel_freqs;
28396
28444
  } else {
@@ -29270,6 +29318,7 @@ __webpack_require__.r(__webpack_exports__);
29270
29318
  /* harmony export */ __webpack_require__.d(__webpack_exports__, {
29271
29319
  /* harmony export */ CharTrie: () => (/* binding */ CharTrie),
29272
29320
  /* harmony export */ DictionarySplitter: () => (/* binding */ DictionarySplitter),
29321
+ /* harmony export */ LRUCache: () => (/* binding */ LRUCache),
29273
29322
  /* harmony export */ PriorityQueue: () => (/* binding */ PriorityQueue),
29274
29323
  /* harmony export */ TokenLattice: () => (/* binding */ TokenLattice)
29275
29324
  /* harmony export */ });
@@ -29795,6 +29844,59 @@ class DictionarySplitter {
29795
29844
  }
29796
29845
  }
29797
29846
 
29847
+ /**
29848
+ * A simple Least Recently Used (LRU) cache implementation in JavaScript.
29849
+ * This cache stores key-value pairs and evicts the least recently used item
29850
+ * when the capacity is exceeded.
29851
+ */
29852
+ class LRUCache {
29853
+ /**
29854
+ * Creates an LRUCache instance.
29855
+ * @param {number} capacity The maximum number of items the cache can hold.
29856
+ */
29857
+ constructor(capacity) {
29858
+ this.capacity = capacity;
29859
+ this.cache = new Map();
29860
+ }
29861
+
29862
+ /**
29863
+ * Retrieves the value associated with the given key and marks the key as recently used.
29864
+ * @param {any} key The key to retrieve.
29865
+ * @returns {any} The value associated with the key, or undefined if the key does not exist.
29866
+ */
29867
+ get(key) {
29868
+ if (!this.cache.has(key)) return undefined;
29869
+ const value = this.cache.get(key);
29870
+ this.cache.delete(key);
29871
+ this.cache.set(key, value);
29872
+ return value;
29873
+ }
29874
+
29875
+ /**
29876
+ * Inserts or updates the key-value pair in the cache.
29877
+ * If the key already exists, it is updated and marked as recently used.
29878
+ * If the cache exceeds its capacity, the least recently used item is evicted.
29879
+ * @param {any} key The key to add or update.
29880
+ * @param {any} value The value to associate with the key.
29881
+ */
29882
+ put(key, value) {
29883
+ if (this.cache.has(key)) {
29884
+ this.cache.delete(key);
29885
+ }
29886
+ this.cache.set(key, value);
29887
+ if (this.cache.size > this.capacity) {
29888
+ this.cache.delete(this.cache.keys().next().value);
29889
+ }
29890
+ }
29891
+
29892
+ /**
29893
+ * Clears the cache.
29894
+ */
29895
+ clear() {
29896
+ this.cache.clear();
29897
+ }
29898
+ }
29899
+
29798
29900
 
29799
29901
  /***/ }),
29800
29902
 
@@ -30406,6 +30508,22 @@ async function getModelFile(path_or_repo_id, filename, fatal = true, options = {
30406
30508
  // First, check if the a caching backend is available
30407
30509
  // If no caching mechanism available, will download the file every time
30408
30510
  let cache;
30511
+ if (!cache && _env_js__WEBPACK_IMPORTED_MODULE_2__.env.useCustomCache) {
30512
+ // Allow the user to specify a custom cache system.
30513
+ if (!_env_js__WEBPACK_IMPORTED_MODULE_2__.env.customCache) {
30514
+ throw Error('`env.useCustomCache=true`, but `env.customCache` is not defined.')
30515
+ }
30516
+
30517
+ // Check that the required methods are defined:
30518
+ if (!_env_js__WEBPACK_IMPORTED_MODULE_2__.env.customCache.match || !_env_js__WEBPACK_IMPORTED_MODULE_2__.env.customCache.put) {
30519
+ throw new Error(
30520
+ "`env.customCache` must be an object which implements the `match` and `put` functions of the Web Cache API. " +
30521
+ "For more information, see https://developer.mozilla.org/en-US/docs/Web/API/Cache"
30522
+ )
30523
+ }
30524
+ cache = _env_js__WEBPACK_IMPORTED_MODULE_2__.env.customCache;
30525
+ }
30526
+
30409
30527
  if (!cache && _env_js__WEBPACK_IMPORTED_MODULE_2__.env.useBrowserCache) {
30410
30528
  if (typeof caches === 'undefined') {
30411
30529
  throw Error('Browser cache is not available in this environment.')
@@ -30423,28 +30541,14 @@ async function getModelFile(path_or_repo_id, filename, fatal = true, options = {
30423
30541
  }
30424
30542
 
30425
30543
  if (!cache && _env_js__WEBPACK_IMPORTED_MODULE_2__.env.useFSCache) {
30426
- // TODO throw error if not available
30544
+ if (!_env_js__WEBPACK_IMPORTED_MODULE_2__.apis.IS_FS_AVAILABLE) {
30545
+ throw Error('File System Cache is not available in this environment.');
30546
+ }
30427
30547
 
30428
30548
  // If `cache_dir` is not specified, use the default cache directory
30429
30549
  cache = new FileCache(options.cache_dir ?? _env_js__WEBPACK_IMPORTED_MODULE_2__.env.cacheDir);
30430
30550
  }
30431
30551
 
30432
- if (!cache && _env_js__WEBPACK_IMPORTED_MODULE_2__.env.useCustomCache) {
30433
- // Allow the user to specify a custom cache system.
30434
- if (!_env_js__WEBPACK_IMPORTED_MODULE_2__.env.customCache) {
30435
- throw Error('`env.useCustomCache=true`, but `env.customCache` is not defined.')
30436
- }
30437
-
30438
- // Check that the required methods are defined:
30439
- if (!_env_js__WEBPACK_IMPORTED_MODULE_2__.env.customCache.match || !_env_js__WEBPACK_IMPORTED_MODULE_2__.env.customCache.put) {
30440
- throw new Error(
30441
- "`env.customCache` must be an object which implements the `match` and `put` functions of the Web Cache API. " +
30442
- "For more information, see https://developer.mozilla.org/en-US/docs/Web/API/Cache"
30443
- )
30444
- }
30445
- cache = _env_js__WEBPACK_IMPORTED_MODULE_2__.env.customCache;
30446
- }
30447
-
30448
30552
  const revision = options.revision ?? 'main';
30449
30553
  const requestURL = pathJoin(path_or_repo_id, filename);
30450
30554
 
@@ -30626,7 +30730,7 @@ async function getModelFile(path_or_repo_id, filename, fatal = true, options = {
30626
30730
  });
30627
30731
 
30628
30732
  if (result) {
30629
- if (return_path) {
30733
+ if (!_env_js__WEBPACK_IMPORTED_MODULE_2__.apis.IS_NODE_ENV && return_path) {
30630
30734
  throw new Error("Cannot return path in a browser environment.")
30631
30735
  }
30632
30736
  return result;
@@ -30635,12 +30739,18 @@ async function getModelFile(path_or_repo_id, filename, fatal = true, options = {
30635
30739
  return response.filePath;
30636
30740
  }
30637
30741
 
30638
- const path = await cache.match(cacheKey);
30639
- if (path instanceof FileResponse) {
30640
- return path.filePath;
30742
+ // Otherwise, return the cached response (most likely a `FileResponse`).
30743
+ // NOTE: A custom cache may return a Response, or a string (file path)
30744
+ const cachedResponse = await cache?.match(cacheKey);
30745
+ if (cachedResponse instanceof FileResponse) {
30746
+ return cachedResponse.filePath;
30747
+ } else if (cachedResponse instanceof Response) {
30748
+ return new Uint8Array(await cachedResponse.arrayBuffer());
30749
+ } else if (typeof cachedResponse === 'string') {
30750
+ return cachedResponse;
30641
30751
  }
30642
- throw new Error("Unable to return path for response.");
30643
30752
 
30753
+ throw new Error("Unable to get model file path or buffer.");
30644
30754
  }
30645
30755
 
30646
30756
  /**