@huggingface/transformers 3.4.1 → 3.4.2

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.
@@ -4076,7 +4076,7 @@ __webpack_require__.r(__webpack_exports__);
4076
4076
 
4077
4077
 
4078
4078
 
4079
- const VERSION = '3.4.1';
4079
+ const VERSION = '3.4.2';
4080
4080
 
4081
4081
  // Check if various APIs are available (depends on environment)
4082
4082
  const IS_BROWSER_ENV = typeof window !== "undefined" && typeof window.document !== "undefined";
@@ -6408,10 +6408,18 @@ __webpack_require__.r(__webpack_exports__);
6408
6408
  /* harmony export */ Qwen2PreTrainedModel: () => (/* binding */ Qwen2PreTrainedModel),
6409
6409
  /* harmony export */ Qwen2VLForConditionalGeneration: () => (/* binding */ Qwen2VLForConditionalGeneration),
6410
6410
  /* harmony export */ Qwen2VLPreTrainedModel: () => (/* binding */ Qwen2VLPreTrainedModel),
6411
+ /* harmony export */ RFDetrForObjectDetection: () => (/* binding */ RFDetrForObjectDetection),
6412
+ /* harmony export */ RFDetrModel: () => (/* binding */ RFDetrModel),
6413
+ /* harmony export */ RFDetrObjectDetectionOutput: () => (/* binding */ RFDetrObjectDetectionOutput),
6414
+ /* harmony export */ RFDetrPreTrainedModel: () => (/* binding */ RFDetrPreTrainedModel),
6411
6415
  /* harmony export */ RTDetrForObjectDetection: () => (/* binding */ RTDetrForObjectDetection),
6412
6416
  /* harmony export */ RTDetrModel: () => (/* binding */ RTDetrModel),
6413
6417
  /* harmony export */ RTDetrObjectDetectionOutput: () => (/* binding */ RTDetrObjectDetectionOutput),
6414
6418
  /* harmony export */ RTDetrPreTrainedModel: () => (/* binding */ RTDetrPreTrainedModel),
6419
+ /* harmony export */ RTDetrV2ForObjectDetection: () => (/* binding */ RTDetrV2ForObjectDetection),
6420
+ /* harmony export */ RTDetrV2Model: () => (/* binding */ RTDetrV2Model),
6421
+ /* harmony export */ RTDetrV2ObjectDetectionOutput: () => (/* binding */ RTDetrV2ObjectDetectionOutput),
6422
+ /* harmony export */ RTDetrV2PreTrainedModel: () => (/* binding */ RTDetrV2PreTrainedModel),
6415
6423
  /* harmony export */ ResNetForImageClassification: () => (/* binding */ ResNetForImageClassification),
6416
6424
  /* harmony export */ ResNetModel: () => (/* binding */ ResNetModel),
6417
6425
  /* harmony export */ ResNetPreTrainedModel: () => (/* binding */ ResNetPreTrainedModel),
@@ -11694,6 +11702,37 @@ class RTDetrObjectDetectionOutput extends ModelOutput {
11694
11702
  }
11695
11703
  //////////////////////////////////////////////////
11696
11704
 
11705
+
11706
+ //////////////////////////////////////////////////
11707
+ class RTDetrV2PreTrainedModel extends PreTrainedModel { }
11708
+ class RTDetrV2Model extends RTDetrV2PreTrainedModel { }
11709
+ class RTDetrV2ForObjectDetection extends RTDetrV2PreTrainedModel {
11710
+ /**
11711
+ * @param {any} model_inputs
11712
+ */
11713
+ async _call(model_inputs) {
11714
+ return new RTDetrV2ObjectDetectionOutput(await super._call(model_inputs));
11715
+ }
11716
+ }
11717
+
11718
+ class RTDetrV2ObjectDetectionOutput extends RTDetrObjectDetectionOutput {}
11719
+ //////////////////////////////////////////////////
11720
+
11721
+ //////////////////////////////////////////////////
11722
+ class RFDetrPreTrainedModel extends PreTrainedModel { }
11723
+ class RFDetrModel extends RFDetrPreTrainedModel { }
11724
+ class RFDetrForObjectDetection extends RFDetrPreTrainedModel {
11725
+ /**
11726
+ * @param {any} model_inputs
11727
+ */
11728
+ async _call(model_inputs) {
11729
+ return new RFDetrObjectDetectionOutput(await super._call(model_inputs));
11730
+ }
11731
+ }
11732
+
11733
+ class RFDetrObjectDetectionOutput extends RTDetrObjectDetectionOutput {}
11734
+ //////////////////////////////////////////////////
11735
+
11697
11736
  //////////////////////////////////////////////////
