Files
it-tools/src/utils/transformers-env.ts
T
Sebastian DanielssonandClaude Opus 5.5 d53e745854 feat(translator, remove-background, math-ocr, ocr-image, gif-to-mp4): ship runtimes with the app
Ship the ONNX runtime, the ffmpeg core, the tesseract worker and core and
English/Simplified Chinese OCR data instead of loading them from
jsdelivr. They load only when their tool runs and are left out of the
PWA precache. Models and other OCR languages can come from an intranet
mirror (transformers.models-url, ocr-image.lang-url). Remove math-ocr's
leftover MathJax typings: MathJax is no longer used.

Refs sharevb/it-tools#571

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-25 18:51:17 +01:00

26 lines
1.7 KiB
TypeScript

/**
* Shared @huggingface/transformers setup for the tools that run models in the browser (translator,
* remove-background, math-ocr). Import it for its side effects before calling `pipeline()`.
*
* - The ONNX runtime (the WASM binary and its JS glue, used by both the WASM and the WebGPU backends) is shipped with
* the app instead of being fetched from cdn.jsdelivr.net, which is what transformers.js defaults to.
* - Models still come from the Hugging Face Hub, unless tools-settings.json points
* `{ "transformers": { "models-url": "..." } }` at a mirror. The mirror must keep the Hub's layout,
* `<models-url>/<org>/<model>/resolve/main/<file>` (e.g. `Xenova/opus-mt-en-fr/resolve/main/config.json`),
* so a plain copy of the huggingface.co repositories works. A relative value is resolved against the app's base URL.
*/
import { env } from '@huggingface/transformers';
// The same files transformers.js would load from the CDN (`dist/` of the exact package version). The aliased path
// (see vite.config.ts) gets around the package's `exports`, which don't list them.
import ortWasmModuleUrl from '@huggingface/transformers/dist/ort-wasm-simd-threaded.jsep.mjs?url';
import ortWasmBinaryUrl from '@huggingface/transformers/dist/ort-wasm-simd-threaded.jsep.wasm?url';
import { getToolsSettingString, toolsSettings } from '@/tools-settings';
// Per-file paths rather than a directory prefix: the build hashes both file names.
env.backends.onnx.wasm!.wasmPaths = { mjs: ortWasmModuleUrl, wasm: ortWasmBinaryUrl };
const modelsUrl = getToolsSettingString(toolsSettings, 'transformers', 'models-url');
if (modelsUrl) {
env.remoteHost = new URL(modelsUrl, document.baseURI).href;
}