@huggingface/transformers 3.3.3 → 3.4.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 (89) hide show
  1. package/README.md +9 -3
  2. package/dist/ort-wasm-simd-threaded.jsep.mjs +124 -115
  3. package/dist/ort-wasm-simd-threaded.jsep.wasm +0 -0
  4. package/dist/transformers.js +2480 -1457
  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.cjs → transformers.node.cjs} +1412 -2395
  9. package/dist/transformers.node.cjs.map +1 -0
  10. package/dist/transformers.node.min.cjs +2 -0
  11. package/dist/transformers.node.min.cjs.map +1 -0
  12. package/dist/transformers.node.min.mjs +2 -0
  13. package/dist/transformers.node.min.mjs.map +1 -0
  14. package/dist/{transformers.mjs → transformers.node.mjs} +1440 -2375
  15. package/dist/transformers.node.mjs.map +1 -0
  16. package/dist/transformers.web.js +35713 -0
  17. package/dist/transformers.web.js.map +1 -0
  18. package/dist/transformers.web.min.js +2 -0
  19. package/dist/transformers.web.min.js.map +1 -0
  20. package/package.json +6 -6
  21. package/src/backends/onnx.js +14 -15
  22. package/src/configs.js +4 -1
  23. package/src/env.js +1 -1
  24. package/src/generation/streamers.js +4 -3
  25. package/src/models/dac/feature_extraction_dac.js +3 -0
  26. package/src/models/encodec/feature_extraction_encodec.js +32 -0
  27. package/src/models/feature_extractors.js +2 -0
  28. package/src/models/idefics3/image_processing_idefics3.js +1 -1
  29. package/src/models/image_processors.js +1 -0
  30. package/src/models/processors.js +2 -0
  31. package/src/models/smolvlm/image_processing_smolvlm.js +2 -0
  32. package/src/models/smolvlm/processing_smolvlm.js +2 -0
  33. package/src/models/ultravox/processing_ultravox.js +54 -0
  34. package/src/models/whisper/common_whisper.js +7 -1
  35. package/src/models/whisper/feature_extraction_whisper.js +18 -10
  36. package/src/models.js +456 -76
  37. package/src/pipelines.js +111 -7
  38. package/src/tokenizers.js +42 -28
  39. package/src/transformers.js +1 -0
  40. package/src/utils/audio.js +2 -0
  41. package/src/utils/hub.js +140 -80
  42. package/src/utils/maths.js +1 -1
  43. package/src/utils/tensor.js +6 -3
  44. package/src/utils/video.js +128 -0
  45. package/types/backends/onnx.d.ts +2 -2
  46. package/types/backends/onnx.d.ts.map +1 -1
  47. package/types/configs.d.ts +1 -1
  48. package/types/configs.d.ts.map +1 -1
  49. package/types/generation/streamers.d.ts.map +1 -1
  50. package/types/models/dac/feature_extraction_dac.d.ts +4 -0
  51. package/types/models/dac/feature_extraction_dac.d.ts.map +1 -0
  52. package/types/models/encodec/feature_extraction_encodec.d.ts +13 -0
  53. package/types/models/encodec/feature_extraction_encodec.d.ts.map +1 -0
  54. package/types/models/feature_extractors.d.ts +2 -0
  55. package/types/models/florence2/processing_florence2.d.ts +1 -1
  56. package/types/models/florence2/processing_florence2.d.ts.map +1 -1
  57. package/types/models/image_processors.d.ts +1 -0
  58. package/types/models/processors.d.ts +2 -0
  59. package/types/models/smolvlm/image_processing_smolvlm.d.ts +2 -0
  60. package/types/models/smolvlm/image_processing_smolvlm.d.ts.map +1 -0
  61. package/types/models/smolvlm/processing_smolvlm.d.ts +2 -0
  62. package/types/models/smolvlm/processing_smolvlm.d.ts.map +1 -0
  63. package/types/models/ultravox/processing_ultravox.d.ts +16 -0
  64. package/types/models/ultravox/processing_ultravox.d.ts.map +1 -0
  65. package/types/models/whisper/common_whisper.d.ts.map +1 -1
  66. package/types/models/whisper/feature_extraction_whisper.d.ts +3 -1
  67. package/types/models/whisper/feature_extraction_whisper.d.ts.map +1 -1
  68. package/types/models.d.ts +132 -4
  69. package/types/models.d.ts.map +1 -1
  70. package/types/pipelines.d.ts +50 -4
  71. package/types/pipelines.d.ts.map +1 -1
  72. package/types/tokenizers.d.ts.map +1 -1
  73. package/types/transformers.d.ts +1 -0
  74. package/types/tsconfig.tsbuildinfo +1 -1
  75. package/types/utils/audio.d.ts.map +1 -1
  76. package/types/utils/hub.d.ts +19 -7
  77. package/types/utils/hub.d.ts.map +1 -1
  78. package/types/utils/maths.d.ts +2 -2
  79. package/types/utils/maths.d.ts.map +1 -1
  80. package/types/utils/tensor.d.ts +17 -18
  81. package/types/utils/tensor.d.ts.map +1 -1
  82. package/types/utils/video.d.ts +37 -0
  83. package/types/utils/video.d.ts.map +1 -0
  84. package/dist/transformers.cjs.map +0 -1
  85. package/dist/transformers.min.cjs +0 -2
  86. package/dist/transformers.min.cjs.map +0 -1
  87. package/dist/transformers.min.mjs +0 -2
  88. package/dist/transformers.min.mjs.map +0 -1
  89. package/dist/transformers.mjs.map +0 -1