11698
11737
  class TableTransformerPreTrainedModel extends PreTrainedModel { }
11699
11738
 
@@ -14001,6 +14040,8 @@ const MODEL_MAPPING_NAMES_ENCODER_ONLY = new Map([
14001
14040
 
14002
14041
  ['detr', ['DetrModel', DetrModel]],
14003
14042
  ['rt_detr', ['RTDetrModel', RTDetrModel]],
14043
+ ['rt_detr_v2', ['RTDetrV2Model', RTDetrV2Model]],
14044
+ ['rf_detr', ['RFDetrModel', RFDetrModel]],
14004
14045
  ['table-transformer', ['TableTransformerModel', TableTransformerModel]],
14005
14046
  ['vit', ['ViTModel', ViTModel]],
14006
14047
  ['ijepa', ['IJepaModel', IJepaModel]],
@@ -14300,6 +14341,8 @@ const MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING_NAMES = new Map([
14300
14341
  const MODEL_FOR_OBJECT_DETECTION_MAPPING_NAMES = new Map([
14301
14342
  ['detr', ['DetrForObjectDetection', DetrForObjectDetection]],
14302
14343
  ['rt_detr', ['RTDetrForObjectDetection', RTDetrForObjectDetection]],
14344
+ ['rt_detr_v2', ['RTDetrV2ForObjectDetection', RTDetrV2ForObjectDetection]],
14345
+ ['rf_detr', ['RFDetrForObjectDetection', RFDetrForObjectDetection]],
14303
14346
  ['table-transformer', ['TableTransformerForObjectDetection', TableTransformerForObjectDetection]],
14304
14347
  ['yolos', ['YolosForObjectDetection', YolosForObjectDetection]],
14305
14348
  ]);
@@ -26254,13 +26297,12 @@ class PreTrainedTokenizer extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__
26254
26297
  this.decoder.end_of_word_suffix = this.model.end_of_word_suffix;
26255
26298
  }
26256
26299
 
26257
- this.added_tokens_regex = this.added_tokens.length > 0 ? new RegExp(
26258
- this.added_tokens.slice()
26259
- // Sort by length (desc) to avoid early partial matches
26260
- .sort((a, b) => b.content.length - a.content.length)
26261
- .map(x => `${x.lstrip ? '\\s*' : ''}(${(0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.escapeRegExp)(x.content)})${x.rstrip ? '\\s*' : ''}`)
26262
- .join('|')
26263
- ) : null;
26300
+ this.added_tokens_splitter = new _utils_data_structures_js__WEBPACK_IMPORTED_MODULE_5__.DictionarySplitter(
26301
+ this.added_tokens.map(x => x.content),
26302
+ );
26303
+
26304
+ /** @type {Map<string, AddedToken>} */
26305
+ this.added_tokens_map = new Map(this.added_tokens.map(x => [x.content, x]))
26264
26306
 
26265
26307
  // Set mask token if present (otherwise will be undefined, which is fine)
26266
26308
  this.mask_token = this.getToken('mask_token');
@@ -26555,40 +26597,50 @@ class PreTrainedTokenizer extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__
26555
26597
  // Actual function which does encoding, for a single text
26556
26598
  // First, we take care of special tokens. Needed to avoid issues arising from
26557
26599
  // normalization and/or pretokenization (which may not preserve special tokens)
26558
- const sections = this.added_tokens_regex ? text.split(this.added_tokens_regex).filter(x => x) : [text];
26559
-
26560
- const tokens = sections.map((x, section_index) => {
26561
- const addedToken = this.added_tokens.find(t => t.content === x);
26562
- if (addedToken !== undefined) {
26563
- // Ignore added tokens
26564
- return x
26565
- } else {
26566
- if (this.remove_space === true) {
26567
- x = x.trim().split(/\s+/).join(' ');
26600
+ const sections = this.added_tokens_splitter.split(text);
26601
+
26602
+ // Process left/right stripping of added tokens
26603
+ for (let i = 0; i < sections.length; ++i) {
26604
+ const addedToken = this.added_tokens_map.get(sections[i]);
26605
+ if (addedToken) {
26606
+ if (addedToken.lstrip && i > 0) {
26607
+ sections[i - 1] = sections[i - 1].trimEnd();
26568
26608
  }
26569
- if (this.do_lowercase_and_remove_accent) {
26570
- x = lowercase_and_remove_accent(x);
26571
- }
26572
-
26573
- if (this.normalizer !== null) {
26574
- x = this.normalizer(x);
26609
+ if (addedToken.rstrip && i < sections.length - 1) {
26610
+ sections[i + 1] = sections[i + 1].trimStart();
26575
26611
  }
26612
+ }
26613
+ }
26576
26614
 
26577
- // If, after normalization, this section is empty (e.g., trimming whitespace),
26578
- // we return an empty array
26579
- if (x.length === 0) {
26580
- return [];
26581
- }
26615
+ const tokens = sections.flatMap((x, section_index) => {
26616
+ if (x.length === 0) return [];
26617
+ if (this.added_tokens_map.has(x)) return [x]; // Return added tokens unchanged
26582
26618
 
26583
- const sectionTokens = (this.pre_tokenizer !== null) ? this.pre_tokenizer(x, {
26584
- section_index,
26585
- }) : [x];
26619
+ if (this.remove_space === true) {
26620
+ x = x.trim().split(/\s+/).join(' ');
26621
+ }
26622
+ if (this.do_lowercase_and_remove_accent) {
26623
+ x = lowercase_and_remove_accent(x);
26624
+ }
26586
26625
 
26587
- const tokens = this.model(sectionTokens);
26626
+ if (this.normalizer !== null) {
26627
+ x = this.normalizer(x);
26628
+ }
26588
26629
 
26589
- return tokens;
26630
+ // If, after normalization, this section is empty (e.g., trimming whitespace),
26631
+ // we return an empty array
26632
+ if (x.length === 0) {
26633
+ return [];
26590
26634
  }
26591
- }).flat();
26635
+
26636
+ const sectionTokens = (this.pre_tokenizer !== null) ? this.pre_tokenizer(x, {
26637
+ section_index,
26638
+ }) : [x];
26639
+
26640
+ const tokens = this.model(sectionTokens);
26641
+
26642
+ return tokens;
26643
+ });
26592
26644
 
26593
26645
  return tokens;
26594
26646
  }
@@ -29217,6 +29269,7 @@ function count(arr, value) {
29217
29269
  __webpack_require__.r(__webpack_exports__);
29218
29270
  /* harmony export */ __webpack_require__.d(__webpack_exports__, {
29219
29271
  /* harmony export */ CharTrie: () => (/* binding */ CharTrie),
29272
+ /* harmony export */ DictionarySplitter: () => (/* binding */ DictionarySplitter),
29220
29273
  /* harmony export */ PriorityQueue: () => (/* binding */ PriorityQueue),
29221
29274
  /* harmony export */ TokenLattice: () => (/* binding */ TokenLattice)
29222
29275
  /* harmony export */ });
@@ -29668,6 +29721,80 @@ class TokenLatticeNode {
29668
29721
  }
29669
29722
  }
29670
29723
 
29724
+ /**
29725
+ * A data structure which uses a trie to split a string into tokens based on a dictionary.
29726
+ * It can also use a regular expression to preprocess the input text before splitting.
29727
+ *
29728
+ * NOTE: To ensure multi-byte characters are handled correctly, we operate at byte-level instead of character-level.
29729
+ */
29730
+ class DictionarySplitter {
29731
+ /**
29732
+ * @param {string[]} dictionary The dictionary of words to use for splitting.
29733
+ */
29734
+ constructor(dictionary) {
29735
+ this.trie = this._buildTrie(dictionary);
29736
+ }
29737
+
29738
+ /**
29739
+ * Builds a trie from the given dictionary.
29740
+ * @param {string[]} dictionary The dictionary of words to build the trie from.
29741
+ * @returns {Object} The root node of the trie.
29742
+ * @private
29743
+ */
29744
+ _buildTrie(dictionary) {
29745
+ const trie = Object.create(null);
29746
+ for (const word of dictionary) {
29747
+ let node = trie;
29748
+ for (let i = 0; i < word.length; ++i) {
29749
+ node = (node[word[i]] ??= Object.create(null));
29750
+ }
29751
+ node.end = word;
29752
+ }
29753
+ return trie;
29754
+ }
29755
+
29756
+ /**
29757
+ * Splits the input text into tokens based on the dictionary.
29758
+ * @param {string} text The input text to split.
29759
+ * @returns {string[]} An array of tokens.
29760
+ */
29761
+ split(text) {
29762
+ const result = [];
29763
+ const n = text.length;
29764
+ let start = 0;
29765
+ let i = 0;
29766
+
29767
+ while (i < n) {
29768
+ let node = this.trie;
29769
+ let match = null;
29770
+ let j = i;
29771
+
29772
+ while (j < n && (node = node[text[j]])) {
29773
+ if (node.end) {
29774
+ // Always keep the last (i.e., longest) match.
29775
+ match = node.end;
29776
+ }
29777
+ ++j;
29778
+ }
29779
+
29780
+ if (match) {
29781
+ if (i > start) {
29782
+ result.push(text.slice(start, i));
29783
+ }
29784
+ result.push(match);
29785
+ i += match.length;
29786
+ start = i;
29787
+ } else {
29788
+ ++i;
29789
+ }
29790
+ }
29791
+ if (start < n) {
29792
+ result.push(text.slice(start));
29793
+ }
29794
+ return result;
29795
+ }
29796
+ }
29797
+
29671
29798
 
29672
29799
  /***/ }),
29673
29800
 
@@ -30051,6 +30178,22 @@ function isValidUrl(string, protocols = null, validHosts = null) {
30051
30178
  return true;
30052
30179
  }
30053
30180
 
30181
+ const REPO_ID_REGEX = /^(\b[\w\-.]+\b\/)?\b[\w\-.]{1,96}\b$/;
30182
+
30183
+ /**
30184
+ * Tests whether a string is a valid Hugging Face model ID or not.
30185
+ * Adapted from https://github.com/huggingface/huggingface_hub/blob/6378820ebb03f071988a96c7f3268f5bdf8f9449/src/huggingface_hub/utils/_validators.py#L119-L170
30186
+ *
30187
+ * @param {string} string The string to test
30188
+ * @returns {boolean} True if the string is a valid model ID, false otherwise.
30189
+ */
30190
+ function isValidHfModelId(string) {
30191
+ if (!REPO_ID_REGEX.test(string)) return false;
30192
+ if (string.includes("..") || string.includes("--")) return false;
30193
+ if (string.endsWith(".git") || string.endsWith(".ipynb")) return false;
30194
+ return true;
30195
+ }
30196
+
30054
30197
  /**
30055
30198
  * Helper function to get a file, using either the Fetch API or FileSystem API.
30056
30199
  *
@@ -30303,12 +30446,13 @@ async function getModelFile(path_or_repo_id, filename, fatal = true, options = {
30303
30446
  }
30304
30447
 
30305
30448
  const revision = options.revision ?? 'main';
30449
+ const requestURL = pathJoin(path_or_repo_id, filename);
30306
30450
 
30307
- let requestURL = pathJoin(path_or_repo_id, filename);
30308
- let cachePath = pathJoin(_env_js__WEBPACK_IMPORTED_MODULE_2__.env.localModelPath, requestURL);
30309
-
30310
- let localPath = requestURL;
30311
- let remoteURL = pathJoin(
30451
+ const validModelId = isValidHfModelId(path_or_repo_id);
30452
+ const localPath = validModelId
30453
+ ? pathJoin(_env_js__WEBPACK_IMPORTED_MODULE_2__.env.localModelPath, requestURL)
30454
+ : requestURL;
30455
+ const remoteURL = pathJoin(
30312
30456
  _env_js__WEBPACK_IMPORTED_MODULE_2__.env.remoteHost,
30313
30457
  _env_js__WEBPACK_IMPORTED_MODULE_2__.env.remotePathTemplate
30314
30458
  .replaceAll('{model}', path_or_repo_id)
@@ -30316,14 +30460,14 @@ async function getModelFile(path_or_repo_id, filename, fatal = true, options = {
30316
30460
  filename
30317
30461
  );
30318
30462
 
30319
- // Choose cache key for filesystem cache
30320
- // When using the main revision (default), we use the request URL as the cache key.
30321
- // If a specific revision is requested, we account for this in the cache key.
30322
- let fsCacheKey = revision === 'main' ? requestURL : pathJoin(path_or_repo_id, revision, filename);
30323
-
30324
30463
  /** @type {string} */
30325
30464
  let cacheKey;
30326
- let proposedCacheKey = cache instanceof FileCache ? fsCacheKey : remoteURL;
30465
+ const proposedCacheKey = cache instanceof FileCache
30466
+ // Choose cache key for filesystem cache
30467
+ // When using the main revision (default), we use the request URL as the cache key.
30468
+ // If a specific revision is requested, we account for this in the cache key.
30469
+ ? revision === 'main' ? requestURL : pathJoin(path_or_repo_id, revision, filename)
30470
+ : remoteURL;
30327
30471
 
30328
30472
  // Whether to cache the final response in the end.
30329
30473
  let toCacheResponse = false;
@@ -30336,11 +30480,10 @@ async function getModelFile(path_or_repo_id, filename, fatal = true, options = {
30336
30480
  // 1. We first try to get from cache using the local path. In some environments (like deno),
30337
30481
  // non-URL cache keys are not allowed. In these cases, `response` will be undefined.
30338
30482
  // 2. If no response is found, we try to get from cache using the remote URL or file system cache.
30339
- response = await tryCache(cache, cachePath, proposedCacheKey);
30483
+ response = await tryCache(cache, localPath, proposedCacheKey);
30340
30484
  }
30341
30485
 
30342
30486
  const cacheHit = response !== undefined;
30343
-
30344
30487
  if (response === undefined) {
30345
30488
  // Caching not available, or file is not cached, so we perform the request
30346
30489
 
@@ -30358,9 +30501,9 @@ async function getModelFile(path_or_repo_id, filename, fatal = true, options = {
30358
30501
  console.warn(`Unable to load from local path "${localPath}": "${e}"`);
30359
30502
  }
30360
30503
  } else if (options.local_files_only) {
30361
- throw new Error(`\`local_files_only=true\`, but attempted to load a remote file from: ${localPath}.`);
30504
+ throw new Error(`\`local_files_only=true\`, but attempted to load a remote file from: ${requestURL}.`);
30362
30505
  } else if (!_env_js__WEBPACK_IMPORTED_MODULE_2__.env.allowRemoteModels) {
30363
- throw new Error(`\`env.allowRemoteModels=false\`, but attempted to load a remote file from: ${localPath}.`);
30506
+ throw new Error(`\`env.allowRemoteModels=false\`, but attempted to load a remote file from: ${requestURL}.`);
30364
30507
  }
30365
30508
  }
30366
30509
 
@@ -30380,6 +30523,11 @@ async function getModelFile(path_or_repo_id, filename, fatal = true, options = {
30380
30523
  return null;
30381
30524
  }
30382
30525
  }
30526
+ if (!validModelId) {
30527
+ // Before making any requests to the remote server, we check if the model ID is valid.
30528
+ // This prevents unnecessary network requests for invalid model IDs.
30529
+ throw Error(`Local file missing at "${localPath}" and download aborted due to invalid model ID "${path_or_repo_id}".`);
30530
+ }
30383
30531
 
30384
30532
  // File not found locally, so we try to download it from the remote server
30385
30533
  response = await getFile(remoteURL);
@@ -34871,11 +35019,19 @@ __webpack_require__.r(__webpack_exports__);
34871
35019
  /* harmony export */ Qwen2VLImageProcessor: () => (/* reexport safe */ _models_image_processors_js__WEBPACK_IMPORTED_MODULE_14__.Qwen2VLImageProcessor),
34872
35020
  /* harmony export */ Qwen2VLPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Qwen2VLPreTrainedModel),
34873
35021
  /* harmony export */ Qwen2VLProcessor: () => (/* reexport safe */ _models_processors_js__WEBPACK_IMPORTED_MODULE_17__.Qwen2VLProcessor),
35022
+ /* harmony export */ RFDetrForObjectDetection: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RFDetrForObjectDetection),
35023
+ /* harmony export */ RFDetrModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RFDetrModel),
35024
+ /* harmony export */ RFDetrObjectDetectionOutput: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RFDetrObjectDetectionOutput),
35025
+ /* harmony export */ RFDetrPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RFDetrPreTrainedModel),
34874
35026
  /* harmony export */ RTDetrForObjectDetection: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RTDetrForObjectDetection),
