@huggingface/transformers 4.0.0-next.4 → 4.0.0-next.6
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.
- package/README.md +15 -5
- package/dist/ort-wasm-simd-threaded.jsep.mjs +28 -28
- package/dist/transformers.js +3446 -2296
- package/dist/transformers.min.js +17 -19
- package/dist/transformers.node.cjs +3456 -2263
- package/dist/transformers.node.min.cjs +20 -22
- package/dist/transformers.node.min.mjs +20 -22
- package/dist/transformers.node.mjs +3426 -2258
- package/dist/transformers.web.js +1647 -471
- package/dist/transformers.web.min.js +18 -18
- package/package.json +4 -4
- package/src/backends/onnx.js +128 -53
- package/src/backends/utils/cacheWasm.js +28 -46
- package/src/configs.js +63 -3
- package/src/env.js +93 -11
- package/src/generation/logits_sampler.js +3 -15
- package/src/image_processors_utils.js +2 -6
- package/src/models/afmoe/modeling_afmoe.js +5 -0
- package/src/models/auto/image_processing_auto.js +2 -1
- package/src/models/auto/modeling_auto.js +16 -2
- package/src/models/auto/tokenization_auto.js +2 -1
- package/src/models/clap/feature_extraction_clap.js +2 -1
- package/src/models/marian/tokenization_marian.js +3 -2
- package/src/models/modeling_utils.js +45 -7
- package/src/models/models.js +10 -0
- package/src/models/olmo_hybrid/modeling_olmo_hybrid.js +5 -0
- package/src/models/paligemma/processing_paligemma.js +3 -2
- package/src/models/processors.js +2 -0
- package/src/models/qwen2_5_vl/modeling_qwen2_5_vl.js +5 -0
- package/src/models/qwen2_5_vl/processing_qwen2_5_vl.js +3 -0
- package/src/models/qwen2_moe/modeling_qwen2_moe.js +5 -0
- package/src/models/qwen2_vl/image_processing_qwen2_vl.js +54 -0
- package/src/models/qwen2_vl/modeling_qwen2_vl.js +45 -6
- package/src/models/qwen3_5/modeling_qwen3_5.js +3 -0
- package/src/models/qwen3_5_moe/modeling_qwen3_5_moe.js +3 -0
- package/src/models/qwen3_moe/modeling_qwen3_moe.js +5 -0
- package/src/models/qwen3_next/modeling_qwen3_next.js +5 -0
- package/src/models/qwen3_vl/modeling_qwen3_vl.js +3 -0
- package/src/models/qwen3_vl/processing_qwen3_vl.js +3 -0
- package/src/models/qwen3_vl_moe/modeling_qwen3_vl_moe.js +3 -0
- package/src/models/registry.js +21 -5
- package/src/models/session.js +16 -50
- package/src/models/whisper/feature_extraction_whisper.js +2 -1
- package/src/models/whisper/modeling_whisper.js +6 -5
- package/src/models/xlm/tokenization_xlm.js +2 -1
- package/src/pipelines/automatic-speech-recognition.js +3 -2
- package/src/pipelines/index.js +313 -0
- package/src/pipelines/text-generation.js +4 -0
- package/src/pipelines/text-to-audio.js +4 -2
- package/src/pipelines/zero-shot-classification.js +3 -2
- package/src/pipelines.js +139 -428
- package/src/tokenization_utils.js +42 -21
- package/src/transformers.js +6 -1
- package/src/utils/audio.js +2 -1
- package/src/utils/cache/FileCache.js +128 -0
- package/src/utils/cache.js +7 -4
- package/src/utils/core.js +23 -1
- package/src/utils/devices.js +22 -0
- package/src/utils/dtypes.js +55 -0
- package/src/utils/hub/{files.js → FileResponse.js} +0 -90
- package/src/utils/hub/utils.js +45 -5
- package/src/utils/hub.js +63 -22
- package/src/utils/image.js +14 -14
- package/src/utils/logger.js +67 -0
- package/src/utils/model-loader.js +35 -17
- package/src/utils/model_registry/ModelRegistry.js +346 -0
- package/src/utils/model_registry/clear_cache.js +128 -0
- package/src/utils/model_registry/get_file_metadata.js +149 -0
- package/src/utils/model_registry/get_files.js +42 -0
- package/src/utils/model_registry/get_model_files.js +193 -0
- package/src/utils/model_registry/get_pipeline_files.js +44 -0
- package/src/utils/model_registry/get_processor_files.js +20 -0
- package/src/utils/model_registry/get_tokenizer_files.js +21 -0
- package/src/utils/model_registry/is_cached.js +169 -0
- package/src/utils/random.js +225 -0
- package/src/utils/tensor.js +8 -21
- package/src/utils/video.js +2 -2
- package/types/backends/onnx.d.ts.map +1 -1
- package/types/backends/utils/cacheWasm.d.ts +3 -17
- package/types/backends/utils/cacheWasm.d.ts.map +1 -1
- package/types/configs.d.ts.map +1 -1
- package/types/env.d.ts +52 -27
- package/types/env.d.ts.map +1 -1
- package/types/generation/logits_sampler.d.ts +2 -2
- package/types/generation/logits_sampler.d.ts.map +1 -1
- package/types/image_processors_utils.d.ts.map +1 -1
- package/types/models/afmoe/modeling_afmoe.d.ts +8 -0
- package/types/models/afmoe/modeling_afmoe.d.ts.map +1 -0
- package/types/models/auto/image_processing_auto.d.ts.map +1 -1
- package/types/models/auto/modeling_auto.d.ts +6 -0
- package/types/models/auto/modeling_auto.d.ts.map +1 -1
- package/types/models/auto/tokenization_auto.d.ts.map +1 -1
- package/types/models/clap/feature_extraction_clap.d.ts.map +1 -1
- package/types/models/marian/tokenization_marian.d.ts.map +1 -1
- package/types/models/modeling_utils.d.ts +13 -2
- package/types/models/modeling_utils.d.ts.map +1 -1
- package/types/models/models.d.ts +10 -0
- package/types/models/olmo_hybrid/modeling_olmo_hybrid.d.ts +8 -0
- package/types/models/olmo_hybrid/modeling_olmo_hybrid.d.ts.map +1 -0
- package/types/models/paligemma/processing_paligemma.d.ts.map +1 -1
- package/types/models/processors.d.ts +2 -0
- package/types/models/qwen2_5_vl/modeling_qwen2_5_vl.d.ts +4 -0
- package/types/models/qwen2_5_vl/modeling_qwen2_5_vl.d.ts.map +1 -0
- package/types/models/qwen2_5_vl/processing_qwen2_5_vl.d.ts +4 -0
- package/types/models/qwen2_5_vl/processing_qwen2_5_vl.d.ts.map +1 -0
- package/types/models/qwen2_moe/modeling_qwen2_moe.d.ts +8 -0
- package/types/models/qwen2_moe/modeling_qwen2_moe.d.ts.map +1 -0
- package/types/models/qwen2_vl/image_processing_qwen2_vl.d.ts +3 -0
- package/types/models/qwen2_vl/image_processing_qwen2_vl.d.ts.map +1 -1
- package/types/models/qwen2_vl/modeling_qwen2_vl.d.ts +1 -0
- package/types/models/qwen2_vl/modeling_qwen2_vl.d.ts.map +1 -1
- package/types/models/qwen3_5/modeling_qwen3_5.d.ts +4 -0
- package/types/models/qwen3_5/modeling_qwen3_5.d.ts.map +1 -0
- package/types/models/qwen3_5_moe/modeling_qwen3_5_moe.d.ts +4 -0
- package/types/models/qwen3_5_moe/modeling_qwen3_5_moe.d.ts.map +1 -0
- package/types/models/qwen3_moe/modeling_qwen3_moe.d.ts +8 -0
- package/types/models/qwen3_moe/modeling_qwen3_moe.d.ts.map +1 -0
- package/types/models/qwen3_next/modeling_qwen3_next.d.ts +8 -0
- package/types/models/qwen3_next/modeling_qwen3_next.d.ts.map +1 -0
- package/types/models/qwen3_vl/modeling_qwen3_vl.d.ts +4 -0
- package/types/models/qwen3_vl/modeling_qwen3_vl.d.ts.map +1 -0
- package/types/models/qwen3_vl/processing_qwen3_vl.d.ts +4 -0
- package/types/models/qwen3_vl/processing_qwen3_vl.d.ts.map +1 -0
- package/types/models/qwen3_vl_moe/modeling_qwen3_vl_moe.d.ts +4 -0
- package/types/models/qwen3_vl_moe/modeling_qwen3_vl_moe.d.ts.map +1 -0
- package/types/models/registry.d.ts +2 -1
- package/types/models/registry.d.ts.map +1 -1
- package/types/models/session.d.ts.map +1 -1
- package/types/models/whisper/feature_extraction_whisper.d.ts.map +1 -1
- package/types/models/whisper/modeling_whisper.d.ts.map +1 -1
- package/types/models/xlm/tokenization_xlm.d.ts.map +1 -1
- package/types/pipelines/automatic-speech-recognition.d.ts.map +1 -1
- package/types/pipelines/index.d.ts +265 -0
- package/types/pipelines/index.d.ts.map +1 -0
- package/types/pipelines/text-generation.d.ts +5 -1
- package/types/pipelines/text-generation.d.ts.map +1 -1
- package/types/pipelines/text-to-audio.d.ts.map +1 -1
- package/types/pipelines/zero-shot-classification.d.ts.map +1 -1
- package/types/pipelines.d.ts +50 -291
- package/types/pipelines.d.ts.map +1 -1
- package/types/tokenization_utils.d.ts +44 -26
- package/types/tokenization_utils.d.ts.map +1 -1
- package/types/transformers.d.ts +4 -1
- package/types/transformers.d.ts.map +1 -1
- package/types/utils/audio.d.ts.map +1 -1
- package/types/utils/cache/FileCache.d.ts +39 -0
- package/types/utils/cache/FileCache.d.ts.map +1 -0
- package/types/utils/cache.d.ts +10 -4
- package/types/utils/cache.d.ts.map +1 -1
- package/types/utils/core.d.ts +59 -2
- package/types/utils/core.d.ts.map +1 -1
- package/types/utils/devices.d.ts +15 -0
- package/types/utils/devices.d.ts.map +1 -1
- package/types/utils/dtypes.d.ts +16 -0
- package/types/utils/dtypes.d.ts.map +1 -1
- package/types/utils/hub/{files.d.ts → FileResponse.d.ts} +1 -32
- package/types/utils/hub/FileResponse.d.ts.map +1 -0
- package/types/utils/hub/utils.d.ts +19 -3
- package/types/utils/hub/utils.d.ts.map +1 -1
- package/types/utils/hub.d.ts +36 -7
- package/types/utils/hub.d.ts.map +1 -1
- package/types/utils/logger.d.ts +28 -0
- package/types/utils/logger.d.ts.map +1 -0
- package/types/utils/model-loader.d.ts +15 -0
- package/types/utils/model-loader.d.ts.map +1 -1
- package/types/utils/model_registry/ModelRegistry.d.ts +271 -0
- package/types/utils/model_registry/ModelRegistry.d.ts.map +1 -0
- package/types/utils/model_registry/clear_cache.d.ts +74 -0
- package/types/utils/model_registry/clear_cache.d.ts.map +1 -0
- package/types/utils/model_registry/get_file_metadata.d.ts +20 -0
- package/types/utils/model_registry/get_file_metadata.d.ts.map +1 -0
- package/types/utils/model_registry/get_files.d.ts +23 -0
- package/types/utils/model_registry/get_files.d.ts.map +1 -0
- package/types/utils/model_registry/get_model_files.d.ts +22 -0
- package/types/utils/model_registry/get_model_files.d.ts.map +1 -0
- package/types/utils/model_registry/get_pipeline_files.d.ts +22 -0
- package/types/utils/model_registry/get_pipeline_files.d.ts.map +1 -0
- package/types/utils/model_registry/get_processor_files.d.ts +9 -0
- package/types/utils/model_registry/get_processor_files.d.ts.map +1 -0
- package/types/utils/model_registry/get_tokenizer_files.d.ts +9 -0
- package/types/utils/model_registry/get_tokenizer_files.d.ts.map +1 -0
- package/types/utils/model_registry/is_cached.d.ts +105 -0
- package/types/utils/model_registry/is_cached.d.ts.map +1 -0
- package/types/utils/random.d.ts +86 -0
- package/types/utils/random.d.ts.map +1 -0
- package/types/utils/tensor.d.ts.map +1 -1
- package/types/utils/hub/files.d.ts.map +0 -1
|
@@ -0,0 +1,346 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Model registry for cache and file operations
|
|
3
|
+
*
|
|
4
|
+
* Provides static methods for:
|
|
5
|
+
* - Discovering which files a model needs
|
|
6
|
+
* - Getting file metadata
|
|
7
|
+
* - Checking cache status
|
|
8
|
+
*
|
|
9
|
+
* **Example:** Get all files needed for a model
|
|
10
|
+
* ```javascript
|
|
11
|
+
* const files = await ModelRegistry.get_files(
|
|
12
|
+
* "onnx-community/all-MiniLM-L6-v2-ONNX",
|
|
13
|
+
* { dtype: "fp16" },
|
|
14
|
+
* );
|
|
15
|
+
* console.log(files); // [ 'config.json', 'onnx/model_fp16.onnx', 'onnx/model_fp16.onnx_data', 'tokenizer.json', 'tokenizer_config.json' ]
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* **Example:** Get all files needed for a specific pipeline task
|
|
19
|
+
* ```javascript
|
|
20
|
+
* const files = await ModelRegistry.get_pipeline_files(
|
|
21
|
+
* "text-generation",
|
|
22
|
+
* "onnx-community/Qwen3-0.6B-ONNX",
|
|
23
|
+
* { dtype: "q4" },
|
|
24
|
+
* );
|
|
25
|
+
* console.log(files); // [ 'config.json', 'onnx/model_q4.onnx', 'generation_config.json', 'tokenizer.json', 'tokenizer_config.json' ]
|
|
26
|
+
* ```
|
|
27
|
+
*
|
|
28
|
+
* **Example:** Get specific component files
|
|
29
|
+
* ```javascript
|
|
30
|
+
* const modelFiles = await ModelRegistry.get_model_files("onnx-community/all-MiniLM-L6-v2-ONNX", { dtype: "q4" });
|
|
31
|
+
* const tokenizerFiles = await ModelRegistry.get_tokenizer_files("onnx-community/all-MiniLM-L6-v2-ONNX");
|
|
32
|
+
* const processorFiles = await ModelRegistry.get_processor_files("onnx-community/all-MiniLM-L6-v2-ONNX");
|
|
33
|
+
* console.log(modelFiles); // [ 'config.json', 'onnx/model_q4.onnx', 'onnx/model_q4.onnx_data' ]
|
|
34
|
+
* console.log(tokenizerFiles); // [ 'tokenizer.json', 'tokenizer_config.json' ]
|
|
35
|
+
* console.log(processorFiles); // [ ]
|
|
36
|
+
* ```
|
|
37
|
+
*
|
|
38
|
+
* **Example:** Check file metadata without downloading
|
|
39
|
+
* ```javascript
|
|
40
|
+
* const metadata = await ModelRegistry.get_file_metadata(
|
|
41
|
+
* "onnx-community/Qwen3-0.6B-ONNX",
|
|
42
|
+
* "config.json"
|
|
43
|
+
* );
|
|
44
|
+
* console.log(metadata); // { exists: true, size: 912, contentType: 'application/json', fromCache: true }
|
|
45
|
+
* ```
|
|
46
|
+
*
|
|
47
|
+
* **Example:** Model cache management
|
|
48
|
+
* ```javascript
|
|
49
|
+
* const modelId = "onnx-community/Qwen3-0.6B-ONNX";
|
|
50
|
+
* const options = { dtype: "q4" };
|
|
51
|
+
*
|
|
52
|
+
* // Quickly check if the model is cached (probably false)
|
|
53
|
+
* let cached = await ModelRegistry.is_cached(modelId, options);
|
|
54
|
+
* console.log(cached); // false
|
|
55
|
+
*
|
|
56
|
+
* // Get per-file cache detail
|
|
57
|
+
* let cacheStatus = await ModelRegistry.is_cached_files(modelId, options);
|
|
58
|
+
* console.log(cacheStatus);
|
|
59
|
+
* // {
|
|
60
|
+
* // allCached: false,
|
|
61
|
+
* // files: [ { file: 'config.json', cached: true }, { file: 'onnx/model_q4.onnx', cached: false }, { file: 'generation_config.json', cached: false }, { file: 'tokenizer.json', cached: false }, { file: 'tokenizer_config.json', cached: false } ]
|
|
62
|
+
* // }
|
|
63
|
+
*
|
|
64
|
+
* // Download the model by instantiating a pipeline
|
|
65
|
+
* const generator = await pipeline("text-generation", modelId, options);
|
|
66
|
+
* const output = await generator(
|
|
67
|
+
* [{ role: "user", content: "What is the capital of France?" }],
|
|
68
|
+
* { max_new_tokens: 256, do_sample: false },
|
|
69
|
+
* );
|
|
70
|
+
* console.log(output[0].generated_text.at(-1).content); // <think>...</think>\n\nThe capital of France is **Paris**.
|
|
71
|
+
*
|
|
72
|
+
* // Check if the model is cached (should be true now)
|
|
73
|
+
* cached = await ModelRegistry.is_cached(modelId, options);
|
|
74
|
+
* console.log(cached); // true
|
|
75
|
+
*
|
|
76
|
+
* // Clear the cache
|
|
77
|
+
* const clearResult = await ModelRegistry.clear_cache(modelId, options);
|
|
78
|
+
* console.log(clearResult);
|
|
79
|
+
* // {
|
|
80
|
+
* // filesDeleted: 5,
|
|
81
|
+
* // filesCached: 5,
|
|
82
|
+
* // files: [ { file: 'config.json', deleted: true, wasCached: true }, { file: 'onnx/model_q4.onnx', deleted: true, wasCached: true }, { file: 'generation_config.json', deleted: true, wasCached: true }, { file: 'tokenizer.json', deleted: true, wasCached: true }, { file: 'tokenizer_config.json', deleted: true, wasCached: true } ]
|
|
83
|
+
* // }
|
|
84
|
+
*
|
|
85
|
+
* // Check if the model is cached (should be false again)
|
|
86
|
+
* cached = await ModelRegistry.is_cached(modelId, options);
|
|
87
|
+
* console.log(cached); // false
|
|
88
|
+
* ```
|
|
89
|
+
*
|
|
90
|
+
* @module utils/model_registry
|
|
91
|
+
*/
|
|
92
|
+
|
|
93
|
+
import { get_files } from './get_files.js';
|
|
94
|
+
import { get_pipeline_files } from './get_pipeline_files.js';
|
|
95
|
+
import { get_model_files } from './get_model_files.js';
|
|
96
|
+
import { get_tokenizer_files } from './get_tokenizer_files.js';
|
|
97
|
+
import { get_processor_files } from './get_processor_files.js';
|
|
98
|
+
import { is_cached, is_cached_files, is_pipeline_cached, is_pipeline_cached_files } from './is_cached.js';
|
|
99
|
+
import { get_file_metadata } from './get_file_metadata.js';
|
|
100
|
+
import { clear_cache, clear_pipeline_cache } from './clear_cache.js';
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Static class for cache and file management operations.
|
|
104
|
+
* @hideconstructor
|
|
105
|
+
*/
|
|
106
|
+
export class ModelRegistry {
|
|
107
|
+
/**
|
|
108
|
+
* Get all files (model, tokenizer, processor) needed for a model.
|
|
109
|
+
*
|
|
110
|
+
* @param {string} modelId - The model id (e.g., "onnx-community/bert-base-uncased-ONNX")
|
|
111
|
+
* @param {Object} [options] - Optional parameters
|
|
112
|
+
* @param {import('../../configs.js').PretrainedConfig} [options.config=null] - Pre-loaded config
|
|
113
|
+
* @param {import('../dtypes.js').DataType|Record<string, import('../dtypes.js').DataType>} [options.dtype=null] - Override dtype
|
|
114
|
+
* @param {import('../devices.js').DeviceType|Record<string, import('../devices.js').DeviceType>} [options.device=null] - Override device
|
|
115
|
+
* @param {string} [options.model_file_name=null] - Override the model file name (excluding .onnx suffix)
|
|
116
|
+
* @param {boolean} [options.include_tokenizer=true] - Whether to check for tokenizer files
|
|
117
|
+
* @param {boolean} [options.include_processor=true] - Whether to check for processor files
|
|
118
|
+
* @returns {Promise<string[]>} Array of file paths
|
|
119
|
+
*
|
|
120
|
+
* @example
|
|
121
|
+
* const files = await ModelRegistry.get_files('onnx-community/gpt2-ONNX');
|
|
122
|
+
* console.log(files); // ['config.json', 'tokenizer.json', 'onnx/model_q4.onnx', ...]
|
|
123
|
+
*/
|
|
124
|
+
static async get_files(modelId, options = {}) {
|
|
125
|
+
return get_files(modelId, options);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Get all files needed for a specific pipeline task.
|
|
130
|
+
* Automatically determines which components are needed based on the task.
|
|
131
|
+
*
|
|
132
|
+
* @param {string} task - The pipeline task (e.g., "text-generation", "background-removal")
|
|
133
|
+
* @param {string} modelId - The model id (e.g., "onnx-community/bert-base-uncased-ONNX")
|
|
134
|
+
* @param {Object} [options] - Optional parameters
|
|
135
|
+
* @param {import('../../configs.js').PretrainedConfig} [options.config=null] - Pre-loaded config
|
|
136
|
+
* @param {import('../dtypes.js').DataType|Record<string, import('../dtypes.js').DataType>} [options.dtype=null] - Override dtype
|
|
137
|
+
* @param {import('../devices.js').DeviceType|Record<string, import('../devices.js').DeviceType>} [options.device=null] - Override device
|
|
138
|
+
* @param {string} [options.model_file_name=null] - Override the model file name (excluding .onnx suffix)
|
|
139
|
+
* @returns {Promise<string[]>} Array of file paths
|
|
140
|
+
*
|
|
141
|
+
* @example
|
|
142
|
+
* const files = await ModelRegistry.get_pipeline_files('text-generation', 'onnx-community/gpt2-ONNX');
|
|
143
|
+
* console.log(files); // ['config.json', 'tokenizer.json', 'onnx/model_q4.onnx', ...]
|
|
144
|
+
*/
|
|
145
|
+
static async get_pipeline_files(task, modelId, options = {}) {
|
|
146
|
+
return get_pipeline_files(task, modelId, options);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Get model files needed for a specific model.
|
|
151
|
+
*
|
|
152
|
+
* @param {string} modelId - The model id
|
|
153
|
+
* @param {Object} [options] - Optional parameters
|
|
154
|
+
* @param {import('../../configs.js').PretrainedConfig} [options.config=null] - Pre-loaded config
|
|
155
|
+
* @param {import('../dtypes.js').DataType|Record<string, import('../dtypes.js').DataType>} [options.dtype=null] - Override dtype
|
|
156
|
+
* @param {import('../devices.js').DeviceType|Record<string, import('../devices.js').DeviceType>} [options.device=null] - Override device
|
|
157
|
+
* @param {string} [options.model_file_name=null] - Override the model file name (excluding .onnx suffix)
|
|
158
|
+
* @returns {Promise<string[]>} Array of model file paths
|
|
159
|
+
*
|
|
160
|
+
* @example
|
|
161
|
+
* const files = await ModelRegistry.get_model_files('onnx-community/bert-base-uncased-ONNX');
|
|
162
|
+
* console.log(files); // ['config.json', 'onnx/model_q4.onnx', 'generation_config.json']
|
|
163
|
+
*/
|
|
164
|
+
static async get_model_files(modelId, options = {}) {
|
|
165
|
+
return get_model_files(modelId, options);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Get tokenizer files needed for a specific model.
|
|
170
|
+
*
|
|
171
|
+
* @param {string} modelId - The model id
|
|
172
|
+
* @returns {Promise<string[]>} Array of tokenizer file paths
|
|
173
|
+
*
|
|
174
|
+
* @example
|
|
175
|
+
* const files = await ModelRegistry.get_tokenizer_files('onnx-community/gpt2-ONNX');
|
|
176
|
+
* console.log(files); // ['tokenizer.json', 'tokenizer_config.json']
|
|
177
|
+
*/
|
|
178
|
+
static async get_tokenizer_files(modelId) {
|
|
179
|
+
return get_tokenizer_files(modelId);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Get processor files needed for a specific model.
|
|
184
|
+
*
|
|
185
|
+
* @param {string} modelId - The model id
|
|
186
|
+
* @returns {Promise<string[]>} Array of processor file paths
|
|
187
|
+
*
|
|
188
|
+
* @example
|
|
189
|
+
* const files = await ModelRegistry.get_processor_files('onnx-community/vit-base-patch16-224-ONNX');
|
|
190
|
+
* console.log(files); // ['preprocessor_config.json']
|
|
191
|
+
*/
|
|
192
|
+
static async get_processor_files(modelId) {
|
|
193
|
+
return get_processor_files(modelId);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Quickly checks if a model is fully cached by verifying `config.json` is present,
|
|
198
|
+
* then confirming all required files are cached.
|
|
199
|
+
* Returns a plain boolean — use `is_cached_files` if you need per-file detail.
|
|
200
|
+
*
|
|
201
|
+
* @param {string} modelId - The model id
|
|
202
|
+
* @param {Object} [options] - Optional parameters
|
|
203
|
+
* @param {string} [options.cache_dir] - Custom cache directory
|
|
204
|
+
* @param {string} [options.revision] - Model revision (default: 'main')
|
|
205
|
+
* @param {import('../../configs.js').PretrainedConfig} [options.config] - Pre-loaded config
|
|
206
|
+
* @param {import('../dtypes.js').DataType|Record<string, import('../dtypes.js').DataType>} [options.dtype=null] - Override dtype
|
|
207
|
+
* @param {import('../devices.js').DeviceType|Record<string, import('../devices.js').DeviceType>} [options.device=null] - Override device
|
|
208
|
+
* @returns {Promise<boolean>} Whether all required files are cached
|
|
209
|
+
*
|
|
210
|
+
* @example
|
|
211
|
+
* const cached = await ModelRegistry.is_cached('onnx-community/bert-base-uncased-ONNX');
|
|
212
|
+
* console.log(cached); // true or false
|
|
213
|
+
*/
|
|
214
|
+
static async is_cached(modelId, options = {}) {
|
|
215
|
+
return is_cached(modelId, options);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Checks if all files for a given model are already cached, with per-file detail.
|
|
220
|
+
* Automatically determines which files are needed using get_files().
|
|
221
|
+
*
|
|
222
|
+
* @param {string} modelId - The model id
|
|
223
|
+
* @param {Object} [options] - Optional parameters
|
|
224
|
+
* @param {string} [options.cache_dir] - Custom cache directory
|
|
225
|
+
* @param {string} [options.revision] - Model revision (default: 'main')
|
|
226
|
+
* @param {import('../../configs.js').PretrainedConfig} [options.config] - Pre-loaded config
|
|
227
|
+
* @param {import('../dtypes.js').DataType|Record<string, import('../dtypes.js').DataType>} [options.dtype=null] - Override dtype
|
|
228
|
+
* @param {import('../devices.js').DeviceType|Record<string, import('../devices.js').DeviceType>} [options.device=null] - Override device
|
|
229
|
+
* @returns {Promise<import('./is_cached.js').CacheCheckResult>} Object with allCached boolean and files array with cache status
|
|
230
|
+
*
|
|
231
|
+
* @example
|
|
232
|
+
* const status = await ModelRegistry.is_cached_files('onnx-community/bert-base-uncased-ONNX');
|
|
233
|
+
* console.log(status.allCached); // true or false
|
|
234
|
+
* console.log(status.files); // [{ file: 'config.json', cached: true }, ...]
|
|
235
|
+
*/
|
|
236
|
+
static async is_cached_files(modelId, options = {}) {
|
|
237
|
+
return is_cached_files(modelId, options);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Quickly checks if all files for a specific pipeline task are cached by verifying
|
|
242
|
+
* `config.json` is present, then confirming all required files are cached.
|
|
243
|
+
* Returns a plain boolean — use `is_pipeline_cached_files` if you need per-file detail.
|
|
244
|
+
*
|
|
245
|
+
* @param {string} task - The pipeline task (e.g., "text-generation", "background-removal")
|
|
246
|
+
* @param {string} modelId - The model id
|
|
247
|
+
* @param {Object} [options] - Optional parameters
|
|
248
|
+
* @param {string} [options.cache_dir] - Custom cache directory
|
|
249
|
+
* @param {string} [options.revision] - Model revision (default: 'main')
|
|
250
|
+
* @param {import('../../configs.js').PretrainedConfig} [options.config] - Pre-loaded config
|
|
251
|
+
* @param {import('../dtypes.js').DataType|Record<string, import('../dtypes.js').DataType>} [options.dtype=null] - Override dtype
|
|
252
|
+
* @param {import('../devices.js').DeviceType|Record<string, import('../devices.js').DeviceType>} [options.device=null] - Override device
|
|
253
|
+
* @returns {Promise<boolean>} Whether all required files are cached
|
|
254
|
+
*
|
|
255
|
+
* @example
|
|
256
|
+
* const cached = await ModelRegistry.is_pipeline_cached('text-generation', 'onnx-community/gpt2-ONNX');
|
|
257
|
+
* console.log(cached); // true or false
|
|
258
|
+
*/
|
|
259
|
+
static async is_pipeline_cached(task, modelId, options = {}) {
|
|
260
|
+
return is_pipeline_cached(task, modelId, options);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* Checks if all files for a specific pipeline task are already cached, with per-file detail.
|
|
265
|
+
* Automatically determines which components are needed based on the task.
|
|
266
|
+
*
|
|
267
|
+
* @param {string} task - The pipeline task (e.g., "text-generation", "background-removal")
|
|
268
|
+
* @param {string} modelId - The model id
|
|
269
|
+
* @param {Object} [options] - Optional parameters
|
|
270
|
+
* @param {string} [options.cache_dir] - Custom cache directory
|
|
271
|
+
* @param {string} [options.revision] - Model revision (default: 'main')
|
|
272
|
+
* @param {import('../../configs.js').PretrainedConfig} [options.config] - Pre-loaded config
|
|
273
|
+
* @param {import('../dtypes.js').DataType|Record<string, import('../dtypes.js').DataType>} [options.dtype=null] - Override dtype
|
|
274
|
+
* @param {import('../devices.js').DeviceType|Record<string, import('../devices.js').DeviceType>} [options.device=null] - Override device
|
|
275
|
+
* @returns {Promise<import('./is_cached.js').CacheCheckResult>} Object with allCached boolean and files array with cache status
|
|
276
|
+
*
|
|
277
|
+
* @example
|
|
278
|
+
* const status = await ModelRegistry.is_pipeline_cached_files('text-generation', 'onnx-community/gpt2-ONNX');
|
|
279
|
+
* console.log(status.allCached); // true or false
|
|
280
|
+
* console.log(status.files); // [{ file: 'config.json', cached: true }, ...]
|
|
281
|
+
*/
|
|
282
|
+
static async is_pipeline_cached_files(task, modelId, options = {}) {
|
|
283
|
+
return is_pipeline_cached_files(task, modelId, options);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* Get metadata for a specific file without downloading it.
|
|
288
|
+
*
|
|
289
|
+
* @param {string} path_or_repo_id - Model id or path
|
|
290
|
+
* @param {string} filename - The file name
|
|
291
|
+
* @param {import('../hub.js').PretrainedOptions} [options] - Optional parameters
|
|
292
|
+
* @returns {Promise<{exists: boolean, size?: number, contentType?: string, fromCache?: boolean}>} File metadata
|
|
293
|
+
*
|
|
294
|
+
* @example
|
|
295
|
+
* const metadata = await ModelRegistry.get_file_metadata('onnx-community/gpt2-ONNX', 'config.json');
|
|
296
|
+
* console.log(metadata.exists, metadata.size); // true, 665
|
|
297
|
+
*/
|
|
298
|
+
static async get_file_metadata(path_or_repo_id, filename, options = {}) {
|
|
299
|
+
return get_file_metadata(path_or_repo_id, filename, options);
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* Clears all cached files for a given model.
|
|
304
|
+
* Automatically determines which files are needed and removes them from the cache.
|
|
305
|
+
*
|
|
306
|
+
* @param {string} modelId - The model id (e.g., "onnx-community/gpt2-ONNX")
|
|
307
|
+
* @param {Object} [options] - Optional parameters
|
|
308
|
+
* @param {string} [options.cache_dir] - Custom cache directory
|
|
309
|
+
* @param {string} [options.revision] - Model revision (default: 'main')
|
|
310
|
+
* @param {import('../../configs.js').PretrainedConfig} [options.config] - Pre-loaded config
|
|
311
|
+
* @param {import('../dtypes.js').DataType|Record<string, import('../dtypes.js').DataType>} [options.dtype] - Override dtype
|
|
312
|
+
* @param {import('../devices.js').DeviceType|Record<string, import('../devices.js').DeviceType>} [options.device] - Override device
|
|
313
|
+
* @param {boolean} [options.include_tokenizer=true] - Whether to clear tokenizer files
|
|
314
|
+
* @param {boolean} [options.include_processor=true] - Whether to clear processor files
|
|
315
|
+
* @returns {Promise<import('./clear_cache.js').CacheClearResult>} Object with deletion statistics and file status
|
|
316
|
+
*
|
|
317
|
+
* @example
|
|
318
|
+
* const result = await ModelRegistry.clear_cache('onnx-community/bert-base-uncased-ONNX');
|
|
319
|
+
* console.log(`Deleted ${result.filesDeleted} of ${result.filesCached} cached files`);
|
|
320
|
+
*/
|
|
321
|
+
static async clear_cache(modelId, options = {}) {
|
|
322
|
+
return clear_cache(modelId, options);
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* Clears all cached files for a specific pipeline task.
|
|
327
|
+
* Automatically determines which components are needed based on the task.
|
|
328
|
+
*
|
|
329
|
+
* @param {string} task - The pipeline task (e.g., "text-generation", "image-classification")
|
|
330
|
+
* @param {string} modelId - The model id (e.g., "onnx-community/gpt2-ONNX")
|
|
331
|
+
* @param {Object} [options] - Optional parameters
|
|
332
|
+
* @param {string} [options.cache_dir] - Custom cache directory
|
|
333
|
+
* @param {string} [options.revision] - Model revision (default: 'main')
|
|
334
|
+
* @param {import('../../configs.js').PretrainedConfig} [options.config] - Pre-loaded config
|
|
335
|
+
* @param {import('../dtypes.js').DataType|Record<string, import('../dtypes.js').DataType>} [options.dtype] - Override dtype
|
|
336
|
+
* @param {import('../devices.js').DeviceType|Record<string, import('../devices.js').DeviceType>} [options.device] - Override device
|
|
337
|
+
* @returns {Promise<import('./clear_cache.js').CacheClearResult>} Object with deletion statistics and file status
|
|
338
|
+
*
|
|
339
|
+
* @example
|
|
340
|
+
* const result = await ModelRegistry.clear_pipeline_cache('text-generation', 'onnx-community/gpt2-ONNX');
|
|
341
|
+
* console.log(`Deleted ${result.filesDeleted} of ${result.filesCached} cached files`);
|
|
342
|
+
*/
|
|
343
|
+
static async clear_pipeline_cache(task, modelId, options = {}) {
|
|
344
|
+
return clear_pipeline_cache(task, modelId, options);
|
|
345
|
+
}
|
|
346
|
+
}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Cache clearing utilities for model files
|
|
3
|
+
*
|
|
4
|
+
* Provides functions to clear cached model files from the cache system.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { getCache } from '../cache.js';
|
|
8
|
+
import { buildResourcePaths, checkCachedResource } from '../hub.js';
|
|
9
|
+
import { get_files } from './get_files.js';
|
|
10
|
+
import { get_pipeline_files } from './get_pipeline_files.js';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @typedef {Object} FileClearStatus
|
|
14
|
+
* @property {string} file - The file path
|
|
15
|
+
* @property {boolean} deleted - Whether the file was successfully deleted
|
|
16
|
+
* @property {boolean} wasCached - Whether the file was cached before deletion
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* @typedef {Object} CacheClearResult
|
|
21
|
+
* @property {number} filesDeleted - Number of files successfully deleted
|
|
22
|
+
* @property {number} filesCached - Number of files that were in cache
|
|
23
|
+
* @property {FileClearStatus[]} files - Array of files with their deletion status
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Internal helper to clear cached files.
|
|
28
|
+
*
|
|
29
|
+
*
|
|
30
|
+
* @private
|
|
31
|
+
* @param {string} modelId - The model id
|
|
32
|
+
* @param {string[]} files - List of file paths to clear
|
|
33
|
+
* @param {Object} options - Options including cache_dir
|
|
34
|
+
* @returns {Promise<CacheClearResult>}
|
|
35
|
+
*/
|
|
36
|
+
async function clear_files_from_cache(modelId, files, options = {}) {
|
|
37
|
+
const cache = await getCache(options?.cache_dir);
|
|
38
|
+
|
|
39
|
+
if (!cache) {
|
|
40
|
+
return {
|
|
41
|
+
filesDeleted: 0,
|
|
42
|
+
filesCached: 0,
|
|
43
|
+
files: files.map((filename) => ({ file: filename, deleted: false, wasCached: false })),
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (!cache.delete) {
|
|
48
|
+
throw new Error('Cache does not support delete operation');
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const results = await Promise.all(
|
|
52
|
+
files.map(async (filename) => {
|
|
53
|
+
const { localPath, proposedCacheKey } = buildResourcePaths(modelId, filename, options, cache);
|
|
54
|
+
|
|
55
|
+
const cached = await checkCachedResource(cache, localPath, proposedCacheKey);
|
|
56
|
+
const wasCached = !!cached;
|
|
57
|
+
|
|
58
|
+
let deleted = false;
|
|
59
|
+
if (wasCached) {
|
|
60
|
+
// Try proposedCacheKey first (remote URL for browser Cache API, request path for FileCache),
|
|
61
|
+
// then fall back to localPath in case the entry was cached under the local key instead.
|
|
62
|
+
const deletedWithProposed = await cache.delete(proposedCacheKey);
|
|
63
|
+
const deletedWithLocal =
|
|
64
|
+
!deletedWithProposed && proposedCacheKey !== localPath ? await cache.delete(localPath) : false;
|
|
65
|
+
|
|
66
|
+
deleted = deletedWithProposed || deletedWithLocal;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return { file: filename, deleted, wasCached };
|
|
70
|
+
}),
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
return {
|
|
74
|
+
filesDeleted: results.filter((r) => r.deleted).length,
|
|
75
|
+
filesCached: results.filter((r) => r.wasCached).length,
|
|
76
|
+
files: results,
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Clears all cached files for a given model.
|
|
82
|
+
* Automatically determines which files are needed using get_files().
|
|
83
|
+
*
|
|
84
|
+
* @param {string} modelId - The model id (e.g., "Xenova/gpt2")
|
|
85
|
+
* @param {Object} [options] - Optional parameters
|
|
86
|
+
* @param {string} [options.cache_dir] - Custom cache directory
|
|
87
|
+
* @param {string} [options.revision] - Model revision (default: 'main')
|
|
88
|
+
* @param {import('../../configs.js').PretrainedConfig} [options.config] - Pre-loaded config
|
|
89
|
+
* @param {import('../dtypes.js').DataType|Record<string, import('../dtypes.js').DataType>} [options.dtype] - Override dtype
|
|
90
|
+
* @param {import('../devices.js').DeviceType|Record<string, import('../devices.js').DeviceType>} [options.device] - Override device
|
|
91
|
+
* @param {boolean} [options.include_tokenizer=true] - Whether to clear tokenizer files
|
|
92
|
+
* @param {boolean} [options.include_processor=true] - Whether to clear processor files
|
|
93
|
+
* @returns {Promise<CacheClearResult>} Object with deletion statistics and file status
|
|
94
|
+
*/
|
|
95
|
+
export async function clear_cache(modelId, options = {}) {
|
|
96
|
+
if (!modelId) {
|
|
97
|
+
throw new Error('modelId is required');
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const files = await get_files(modelId, options);
|
|
101
|
+
return await clear_files_from_cache(modelId, files, options);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Clears all cached files for a specific pipeline task.
|
|
106
|
+
* Automatically determines which components are needed based on the task.
|
|
107
|
+
*
|
|
108
|
+
* @param {string} task - The pipeline task (e.g., "text-generation", "image-classification")
|
|
109
|
+
* @param {string} modelId - The model id (e.g., "Xenova/gpt2")
|
|
110
|
+
* @param {Object} [options] - Optional parameters
|
|
111
|
+
* @param {string} [options.cache_dir] - Custom cache directory
|
|
112
|
+
* @param {string} [options.revision] - Model revision (default: 'main')
|
|
113
|
+
* @param {import('../../configs.js').PretrainedConfig} [options.config] - Pre-loaded config
|
|
114
|
+
* @param {import('../dtypes.js').DataType|Record<string, import('../dtypes.js').DataType>} [options.dtype] - Override dtype
|
|
115
|
+
* @param {import('../devices.js').DeviceType|Record<string, import('../devices.js').DeviceType>} [options.device] - Override device
|
|
116
|
+
* @returns {Promise<CacheClearResult>} Object with deletion statistics and file status
|
|
117
|
+
*/
|
|
118
|
+
export async function clear_pipeline_cache(task, modelId, options = {}) {
|
|
119
|
+
if (!task) {
|
|
120
|
+
throw new Error('task is required');
|
|
121
|
+
}
|
|
122
|
+
if (!modelId) {
|
|
123
|
+
throw new Error('modelId is required');
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const files = await get_pipeline_files(task, modelId, options);
|
|
127
|
+
return await clear_files_from_cache(modelId, files, options);
|
|
128
|
+
}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file File metadata utilities for cache-aware operations
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { env } from '../../env.js';
|
|
6
|
+
import { getCache } from '../cache.js';
|
|
7
|
+
import { buildResourcePaths, checkCachedResource, getFetchHeaders, getFile } from '../hub.js';
|
|
8
|
+
import { isValidUrl } from '../hub/utils.js';
|
|
9
|
+
import { logger } from '../logger.js';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @typedef {import('../hub.js').PretrainedOptions} PretrainedOptions
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Makes a Range request to get file metadata without downloading full content.
|
|
17
|
+
*
|
|
18
|
+
* We use a Range request (first byte only) instead of HEAD because:
|
|
19
|
+
* 1. Ensures we get the uncompressed file size, which is critical for accurate progress tracking
|
|
20
|
+
* 2. When compression is used, HEAD request content-length reflects compressed size
|
|
21
|
+
* 3. But during actual download, fetch API decompresses transparently and reports uncompressed bytes
|
|
22
|
+
* 4. This mismatch causes progress bar inconsistencies when tracking multiple files
|
|
23
|
+
* 5. Range requests typically aren't compressed, and content-range header shows true uncompressed size
|
|
24
|
+
*
|
|
25
|
+
* @param {URL|string} urlOrPath The URL/path of the file.
|
|
26
|
+
* @returns {Promise<Response|null>} A promise that resolves to a Response object or null if not supported.
|
|
27
|
+
* @private
|
|
28
|
+
*/
|
|
29
|
+
async function fetch_file_head(urlOrPath) {
|
|
30
|
+
// Range requests only make sense for HTTP URLs
|
|
31
|
+
if (!isValidUrl(urlOrPath, ['http:', 'https:'])) {
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const headers = getFetchHeaders(urlOrPath);
|
|
36
|
+
headers.set('Range', 'bytes=0-0');
|
|
37
|
+
return env.fetch(urlOrPath, { method: 'GET', headers });
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Gets file metadata (size, content-type, etc.) without downloading the full content.
|
|
42
|
+
* Uses Range requests for remote files to be efficient.
|
|
43
|
+
* Can also be used as a lightweight file existence check by checking the `.exists` property.
|
|
44
|
+
*
|
|
45
|
+
* @param {string} path_or_repo_id This can be either:
|
|
46
|
+
* - a string, the *model id* of a model repo on huggingface.co.
|
|
47
|
+
* - a path to a *directory* potentially containing the file.
|
|
48
|
+
* @param {string} filename The name of the file to check.
|
|
49
|
+
* @param {PretrainedOptions} [options] An object containing optional parameters.
|
|
50
|
+
* @returns {Promise<{exists: boolean, size?: number, contentType?: string, fromCache?: boolean}>} A Promise that resolves to file metadata.
|
|
51
|
+
*/
|
|
52
|
+
export async function get_file_metadata(path_or_repo_id, filename, options = {}) {
|
|
53
|
+
/** @type {import('../cache.js').CacheInterface | null} */
|
|
54
|
+
const cache = await getCache(options?.cache_dir);
|
|
55
|
+
const { localPath, remoteURL, proposedCacheKey, validModelId } = buildResourcePaths(
|
|
56
|
+
path_or_repo_id,
|
|
57
|
+
filename,
|
|
58
|
+
options,
|
|
59
|
+
cache,
|
|
60
|
+
);
|
|
61
|
+
|
|
62
|
+
// Check cache first - if cached, we can get metadata from the cached response
|
|
63
|
+
const cachedResponse = await checkCachedResource(cache, localPath, proposedCacheKey);
|
|
64
|
+
if (cachedResponse !== undefined && typeof cachedResponse !== 'string') {
|
|
65
|
+
const size = cachedResponse.headers.get('content-length');
|
|
66
|
+
const contentType = cachedResponse.headers.get('content-type');
|
|
67
|
+
return {
|
|
68
|
+
exists: true,
|
|
69
|
+
size: size ? parseInt(size, 10) : undefined,
|
|
70
|
+
contentType: contentType || undefined,
|
|
71
|
+
fromCache: true,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// Check local file system
|
|
76
|
+
if (env.allowLocalModels) {
|
|
77
|
+
const isURL = isValidUrl(localPath, ['http:', 'https:']);
|
|
78
|
+
if (!isURL) {
|
|
79
|
+
try {
|
|
80
|
+
const response = await getFile(localPath);
|
|
81
|
+
if (typeof response !== 'string' && response.status !== 404) {
|
|
82
|
+
const size = response.headers.get('content-length');
|
|
83
|
+
const contentType = response.headers.get('content-type');
|
|
84
|
+
return {
|
|
85
|
+
exists: true,
|
|
86
|
+
size: size ? parseInt(size, 10) : undefined,
|
|
87
|
+
contentType: contentType || undefined,
|
|
88
|
+
fromCache: false,
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
} catch (e) {
|
|
92
|
+
// File doesn't exist locally, continue to remote check
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// Check remote if allowed - use Range request for efficiency
|
|
98
|
+
if (env.allowRemoteModels && !options.local_files_only && validModelId) {
|
|
99
|
+
try {
|
|
100
|
+
// Make a Range request to get metadata without downloading full content
|
|
101
|
+
const rangeResponse = await fetch_file_head(remoteURL);
|
|
102
|
+
|
|
103
|
+
if (rangeResponse && rangeResponse.status >= 200 && rangeResponse.status < 300) {
|
|
104
|
+
let size;
|
|
105
|
+
const contentType = rangeResponse.headers.get('content-type');
|
|
106
|
+
|
|
107
|
+
// If server supports Range requests, we get a 206 Partial Content response
|
|
108
|
+
// with a content-range header showing the total uncompressed file size
|
|
109
|
+
if (rangeResponse.status === 206) {
|
|
110
|
+
const contentRange = rangeResponse.headers.get('content-range');
|
|
111
|
+
if (contentRange) {
|
|
112
|
+
// Format: "bytes 0-0/12345" where 12345 is the total uncompressed size
|
|
113
|
+
const match = contentRange.match(/bytes \d+-\d+\/(\d+)/);
|
|
114
|
+
if (match) {
|
|
115
|
+
size = parseInt(match[1], 10);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
} else if (rangeResponse.status === 200) {
|
|
119
|
+
// Server doesn't support Range requests and returned the full file (200 OK)
|
|
120
|
+
// Cancel the response body immediately to avoid downloading the entire file
|
|
121
|
+
try {
|
|
122
|
+
await rangeResponse.body?.cancel();
|
|
123
|
+
} catch (cancelError) {
|
|
124
|
+
// Ignore cancellation errors - body might already be consumed or not cancellable
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// Fallback to content-length if content-range is not available (200 OK response)
|
|
129
|
+
// This can happen if server doesn't support Range requests
|
|
130
|
+
if (size === undefined) {
|
|
131
|
+
const contentLength = rangeResponse.headers.get('content-length');
|
|
132
|
+
size = contentLength ? parseInt(contentLength, 10) : undefined;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
return {
|
|
136
|
+
exists: true,
|
|
137
|
+
size,
|
|
138
|
+
contentType: contentType || undefined,
|
|
139
|
+
fromCache: false,
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
} catch (e) {
|
|
143
|
+
// Range request failed most likely because of a network error, timeout, etc.
|
|
144
|
+
logger.warn(`Unable to fetch file metadata for "${remoteURL}": ${e}`);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
return { exists: false, fromCache: false };
|
|
149
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { get_tokenizer_files } from './get_tokenizer_files.js';
|
|
2
|
+
import { get_model_files } from './get_model_files.js';
|
|
3
|
+
import { get_processor_files } from './get_processor_files.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Returns the list of files that will be loaded for a model based on its configuration.
|
|
7
|
+
* Automatically detects which files are needed (tokenizer, processor, model files).
|
|
8
|
+
*
|
|
9
|
+
* @param {string} modelId The model id (e.g., "Xenova/llama-2-7b")
|
|
10
|
+
* @param {Object} [options] Optional parameters
|
|
11
|
+
* @param {import('../../configs.js').PretrainedConfig} [options.config=null] Pre-loaded model config (optional, will be fetched if not provided)
|
|
12
|
+
* @param {import('../dtypes.js').DataType|Record<string, import('../dtypes.js').DataType>} [options.dtype=null] Override dtype (use this if passing dtype to pipeline)
|
|
13
|
+
* @param {import('../devices.js').DeviceType|Record<string, import('../devices.js').DeviceType>} [options.device=null] Override device (use this if passing device to pipeline)
|
|
14
|
+
* @param {string|null} [options.model_file_name=null|null] Override the model file name (excluding .onnx suffix)
|
|
15
|
+
* @param {boolean} [options.include_tokenizer=true] Whether to check for tokenizer files (set to false for vision-only models)
|
|
16
|
+
* @param {boolean} [options.include_processor=true] Whether to check for processor files
|
|
17
|
+
* @returns {Promise<string[]>} Array of file paths that will be loaded
|
|
18
|
+
*/
|
|
19
|
+
export async function get_files(
|
|
20
|
+
modelId,
|
|
21
|
+
{
|
|
22
|
+
config = null,
|
|
23
|
+
dtype = null,
|
|
24
|
+
device = null,
|
|
25
|
+
model_file_name = null,
|
|
26
|
+
include_tokenizer = true,
|
|
27
|
+
include_processor = true,
|
|
28
|
+
} = {},
|
|
29
|
+
) {
|
|
30
|
+
const files = await get_model_files(modelId, { config, dtype, device, model_file_name });
|
|
31
|
+
|
|
32
|
+
if (include_tokenizer) {
|
|
33
|
+
const tokenizerFiles = await get_tokenizer_files(modelId);
|
|
34
|
+
files.push(...tokenizerFiles);
|
|
35
|
+
}
|
|
36
|
+
if (include_processor) {
|
|
37
|
+
const processorFiles = await get_processor_files(modelId);
|
|
38
|
+
files.push(...processorFiles);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return files;
|
|
42
|
+
}
|