@huggingface/transformers 4.0.0-next.0 → 4.0.0-next.10

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 (364) hide show
  1. package/README.md +32 -6
  2. package/dist/ort-wasm-simd-threaded.jsep.mjs +31 -31
  3. package/dist/transformers.js +9261 -1709
  4. package/dist/transformers.min.js +25 -18
  5. package/dist/transformers.node.cjs +6119 -3174
  6. package/dist/transformers.node.min.cjs +25 -23
  7. package/dist/transformers.node.min.mjs +25 -23
  8. package/dist/transformers.node.mjs +6034 -3168
  9. package/dist/transformers.web.js +4255 -1381
  10. package/dist/transformers.web.min.js +23 -19
  11. package/package.json +6 -6
  12. package/src/backends/onnx.js +128 -53
  13. package/src/backends/utils/cacheWasm.js +28 -46
  14. package/src/cache_utils.js +62 -0
  15. package/src/configs.js +123 -23
  16. package/src/env.js +100 -11
  17. package/src/generation/logits_sampler.js +3 -15
  18. package/src/generation/parameters.js +1 -1
  19. package/src/generation/streamers.js +21 -0
  20. package/src/image_processors_utils.js +29 -23
  21. package/src/models/afmoe/modeling_afmoe.js +5 -0
  22. package/src/models/auto/image_processing_auto.js +2 -1
  23. package/src/models/auto/modeling_auto.js +16 -2
  24. package/src/models/auto/tokenization_auto.js +2 -1
  25. package/src/models/chatterbox/modeling_chatterbox.js +1 -1
  26. package/src/models/chmv2/image_processing_chmv2.js +3 -0
  27. package/src/models/chmv2/modeling_chmv2.js +4 -0
  28. package/src/models/clap/feature_extraction_clap.js +2 -1
  29. package/src/models/cohere2/modeling_cohere2.js +5 -0
  30. package/src/models/cohere_asr/feature_extraction_cohere_asr.js +117 -0
  31. package/src/models/cohere_asr/modeling_cohere_asr.js +11 -0
  32. package/src/models/cohere_asr/processing_cohere_asr.js +55 -0
  33. package/src/models/cohere_asr/tokenization_cohere_asr.js +3 -0
  34. package/src/models/deepseek_v3/modeling_deepseek_v3.js +5 -0
  35. package/src/models/detr/image_processing_detr.js +1 -1
  36. package/src/models/eurobert/modeling_eurobert.js +41 -0
  37. package/src/models/feature_extractors.js +3 -0
  38. package/src/models/gemma3/image_processing_gemma3.js +3 -0
  39. package/src/models/gemma3/modeling_gemma3.js +4 -1
  40. package/src/models/gemma3/processing_gemma3.js +45 -0
  41. package/src/models/gemma3n/modeling_gemma3n.js +2 -0
  42. package/src/models/glm46v/image_processing_glm46v.js +12 -0
  43. package/src/models/glm46v/processing_glm46v.js +5 -0
  44. package/src/models/glm_moe_dsa/modeling_glm_moe_dsa.js +5 -0
  45. package/src/models/glm_ocr/modeling_glm_ocr.js +78 -0
  46. package/src/models/granite_speech/feature_extraction_granite_speech.js +58 -0
  47. package/src/models/granite_speech/modeling_granite_speech.js +5 -0
  48. package/src/models/granite_speech/processing_granite_speech.js +62 -0
  49. package/src/models/grounding_dino/image_processing_grounding_dino.js +1 -1
  50. package/src/models/idefics3/modeling_idefics3.js +5 -32
  51. package/src/models/image_processors.js +4 -0
  52. package/src/models/lfm2_vl/image_processing_lfm2_vl.js +305 -0
  53. package/src/models/lfm2_vl/modeling_lfm2_vl.js +13 -0
  54. package/src/models/lfm2_vl/processing_lfm2_vl.js +77 -0
  55. package/src/models/lighton_ocr/modeling_lighton_ocr.js +3 -0
  56. package/src/models/llava/modeling_llava.js +1 -1
  57. package/src/models/marian/tokenization_marian.js +3 -2
  58. package/src/models/mistral3/modeling_mistral3.js +2 -2
  59. package/src/models/mistral4/modeling_mistral4.js +5 -0
  60. package/src/models/modeling_utils.js +283 -300
  61. package/src/models/models.js +26 -1
  62. package/src/models/nemotron_h/modeling_nemotron_h.js +5 -0
  63. package/src/models/olmo_hybrid/modeling_olmo_hybrid.js +5 -0
  64. package/src/models/paligemma/modeling_paligemma.js +2 -25
  65. package/src/models/paligemma/processing_paligemma.js +3 -2
  66. package/src/models/processors.js +8 -0
  67. package/src/models/qwen2_5_vl/modeling_qwen2_5_vl.js +9 -0
  68. package/src/models/qwen2_5_vl/processing_qwen2_5_vl.js +3 -0
  69. package/src/models/qwen2_moe/modeling_qwen2_moe.js +5 -0
  70. package/src/models/qwen2_vl/image_processing_qwen2_vl.js +15 -1
  71. package/src/models/qwen2_vl/modeling_qwen2_vl.js +240 -143
  72. package/src/models/qwen2_vl/processing_qwen2_vl.js +5 -4
  73. package/src/models/qwen3_5/modeling_qwen3_5.js +4 -0
  74. package/src/models/qwen3_5_moe/modeling_qwen3_5_moe.js +4 -0
  75. package/src/models/qwen3_moe/modeling_qwen3_moe.js +5 -0
  76. package/src/models/qwen3_next/modeling_qwen3_next.js +5 -0
  77. package/src/models/qwen3_vl/modeling_qwen3_vl.js +4 -0
  78. package/src/models/qwen3_vl/processing_qwen3_vl.js +3 -0
  79. package/src/models/qwen3_vl_moe/modeling_qwen3_vl_moe.js +4 -0
  80. package/src/models/registry.js +61 -5
  81. package/src/models/sam/image_processing_sam.js +1 -1
  82. package/src/models/session.js +33 -56
  83. package/src/models/smolvlm/modeling_smolvlm.js +7 -0
  84. package/src/models/solar_open/modeling_solar_open.js +5 -0
  85. package/src/models/tokenizers.js +1 -0
  86. package/src/models/ultravox/modeling_ultravox.js +1 -3
  87. package/src/models/voxtral/modeling_voxtral.js +3 -0
  88. package/src/models/voxtral_realtime/feature_extraction_voxtral_realtime.js +71 -0
  89. package/src/models/voxtral_realtime/modeling_voxtral_realtime.js +239 -0
  90. package/src/models/voxtral_realtime/processing_voxtral_realtime.js +113 -0
  91. package/src/models/whisper/feature_extraction_whisper.js +4 -13
  92. package/src/models/whisper/modeling_whisper.js +6 -5
  93. package/src/models/xlm/tokenization_xlm.js +2 -1
  94. package/src/pipelines/automatic-speech-recognition.js +47 -3
  95. package/src/pipelines/document-question-answering.js +1 -1
  96. package/src/pipelines/image-to-text.js +2 -2
  97. package/src/pipelines/index.js +313 -0
  98. package/src/pipelines/summarization.js +1 -1
  99. package/src/pipelines/text-generation.js +5 -1
  100. package/src/pipelines/text-to-audio.js +4 -2
  101. package/src/pipelines/text2text-generation.js +1 -1
  102. package/src/pipelines/translation.js +1 -1
  103. package/src/pipelines/zero-shot-classification.js +3 -2
  104. package/src/pipelines.js +140 -428
  105. package/src/tokenization_utils.js +42 -21
  106. package/src/transformers.js +10 -1
  107. package/src/utils/audio.js +20 -3
  108. package/src/utils/cache/CrossOriginStorageCache.js +251 -0
  109. package/src/utils/cache/FileCache.js +128 -0
  110. package/src/utils/cache/cross-origin-storage.d.ts +38 -0
  111. package/src/utils/cache.js +12 -4
  112. package/src/utils/core.js +23 -1
  113. package/src/utils/devices.js +22 -0
  114. package/src/utils/dtypes.js +55 -0
  115. package/src/utils/hub/{files.js → FileResponse.js} +0 -90
  116. package/src/utils/hub/utils.js +45 -5
  117. package/src/utils/hub.js +67 -23
  118. package/src/utils/image.js +14 -14
  119. package/src/utils/logger.js +67 -0
  120. package/src/utils/lru_cache.js +67 -0
  121. package/src/utils/memoize_promise.js +45 -0
  122. package/src/utils/model-loader.js +35 -17
  123. package/src/utils/model_registry/ModelRegistry.js +382 -0
  124. package/src/utils/model_registry/clear_cache.js +128 -0
  125. package/src/utils/model_registry/get_available_dtypes.js +68 -0
  126. package/src/utils/model_registry/get_file_metadata.js +162 -0
  127. package/src/utils/model_registry/get_files.js +42 -0
  128. package/src/utils/model_registry/get_model_files.js +114 -0
  129. package/src/utils/model_registry/get_pipeline_files.js +44 -0
  130. package/src/utils/model_registry/get_processor_files.js +20 -0
  131. package/src/utils/model_registry/get_tokenizer_files.js +21 -0
  132. package/src/utils/model_registry/is_cached.js +169 -0
  133. package/src/utils/model_registry/resolve_model_type.js +66 -0
  134. package/src/utils/random.js +225 -0
  135. package/src/utils/tensor.js +26 -23
  136. package/src/utils/video.js +2 -2
  137. package/types/backends/onnx.d.ts.map +1 -1
  138. package/types/backends/utils/cacheWasm.d.ts +3 -17
  139. package/types/backends/utils/cacheWasm.d.ts.map +1 -1
  140. package/types/cache_utils.d.ts +29 -0
  141. package/types/cache_utils.d.ts.map +1 -0
  142. package/types/configs.d.ts.map +1 -1
  143. package/types/env.d.ts +60 -27
  144. package/types/env.d.ts.map +1 -1
  145. package/types/generation/logits_sampler.d.ts +2 -2
  146. package/types/generation/logits_sampler.d.ts.map +1 -1
  147. package/types/generation/parameters.d.ts +1 -1
  148. package/types/generation/parameters.d.ts.map +1 -1
  149. package/types/generation/streamers.d.ts +1 -0
  150. package/types/generation/streamers.d.ts.map +1 -1
  151. package/types/image_processors_utils.d.ts +18 -1
  152. package/types/image_processors_utils.d.ts.map +1 -1
  153. package/types/models/afmoe/modeling_afmoe.d.ts +8 -0
  154. package/types/models/afmoe/modeling_afmoe.d.ts.map +1 -0
  155. package/types/models/{ast/modeling_ast.d.ts → audio_spectrogram_transformer/modeling_audio_spectrogram_transformer.d.ts} +1 -1
  156. package/types/models/audio_spectrogram_transformer/modeling_audio_spectrogram_transformer.d.ts.map +1 -0
  157. package/types/models/auto/image_processing_auto.d.ts.map +1 -1
  158. package/types/models/auto/modeling_auto.d.ts +6 -0
  159. package/types/models/auto/modeling_auto.d.ts.map +1 -1
  160. package/types/models/auto/tokenization_auto.d.ts.map +1 -1
  161. package/types/models/chmv2/image_processing_chmv2.d.ts +4 -0
  162. package/types/models/chmv2/image_processing_chmv2.d.ts.map +1 -0
  163. package/types/models/chmv2/modeling_chmv2.d.ts +6 -0
  164. package/types/models/chmv2/modeling_chmv2.d.ts.map +1 -0
  165. package/types/models/clap/feature_extraction_clap.d.ts.map +1 -1
  166. package/types/models/cohere2/modeling_cohere2.d.ts +8 -0
  167. package/types/models/cohere2/modeling_cohere2.d.ts.map +1 -0
  168. package/types/models/cohere_asr/feature_extraction_cohere_asr.d.ts +25 -0
  169. package/types/models/cohere_asr/feature_extraction_cohere_asr.d.ts.map +1 -0
  170. package/types/models/cohere_asr/modeling_cohere_asr.d.ts +9 -0
  171. package/types/models/cohere_asr/modeling_cohere_asr.d.ts.map +1 -0
  172. package/types/models/cohere_asr/processing_cohere_asr.d.ts +27 -0
  173. package/types/models/cohere_asr/processing_cohere_asr.d.ts.map +1 -0
  174. package/types/models/cohere_asr/tokenization_cohere_asr.d.ts +4 -0
  175. package/types/models/cohere_asr/tokenization_cohere_asr.d.ts.map +1 -0
  176. package/types/models/deepseek_v3/modeling_deepseek_v3.d.ts +8 -0
  177. package/types/models/deepseek_v3/modeling_deepseek_v3.d.ts.map +1 -0
  178. package/types/models/detr/image_processing_detr.d.ts +1 -1
  179. package/types/models/eurobert/modeling_eurobert.d.ts +36 -0
  180. package/types/models/eurobert/modeling_eurobert.d.ts.map +1 -0
  181. package/types/models/feature_extractors.d.ts +3 -0
  182. package/types/models/gemma3/image_processing_gemma3.d.ts +4 -0
  183. package/types/models/gemma3/image_processing_gemma3.d.ts.map +1 -0
  184. package/types/models/gemma3/modeling_gemma3.d.ts +4 -1
  185. package/types/models/gemma3/modeling_gemma3.d.ts.map +1 -1
  186. package/types/models/gemma3/processing_gemma3.d.ts +20 -0
  187. package/types/models/gemma3/processing_gemma3.d.ts.map +1 -0
  188. package/types/models/gemma3n/modeling_gemma3n.d.ts +2 -0
  189. package/types/models/gemma3n/modeling_gemma3n.d.ts.map +1 -1
  190. package/types/models/glm46v/image_processing_glm46v.d.ts +4 -0
  191. package/types/models/glm46v/image_processing_glm46v.d.ts.map +1 -0
  192. package/types/models/glm46v/processing_glm46v.d.ts +4 -0
  193. package/types/models/glm46v/processing_glm46v.d.ts.map +1 -0
  194. package/types/models/glm_moe_dsa/modeling_glm_moe_dsa.d.ts +8 -0
  195. package/types/models/glm_moe_dsa/modeling_glm_moe_dsa.d.ts.map +1 -0
  196. package/types/models/glm_ocr/modeling_glm_ocr.d.ts +26 -0
  197. package/types/models/glm_ocr/modeling_glm_ocr.d.ts.map +1 -0
  198. package/types/models/granite_speech/feature_extraction_granite_speech.d.ts +16 -0
  199. package/types/models/granite_speech/feature_extraction_granite_speech.d.ts.map +1 -0
  200. package/types/models/granite_speech/modeling_granite_speech.d.ts +4 -0
  201. package/types/models/granite_speech/modeling_granite_speech.d.ts.map +1 -0
  202. package/types/models/granite_speech/processing_granite_speech.d.ts +19 -0
  203. package/types/models/granite_speech/processing_granite_speech.d.ts.map +1 -0
  204. package/types/models/grounding_dino/image_processing_grounding_dino.d.ts +1 -1
  205. package/types/models/idefics3/modeling_idefics3.d.ts +2 -18
  206. package/types/models/idefics3/modeling_idefics3.d.ts.map +1 -1
  207. package/types/models/image_processors.d.ts +4 -0
  208. package/types/models/lfm2_vl/image_processing_lfm2_vl.d.ts +41 -0
  209. package/types/models/lfm2_vl/image_processing_lfm2_vl.d.ts.map +1 -0
  210. package/types/models/lfm2_vl/modeling_lfm2_vl.d.ts +4 -0
  211. package/types/models/lfm2_vl/modeling_lfm2_vl.d.ts.map +1 -0
  212. package/types/models/lfm2_vl/processing_lfm2_vl.d.ts +18 -0
  213. package/types/models/lfm2_vl/processing_lfm2_vl.d.ts.map +1 -0
  214. package/types/models/lighton_ocr/modeling_lighton_ocr.d.ts +4 -0
  215. package/types/models/lighton_ocr/modeling_lighton_ocr.d.ts.map +1 -0
  216. package/types/models/marian/tokenization_marian.d.ts.map +1 -1
  217. package/types/models/mistral3/modeling_mistral3.d.ts +2 -2
  218. package/types/models/mistral3/modeling_mistral3.d.ts.map +1 -1
  219. package/types/models/mistral4/modeling_mistral4.d.ts +8 -0
  220. package/types/models/mistral4/modeling_mistral4.d.ts.map +1 -0
  221. package/types/models/modeling_utils.d.ts +46 -27
  222. package/types/models/modeling_utils.d.ts.map +1 -1
  223. package/types/models/models.d.ts +26 -1
  224. package/types/models/nemotron_h/modeling_nemotron_h.d.ts +8 -0
  225. package/types/models/nemotron_h/modeling_nemotron_h.d.ts.map +1 -0
  226. package/types/models/olmo_hybrid/modeling_olmo_hybrid.d.ts +8 -0
  227. package/types/models/olmo_hybrid/modeling_olmo_hybrid.d.ts.map +1 -0
  228. package/types/models/paligemma/modeling_paligemma.d.ts +2 -8
  229. package/types/models/paligemma/modeling_paligemma.d.ts.map +1 -1
  230. package/types/models/paligemma/processing_paligemma.d.ts.map +1 -1
  231. package/types/models/processors.d.ts +8 -0
  232. package/types/models/qwen2_5_vl/modeling_qwen2_5_vl.d.ts +7 -0
  233. package/types/models/qwen2_5_vl/modeling_qwen2_5_vl.d.ts.map +1 -0
  234. package/types/models/qwen2_5_vl/processing_qwen2_5_vl.d.ts +4 -0
  235. package/types/models/qwen2_5_vl/processing_qwen2_5_vl.d.ts.map +1 -0
  236. package/types/models/qwen2_moe/modeling_qwen2_moe.d.ts +8 -0
  237. package/types/models/qwen2_moe/modeling_qwen2_moe.d.ts.map +1 -0
  238. package/types/models/qwen2_vl/image_processing_qwen2_vl.d.ts +3 -0
  239. package/types/models/qwen2_vl/image_processing_qwen2_vl.d.ts.map +1 -1
  240. package/types/models/qwen2_vl/modeling_qwen2_vl.d.ts +44 -6
  241. package/types/models/qwen2_vl/modeling_qwen2_vl.d.ts.map +1 -1
  242. package/types/models/qwen2_vl/processing_qwen2_vl.d.ts +1 -0
  243. package/types/models/qwen2_vl/processing_qwen2_vl.d.ts.map +1 -1
  244. package/types/models/qwen3_5/modeling_qwen3_5.d.ts +6 -0
  245. package/types/models/qwen3_5/modeling_qwen3_5.d.ts.map +1 -0
  246. package/types/models/qwen3_5_moe/modeling_qwen3_5_moe.d.ts +7 -0
  247. package/types/models/qwen3_5_moe/modeling_qwen3_5_moe.d.ts.map +1 -0
  248. package/types/models/qwen3_moe/modeling_qwen3_moe.d.ts +8 -0
  249. package/types/models/qwen3_moe/modeling_qwen3_moe.d.ts.map +1 -0
  250. package/types/models/qwen3_next/modeling_qwen3_next.d.ts +8 -0
  251. package/types/models/qwen3_next/modeling_qwen3_next.d.ts.map +1 -0
  252. package/types/models/qwen3_vl/modeling_qwen3_vl.d.ts +7 -0
  253. package/types/models/qwen3_vl/modeling_qwen3_vl.d.ts.map +1 -0
  254. package/types/models/qwen3_vl/processing_qwen3_vl.d.ts +4 -0
  255. package/types/models/qwen3_vl/processing_qwen3_vl.d.ts.map +1 -0
  256. package/types/models/qwen3_vl_moe/modeling_qwen3_vl_moe.d.ts +7 -0
  257. package/types/models/qwen3_vl_moe/modeling_qwen3_vl_moe.d.ts.map +1 -0
  258. package/types/models/registry.d.ts +2 -1
  259. package/types/models/registry.d.ts.map +1 -1
  260. package/types/models/sam/image_processing_sam.d.ts +1 -1
  261. package/types/models/session.d.ts +3 -2
  262. package/types/models/session.d.ts.map +1 -1
  263. package/types/models/smolvlm/modeling_smolvlm.d.ts +8 -0
  264. package/types/models/smolvlm/modeling_smolvlm.d.ts.map +1 -0
  265. package/types/models/solar_open/modeling_solar_open.d.ts +8 -0
  266. package/types/models/solar_open/modeling_solar_open.d.ts.map +1 -0
  267. package/types/models/tokenizers.d.ts +1 -0
  268. package/types/models/ultravox/modeling_ultravox.d.ts +0 -2
  269. package/types/models/ultravox/modeling_ultravox.d.ts.map +1 -1
  270. package/types/models/voxtral/modeling_voxtral.d.ts +4 -0
  271. package/types/models/voxtral/modeling_voxtral.d.ts.map +1 -0
  272. package/types/models/voxtral_realtime/feature_extraction_voxtral_realtime.d.ts +28 -0
  273. package/types/models/voxtral_realtime/feature_extraction_voxtral_realtime.d.ts.map +1 -0
  274. package/types/models/voxtral_realtime/modeling_voxtral_realtime.d.ts +17 -0
  275. package/types/models/voxtral_realtime/modeling_voxtral_realtime.d.ts.map +1 -0
  276. package/types/models/voxtral_realtime/processing_voxtral_realtime.d.ts +44 -0
  277. package/types/models/voxtral_realtime/processing_voxtral_realtime.d.ts.map +1 -0
  278. package/types/models/whisper/feature_extraction_whisper.d.ts.map +1 -1
  279. package/types/models/whisper/modeling_whisper.d.ts.map +1 -1
  280. package/types/models/xlm/tokenization_xlm.d.ts.map +1 -1
  281. package/types/pipelines/automatic-speech-recognition.d.ts +7 -2
  282. package/types/pipelines/automatic-speech-recognition.d.ts.map +1 -1
  283. package/types/pipelines/document-question-answering.d.ts +2 -2
  284. package/types/pipelines/document-question-answering.d.ts.map +1 -1
  285. package/types/pipelines/image-to-text.d.ts +4 -4
  286. package/types/pipelines/image-to-text.d.ts.map +1 -1
  287. package/types/pipelines/index.d.ts +265 -0
  288. package/types/pipelines/index.d.ts.map +1 -0
  289. package/types/pipelines/summarization.d.ts +2 -2
  290. package/types/pipelines/summarization.d.ts.map +1 -1
  291. package/types/pipelines/text-generation.d.ts +7 -3
  292. package/types/pipelines/text-generation.d.ts.map +1 -1
  293. package/types/pipelines/text-to-audio.d.ts.map +1 -1
  294. package/types/pipelines/text2text-generation.d.ts +3 -3
  295. package/types/pipelines/text2text-generation.d.ts.map +1 -1
  296. package/types/pipelines/translation.d.ts +2 -2
  297. package/types/pipelines/translation.d.ts.map +1 -1
  298. package/types/pipelines/zero-shot-classification.d.ts.map +1 -1
  299. package/types/pipelines.d.ts +51 -291
  300. package/types/pipelines.d.ts.map +1 -1
  301. package/types/tokenization_utils.d.ts +44 -26
  302. package/types/tokenization_utils.d.ts.map +1 -1
  303. package/types/transformers.d.ts +7 -1
  304. package/types/transformers.d.ts.map +1 -1
  305. package/types/utils/audio.d.ts +5 -2
  306. package/types/utils/audio.d.ts.map +1 -1
  307. package/types/utils/cache/CrossOriginStorageCache.d.ts +120 -0
  308. package/types/utils/cache/CrossOriginStorageCache.d.ts.map +1 -0
  309. package/types/utils/cache/FileCache.d.ts +39 -0
  310. package/types/utils/cache/FileCache.d.ts.map +1 -0
  311. package/types/utils/cache.d.ts +10 -4
  312. package/types/utils/cache.d.ts.map +1 -1
  313. package/types/utils/core.d.ts +59 -2
  314. package/types/utils/core.d.ts.map +1 -1
  315. package/types/utils/devices.d.ts +15 -0
  316. package/types/utils/devices.d.ts.map +1 -1
  317. package/types/utils/dtypes.d.ts +17 -1
  318. package/types/utils/dtypes.d.ts.map +1 -1
  319. package/types/utils/hub/{files.d.ts → FileResponse.d.ts} +1 -32
  320. package/types/utils/hub/FileResponse.d.ts.map +1 -0
  321. package/types/utils/hub/utils.d.ts +19 -3
  322. package/types/utils/hub/utils.d.ts.map +1 -1
  323. package/types/utils/hub.d.ts +36 -7
  324. package/types/utils/hub.d.ts.map +1 -1
  325. package/types/utils/image.d.ts +1 -1
  326. package/types/utils/logger.d.ts +28 -0
  327. package/types/utils/logger.d.ts.map +1 -0
  328. package/types/utils/lru_cache.d.ts +38 -0
  329. package/types/utils/lru_cache.d.ts.map +1 -0
  330. package/types/utils/memoize_promise.d.ts +14 -0
  331. package/types/utils/memoize_promise.d.ts.map +1 -0
  332. package/types/utils/model-loader.d.ts +15 -0
  333. package/types/utils/model-loader.d.ts.map +1 -1
  334. package/types/utils/model_registry/ModelRegistry.d.ts +298 -0
  335. package/types/utils/model_registry/ModelRegistry.d.ts.map +1 -0
  336. package/types/utils/model_registry/clear_cache.d.ts +74 -0
  337. package/types/utils/model_registry/clear_cache.d.ts.map +1 -0
  338. package/types/utils/model_registry/get_available_dtypes.d.ts +26 -0
  339. package/types/utils/model_registry/get_available_dtypes.d.ts.map +1 -0
  340. package/types/utils/model_registry/get_file_metadata.d.ts +20 -0
  341. package/types/utils/model_registry/get_file_metadata.d.ts.map +1 -0
  342. package/types/utils/model_registry/get_files.d.ts +23 -0
  343. package/types/utils/model_registry/get_files.d.ts.map +1 -0
  344. package/types/utils/model_registry/get_model_files.d.ts +48 -0
  345. package/types/utils/model_registry/get_model_files.d.ts.map +1 -0
  346. package/types/utils/model_registry/get_pipeline_files.d.ts +22 -0
  347. package/types/utils/model_registry/get_pipeline_files.d.ts.map +1 -0
  348. package/types/utils/model_registry/get_processor_files.d.ts +9 -0
  349. package/types/utils/model_registry/get_processor_files.d.ts.map +1 -0
  350. package/types/utils/model_registry/get_tokenizer_files.d.ts +9 -0
  351. package/types/utils/model_registry/get_tokenizer_files.d.ts.map +1 -0
  352. package/types/utils/model_registry/is_cached.d.ts +105 -0
  353. package/types/utils/model_registry/is_cached.d.ts.map +1 -0
  354. package/types/utils/model_registry/resolve_model_type.d.ts +24 -0
  355. package/types/utils/model_registry/resolve_model_type.d.ts.map +1 -0
  356. package/types/utils/random.d.ts +86 -0
  357. package/types/utils/random.d.ts.map +1 -0
  358. package/types/utils/tensor.d.ts.map +1 -1
  359. package/src/utils/data-structures.js +0 -572
  360. package/types/models/ast/modeling_ast.d.ts.map +0 -1
  361. package/types/utils/data-structures.d.ts +0 -294
  362. package/types/utils/data-structures.d.ts.map +0 -1
  363. package/types/utils/hub/files.d.ts.map +0 -1
  364. /package/src/models/{ast/modeling_ast.js → audio_spectrogram_transformer/modeling_audio_spectrogram_transformer.js} +0 -0