34875
35027
  /* harmony export */ RTDetrImageProcessor: () => (/* reexport safe */ _models_image_processors_js__WEBPACK_IMPORTED_MODULE_14__.RTDetrImageProcessor),
34876
35028
  /* harmony export */ RTDetrModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RTDetrModel),
34877
35029
  /* harmony export */ RTDetrObjectDetectionOutput: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RTDetrObjectDetectionOutput),
34878
35030
  /* harmony export */ RTDetrPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RTDetrPreTrainedModel),
35031
+ /* harmony export */ RTDetrV2ForObjectDetection: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RTDetrV2ForObjectDetection),
35032
+ /* harmony export */ RTDetrV2Model: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RTDetrV2Model),
35033
+ /* harmony export */ RTDetrV2ObjectDetectionOutput: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RTDetrV2ObjectDetectionOutput),
35034
+ /* harmony export */ RTDetrV2PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RTDetrV2PreTrainedModel),
34879
35035
  /* harmony export */ RawAudio: () => (/* reexport safe */ _utils_audio_js__WEBPACK_IMPORTED_MODULE_5__.RawAudio),
34880
35036
  /* harmony export */ RawImage: () => (/* reexport safe */ _utils_image_js__WEBPACK_IMPORTED_MODULE_6__.RawImage),
34881
35037
  /* harmony export */ RawVideo: () => (/* reexport safe */ _utils_video_js__WEBPACK_IMPORTED_MODULE_7__.RawVideo),