package/src/utils/hub.js CHANGED
@@ -8,9 +8,16 @@
8
8
  import fs from 'fs';
9
9
  import path from 'path';
10
10
 
11
- import { env } from '../env.js';
11
+ import { apis, env } from '../env.js';
12
12
  import { dispatchCallback } from './core.js';
13
13
 
14
+ /**
15
+ * @typedef {boolean|number} ExternalData Whether to load the model using the external data format (used for models >= 2GB in size).
16
+ * If `true`, the model will be loaded using the external data format.
17
+ * If a number, this many chunks will be loaded using the external data format (of the form: "model.onnx_data[_{chunk_number}]").
18
+ */
19
+ export const MAX_EXTERNAL_DATA_CHUNKS = 100;
20
+
14
21
  /**
15
22
  * @typedef {Object} PretrainedOptions Options for loading a pretrained model.
16
23
  * @property {import('./core.js').ProgressCallback} [progress_callback=null] If specified, this function will be called during model construction, to provide the user with progress updates.
@@ -31,7 +38,7 @@ import { dispatchCallback } from './core.js';
31
38
  * @property {string} [model_file_name=null] If specified, load the model with this name (excluding the .onnx suffix). Currently only valid for encoder- or decoder-only models.
32
39
  * @property {import("./devices.js").DeviceType|Record<string, import("./devices.js").DeviceType>} [device=null] The device to run the model on. If not specified, the device will be chosen from the environment settings.
33
40
  * @property {import("./dtypes.js").DataType|Record<string, import("./dtypes.js").DataType>} [dtype=null] The data type to use for the model. If not specified, the data type will be chosen from the environment settings.
34
- * @property {boolean|Record<string, boolean>} [use_external_data_format=false] Whether to load the model using the external data format (used for models >= 2GB in size).
41
+ * @property {ExternalData|Record<string, ExternalData>} [use_external_data_format=false] Whether to load the model using the external data format (used for models >= 2GB in size).
35
42
  * @property {import('onnxruntime-common').InferenceSession.SessionOptions} [session_options] (Optional) User-specified session options passed to the runtime. If not provided, suitable defaults will be chosen.
36
43
  */
37
44
 