@@ -1,12 +1,16 @@
1
1
  import { apis, env } from '../env.js';
2
- import { FileCache } from './hub/files.js';
2
+ import { FileCache } from './cache/FileCache.js';
3
+ import { logger } from './logger.js';
4
+ import { CrossOriginStorage } from './cache/CrossOriginStorageCache.js';
3
5
 
4
6
  /**
5
7
  * @typedef {Object} CacheInterface
6
- * @property {(request: string) => Promise<Response|import('./hub/files.js').FileResponse|undefined|string>} match
8
+ * @property {(request: string) => Promise<Response|import('./hub/FileResponse.js').FileResponse|undefined|string>} match
7
9
  * Checks if a request is in the cache and returns the cached response if found.
8
10
  * @property {(request: string, response: Response, progress_callback?: (data: {progress: number, loaded: number, total: number}) => void) => Promise<void>} put
9
11
  * Adds a response to the cache.
12
+ * @property {(request: string) => Promise<boolean>} [delete]
13
+ * Deletes a request from the cache. Returns true if deleted, false otherwise.
10
14
  */
11
15
 
12
16
  /**
@@ -35,6 +39,10 @@ export async function getCache(file_cache_dir = null) {
35
39
  cache = env.customCache;
36
40
  }
37
41
 
42
+ if (!cache && env.experimental_useCrossOriginStorage && CrossOriginStorage.isAvailable()) {
43
+ cache = new CrossOriginStorage();
44
+ }
45
+
38
46
  if (!cache && env.useBrowserCache) {
39
47
  if (typeof caches === 'undefined') {
40
48
  throw Error('Browser cache is not available in this environment.');
@@ -47,7 +55,7 @@ export async function getCache(file_cache_dir = null) {
47
55
  // So, instead of crashing, we just ignore the error and continue without using the cache.
48
56
  cache = await caches.open(env.cacheKey);
49
57
  } catch (e) {
50
- console.warn('An error occurred while opening the browser cache:', e);
58
+ logger.warn('An error occurred while opening the browser cache:', e);
51
59
  }
52
60
  }
53
61
 
@@ -67,7 +75,7 @@ export async function getCache(file_cache_dir = null) {
67
75
  * Searches the cache for any of the provided names and returns the first match found.
68
76
  * @param {CacheInterface} cache The cache to search
69
77
  * @param {...string} names The names of the items to search for
70
- * @returns {Promise<import('./hub/files.js').FileResponse|Response|undefined|string>} The item from the cache, or undefined if not found.
78
+ * @returns {Promise<import('./hub/FileResponse.js').FileResponse|Response|undefined|string>} The item from the cache, or undefined if not found.
71
79
  */
