@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
@@ -152,6 +152,7 @@ var TOKEN_TYPES = Object.freeze({
152
152
  Is: "Is",
153
153
  NotIn: "NotIn",
154
154
  Else: "Else",
155
+ EndSet: "EndSet",
155
156
  EndIf: "EndIf",
156
157
  ElseIf: "ElseIf",
157
158
  EndFor: "EndFor",
@@ -168,6 +169,7 @@ var KEYWORDS = Object.freeze({
168
169
  is: TOKEN_TYPES.Is,
169
170
  if: TOKEN_TYPES.If,
170
171
  else: TOKEN_TYPES.Else,
172
+ endset: TOKEN_TYPES.EndSet,
171
173
  endif: TOKEN_TYPES.EndIf,
172
174
  elif: TOKEN_TYPES.ElseIf,
173
175
  endfor: TOKEN_TYPES.EndFor,
@@ -404,10 +406,11 @@ var For = class extends Statement {
404
406
  type = "For";
405
407
  };
406
408
  var SetStatement = class extends Statement {
407
- constructor(assignee, value) {
409
+ constructor(assignee, value, body) {
408
410
  super();
409
411
  this.assignee = assignee;
410
412
  this.value = value;
413
+ this.body = body;
411
414
  }
412
415
  type = "Set";
413
416
  };
@@ -615,10 +618,19 @@ function parse(tokens) {
615
618
  const left = parseExpression();
616
619
  if (is(TOKEN_TYPES.Equals)) {
617
620
  ++current;
618
- const value = parseSetStatement();
619
- return new SetStatement(left, value);
621
+ const value = parseExpression();
622
+ return new SetStatement(left, value, []);
623
+ } else {
624
+ const body = [];
625
+ expect(TOKEN_TYPES.CloseStatement, "Expected %} token");
626
+ while (!(tokens[current]?.type === TOKEN_TYPES.OpenStatement && tokens[current + 1]?.type === TOKEN_TYPES.EndSet)) {
627
+ const another = parseAny();
628
+ body.push(another);
629
+ }
630
+ expect(TOKEN_TYPES.OpenStatement, "Expected {% token");
631
+ expect(TOKEN_TYPES.EndSet, "Expected endset token");
632
+ return new SetStatement(left, null, body);
620
633
  }
621
- return left;
622
634
  }
623
635
  function parseIfStatement() {
624
636
  const test = parseExpression();
@@ -1425,6 +1437,8 @@ var Interpreter = class {
1425
1437
  );
1426
1438
  case "join":
1427
1439
  return new StringValue(operand.value.map((x) => x.value).join(""));
1440
+ case "string":
1441
+ return new StringValue(toJSON(operand));
1428
1442
  default:
1429
1443
  throw new Error(`Unknown ArrayValue filter: ${filter.value}`);
1430
1444
  }
@@ -1692,7 +1706,7 @@ var Interpreter = class {
1692
1706
  return value instanceof RuntimeValue ? value : new UndefinedValue();
1693
1707
  }
1694
1708
  evaluateSet(node, environment) {
1695
- const rhs = this.evaluate(node.value, environment);
1709
+ const rhs = node.value ? this.evaluate(node.value, environment) : this.evaluateBlock(node.body, environment);
1696
1710
  if (node.assignee.type === "Identifier") {
1697
1711
  const variableName = node.assignee.value;
1698
1712
  environment.setVariable(variableName, rhs);
@@ -4013,6 +4027,7 @@ class AutoConfig {
4013
4027
  /**
4014
4028
  * Transformers.js-specific configuration, possibly present in config.json under the key `transformers.js_config`.
4015
4029
  * @typedef {Object} TransformersJSConfig
4030
+ * @property {Record<import('./utils/devices.js').DeviceType, DeviceConfig>} [device_config] Device-specific configurations.
4016
4031
  * @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.
4017
4032
  * @property {Record<string, number>} [free_dimension_overrides] Override the free dimensions of the model.
4018
4033
  * See https://onnxruntime.ai/docs/tutorials/web/env-flags-and-session-options.html#freedimensionoverrides
@@ -4022,6 +4037,11 @@ class AutoConfig {
4022
4037
  * @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).
4023
4038
  */
4024
4039
 
4040
+ /**
4041
+ * Device-specific configuration options.
4042
+ * @typedef {Omit<TransformersJSConfig, "device" | "device_config">} DeviceConfig
4043
+ */
4044
+
4025
4045
 
4026
4046
  /***/ }),
4027
4047
 
@@ -4067,7 +4087,7 @@ __webpack_require__.r(__webpack_exports__);
4067
4087
 
4068
4088
 
4069
4089
 
4070
- const VERSION = '3.4.2';
4090
+ const VERSION = '3.5.0';
4071
4091
 
4072
4092
  // Check if various APIs are available (depends on environment)
4073
4093
  const IS_BROWSER_ENV = typeof window !== "undefined" && typeof window.document !== "undefined";
@@ -4159,7 +4179,8 @@ const localModelPath = RUNNING_LOCALLY
4159
4179
  * @property {string} cacheDir The directory to use for caching files with the file system. By default, it is `./.cache`.
4160
4180
  * @property {boolean} useCustomCache Whether to use a custom cache system (defined by `customCache`), defaults to `false`.
4161
4181
  * @property {Object} customCache The custom cache to use. Defaults to `null`. Note: this must be an object which
4162
- * 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
4182
+ * 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.
4183
+ * 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>`.
4163
4184
  */
4164
4185
 
4165
4186
  /** @type {TransformersEnvironment} */
@@ -5186,13 +5207,15 @@ class NoBadWordsLogitsProcessor extends LogitsProcessor {
5186
5207
  const batch_logits_data = /** @type {Float32Array} */(logits[i].data);
5187
5208
  const ids = input_ids[i];
5188
5209
  for (const bad_word_ids of this.bad_words_ids) {
5210
+ // There aren't enough tokens to match the banned sequence
5211
+ if (ids.length < bad_word_ids.length - 1) continue;
5212
+
5189
5213
  // Whether to modify the logits of the last token in the bad word id sequence
5190
5214
  let mark = true;
5191
5215
 
5192
5216
  // For each bad word in the list, if the current sequence of input ids ends with this sequence (excluding the last),
5193
5217
  // then we set the logits of the last bad word id to -Infinity.
5194
- for (let j = 1; j <= bad_word_ids.length - 1 && bad_word_ids.length < ids.length; ++j) {
5195
-
5218
+ for (let j = 1; j <= bad_word_ids.length - 1; ++j) {
5196
5219
  // NOTE: We use != instead of !== to compare bigint and number
5197
5220
  // @ts-ignore
5198
5221
  if (bad_word_ids.at(-j - 1) != ids.at(-j)) {
@@ -6664,7 +6687,8 @@ const MODEL_CLASS_TO_NAME_MAPPING = new Map();
6664
6687
  * @private
6665
6688
  */
6666
6689
  async function getSession(pretrained_model_name_or_path, fileName, options) {
6667
- const custom_config = options.config?.['transformers.js_config'] ?? {};
6690
+ let custom_config = options.config?.['transformers.js_config'] ?? {};
6691
+
6668
6692
  let device = options.device ?? custom_config.device;
6669
6693
  if (device && typeof device !== 'string') {
6670
6694
  if (device.hasOwnProperty(fileName)) {
@@ -6679,8 +6703,18 @@ async function getSession(pretrained_model_name_or_path, fileName, options) {
6679
6703
  const selectedDevice = /** @type {import("./utils/devices.js").DeviceType} */(
6680
6704
  device ?? (_env_js__WEBPACK_IMPORTED_MODULE_14__.apis.IS_NODE_ENV ? 'cpu' : 'wasm')
6681
6705
  );
6706
+
6682
6707
  const executionProviders = (0,_backends_onnx_js__WEBPACK_IMPORTED_MODULE_1__.deviceToExecutionProviders)(selectedDevice);
6683
6708
 
6709
+ // Update custom config with the selected device's config, if it exists
6710
+ const device_config = custom_config.device_config ?? {};
6711
+ if (device_config.hasOwnProperty(selectedDevice)) {
6712
+ custom_config = {
6713
+ ...custom_config,
6714
+ ...device_config[selectedDevice],
6715
+ };
6716
+ }
6717
+
6684
6718
  // If options.dtype is specified, we use it to choose the suffix for the model file.
6685
6719
  // Otherwise, we use the default dtype for the device.
6686
6720
  let dtype = options.dtype ?? custom_config.dtype;
@@ -6697,11 +6731,11 @@ async function getSession(pretrained_model_name_or_path, fileName, options) {
6697
6731
  // Try to choose the auto dtype based on the custom config
6698
6732
  let config_dtype = custom_config.dtype;
6699
6733
  if (typeof config_dtype !== 'string') {
6700
- config_dtype = config_dtype[fileName];
6734
+ config_dtype = config_dtype?.[fileName];
6701
6735
  }
6702
6736
 
6703
6737
  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)) {
6704
- // Defined by the custom config, and is not "auto"
6738
+ // Defined by the config, and is not "auto"
6705
6739
  dtype = config_dtype;
6706
6740
  } else {
6707
6741
  // Choose default dtype based on device, falling back to fp32
@@ -6718,10 +6752,11 @@ async function getSession(pretrained_model_name_or_path, fileName, options) {
6718
6752
  }
6719
6753
 
6720
6754
  // Only valid for models with a decoder
6721
- const kv_cache_dtype = custom_config.kv_cache_dtype
6722
- ? (typeof custom_config.kv_cache_dtype === 'string'
6723
- ? custom_config.kv_cache_dtype
6724
- : custom_config.kv_cache_dtype[selectedDtype] ?? 'float32')
6755
+ const kv_cache_dtype_config = custom_config.kv_cache_dtype;
6756
+ const kv_cache_dtype = kv_cache_dtype_config
6757
+ ? (typeof kv_cache_dtype_config === 'string'
6758
+ ? kv_cache_dtype_config
6759
+ : kv_cache_dtype_config[selectedDtype] ?? 'float32')
6725
6760
  : undefined;
6726
6761
 
6727
6762
  if (kv_cache_dtype && !['float32', 'float16'].includes(kv_cache_dtype)) {
@@ -6749,14 +6784,15 @@ async function getSession(pretrained_model_name_or_path, fileName, options) {
6749
6784
  session_options.freeDimensionOverrides ??= free_dimension_overrides;
6750
6785
  } else if (selectedDevice.startsWith('webnn') && !session_options.freeDimensionOverrides) {
6751
6786
  console.warn(
6752
- '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". ' +
6753
- 'When `free_dimension_overrides` is not set, you may experience significant performance degradation.'
6787
+ `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}"]. ` +
6788
+ `When 'free_dimension_overrides' is not set, you may experience significant performance degradation.`
6754
6789
  );
6755
6790
  }
6756
6791
 
6757
- 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);
6792
+ const return_path = _env_js__WEBPACK_IMPORTED_MODULE_14__.apis.IS_NODE_ENV && _env_js__WEBPACK_IMPORTED_MODULE_14__.env.useFSCache;
6793
+ const bufferOrPathPromise = (0,_utils_hub_js__WEBPACK_IMPORTED_MODULE_5__.getModelFile)(pretrained_model_name_or_path, modelFileName, true, options, return_path);
6758
6794
 
6759
- // handle onnx external data files
6795
+ // Handle onnx external data files
6760
6796
  const use_external_data_format = options.use_external_data_format ?? custom_config.use_external_data_format;
6761
6797
  /** @type {Promise<string|{path: string, data: Uint8Array}>[]} */
6762
6798
  let externalDataPromises = [];
@@ -6782,7 +6818,7 @@ async function getSession(pretrained_model_name_or_path, fileName, options) {
6782
6818
  const path = `${baseName}_data${i === 0 ? '' : '_' + i}`;
6783
6819
  const fullPath = `${options.subfolder ?? ''}/${path}`;
6784
6820
  externalDataPromises.push(new Promise(async (resolve, reject) => {
6785
- 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);
6821
+ const data = await (0,_utils_hub_js__WEBPACK_IMPORTED_MODULE_5__.getModelFile)(pretrained_model_name_or_path, fullPath, true, options, return_path);
6786
6822
  resolve(data instanceof Uint8Array ? { path, data } : path);
6787
6823
  }));
6788
6824
  }
@@ -14961,7 +14997,7 @@ class ASTFeatureExtractor extends _base_feature_extraction_utils_js__WEBPACK_IMP
14961
14997
 
14962
14998
  const sampling_rate = this.config.sampling_rate;
14963
14999
  const mel_filters = (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_2__.mel_filter_bank)(
14964
- 256, // num_frequency_bins
15000
+ 257, // num_frequency_bins
14965
15001
  this.config.num_mel_bins, // num_mel_filters
14966
15002
  20, // min_frequency
14967
15003
  Math.floor(sampling_rate / 2), // max_frequency
@@ -14970,11 +15006,6 @@ class ASTFeatureExtractor extends _base_feature_extraction_utils_js__WEBPACK_IMP
14970
15006
  "kaldi", // mel_scale
14971
15007
  true, // triangularize_in_mel_space
14972
15008
  );
14973
-
14974
- // Do padding:
14975
- for (let i = 0; i < mel_filters.length; ++i) {
14976
- mel_filters[i].push(0);
14977
- }
14978
15009
  this.mel_filters = mel_filters;
14979
15010
 
14980
15011
  this.window = (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_2__.window_function)(400, 'hann', {
@@ -18564,7 +18595,7 @@ class SeamlessM4TFeatureExtractor extends _base_feature_extraction_utils_js__WEB
18564
18595
 
18565
18596
  const sampling_rate = this.config.sampling_rate;
18566
18597
  const mel_filters = (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_2__.mel_filter_bank)(
18567
- 256, // num_frequency_bins
18598
+ 257, // num_frequency_bins
18568
18599
  this.config.num_mel_bins, // num_mel_filters
18569
18600
  20, // min_frequency
18570
18601
  Math.floor(sampling_rate / 2), // max_frequency
@@ -18573,11 +18604,6 @@ class SeamlessM4TFeatureExtractor extends _base_feature_extraction_utils_js__WEB
18573
18604
  "kaldi", // mel_scale
18574
18605
  true, // triangularize_in_mel_space
18575
18606
  );
18576
-
18577
- // Do padding:
18578
- for (let i = 0; i < mel_filters.length; ++i) {
18579
- mel_filters[i].push(0);
18580
- }
18581
18607
  this.mel_filters = mel_filters;
18582
18608
 
18583
18609
  this.window = (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_2__.window_function)(400, 'povey', {
@@ -19330,7 +19356,7 @@ class WeSpeakerFeatureExtractor extends _base_feature_extraction_utils_js__WEBPA
19330
19356
 
19331
19357
  const sampling_rate = this.config.sampling_rate;
19332
19358
  const mel_filters = (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_2__.mel_filter_bank)(
19333
- 256, // num_frequency_bins
19359
+ 257, // num_frequency_bins
19334
19360
  this.config.num_mel_bins, // num_mel_filters
19335
19361
  20, // min_frequency
19336
19362
  Math.floor(sampling_rate / 2), // max_frequency
@@ -19339,11 +19365,6 @@ class WeSpeakerFeatureExtractor extends _base_feature_extraction_utils_js__WEBPA
19339
19365
  "kaldi", // mel_scale
19340
19366
  true, // triangularize_in_mel_space
19341
19367
  );
19342
-
19343
- // Do padding:
19344
- for (let i = 0; i < mel_filters.length; ++i) {
19345
- mel_filters[i].push(0);
19346
- }
19347
19368
  this.mel_filters = mel_filters;
19348
19369
 
19349
19370
  this.window = (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_2__.window_function)(400, 'hamming', {
@@ -24330,8 +24351,24 @@ class BPE extends TokenizerModel {
24330
24351
 
24331
24352
  this.ignore_merges = this.config.ignore_merges ?? false;
24332
24353
 
24333
- /** @type {Map<string, string[]>} */
24334
- this.cache = new Map();
24354
+ /**
24355
+ * The maximum length we should cache in a model.
24356
+ * Strings that are too long have minimal chances to cache hit anyway
24357
+ */
24358
+ this.max_length_to_cache = 256;
24359
+
24360
+ /**
24361
+ * The default capacity for a `BPE`'s internal cache.
24362
+ */
24363
+ this.cache_capacity = 10000;
24364
+ this.cache = new _utils_data_structures_js__WEBPACK_IMPORTED_MODULE_5__.LRUCache(this.cache_capacity);
24365
+ }
24366
+
24367
+ /**
24368
+ * Clears the cache.
24369
+ */
24370
+ clear_cache() {
24371
+ this.cache.clear();
24335
24372
  }
24336
24373
 
24337
24374
  /**
@@ -24458,8 +24495,10 @@ class BPE extends TokenizerModel {
24458
24495
  }
24459
24496
  }
24460
24497
 
24461
- // Save the result to the cache
24462
- this.cache.set(token, result);
24498
+ if (token.length < this.max_length_to_cache) {
24499
+ // Save the result to the cache
24500
+ this.cache.put(token, result);
24501
+ }
24463
24502
 
24464
24503
  return result;
24465
24504
  }
@@ -28259,7 +28298,8 @@ function linspace(start, end, num) {
28259
28298
  * various implementation exist, which differ in the number of filters, the shape of the filters, the way the filters
28260
28299
  * are spaced, the bandwidth of the filters, and the manner in which the spectrum is warped. The goal of these
28261
28300
  * features is to approximate the non-linear human perception of the variation in pitch with respect to the frequency.
28262
- * @param {number} num_frequency_bins Number of frequencies used to compute the spectrogram (should be the same as in `stft`).
28301
+ * @param {number} num_frequency_bins Number of frequency bins (should be the same as `n_fft // 2 + 1`
28302
+ * where `n_fft` is the size of the Fourier Transform used to compute the spectrogram).
28263
28303
  * @param {number} num_mel_filters Number of mel filters to generate.
28264
28304
  * @param {number} min_frequency Lowest frequency of interest in Hz.
28265
28305
  * @param {number} max_frequency Highest frequency of interest in Hz. This should not exceed `sampling_rate / 2`.
@@ -28285,6 +28325,14 @@ function mel_filter_bank(
28285
28325
  throw new Error('norm must be one of null or "slaney"');
28286
28326
  }
28287
28327
 
28328
+ if (num_frequency_bins < 2) {
28329
+ throw new Error(`Require num_frequency_bins: ${num_frequency_bins} >= 2`);
28330
+ }
28331
+
28332
+ if (min_frequency > max_frequency) {
28333
+ throw new Error(`Require min_frequency: ${min_frequency} <= max_frequency: ${max_frequency}`);
28334
+ }
28335
+
28288
28336
  const mel_min = hertz_to_mel(min_frequency, mel_scale);
28289
28337
  const mel_max = hertz_to_mel(max_frequency, mel_scale);
28290
28338
  const mel_freqs = linspace(mel_min, mel_max, num_mel_filters + 2);
@@ -28293,7 +28341,7 @@ function mel_filter_bank(
28293
28341
  let fft_freqs; // frequencies of FFT bins in Hz
28294
28342
 
28295
28343
  if (triangularize_in_mel_space) {
28296
- const fft_bin_width = sampling_rate / (num_frequency_bins * 2);
28344
+ const fft_bin_width = sampling_rate / ((num_frequency_bins - 1) * 2);
28297
28345
  fft_freqs = hertz_to_mel(Float64Array.from({ length: num_frequency_bins }, (_, i) => i * fft_bin_width), mel_scale);
28298
28346
  filter_freqs = mel_freqs;
28299
28347
  } else {
@@ -29170,6 +29218,7 @@ __webpack_require__.r(__webpack_exports__);
29170
29218
  /* harmony export */ __webpack_require__.d(__webpack_exports__, {
29171
29219
  /* harmony export */ CharTrie: () => (/* binding */ CharTrie),
29172
29220
  /* harmony export */ DictionarySplitter: () => (/* binding */ DictionarySplitter),
29221
+ /* harmony export */ LRUCache: () => (/* binding */ LRUCache),
29173
29222
  /* harmony export */ PriorityQueue: () => (/* binding */ PriorityQueue),
29174
29223
  /* harmony export */ TokenLattice: () => (/* binding */ TokenLattice)
29175
29224
  /* harmony export */ });
@@ -29695,6 +29744,59 @@ class DictionarySplitter {
29695
29744
  }
29696
29745
  }
29697
29746
 
29747
+ /**
29748
+ * A simple Least Recently Used (LRU) cache implementation in JavaScript.
29749
+ * This cache stores key-value pairs and evicts the least recently used item
29750
+ * when the capacity is exceeded.
29751
+ */
29752
+ class LRUCache {
29753
+ /**
29754
+ * Creates an LRUCache instance.
29755
+ * @param {number} capacity The maximum number of items the cache can hold.
29756
+ */
29757
+ constructor(capacity) {
29758
+ this.capacity = capacity;
29759
+ this.cache = new Map();
29760
+ }
29761
+
29762
+ /**
29763
+ * Retrieves the value associated with the given key and marks the key as recently used.
29764
+ * @param {any} key The key to retrieve.
29765
+ * @returns {any} The value associated with the key, or undefined if the key does not exist.
29766
+ */
29767
+ get(key) {
29768
+ if (!this.cache.has(key)) return undefined;
29769
+ const value = this.cache.get(key);
29770
+ this.cache.delete(key);
29771
+ this.cache.set(key, value);
29772
+ return value;
29773
+ }
29774
+
29775
+ /**
29776
+ * Inserts or updates the key-value pair in the cache.
29777
+ * If the key already exists, it is updated and marked as recently used.
29778
+ * If the cache exceeds its capacity, the least recently used item is evicted.
29779
+ * @param {any} key The key to add or update.
29780
+ * @param {any} value The value to associate with the key.
29781
+ */
29782
+ put(key, value) {
29783
+ if (this.cache.has(key)) {
29784
+ this.cache.delete(key);
29785
+ }
29786
+ this.cache.set(key, value);
29787
+ if (this.cache.size > this.capacity) {
29788
+ this.cache.delete(this.cache.keys().next().value);
29789
+ }
29790
+ }
29791
+
29792
+ /**
29793
+ * Clears the cache.
29794
+ */
29795
+ clear() {
29796
+ this.cache.clear();
29797
+ }
29798
+ }
29799
+
29698
29800
 
29699
29801
  /***/ }),
29700
29802
 
@@ -30302,6 +30404,22 @@ async function getModelFile(path_or_repo_id, filename, fatal = true, options = {
30302
30404
  // First, check if the a caching backend is available
30303
30405
  // If no caching mechanism available, will download the file every time
30304
30406
  let cache;
30407
+ if (!cache && _env_js__WEBPACK_IMPORTED_MODULE_2__.env.useCustomCache) {
30408
+ // Allow the user to specify a custom cache system.
30409
+ if (!_env_js__WEBPACK_IMPORTED_MODULE_2__.env.customCache) {
30410
+ throw Error('`env.useCustomCache=true`, but `env.customCache` is not defined.')
30411
+ }
30412
+
30413
+ // Check that the required methods are defined:
30414
+ if (!_env_js__WEBPACK_IMPORTED_MODULE_2__.env.customCache.match || !_env_js__WEBPACK_IMPORTED_MODULE_2__.env.customCache.put) {
30415
+ throw new Error(
30416
+ "`env.customCache` must be an object which implements the `match` and `put` functions of the Web Cache API. " +
30417
+ "For more information, see https://developer.mozilla.org/en-US/docs/Web/API/Cache"
30418
+ )
30419
+ }
30420
+ cache = _env_js__WEBPACK_IMPORTED_MODULE_2__.env.customCache;
30421
+ }
30422
+
30305
30423
  if (!cache && _env_js__WEBPACK_IMPORTED_MODULE_2__.env.useBrowserCache) {
30306
30424
  if (typeof caches === 'undefined') {
30307
30425
  throw Error('Browser cache is not available in this environment.')
@@ -30319,28 +30437,14 @@ async function getModelFile(path_or_repo_id, filename, fatal = true, options = {
30319
30437
  }
30320
30438
 
30321
30439
  if (!cache && _env_js__WEBPACK_IMPORTED_MODULE_2__.env.useFSCache) {
30322
- // TODO throw error if not available
30440
+ if (!_env_js__WEBPACK_IMPORTED_MODULE_2__.apis.IS_FS_AVAILABLE) {
30441
+ throw Error('File System Cache is not available in this environment.');
30442
+ }
30323
30443
 
30324
30444
  // If `cache_dir` is not specified, use the default cache directory
30325
30445
  cache = new FileCache(options.cache_dir ?? _env_js__WEBPACK_IMPORTED_MODULE_2__.env.cacheDir);
30326
30446
  }
30327
30447
 
30328
- if (!cache && _env_js__WEBPACK_IMPORTED_MODULE_2__.env.useCustomCache) {
30329
- // Allow the user to specify a custom cache system.
30330
- if (!_env_js__WEBPACK_IMPORTED_MODULE_2__.env.customCache) {
30331
- throw Error('`env.useCustomCache=true`, but `env.customCache` is not defined.')
30332
- }
30333
-
30334
- // Check that the required methods are defined:
30335
- if (!_env_js__WEBPACK_IMPORTED_MODULE_2__.env.customCache.match || !_env_js__WEBPACK_IMPORTED_MODULE_2__.env.customCache.put) {
30336
- throw new Error(
30337
- "`env.customCache` must be an object which implements the `match` and `put` functions of the Web Cache API. " +
30338
- "For more information, see https://developer.mozilla.org/en-US/docs/Web/API/Cache"
30339
- )
30340
- }
30341
- cache = _env_js__WEBPACK_IMPORTED_MODULE_2__.env.customCache;
30342
- }
30343
-
30344
30448
  const revision = options.revision ?? 'main';
30345
30449
  const requestURL = pathJoin(path_or_repo_id, filename);
30346
30450
 
@@ -30522,7 +30626,7 @@ async function getModelFile(path_or_repo_id, filename, fatal = true, options = {
30522
30626
  });
30523
30627
 
30524
30628
  if (result) {
30525
- if (return_path) {
30629
+ if (!_env_js__WEBPACK_IMPORTED_MODULE_2__.apis.IS_NODE_ENV && return_path) {
30526
30630
  throw new Error("Cannot return path in a browser environment.")
30527
30631
  }
30528
30632
  return result;
@@ -30531,12 +30635,18 @@ async function getModelFile(path_or_repo_id, filename, fatal = true, options = {
30531
30635
  return response.filePath;
30532
30636
  }
30533
30637
 
30534
- const path = await cache.match(cacheKey);
30535
- if (path instanceof FileResponse) {
30536
- return path.filePath;
30638
+ // Otherwise, return the cached response (most likely a `FileResponse`).
30639
+ // NOTE: A custom cache may return a Response, or a string (file path)
30640
+ const cachedResponse = await cache?.match(cacheKey);
30641
+ if (cachedResponse instanceof FileResponse) {
30642
+ return cachedResponse.filePath;
30643
+ } else if (cachedResponse instanceof Response) {
30644
+ return new Uint8Array(await cachedResponse.arrayBuffer());
30645
+ } else if (typeof cachedResponse === 'string') {
30646
+ return cachedResponse;
30537
30647
  }
30538
- throw new Error("Unable to return path for response.");
30539
30648
 
30649
+ throw new Error("Unable to get model file path or buffer.");
30540
30650
  }
30541
30651
 
30542
30652
  /**