@@ -57,7 +64,7 @@ class FileResponse {
57
64
 
58
65
  /**
59
66
  * Creates a new `FileResponse` object.
60
- * @param {string|URL} filePath
67
+ * @param {string} filePath
61
68
  */
62
69
  constructor(filePath) {
63
70
  this.filePath = filePath;
@@ -73,13 +80,15 @@ class FileResponse {
73
80
 
74
81
  this.updateContentType();
75
82
 
76
- let self = this;
83
+ const stream = fs.createReadStream(filePath);
77
84
  this.body = new ReadableStream({
78
85
  start(controller) {
79
- self.arrayBuffer().then(buffer => {
80
- controller.enqueue(new Uint8Array(buffer));
81
- controller.close();
82
- })
86
+ stream.on('data', (chunk) => controller.enqueue(chunk));
87
+ stream.on('end', () => controller.close());
88
+ stream.on('error', (err) => controller.error(err));
89
+ },
90
+ cancel() {
91
+ stream.destroy();
83
92
  }
84
93
  });
85
94
  } else {
@@ -190,7 +199,7 @@ function isValidUrl(string, protocols = null, validHosts = null) {
190
199
  export async function getFile(urlOrPath) {
191
200
 
192
201
  if (env.useFS && !isValidUrl(urlOrPath, ['http:', 'https:', 'blob:'])) {
193
- return new FileResponse(urlOrPath);
202
+ return new FileResponse(urlOrPath.toString());
194
203
 
195
204
  } else if (typeof process !== 'undefined' && process?.release?.name === 'node') {
196
205
  const IS_CI = !!process.env?.TESTING_REMOTELY;
@@ -281,20 +290,52 @@ class FileCache {
281
290
  /**
282
291
  * Adds the given response to the cache.
283
292
  * @param {string} request
284
- * @param {Response|FileResponse} response
293
+ * @param {Response} response
294
+ * @param {(data: {progress: number, loaded: number, total: number}) => void} [progress_callback] Optional.
295
+ * The function to call with progress updates
285
296
  * @returns {Promise<void>}
286
297
  */
287
- async put(request, response) {
288
- const buffer = Buffer.from(await response.arrayBuffer());
289
-
290
- let outputPath = path.join(this.path, request);
298
+ async put(request, response, progress_callback = undefined) {
299
+ let filePath = path.join(this.path, request);
291
300
 
292
301
  try {
293
- await fs.promises.mkdir(path.dirname(outputPath), { recursive: true });
294
- await fs.promises.writeFile(outputPath, buffer);
302
+ const contentLength = response.headers.get('Content-Length');
303
+ const total = parseInt(contentLength ?? '0');
304
+ let loaded = 0;
305
+
306
+ await fs.promises.mkdir(path.dirname(filePath), { recursive: true });
307
+ const fileStream = fs.createWriteStream(filePath);
308
+ const reader = response.body.getReader();
309
+
310
+ while (true) {
311
+ const { done, value } = await reader.read();
312
+ if (done) {
313
+ break;
314
+ }
315
+
316
+ await new Promise((resolve, reject) => {
317
+ fileStream.write(value, (err) => {
318
+ if (err) {
319
+ reject(err);
320
+ return;
321
+ }
322
+ resolve();
323
+ });
324
+ });
325
+
326
+ loaded += value.length;
327
+ const progress = total ? (loaded / total) * 100 : 0;
328
+
329
+ progress_callback?.({ progress, loaded, total });
330
+ }
295
331
 
296
- } catch (err) {
297
- console.warn('An error occurred while writing the file to cache:', err)
332
+ fileStream.close();
333
+ } catch (error) {
334
+ // Clean up the file if an error occurred during download
335
+ try {
336
+ await fs.promises.unlink(filePath);
337
+ } catch { }
338
+ throw error;
298
339
  }
299
340
  }
300
341
 
@@ -325,21 +366,21 @@ async function tryCache(cache, ...names) {
325
366
  }
326
367
 
327
368
  /**
328
- *
329
369
  * Retrieves a file from either a remote URL using the Fetch API or from the local file system using the FileSystem API.
330
370
  * If the filesystem is available and `env.useCache = true`, the file will be downloaded and cached.
331
- *
371
+ *
332
372
  * @param {string} path_or_repo_id This can be either:
333
373
  * - a string, the *model id* of a model repo on huggingface.co.
334
374
  * - a path to a *directory* potentially containing the file.
335
375
  * @param {string} filename The name of the file to locate in `path_or_repo`.
336
376
  * @param {boolean} [fatal=true] Whether to throw an error if the file is not found.
337
377
  * @param {PretrainedOptions} [options] An object containing optional parameters.
338
- *
378
+ * @param {boolean} [return_path=false] Whether to return the path of the file instead of the file content.
379
+ *
339
380
  * @throws Will throw an error if the file is not found and `fatal` is true.
340
- * @returns {Promise<Uint8Array>} A Promise that resolves with the file content as a buffer.
381
+ * @returns {Promise<string|Uint8Array>} A Promise that resolves with the file content as a Uint8Array if `return_path` is false, or the file path as a string if `return_path` is true.
341
382
  */
342
- export async function getModelFile(path_or_repo_id, filename, fatal = true, options = {}) {
383
+ export async function getModelFile(path_or_repo_id, filename, fatal = true, options = {}, return_path = false) {
343
384
 
344
385
  if (!env.allowLocalModels) {
345
386
  // User has disabled local models, so we just make sure other settings are correct.
@@ -403,8 +444,9 @@ export async function getModelFile(path_or_repo_id, filename, fatal = true, opti
403
444
  const revision = options.revision ?? 'main';
404
445
 
405
446
  let requestURL = pathJoin(path_or_repo_id, filename);
406
- let localPath = pathJoin(env.localModelPath, requestURL);
447
+ let cachePath = pathJoin(env.localModelPath, requestURL);
407
448
 
449
+ let localPath = requestURL;
408
450
  let remoteURL = pathJoin(
409
451
  env.remoteHost,
410
452
  env.remotePathTemplate
@@ -433,7 +475,7 @@ export async function getModelFile(path_or_repo_id, filename, fatal = true, opti
433
475
  // 1. We first try to get from cache using the local path. In some environments (like deno),
434
476
  // non-URL cache keys are not allowed. In these cases, `response` will be undefined.
435
477
  // 2. If no response is found, we try to get from cache using the remote URL or file system cache.
436
- response = await tryCache(cache, localPath, proposedCacheKey);
478
+ response = await tryCache(cache, cachePath, proposedCacheKey);
437
479
  }
438
480
 
439
481
  const cacheHit = response !== undefined;
@@ -455,9 +497,9 @@ export async function getModelFile(path_or_repo_id, filename, fatal = true, opti
455
497
  console.warn(`Unable to load from local path "${localPath}": "${e}"`);
456
498
  }
457
499
  } else if (options.local_files_only) {
458
- throw new Error(`\`local_files_only=true\`, but attempted to load a remote file from: ${requestURL}.`);
500
+ throw new Error(`\`local_files_only=true\`, but attempted to load a remote file from: ${localPath}.`);
459
501
  } else if (!env.allowRemoteModels) {
460
- throw new Error(`\`env.allowRemoteModels=false\`, but attempted to load a remote file from: ${requestURL}.`);
502
+ throw new Error(`\`env.allowRemoteModels=false\`, but attempted to load a remote file from: ${localPath}.`);
461
503
  }
462
504
  }
463
505
 
@@ -504,41 +546,45 @@ export async function getModelFile(path_or_repo_id, filename, fatal = true, opti
504
546
  file: filename
505
547
  })
506
548
 
507
- /** @type {Uint8Array} */
508
- let buffer;
509
-
510
- if (!options.progress_callback) {
511
- // If no progress callback is specified, we can use the `.arrayBuffer()`
512
- // method to read the response.
513
- buffer = new Uint8Array(await response.arrayBuffer());
514
-
515
- } else if (
516
- cacheHit // The item is being read from the cache
517
- &&
518
- typeof navigator !== 'undefined' && /firefox/i.test(navigator.userAgent) // We are in Firefox
519
- ) {
520
- // Due to bug in Firefox, we cannot display progress when loading from cache.
521
- // Fortunately, since this should be instantaneous, this should not impact users too much.
522
- buffer = new Uint8Array(await response.arrayBuffer());
523
-
524
- // For completeness, we still fire the final progress callback
525
- dispatchCallback(options.progress_callback, {
526
- status: 'progress',
527
- name: path_or_repo_id,
528
- file: filename,
529
- progress: 100,
530
- loaded: buffer.length,
531
- total: buffer.length,
532
- })
533
- } else {
534
- buffer = await readResponse(response, data => {
549
+ let result;
550
+ if (!(apis.IS_NODE_ENV && return_path)) {
551
+ /** @type {Uint8Array} */
552
+ let buffer;
553
+
554
+ if (!options.progress_callback) {
555
+ // If no progress callback is specified, we can use the `.arrayBuffer()`
556
+ // method to read the response.
557
+ buffer = new Uint8Array(await response.arrayBuffer());
558
+
559
+ } else if (
560
+ cacheHit // The item is being read from the cache
561
+ &&
562
+ typeof navigator !== 'undefined' && /firefox/i.test(navigator.userAgent) // We are in Firefox
563
+ ) {
564
+ // Due to bug in Firefox, we cannot display progress when loading from cache.
565
+ // Fortunately, since this should be instantaneous, this should not impact users too much.
566
+ buffer = new Uint8Array(await response.arrayBuffer());
567
+
568
+ // For completeness, we still fire the final progress callback
535
569
  dispatchCallback(options.progress_callback, {
536
570
  status: 'progress',
537
571
  name: path_or_repo_id,
538
572
  file: filename,
539
- ...data,
573
+ progress: 100,
574
+ loaded: buffer.length,
575
+ total: buffer.length,
540
576
  })
541
- })
577
+ } else {
578
+ buffer = await readResponse(response, data => {
579
+ dispatchCallback(options.progress_callback, {
580
+ status: 'progress',
581
+ name: path_or_repo_id,
582
+ file: filename,
583
+ ...data,
584
+ })
585
+ })
586
+ }
587
+ result = buffer;
542
588
  }
543
589
 
544
590
  if (
@@ -549,25 +595,43 @@ export async function getModelFile(path_or_repo_id, filename, fatal = true, opti
549
595
  // Check again whether request is in cache. If not, we add the response to the cache
550
596
  (await cache.match(cacheKey) === undefined)
551
597
  ) {
552
- // NOTE: We use `new Response(buffer, ...)` instead of `response.clone()` to handle LFS files
553
- await cache.put(cacheKey, new Response(buffer, {
554
- headers: response.headers
555
- }))
556
- .catch(err => {
557
- // Do not crash if unable to add to cache (e.g., QuotaExceededError).
558
- // Rather, log a warning and proceed with execution.
559
- console.warn(`Unable to add response to browser cache: ${err}.`);
560
- });
561
-
598
+ if (!result) {
599
+ // We haven't yet read the response body, so we need to do so now.
600
+ await cache.put(cacheKey, /** @type {Response} */(response), options.progress_callback);
601
+ } else {
602
+ // NOTE: We use `new Response(buffer, ...)` instead of `response.clone()` to handle LFS files
603
+ await cache.put(cacheKey, new Response(result, {
604
+ headers: response.headers
605
+ }))
606
+ .catch(err => {
607
+ // Do not crash if unable to add to cache (e.g., QuotaExceededError).
608
+ // Rather, log a warning and proceed with execution.
609
+ console.warn(`Unable to add response to browser cache: ${err}.`);
610
+ });
611
+ }
562
612
  }
563
-
564
613
  dispatchCallback(options.progress_callback, {
565
614
  status: 'done',
566
615
  name: path_or_repo_id,
567
616
  file: filename
568
617
  });
569
618
 
570
- return buffer;
619
+ if (result) {
620
+ if (return_path) {
621
+ throw new Error("Cannot return path in a browser environment.")
622
+ }
623
+ return result;
624
+ }
625
+ if (response instanceof FileResponse) {
626
+ return response.filePath;
627
+ }
628
+
629
+ const path = await cache.match(cacheKey);
630
+ if (path instanceof FileResponse) {
631
+ return path.filePath;
632
+ }
633
+ throw new Error("Unable to return path for response.");
634
+
571
635
  }
572
636
 
573
637
  /**
@@ -581,14 +645,14 @@ export async function getModelFile(path_or_repo_id, filename, fatal = true, opti
581
645
  * @throws Will throw an error if the file is not found and `fatal` is true.
582
646
  */
583
647
  export async function getModelJSON(modelPath, fileName, fatal = true, options = {}) {
584
- let buffer = await getModelFile(modelPath, fileName, fatal, options);
648
+ const buffer = await getModelFile(modelPath, fileName, fatal, options, false);
585
649
  if (buffer === null) {
586
650
  // Return empty object
587
651
  return {}
588
652
  }
589
653
 
590
- let decoder = new TextDecoder('utf-8');
591
- let jsonData = decoder.decode(buffer);
654
+ const decoder = new TextDecoder('utf-8');
655
+ const jsonData = decoder.decode(/** @type {Uint8Array} */(buffer));
592
656
 
593
657
  return JSON.parse(jsonData);
594
658
  }
@@ -614,30 +678,26 @@ async function readResponse(response, progress_callback) {
614
678
  const { done, value } = await reader.read();
615
679
  if (done) return;
616
680
 
617
- let newLoaded = loaded + value.length;
681
+ const newLoaded = loaded + value.length;
618
682
  if (newLoaded > total) {
619
683
  total = newLoaded;
620
684
 
621
685
  // Adding the new data will overflow buffer.
622
686
  // In this case, we extend the buffer
623
- let newBuffer = new Uint8Array(total);
687
+ const newBuffer = new Uint8Array(total);
624
688
 
625
689
  // copy contents
626
690
  newBuffer.set(buffer);
627
691
 
628
692
  buffer = newBuffer;
629
693
  }
630
- buffer.set(value, loaded)
694
+ buffer.set(value, loaded);
631
695
  loaded = newLoaded;
632
696
 
633
697
  const progress = (loaded / total) * 100;
634
698
 
635
699
  // Call your function here
636
- progress_callback({
637
- progress: progress,
638
- loaded: loaded,
639
- total: total,
640
- })
700
+ progress_callback({ progress, loaded, total });
641
701
 
642
702
  return read();
643
703
  }
@@ -9,7 +9,7 @@
9
9
  */
10
10
 
11
11
  /**
12
- * @typedef {Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array} TypedArray
12
+ * @typedef {Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float16Array | Float32Array | Float64Array} TypedArray
13
13
  * @typedef {BigInt64Array | BigUint64Array} BigTypedArray
14
14
  * @typedef {TypedArray | BigTypedArray} AnyTypedArray
15
15
  */
@@ -20,9 +20,11 @@ import {
20
20
 
21
21
  import { TensorOpRegistry } from '../ops/registry.js';
22
22
 
23
- const DataTypeMap = Object.freeze({
23
+ export const DataTypeMap = Object.freeze({
24
24
  float32: Float32Array,
25
- float16: Uint16Array,
25
+ // @ts-ignore ts(2552) Limited availability of Float16Array across browsers:
26
+ // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Float16Array
27
+ float16: typeof Float16Array !== "undefined" ? Float16Array: Uint16Array,
26
28
  float64: Float64Array,
27
29
  string: Array, // string[]
28
30
  int8: Int8Array,
@@ -89,8 +91,9 @@ export class Tensor {
89
91
  // Create new tensor
90
92
  this.ort_tensor = new ONNXTensor(
91
93
  /** @type {DataType} */(args[0]),
94
+ // @ts-expect-error ts(2769) Type 'number' is not assignable to type 'bigint'.
92
95
  /** @type {Exclude<import('./maths.js').AnyTypedArray, Uint8ClampedArray>} */(args[1]),
93
- args[2]
96
+ args[2],
94
97
  );
95
98
  }
96
99
 
@@ -0,0 +1,128 @@
1
+ import { RawImage } from "./image.js";
2
+ import { apis } from "../env.js";
3
+
4
+ export class RawVideoFrame {
5
+
6
+ /**
7
+ * @param {RawImage} image
8
+ * @param {number} timestamp
9
+ */
10
+ constructor(image, timestamp) {
11
+ this.image = image;
12
+ this.timestamp = timestamp;
13
+ }
14
+ }
15
+
16
+ export class RawVideo {
17
+ /**
18
+ * @param {RawVideoFrame[]|RawImage[]} frames
19
+ * @param {number} duration
20
+ */
21
+ constructor(frames, duration) {
22
+ if (frames.length > 0 && frames[0] instanceof RawImage) {
23
+ // Assume uniform timestamps
24
+ frames = frames.map((image, i) => new RawVideoFrame(image, (i + 1) / (frames.length + 1) * duration));
25
+ }
26
+ this.frames = /** @type {RawVideoFrame[]} */ (frames);
27
+ this.duration = duration;
28
+ }
29
+
30
+ get width() {
31
+ return this.frames[0].image.width;
32
+ }
33
+ get height() {
34
+ return this.frames[0].image.height;
35
+ }
36
+
37
+ get fps() {
38
+ return this.frames.length / this.duration;
39
+ }
40
+ }
41
+
42
+
43
+ /**
44
+ * Loads a video.
45
+ *
46
+ * @param {string|Blob|HTMLVideoElement} src The video to process.
47
+ * @param {Object} [options] Optional parameters.
48
+ * @param {number} [options.num_frames=null] The number of frames to sample uniformly.
49
+ * @param {number} [options.fps=null] The number of frames to sample per second.
50
+ *
51
+ * @returns {Promise<RawVideo>} The loaded video.
52
+ */
53
+ export async function load_video(src, { num_frames = null, fps = null } = {}) {
54
+ if (!apis.IS_BROWSER_ENV) {
55
+ throw new Error("`load_video` is currently only supported in browser environments.");
56
+ }
57
+
58
+ // TODO: Support efficiently loading all frames using the WebCodecs API.
59
+ // Specfically, https://developer.mozilla.org/en-US/docs/Web/API/VideoDecoder
60
+ if (num_frames == null && fps == null) {
61
+ throw new Error("Either num_frames or fps must be provided.");
62
+ }
63
+
64
+ const frames = [];
65
+
66
+ const video = document.createElement("video");
67
+ video.crossOrigin = "anonymous";
68
+ video.muted = true; // mute to allow autoplay and seeking
69
+
70
+ if (typeof src === 'string') {
71
+ video.src = src;
72
+ } else if (src instanceof Blob) {
73
+ video.src = URL.createObjectURL(src);
74
+ } else if (src instanceof HTMLVideoElement) {
75
+ video.src = src.src;
76
+ } else {
77
+ throw new Error("Invalid URL or video element provided.");
78
+ }
79
+ // Wait for metadata to load to obtain duration
80
+ await new Promise((resolve) => video.onloadedmetadata = resolve);
81
+
82
+ if (video.seekable.start(0) === video.seekable.end(0)) {
83
+ // Fallback: Download entire video if not seekable
84
+ const response = await fetch(video.src);
85
+ const blob = await response.blob();
86
+ video.src = URL.createObjectURL(blob);
87
+ await new Promise((resolve) => video.onloadedmetadata = resolve);
88
+ }
89
+
90
+ const duration = video.duration;
91
+
92
+ let count, step;
93
+ if (num_frames != null) {
94
+ count = num_frames;
95
+ step = num_frames === 1 ? 0 : duration / (num_frames - 1);
96
+ } else {
97
+ step = 1 / fps;
98
+ count = Math.floor(duration / step);
99
+ }
100
+
101
+ // Build an array of sample times based on num_frames or fps
102
+ let sampleTimes = [];
103
+ for (let i = 0; i < count; ++i) {
104
+ sampleTimes.push(num_frames === 1 ? duration / 2 : i * step);
105
+ }
106
+
107
+ const canvas = document.createElement("canvas");
108
+ canvas.width = video.videoWidth;
109
+ canvas.height = video.videoHeight;
110
+ const ctx = canvas.getContext("2d", { willReadFrequently: true });
111
+ for (const t of sampleTimes) {
112
+ video.currentTime = t;
113
+ await new Promise((resolve) => {
114
+ video.onseeked = resolve;
115
+ });
116
+ ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
117
+ const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
118
+ const frameData = new RawImage(imageData.data, canvas.width, canvas.height, 4);
119
+
120
+ const frame = new RawVideoFrame(frameData, t);
121
+ frames.push(frame);
122
+ }
123
+
124
+ // Clean up video element.
125
+ video.remove();
126
+
127
+ return new RawVideo(frames, duration);
128
+ }
@@ -6,12 +6,12 @@
6
6
  export function deviceToExecutionProviders(device?: import("../utils/devices.js").DeviceType | "auto" | null): ONNXExecutionProviders[];
7
7
  /**
8
8
  * Create an ONNX inference session.
9
- * @param {Uint8Array} buffer The ONNX model buffer.
9
+ * @param {Uint8Array|string} buffer_or_path The ONNX model buffer or path.
10
10
  * @param {import('onnxruntime-common').InferenceSession.SessionOptions} session_options ONNX inference session options.
11
11
  * @param {Object} session_config ONNX inference session configuration.
12
12
  * @returns {Promise<import('onnxruntime-common').InferenceSession & { config: Object}>} The ONNX inference session.
13
13
  */
14
- export function createInferenceSession(buffer: Uint8Array, session_options: import("onnxruntime-common").InferenceSession.SessionOptions, session_config: any): Promise<import("onnxruntime-common").InferenceSession & {
14
+ export function createInferenceSession(buffer_or_path: Uint8Array | string, session_options: import("onnxruntime-common").InferenceSession.SessionOptions, session_config: any): Promise<import("onnxruntime-common").InferenceSession & {
15
15
  config: any;
16
16
  }>;
17
17
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"onnx.d.ts","sourceRoot":"","sources":["../../src/backends/onnx.js"],"names":[],"mappings":"AA0GA;;;;GAIG;AACH,oDAHW,OAAO,qBAAqB,EAAE,UAAU,GAAC,MAAM,GAAC,IAAI,GAClD,sBAAsB,EAAE,CAqBpC;AAWD;;;;;;GAMG;AACH,+CALW,UAAU,mBACV,OAAO,oBAAoB,EAAE,gBAAgB,CAAC,cAAc,wBAE1D,OAAO,CAAC,OAAO,oBAAoB,EAAE,gBAAgB,GAAG;IAAE,MAAM,MAAQ;CAAC,CAAC,CActF;AAED;;;;GAIG;AACH,gCAHW,GAAG,GACD,OAAO,CAInB;AA+BD;;;GAGG;AACH,+BAFa,OAAO,CAKnB;;qCAnLY,OAAO,oBAAoB,EAAE,gBAAgB,CAAC,uBAAuB"}
1
+ {"version":3,"file":"onnx.d.ts","sourceRoot":"","sources":["../../src/backends/onnx.js"],"names":[],"mappings":"AA0GA;;;;GAIG;AACH,oDAHW,OAAO,qBAAqB,EAAE,UAAU,GAAC,MAAM,GAAC,IAAI,GAClD,sBAAsB,EAAE,CAqBpC;AAWD;;;;;;GAMG;AACH,uDALW,UAAU,GAAC,MAAM,mBACjB,OAAO,oBAAoB,EAAE,gBAAgB,CAAC,cAAc,wBAE1D,OAAO,CAAC,OAAO,oBAAoB,EAAE,gBAAgB,GAAG;IAAE,MAAM,MAAQ;CAAC,CAAC,CActF;AAED;;;;GAIG;AACH,gCAHW,GAAG,GACD,OAAO,CAInB;AA8BD;;;GAGG;AACH,+BAFa,OAAO,CAKnB;;qCAlLY,OAAO,oBAAoB,EAAE,gBAAgB,CAAC,uBAAuB"}
@@ -83,6 +83,6 @@ export type TransformersJSConfig = {
83
83
  /**
84
84
  * Whether to load the model using the external data format (used for models >= 2GB in size).
85
85
  */
86
- use_external_data_format?: boolean | Record<string, boolean>;
86
+ use_external_data_format?: import("./utils/hub.js").ExternalData | Record<string, import("./utils/hub.js").ExternalData>;
87
87
  };
88
88
  //# sourceMappingURL=configs.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"configs.d.ts","sourceRoot":"","sources":["../src/configs.js"],"names":[],"mappings":"AAuPA;;;;GAIG;AACH,0CAHW,gBAAgB;;;IACd,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,CAAC,CA2EpC;AACD;;;GAGG;AACH;IAwBI;;;;;;;;OAQG;IACH,sDANW,MAAM,0EACN,iBAAiB,GAGf,OAAO,CAAC,gBAAgB,CAAC,CAqBrC;IArCD;;;OAGG;IACH,6BAGC;IAnBD,0BAA0B;IAC1B,YADW,MAAM,GAAC,IAAI,CACJ;IAElB,sBAAsB;IACtB,oBADW,OAAO,CACS;IAE3B,qBAAqB;IACrB,yBADW,MAAM,CACO;IAExB,mCAAmC;IACnC,0BADW,oBAAoB,CACN;IAQrB,uBAAkD;CAgCzD;AAED;;;;;GAKG;AACH;IArCI;;;;;;;;OAQG;IACH,sDANW,MAAM,0EACN,iBAAiB,GAGf,OAAO,CAAC,gBAAgB,CAAC,CAqBrC;CAcJ;gCAzWY,OAAO,gBAAgB,EAAE,iBAAiB;+BAI1C,OAAO,iBAAiB,EAAE,gBAAgB;2BAI1C,OAAO,iBAAiB,EAAE,YAAY;;;;;;;;qBAsWrC,OAAO,mBAAmB,EAAE,QAAQ,GAAC,MAAM,CAAC,OAAO,mBAAmB,EAAE,QAAQ,EAAE,OAAO,mBAAmB,EAAE,QAAQ,CAAC;;;;;;+BACvH,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC;;;;aAGtB,OAAO,oBAAoB,EAAE,UAAU;;;;YACvC,OAAO,mBAAmB,EAAE,QAAQ,GAAC,MAAM,CAAC,MAAM,EAAE,OAAO,mBAAmB,EAAE,QAAQ,CAAC;;;;+BACzF,OAAO,GAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC"}
1
+ {"version":3,"file":"configs.d.ts","sourceRoot":"","sources":["../src/configs.js"],"names":[],"mappings":"AA0PA;;;;GAIG;AACH,0CAHW,gBAAgB;;;IACd,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,CAAC,CA2EpC;AACD;;;GAGG;AACH;IAwBI;;;;;;;;OAQG;IACH,sDANW,MAAM,0EACN,iBAAiB,GAGf,OAAO,CAAC,gBAAgB,CAAC,CAqBrC;IArCD;;;OAGG;IACH,6BAGC;IAnBD,0BAA0B;IAC1B,YADW,MAAM,GAAC,IAAI,CACJ;IAElB,sBAAsB;IACtB,oBADW,OAAO,CACS;IAE3B,qBAAqB;IACrB,yBADW,MAAM,CACO;IAExB,mCAAmC;IACnC,0BADW,oBAAoB,CACN;IAQrB,uBAAkD;CAgCzD;AAED;;;;;GAKG;AACH;IArCI;;;;;;;;OAQG;IACH,sDANW,MAAM,0EACN,iBAAiB,GAGf,OAAO,CAAC,gBAAgB,CAAC,CAqBrC;CAcJ;gCA5WY,OAAO,gBAAgB,EAAE,iBAAiB;+BAI1C,OAAO,iBAAiB,EAAE,gBAAgB;2BAI1C,OAAO,iBAAiB,EAAE,YAAY;;;;;;;;qBAyWrC,OAAO,mBAAmB,EAAE,QAAQ,GAAC,MAAM,CAAC,OAAO,mBAAmB,EAAE,QAAQ,EAAE,OAAO,mBAAmB,EAAE,QAAQ,CAAC;;;;;;+BACvH,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC;;;;aAGtB,OAAO,oBAAoB,EAAE,UAAU;;;;YACvC,OAAO,mBAAmB,EAAE,QAAQ,GAAC,MAAM,CAAC,MAAM,EAAE,OAAO,mBAAmB,EAAE,QAAQ,CAAC;;;;+BACzF,OAAO,gBAAgB,EAAE,YAAY,GAAC,MAAM,CAAC,MAAM,EAAE,OAAO,gBAAgB,EAAE,YAAY,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"streamers.d.ts","sourceRoot":"","sources":["../../src/generation/streamers.js"],"names":[],"mappings":"AASA;IAC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zC"}
@@ -0,0 +1,4 @@
1
+ export class DacFeatureExtractor extends EncodecFeatureExtractor {
2
+ }
3
+ import { EncodecFeatureExtractor } from '../encodec/feature_extraction_encodec.js';
4
+ //# sourceMappingURL=feature_extraction_dac.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"feature_extraction_dac.d.ts","sourceRoot":"","sources":["../../../src/models/dac/feature_extraction_dac.js"],"names":[],"mappings":"AAEA;CAAoE;wCAF5B,0CAA0C"}
@@ -0,0 +1,13 @@
1
+ export class EncodecFeatureExtractor extends FeatureExtractor {
2
+ /**
3
+ * Asynchronously extracts input values from a given audio using the provided configuration.
4
+ * @param {Float32Array|Float64Array} audio The audio data as a Float32Array/Float64Array.
5
+ * @returns {Promise<{ input_values: Tensor; }>} The extracted input values.
6
+ */
7
+ _call(audio: Float32Array | Float64Array): Promise<{
8
+ input_values: Tensor;
9
+ }>;
10
+ }
11
+ import { FeatureExtractor } from '../../base/feature_extraction_utils.js';
12
+ import { Tensor } from '../../utils/tensor.js';
13
+ //# sourceMappingURL=feature_extraction_encodec.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"feature_extraction_encodec.d.ts","sourceRoot":"","sources":["../../../src/models/encodec/feature_extraction_encodec.js"],"names":[],"mappings":"AAIA;IACI;;;;OAIG;IACH,aAHW,YAAY,GAAC,YAAY,GACvB,OAAO,CAAC;QAAE,YAAY,EAAE,MAAM,CAAC;KAAE,CAAC,CAsB9C;CACJ;iCA/BuD,wCAAwC;uBACzE,uBAAuB"}
@@ -1,5 +1,7 @@
1
1
  export * from "./audio_spectrogram_transformer/feature_extraction_audio_spectrogram_transformer.js";
2
+ export * from "./encodec/feature_extraction_encodec.js";
2
3
  export * from "./clap/feature_extraction_clap.js";
4
+ export * from "./dac/feature_extraction_dac.js";
3
5
  export * from "./moonshine/feature_extraction_moonshine.js";
4
6
  export * from "./pyannote/feature_extraction_pyannote.js";
5
7
  export * from "./seamless_m4t/feature_extraction_seamless_m4t.js";
@@ -26,7 +26,7 @@ export class Florence2Processor extends Processor {
26
26
  * @param {[number, number]} image_size The size of the image. height x width.
27
27
  */
28
28
  post_process_generation(text: string, task: string, image_size: [number, number]): {
29
- [x: string]: string | {
29
+ [task]: string | {
30
30
  [x: string]: any[];
31
31
  labels: any[];
32
32
  };
@@ -1 +1 @@
1
- {"version":3,"file":"processing_florence2.d.ts","sourceRoot":"","sources":["../../../src/models/florence2/processing_florence2.js"],"names":[],"mappings":"AAIA;IACI,6CAAsC;IACtC,wDAAiD;IAEjD,0CA0BC;IAdG,kCAAkC;IAClC,mCADW,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAC2E;IAEzG,kCAAkC;IAClC,6BADW,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAC+D;IAE7F,kCAAkC;IAClC,yBADW,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CACuD;IAErF;;;MAGC;IACD,qBAAwB;IAG5B;;;;OAIG;IACH,wBAHW,MAAM,GAAC,MAAM,EAAE,GACb,MAAM,EAAE,CA6BpB;IAED;;;;;OAKG;IACH,8BAJW,MAAM,QACN,MAAM,cACN,CAAC,MAAM,EAAE,MAAM,CAAC;;;;;MAsC1B;IAID,0DAaC;CACJ;0BAlIyB,gCAAgC;8BAE5B,qBAAqB;mCADhB,kCAAkC"}
1
+ {"version":3,"file":"processing_florence2.d.ts","sourceRoot":"","sources":["../../../src/models/florence2/processing_florence2.js"],"names":[],"mappings":"AAIA;IACI,6CAAsC;IACtC,wDAAiD;IAEjD,0CA0BC;IAdG,kCAAkC;IAClC,mCADW,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAC2E;IAEzG,kCAAkC;IAClC,6BADW,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAC+D;IAE7F,kCAAkC;IAClC,yBADW,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CACuD;IAErF;;;MAGC;IACD,qBAAwB;IAG5B;;;;OAIG;IACH,wBAHW,MAAM,GAAC,MAAM,EAAE,GACb,MAAM,EAAE,CA6BpB;IAED;;;;;OAKG;IACH,8BAJW,MAAM,QACN,MAAM,cACN,CAAC,MAAM,EAAE,MAAM,CAAC;QAqCd,CAAC,IAAI,CAAC;;;UAAc;MAChC;IAID,0DAaC;CACJ;0BAlIyB,gCAAgC;8BAE5B,qBAAqB;mCADhB,kCAAkC"}
@@ -31,6 +31,7 @@ export * from "./rt_detr/image_processing_rt_detr.js";
31
31
  export * from "./sam/image_processing_sam.js";
32
32
  export * from "./segformer/image_processing_segformer.js";
33
33
  export * from "./siglip/image_processing_siglip.js";
34
+ export * from "./smolvlm/image_processing_smolvlm.js";
34
35
  export * from "./swin2sr/image_processing_swin2sr.js";
35
36
  export * from "./vit/image_processing_vit.js";
36
37
  export * from "./vitmatte/image_processing_vitmatte.js";