72
80
  export async function tryCache(cache, ...names) {
73
81
  for (let name of names) {
package/src/utils/core.js CHANGED
@@ -31,6 +31,28 @@
31
31
  * @property {number} total The total number of bytes to be loaded.
32
32
  */
33
33
 
34
+ /**
35
+ * @typedef {Object} FileLoadingProgress
36
+ * @property {number} loaded The number of bytes loaded for this file.
37
+ * @property {number} total The total number of bytes for this file.
38
+ */
39
+
40
+ /**
41
+ * @typedef {Record<string, FileLoadingProgress>} FilesLoadingMap
42
+ * A mapping of file names to their loading progress. Each key is a file path and each value contains
43
+ * the loaded and total bytes for that file.
44
+ */
45
+
46
+ /**
47
+ * @typedef {Object} TotalProgressInfo
48
+ * @property {'progress_total'} status
49
+ * @property {string} name The model id or directory path.
50
+ * @property {number} progress A number between 0 and 100.
51
+ * @property {number} loaded The number of bytes loaded.
52
+ * @property {number} total The total number of bytes to be loaded.
53
+ * @property {FilesLoadingMap} files A mapping of file names to their loading progress.
54
+ */
55
+
34
56
  /**
35
57
  * @typedef {Object} DoneProgressInfo
36
58
  * @property {'done'} status
@@ -46,7 +68,7 @@
46
68
  */
47
69
 
48
70
  /**
49
- * @typedef {InitiateProgressInfo | DownloadProgressInfo | ProgressStatusInfo | DoneProgressInfo | ReadyProgressInfo} ProgressInfo
71
+ * @typedef {InitiateProgressInfo | DownloadProgressInfo | ProgressStatusInfo | DoneProgressInfo | ReadyProgressInfo | TotalProgressInfo} ProgressInfo
50
72
  */
51
73
 
52
74
  /**
@@ -1,3 +1,5 @@
1
+ import { apis } from '../env.js';
2
+
1
3
  /**
2
4
  * The list of devices supported by Transformers.js
3
5
  */
@@ -16,7 +18,27 @@ export const DEVICE_TYPES = Object.freeze({
16
18
  'webnn-gpu': 'webnn-gpu', // WebNN GPU
17
19
  'webnn-cpu': 'webnn-cpu', // WebNN CPU
18
20
  });
21
+ const DEFAULT_DEVICE = apis.IS_NODE_ENV ? 'cpu' : 'wasm';
19
22
 
20
23
  /**
21
24
  * @typedef {keyof typeof DEVICE_TYPES} DeviceType
22
25
  */
26
+
27
+ /**
28
+ * Resolves a device configuration value to a concrete device string.
29
+ * Handles string, per-file object, or undefined config, with a default fallback.
30
+ * @param {string|Record<string, string>|null|undefined} deviceConfig The device config value.
31
+ * @param {string} fileName The model file name to look up if deviceConfig is an object.
32
+ * @param {Object} [options]
33
+ * @param {(message: string) => void} [options.warn] Optional callback invoked when deviceConfig is a per-file object but fileName is not found.
34
+ * @returns {string} The resolved device string.
35
+ */
36
+ export function selectDevice(deviceConfig, fileName, { warn } = {}) {
37
+ if (!deviceConfig) return DEFAULT_DEVICE;
38
+ if (typeof deviceConfig === 'string') return deviceConfig;
39
+ if (deviceConfig.hasOwnProperty(fileName)) return deviceConfig[fileName];
40
+ if (warn) {
41
+ warn(`device not specified for "${fileName}". Using the default device (${DEFAULT_DEVICE}).`);
42
+ }
43
+ return DEFAULT_DEVICE;
44
+ }
@@ -45,6 +45,7 @@ export const DATA_TYPES = Object.freeze({
45
45
  });
46
46
  /** @typedef {keyof typeof DATA_TYPES} DataType */
47
47
 
48
+ export const DEFAULT_DEVICE_DTYPE = DATA_TYPES.fp32;
48
49
  export const DEFAULT_DEVICE_DTYPE_MAPPING = Object.freeze({
49
50
  // NOTE: If not specified, will default to fp32
50
51
  [DEVICE_TYPES.wasm]: DATA_TYPES.q8,
@@ -62,6 +63,60 @@ export const DEFAULT_DTYPE_SUFFIX_MAPPING = Object.freeze({
62
63
  [DATA_TYPES.bnb4]: '_bnb4',
63
64
  });
64
65
 
66
+ /**
67
+ * Resolves a dtype configuration value to a concrete dtype string.
68
+ * Handles string, per-file object, and "auto" forms with device-based fallback.
69
+ * @param {DataType|Record<string, DataType>|null|undefined} dtype The dtype config value.
70
+ * @param {string} fileName The model file name to look up if dtype is an object.
71
+ * @param {string} selectedDevice The resolved device string for fallback.
72
+ * @param {Object} [options]
73
+ * @param {DataType|Record<string, DataType>|null} [options.configDtype=null] Config dtype used as fallback when dtype is "auto" (supports device_config overlay in session.js).
74
+ * @param {(message: string) => void} [options.warn] Optional callback invoked when dtype is a per-file object but fileName is not found.
75
+ * @returns {DataType} The resolved dtype string.
76
+ */
77
+ export function selectDtype(dtype, fileName, selectedDevice, { configDtype = null, warn } = {}) {
78
+ /** @type {string|null|undefined} */
79
+ let resolved;
80
+ let needsWarn = false;
81
+ if (dtype && typeof dtype !== 'string') {
82
+ if (dtype.hasOwnProperty(fileName)) {
83
+ resolved = dtype[fileName];
84
+ } else {
85
+ resolved = null;
86
+ needsWarn = true;
87
+ }
88
+ } else {
89
+ resolved = /** @type {string|null|undefined} */ (dtype);
90
+ }
91
+
92
+ /** @type {DataType} */
93
+ let result;
94
+
95
+ // Handle 'auto': try configDtype fallback
96
+ if (resolved === DATA_TYPES.auto) {
97
+ if (configDtype) {
98
+ const fallback = typeof configDtype === 'string' ? configDtype : configDtype?.[fileName];
99
+ if (fallback && fallback !== DATA_TYPES.auto && DATA_TYPES.hasOwnProperty(fallback)) {
100
+ return /** @type {DataType} */ (fallback);
101
+ }
102
+ }
103
+ result = DEFAULT_DEVICE_DTYPE_MAPPING[selectedDevice] ?? DEFAULT_DEVICE_DTYPE;
104
+ } else if (resolved && DATA_TYPES.hasOwnProperty(resolved)) {
105
+ // Valid known dtype
106
+ result = /** @type {DataType} */ (resolved);
107
+ } else {
108
+ // Fallback to device default
109
+ result = DEFAULT_DEVICE_DTYPE_MAPPING[selectedDevice] ?? DEFAULT_DEVICE_DTYPE;
110
+ }
111
+
112
+ if (needsWarn && warn) {
113
+ warn(
114
+ `dtype not specified for "${fileName}". Using the default dtype (${result}) for this device (${selectedDevice}).`,
115
+ );
116
+ }
117
+ return result;
118
+ }
119
+
65
120
  export const DataTypeMap = Object.freeze({
66
121
  float32: Float32Array,
67
122
  // @ts-ignore ts(2552) Limited availability of Float16Array across browsers:
@@ -1,5 +1,4 @@
1
1
  import fs from 'node:fs';
2
- import path from 'node:path';
3
2
 
4
3
  /**
5
4
  * Mapping from file extensions to MIME types.
@@ -120,92 +119,3 @@ export class FileResponse {
120
119
  return JSON.parse(await this.text());
121
120
  }
122
121
  }
123
-
124
- /**
125
- * File system cache implementation that implements the CacheInterface.
126
- * Provides `match` and `put` methods compatible with the Web Cache API.
127
- */
128
- export class FileCache {
129
- /**
130
- * Instantiate a `FileCache` object.
131
- * @param {string} path
132
- */
133
- constructor(path) {
134
- this.path = path;
135
- }
136
-
137
- /**
138
- * Checks whether the given request is in the cache.
139
- * @param {string} request
140
- * @returns {Promise<FileResponse | undefined>}
141
- */
142
- async match(request) {
143
- let filePath = path.join(this.path, request);
144
- let file = new FileResponse(filePath);
145
-
146
- if (file.exists) {
147
- return file;
148
- } else {
149
- return undefined;
150
- }
151
- }
152
-
153
- /**
154
- * Adds the given response to the cache.
155
- * @param {string} request
156
- * @param {Response} response
157
- * @param {(data: {progress: number, loaded: number, total: number}) => void} [progress_callback] Optional.
158
- * The function to call with progress updates
159
- * @returns {Promise<void>}
160
- */
161
- async put(request, response, progress_callback = undefined) {
162
- let filePath = path.join(this.path, request);
163
-
164
- try {
165
- const contentLength = response.headers.get('Content-Length');
166
- const total = parseInt(contentLength ?? '0');
167
- let loaded = 0;
168
-
169
- await fs.promises.mkdir(path.dirname(filePath), { recursive: true });
170
- const fileStream = fs.createWriteStream(filePath);
171
- const reader = response.body.getReader();
172
-
173
- while (true) {
174
- const { done, value } = await reader.read();
175
- if (done) {
176
- break;
177
- }
178
-
179
- await new Promise((resolve, reject) => {
180
- fileStream.write(value, (err) => {
181
- if (err) {
182
- reject(err);
183
- return;
184
- }
185
- resolve();
186
- });
187
- });
188
-
189
- loaded += value.length;
190
- const progress = total ? (loaded / total) * 100 : 0;
191
-
192
- progress_callback?.({ progress, loaded, total });
193
- }
194
-
195
- fileStream.close();
196
- } catch (error) {
197
- // Clean up the file if an error occurred during download
198
- try {
199
- await fs.promises.unlink(filePath);
200
- } catch {}
201
- throw error;
202
- }
203
- }
204
-
205
- // TODO add the rest?
206
- // addAll(requests: RequestInfo[]): Promise<void>;
207
- // delete(request: RequestInfo | URL, options?: CacheQueryOptions): Promise<boolean>;
208
- // keys(request?: RequestInfo | URL, options?: CacheQueryOptions): Promise<ReadonlyArray<Request>>;
209
- // match(request: RequestInfo | URL, options?: CacheQueryOptions): Promise<Response | undefined>;
210
- // matchAll(request?: RequestInfo | URL, options?: CacheQueryOptions): Promise<ReadonlyArray<Response>>;
211
- }
@@ -1,4 +1,5 @@
1
1
  import { ERROR_MAPPING, REPO_ID_REGEX } from './constants.js';
2
+ import { logger } from '../logger.js';
2
3
 
3
4
  /**
4
5
  * Joins multiple parts of a path into a single path, while handling leading and trailing slashes.
@@ -79,16 +80,21 @@ export function handleError(status, remoteURL, fatal) {
79
80
  /**
80
81
  * Read and track progress when reading a Response object
81
82
  *
82
- * @param {Response|import('./files.js').FileResponse} response The Response object to read
83
+ * @param {Response|import('./FileResponse.js').FileResponse} response The Response object to read
83
84
  * @param {(data: {progress: number, loaded: number, total: number}) => void} progress_callback The function to call with progress updates
85
+ * @param {number} [expectedSize] The expected size of the file (used when content-length header is missing)
84
86
  * @returns {Promise<Uint8Array>} A Promise that resolves with the Uint8Array buffer
85
87
  */
86
- export async function readResponse(response, progress_callback) {
88
+ export async function readResponse(response, progress_callback, expectedSize) {
87
89
  const contentLength = response.headers.get('Content-Length');
88
- if (contentLength === null) {
89
- console.warn('Unable to determine content-length from response headers. Will expand buffer when needed.');
90
+
91
+ // Use content-length if available, otherwise fall back to expectedSize (from metadata)
92
+ let total = contentLength ? parseInt(contentLength, 10) : (expectedSize ?? 0);
93
+
94
+ if (contentLength === null && !expectedSize) {
95
+ logger.warn('Unable to determine content-length from response headers. Will expand buffer when needed.');
90
96
  }
91
- let total = parseInt(contentLength ?? '0');
97
+
92
98
  let buffer = new Uint8Array(total);
93
99
  let loaded = 0;
94
100
 
@@ -126,3 +132,37 @@ export async function readResponse(response, progress_callback) {
126
132
 
127
133
  return buffer;
128
134
  }
135
+
136
+ /**
137
+ * Checks if the given URL is a blob URL (created via URL.createObjectURL).
138
+ * Blob URLs should not be cached as they are temporary in-memory references.
139
+ * @param {string} url - The URL to check.
140
+ * @returns {boolean} True if the URL is a blob URL, false otherwise.
141
+ */
142
+ export function isBlobURL(url) {
143
+ return isValidUrl(url, ['blob:']);
144
+ }
145
+
146
+ /**
147
+ * Converts any URL to an absolute URL if needed.
148
+ * If the URL is already absolute (http://, https://, or blob:), returns it unchanged (handled by new URL(...)).
149
+ * Otherwise, resolves it relative to the current page location (browser) or module location (Node/Bun/Deno).
150
+ * @param {string} url - The URL to convert (can be relative or absolute).
151
+ * @returns {string} The absolute URL.
152
+ */
153
+ export function toAbsoluteURL(url) {
154
+ let baseURL;
155
+
156
+ if (typeof location !== 'undefined' && location.href) {
157
+ // Browser environment: use location.href
158
+ baseURL = location.href;
159
+ } else if (typeof import.meta !== 'undefined' && import.meta.url) {
160
+ // Node.js/Bun/Deno module environment: use import.meta.url
161
+ baseURL = import.meta.url;
162
+ } else {
163
+ // Fallback: if no base is available, return the URL unchanged
164
+ return url;
165
+ }
166
+
167
+ return new URL(url, baseURL).href;
168
+ }
package/src/utils/hub.js CHANGED
@@ -6,9 +6,12 @@
6
6
 
7
7
  import { apis, env } from '../env.js';
8
8
  import { dispatchCallback } from './core.js';
9
- import { FileResponse, FileCache } from './hub/files.js';
9
+ import { FileResponse } from './hub/FileResponse.js';
10
+ import { FileCache } from './cache/FileCache.js';
10
11
  import { handleError, isValidUrl, pathJoin, isValidHfModelId, readResponse } from './hub/utils.js';
11
12
  import { getCache, tryCache } from './cache.js';
13
+ import { get_file_metadata } from './model_registry/get_file_metadata.js';
14
+ import { logger } from './logger.js';
12
15
 
13
16
  export { MAX_EXTERNAL_DATA_CHUNKS } from './hub/constants.js';
14
17
 
@@ -37,7 +40,7 @@ export { MAX_EXTERNAL_DATA_CHUNKS } from './hub/constants.js';
37
40
  * @typedef {Object} ModelSpecificPretrainedOptions Options for loading a pretrained model.
38
41
  * @property {string} [subfolder='onnx'] In case the relevant files are located inside a subfolder of the model repo on huggingface.co,
39
42
  * you can specify the folder name here.
40
- * @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.
43
+ * @property {string} [model_file_name=null] If specified, load the model with this name (excluding the dtype and .onnx suffixes). Currently only valid for encoder- or decoder-only models.
41
44
  * @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.
42
45
  * @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.
43
46
  * @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).
@@ -63,14 +66,30 @@ export async function getFile(urlOrPath) {
63
66
  : urlOrPath.toString()
64
67
  : urlOrPath,
65
68
  );
66
- } else if (typeof process !== 'undefined' && process?.release?.name === 'node') {
69
+ } else {
70
+ return env.fetch(urlOrPath, {
71
+ headers: getFetchHeaders(urlOrPath),
72
+ });
73
+ }
74
+ }
75
+
76
+ /**
77
+ * Generates appropriate HTTP headers for fetching resources.
78
+ * In Node.js environments, adds User-Agent and Authorization headers when applicable.
79
+ * In browser environments, returns minimal headers for security.
80
+ *
81
+ * @param {URL|string} urlOrPath The URL or path being fetched.
82
+ * @returns {Headers} A Headers object with appropriate headers for the request.
83
+ */
84
+ export function getFetchHeaders(urlOrPath) {
85
+ const isNode = typeof process !== 'undefined' && process?.release?.name === 'node';
86
+ const headers = new Headers();
87
+
88
+ if (isNode) {
67
89
  const IS_CI = !!process.env?.TESTING_REMOTELY;
68
90
  const version = env.version;
69
-
70
- const headers = new Headers();
71
91
  headers.set('User-Agent', `transformers.js/${version}; is_ci/${IS_CI};`);
72
92
 
73
- // Check whether we are making a request to the Hugging Face Hub.
74
93
  const isHFURL = isValidUrl(urlOrPath, ['http:', 'https:'], ['huggingface.co', 'hf.co']);
75
94
  if (isHFURL) {
76
95
  // If an access token is present in the environment variables,
@@ -81,13 +100,13 @@ export async function getFile(urlOrPath) {
81
100
  headers.set('Authorization', `Bearer ${token}`);
82
101
  }
83
102
  }
84
- return fetch(urlOrPath, { headers });
85
103
  } else {
86
104
  // Running in a browser-environment, so we use default headers
87
105
  // NOTE: We do not allow passing authorization headers in the browser,
88
106
  // since this would require exposing the token to the client.
89
- return fetch(urlOrPath);
90
107
  }
108
+
109
+ return headers;
91
110
  }
92
111
 
93
112
  /**
@@ -103,7 +122,7 @@ export async function getFile(urlOrPath) {
103
122
  * @returns {{ requestURL: string, localPath: string, remoteURL: string, proposedCacheKey: string, validModelId: boolean }}
104
123
  * An object containing all the paths and URLs for the resource.
105
124
  */
106
- function buildResourcePaths(path_or_repo_id, filename, options = {}, cache = null) {
125
+ export function buildResourcePaths(path_or_repo_id, filename, options = {}, cache = null) {
107
126
  const revision = options.revision ?? 'main';
108
127
  const requestURL = pathJoin(path_or_repo_id, filename);
109
128
 
@@ -142,7 +161,7 @@ function buildResourcePaths(path_or_repo_id, filename, options = {}, cache = nul
142
161
  * @param {import('./cache.js').CacheInterface | null} cache The cache instance to check.
143
162
  * @param {string} localPath The local path to try first.
144
163
  * @param {string} proposedCacheKey The proposed cache key to try second.
145
- * @returns {Promise<Response|import('./hub/files.js').FileResponse|undefined|string>}
164
+ * @returns {Promise<Response|import('./hub/FileResponse.js').FileResponse|undefined|string>}
146
165
  * The cached response if found, undefined otherwise.
147
166
  */
148
167
  export async function checkCachedResource(cache, localPath, proposedCacheKey) {
@@ -164,7 +183,7 @@ export async function checkCachedResource(cache, localPath, proposedCacheKey) {
164
183
  * @param {string} filename The name of the file to cache.
165
184
  * @param {import('./cache.js').CacheInterface} cache The cache instance to store in.
166
185
  * @param {string} cacheKey The cache key to use.
167
- * @param {Response|import('./hub/files.js').FileResponse} response The response to cache.
186
+ * @param {Response|import('./hub/FileResponse.js').FileResponse} response The response to cache.
168
187
  * @param {Uint8Array} [result] The result buffer if already read.
169
188
  * @param {PretrainedOptions} [options] Options containing progress callback and context for progress updates.
170
189
  * @returns {Promise<void>}
@@ -190,17 +209,20 @@ export async function storeCachedResource(path_or_repo_id, filename, cache, cach
190
209
  await cache.put(cacheKey, /** @type {Response} */ (response), wrapped_progress);
191
210
  } else if (typeof response !== 'string') {
192
211
  // NOTE: We use `new Response(buffer, ...)` instead of `response.clone()` to handle LFS files
212
+ // Explicitly set content-length from the buffer size, since the browser Cache API may strip it.
213
+ const headers = new Headers(response.headers);
214
+ headers.set('content-length', result.byteLength.toString());
193
215
  await cache
194
216
  .put(
195
217
  cacheKey,
196
218
  new Response(/** @type {any} */ (result), {
197
- headers: response.headers,
219
+ headers,
198
220
  }),
199
221
  )
200
222
  .catch((err) => {
201
223
  // Do not crash if unable to add to cache (e.g., QuotaExceededError).
202
224
  // Rather, log a warning and proceed with execution.
203
- console.warn(`Unable to add response to browser cache: ${err}.`);
225
+ logger.warn(`Unable to add response to browser cache: ${err}.`);
204
226
  });
205
227
  }
206
228
  }
@@ -241,7 +263,7 @@ export async function loadResourceFile(
241
263
  // Whether to cache the final response in the end.
242
264
  let toCacheResponse = false;
243
265
 
244
- /** @type {Response|import('./hub/files.js').FileResponse|undefined|string} */
266
+ /** @type {Response|import('./hub/FileResponse.js').FileResponse|undefined|string} */
245
267
  let response;
246
268
 
247
269
  // Check cache
@@ -262,7 +284,7 @@ export async function loadResourceFile(
262
284
  } catch (e) {
263
285
  // Something went wrong while trying to get the file locally.
264
286
  // NOTE: error handling is done in the next step (since `response` will be undefined)
265
- console.warn(`Unable to load from local path "${localPath}": "${e}"`);
287
+ logger.warn(`Unable to load from local path "${localPath}": "${e}"`);
266
288
  }
267
289
  } else if (options.local_files_only) {
268
290
  throw new Error(`\`local_files_only=true\`, but attempted to load a remote file from: ${requestURL}.`);
@@ -354,14 +376,36 @@ export async function loadResourceFile(
354
376
  total: buffer.length,
355
377
  });
356
378
  } else {
357
- buffer = await readResponse(response, (data) => {
358
- dispatchCallback(options.progress_callback, {
359
- status: 'progress',
360
- name: path_or_repo_id,
361
- file: filename,
362
- ...data,
363
- });
364
- });
379
+ // Get expected file size from response headers or metadata
380
+ // This helps with progress tracking when content-length is missing
381
+ let expectedSize;
382
+ const contentLength = response.headers.get('content-length');
383
+ if (contentLength) {
384
+ expectedSize = parseInt(contentLength, 10);
385
+ } else {
386
+ // Try to get size from metadata (useful when content-length is missing during download)
387
+ try {
388
+ const metadata = await get_file_metadata(path_or_repo_id, filename, options);
389
+ if (metadata.size) {
390
+ expectedSize = metadata.size;
391
+ }
392
+ } catch (e) {
393
+ // Ignore metadata fetch errors
394
+ }
395
+ }
396
+
397
+ buffer = await readResponse(
398
+ response,
399
+ (data) => {
400
+ dispatchCallback(options.progress_callback, {
401
+ status: 'progress',
402
+ name: path_or_repo_id,
403
+ file: filename,
404
+ ...data,
405
+ });
406
+ },
407
+ expectedSize,
408
+ );
365
409
  }
366
410
  }
367
411
  result = buffer;
@@ -15,16 +15,16 @@ import { saveBlob } from './io.js';
15
15
 
16
16
  // Will be empty (or not used) if running in browser or web-worker
17
17
  import sharp from 'sharp';
18
+ import { logger } from './logger.js';
18
19
 
19
20
  let createCanvasFunction;
20
21
  let ImageDataClass;
21
22
  let loadImageFunction;
22
- const IS_BROWSER_OR_WEBWORKER = apis.IS_BROWSER_ENV || apis.IS_WEBWORKER_ENV;
23
- if (IS_BROWSER_OR_WEBWORKER) {
23
+ if (apis.IS_WEB_ENV) {
24
24
  // Running in browser or web-worker
25
25
  createCanvasFunction = (/** @type {number} */ width, /** @type {number} */ height) => {
26
26
  if (!self.OffscreenCanvas) {
27
- throw new Error('OffscreenCanvas not supported by this browser.');
27
+ throw new Error('OffscreenCanvas not supported by this environment.');
28
28
  }
29
29
  return new self.OffscreenCanvas(width, height);
30
30
  };
@@ -133,7 +133,7 @@ export class RawImage {
133
133
  * @returns {RawImage} The image object.
134
134
  */
135
135
  static fromCanvas(canvas) {
136
- if (!IS_BROWSER_OR_WEBWORKER) {
136
+ if (!apis.IS_WEB_ENV) {
137
137
  throw new Error('fromCanvas() is only supported in browser environments.');
138
138
  }
139
139
 
@@ -164,7 +164,7 @@ export class RawImage {
164
164
  * @returns {Promise<RawImage>} The image object.
165
165
  */
166
166
  static async fromBlob(blob) {
167
- if (IS_BROWSER_OR_WEBWORKER) {
167
+ if (apis.IS_WEB_ENV) {
168
168
  // Running in environment with canvas
169
169
  const img = await loadImageFunction(blob);
170
170
 
@@ -378,7 +378,7 @@ export class RawImage {
378
378
  height = (width / this.width) * this.height;
379
379
  }
380
380
 
381
- if (IS_BROWSER_OR_WEBWORKER) {
381
+ if (apis.IS_WEB_ENV) {
382
382
  // TODO use `resample` in browser environment
383
383
 
384
384
  // Store number of channels before resizing
@@ -406,7 +406,7 @@ export class RawImage {
406
406
  case 'box':
407
407
  case 'hamming':
408
408
  if (resampleMethod === 'box' || resampleMethod === 'hamming') {
409
- console.warn(
409
+ logger.warn(
410
410
  `Resampling method ${resampleMethod} is not yet supported. Using bilinear instead.`,
411
411
  );
412
412
  resampleMethod = 'bilinear';
@@ -452,7 +452,7 @@ export class RawImage {
452
452
  return this;
453
453
  }
454
454
 
455
- if (IS_BROWSER_OR_WEBWORKER) {
455
+ if (apis.IS_WEB_ENV) {
456
456
  // Store number of channels before padding
457
457
  const numChannels = this.channels;
458
458
 
@@ -494,7 +494,7 @@ export class RawImage {
494
494
  const crop_width = x_max - x_min + 1;
495
495
  const crop_height = y_max - y_min + 1;
496
496
 
497
- if (IS_BROWSER_OR_WEBWORKER) {
497
+ if (apis.IS_WEB_ENV) {
498
498
  // Store number of channels before resizing
499
499
  const numChannels = this.channels;
500
500
 
@@ -541,7 +541,7 @@ export class RawImage {
541
541
  const width_offset = (this.width - crop_width) / 2;
542
542
  const height_offset = (this.height - crop_height) / 2;
543
543
 
544
- if (IS_BROWSER_OR_WEBWORKER) {
544
+ if (apis.IS_WEB_ENV) {
545
545
  // Store number of channels before resizing
546
546
  const numChannels = this.channels;
547
547
 
@@ -649,7 +649,7 @@ export class RawImage {
649
649
  }
650
650
 
651
651
  async toBlob(type = 'image/png', quality = 1) {
652
- if (!IS_BROWSER_OR_WEBWORKER) {
652
+ if (!apis.IS_WEB_ENV) {
653
653
  throw new Error('toBlob() is only supported in browser environments.');
654
654
  }
655
655
 
@@ -672,7 +672,7 @@ export class RawImage {
672
672
  }
673
673
 
674
674
  toCanvas() {
675
- if (!IS_BROWSER_OR_WEBWORKER) {
675
+ if (!apis.IS_WEB_ENV) {
676
676
  throw new Error('toCanvas() is only supported in browser environments.');
677
677
  }
678
678
 
@@ -773,7 +773,7 @@ export class RawImage {
773
773
  * @returns {Promise<void>}
774
774
  */
775
775
  async save(path) {
776
- if (IS_BROWSER_OR_WEBWORKER) {
776
+ if (apis.IS_WEB_ENV) {
777
777
  if (apis.IS_WEBWORKER_ENV) {
778
778
  throw new Error('Unable to save an image from a Web Worker.');
779
779
  }
@@ -798,7 +798,7 @@ export class RawImage {
798
798
  * @returns {import('sharp').Sharp} The Sharp instance.
799
799
  */
800
800
  toSharp() {
801
- if (IS_BROWSER_OR_WEBWORKER) {
801
+ if (apis.IS_WEB_ENV) {
802
802
  throw new Error('toSharp() is only supported in server-side environments.');
803
803
  }
804
804