mirror of
https://github.com/sharevb/it-tools.git
synced 2026-11-20 21:23:08 +03:00
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>
This commit is contained in:
1 parent
94c0bbfcbc
commit
d53e745854
12 files changed
+121
-19
No files matched your search
@@ -4,8 +4,9 @@ import { ref } from 'vue';
|
||||
import { FFmpeg } from '@ffmpeg/ffmpeg';
|
||||
|
||||
import ffmpegClassWorkerUrl from '@ffmpeg/ffmpeg/worker?worker&url';
|
||||
|
||||
import { toBlobURL } from '@ffmpeg/util';
|
||||
// Shipped with the app (single-threaded ESM build). The worker above is a module worker, so it `import()`s the core.
|
||||
import ffmpegCoreUrl from '@ffmpeg/core?url';
|
||||
import ffmpegWasmUrl from '@ffmpeg/core/wasm?url';
|
||||
|
||||
const { t } = useI18n();
|
||||
|
||||
@@ -22,11 +23,9 @@ const loop = ref(5);
|
||||
|
||||
async function loadFFmpeg() {
|
||||
if (!ffmpeg.loaded) {
|
||||
const baseURL = 'https://cdn.jsdelivr.net/npm/@ffmpeg/core@0.12.10/dist/esm';
|
||||
|
||||
await ffmpeg.load({
|
||||
coreURL: await toBlobURL(`${baseURL}/ffmpeg-core.js`, 'text/javascript'),
|
||||
wasmURL: await toBlobURL(`${baseURL}/ffmpeg-core.wasm`, 'application/wasm'),
|
||||
coreURL: ffmpegCoreUrl,
|
||||
wasmURL: ffmpegWasmUrl,
|
||||
classWorkerURL: ffmpegClassWorkerUrl,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -10,6 +10,4 @@ export const tool = defineTool({
|
||||
icon: defineAsyncComponent(() => import('@vicons/tabler/es/VideoPlus')),
|
||||
createdAt: new Date('2026-03-15'),
|
||||
category: 'Images',
|
||||
externalHTMLContent:
|
||||
'Download FFMPEG from https://cdn.jsdelivr.net/npm/@ffmpeg/core@0.12.10. All processing done in your browser.',
|
||||
});
|
||||
@@ -1,4 +1,5 @@
|
||||
import { defineTool } from '../tool';
|
||||
import { getToolsSettingString } from '@/utils/tools-settings';
|
||||
import { translate as t } from '@/plugins/i18n.plugin';
|
||||
|
||||
export const tool = defineTool({
|
||||
@@ -11,4 +12,5 @@ export const tool = defineTool({
|
||||
createdAt: new Date('2024-08-15'),
|
||||
category: 'Maths',
|
||||
externAccessDescription: t('tools.math-ocr.externalAccess'),
|
||||
requiresInternet: (settings) => !getToolsSettingString(settings, 'transformers', 'models-url'),
|
||||
});
|
||||
@@ -2,7 +2,7 @@
|
||||
import { useI18n } from 'vue-i18n';
|
||||
import type { Ref } from 'vue';
|
||||
import { pipeline } from '@huggingface/transformers';
|
||||
import { useScriptTag } from '@vueuse/core';
|
||||
import '@/utils/transformers-env';
|
||||
import TextareaCopyable from '@/components/TextareaCopyable.vue';
|
||||
|
||||
const { t } = useI18n();
|
||||
|
||||
-1
@@ -1 +0,0 @@
|
||||
declare module 'vue-mathjax-next' {}
|
||||
@@ -1,13 +1,20 @@
|
||||
<script setup lang="ts">
|
||||
import { useI18n } from 'vue-i18n';
|
||||
import type { Ref } from 'vue';
|
||||
import { createWorker } from 'tesseract.js';
|
||||
import { OEM, createWorker } from 'tesseract.js';
|
||||
import workerUrl from 'tesseract.js/dist/worker.min.js?url';
|
||||
import coreLstmUrl from 'tesseract.js-core/tesseract-core-lstm.wasm.js?url';
|
||||
import coreSimdLstmUrl from 'tesseract.js-core/tesseract-core-simd-lstm.wasm.js?url';
|
||||
import coreRelaxedSimdLstmUrl from 'tesseract.js-core/tesseract-core-relaxedsimd-lstm.wasm.js?url';
|
||||
import engDataUrl from '@tesseract.js-data/eng/4.0.0_best_int/eng.traineddata.gz?url';
|
||||
import chiSimDataUrl from '@tesseract.js-data/chi_sim/4.0.0_best_int/chi_sim.traineddata.gz?url';
|
||||
import { getDocument } from 'pdfjs-dist';
|
||||
import * as pdfJS from 'pdfjs-dist';
|
||||
import pdfJSWorkerURL from 'pdfjs-dist/build/pdf.worker?url';
|
||||
import { textStatistics } from '../text-statistics/text-statistics.service';
|
||||
import TextareaCopyable from '@/components/TextareaCopyable.vue';
|
||||
import { useQueryParamOrStorage } from '@/composable/queryParams';
|
||||
import { getToolsSettingString, isOfflineMode, toolsSettings } from '@/tools-settings';
|
||||
|
||||
const { t } = useI18n();
|
||||
|
||||
@@ -112,14 +119,56 @@ const languages = [
|
||||
{ name: 'Welsh', code: 'cym' },
|
||||
{ name: 'Yiddish', code: 'yid' },
|
||||
];
|
||||
const languagesOptions = Array.from(
|
||||
languages.map((l) => ({
|
||||
label: l.name,
|
||||
value: l.code,
|
||||
})),
|
||||
);
|
||||
|
||||
// Language data shipped with the app: the LSTM-only `4.0.0_best_int` variant tesseract.js downloads by default.
|
||||
// tesseract.js fetches `<langPath>/<lang>.traineddata.gz`, and the build keeps these file names (the content hash
|
||||
// is a directory instead, see vite.config.ts), so the containing directory is the langPath.
|
||||
const bundledLanguageData: Record<string, string> = { eng: engDataUrl, chi_sim: chiSimDataUrl };
|
||||
|
||||
// Other languages come from `{ "ocr-image": { "lang-url": "..." } }` in tools-settings.json when set (a directory
|
||||
// of `<lang>.traineddata.gz` files, or a URL with a `{lang}` placeholder), otherwise from tesseract.js' default CDN.
|
||||
const languageDataUrl = getToolsSettingString(toolsSettings, 'ocr-image', 'lang-url');
|
||||
|
||||
function getLangPath(lang: string) {
|
||||
const bundled = bundledLanguageData[lang];
|
||||
if (bundled) {
|
||||
return new URL('.', new URL(bundled, document.baseURI)).href;
|
||||
}
|
||||
if (languageDataUrl) {
|
||||
return new URL(languageDataUrl.replaceAll('{lang}', lang), document.baseURI).href;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
// Offline without a mirror, only the bundled languages can be loaded.
|
||||
const availableLanguages =
|
||||
isOfflineMode && !languageDataUrl ? languages.filter(({ code }) => code in bundledLanguageData) : languages;
|
||||
const languagesOptions = availableLanguages.map((l) => ({
|
||||
label: l.name,
|
||||
value: l.code,
|
||||
}));
|
||||
|
||||
const language = useQueryParamOrStorage({ name: 'lang', storageName: 'ocr-image:lang', defaultValue: 'eng' });
|
||||
if (!availableLanguages.some(({ code }) => code === language.value)) {
|
||||
language.value = 'eng';
|
||||
}
|
||||
|
||||
// Pick the tesseract core build the way tesseract.js does when given a directory (it can't be, as the build hashes
|
||||
// the file names). Same feature probes as wasm-feature-detect, which tesseract.js uses for this.
|
||||
function getCorePath() {
|
||||
const supports = (bytes: number[]) => WebAssembly.validate(new Uint8Array(bytes));
|
||||
const relaxedSimd = [
|
||||
0, 97, 115, 109, 1, 0, 0, 0, 1, 5, 1, 96, 0, 1, 123, 3, 2, 1, 0, 10, 15, 1, 13, 0, 65, 1, 253, 15, 65, 2, 253, 15,
|
||||
253, 128, 2, 11,
|
||||
];
|
||||
const simd = [
|
||||
0, 97, 115, 109, 1, 0, 0, 0, 1, 5, 1, 96, 0, 1, 123, 3, 2, 1, 0, 10, 10, 1, 8, 0, 65, 0, 253, 15, 253, 98, 11,
|
||||
];
|
||||
if (supports(relaxedSimd)) {
|
||||
return coreRelaxedSimdLstmUrl;
|
||||
}
|
||||
return supports(simd) ? coreSimdLstmUrl : coreLstmUrl;
|
||||
}
|
||||
|
||||
const pageSeparator = '\n=============\n';
|
||||
const ocrInProgress = ref(false);
|
||||
@@ -171,8 +220,11 @@ async function ocr(file: File, language: string) {
|
||||
return '';
|
||||
}
|
||||
ocrInProgress.value = true;
|
||||
const worker = await createWorker();
|
||||
await worker.reinitialize(language);
|
||||
const worker = await createWorker(language, OEM.LSTM_ONLY, {
|
||||
workerPath: workerUrl,
|
||||
corePath: getCorePath(),
|
||||
langPath: getLangPath(language),
|
||||
});
|
||||
const allTexts = [];
|
||||
if (file.type.match('^image/')) {
|
||||
const ret = await worker.recognize(file);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { translate as t } from '@/plugins/i18n.plugin';
|
||||
import { defineTool } from '../tool';
|
||||
import { getToolsSettingString } from '@/utils/tools-settings';
|
||||
|
||||
export const tool = defineTool({
|
||||
name: t('tools.remove-background.title'),
|
||||
@@ -11,4 +12,5 @@ export const tool = defineTool({
|
||||
createdAt: new Date('2026-03-15'),
|
||||
category: 'Images',
|
||||
externAccessDescription: t('tools.remove-background.externalAccess'),
|
||||
requiresInternet: (settings) => !getToolsSettingString(settings, 'transformers', 'models-url'),
|
||||
});
|
||||
@@ -3,6 +3,7 @@ import { useI18n } from 'vue-i18n';
|
||||
import { onMounted, ref } from 'vue';
|
||||
import type { BackgroundRemovalPipeline, ProgressInfo } from '@huggingface/transformers';
|
||||
import { RawImage, pipeline } from '@huggingface/transformers';
|
||||
import '@/utils/transformers-env';
|
||||
import { useQueryParamOrStorage } from '@/composable/queryParams';
|
||||
|
||||
const { t } = useI18n();
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { translate as t } from '@/plugins/i18n.plugin';
|
||||
import { defineTool } from '../tool';
|
||||
import { getToolsSettingString } from '@/utils/tools-settings';
|
||||
|
||||
export const tool = defineTool({
|
||||
name: t('tools.translator.title'),
|
||||
@@ -11,4 +12,5 @@ export const tool = defineTool({
|
||||
createdAt: new Date('2025-08-15'),
|
||||
category: 'Text',
|
||||
externAccessDescription: t('tools.translator.externalAccess'),
|
||||
requiresInternet: (settings) => !getToolsSettingString(settings, 'transformers', 'models-url'),
|
||||
});
|
||||
@@ -2,6 +2,7 @@
|
||||
import { useI18n } from 'vue-i18n';
|
||||
import type { TranslationPipeline, TranslationSingle } from '@huggingface/transformers';
|
||||
import { env, pipeline } from '@huggingface/transformers';
|
||||
import '@/utils/transformers-env';
|
||||
import { useQueryParamOrStorage } from '@/composable/queryParams';
|
||||
|
||||
const { t } = useI18n();
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
@@ -138,6 +138,17 @@ export default defineConfig({
|
||||
? ['**\/*.{js,wasm,css,html}']
|
||||
: ['**\/*.{css,html}'],
|
||||
maximumFileSizeToCacheInBytes: 25 * 1024 ** 2,
|
||||
// Runtimes and data that only one tool needs and that are far too big to download up front, even for a full
|
||||
// precache: ffmpeg core (~31 MB), the ONNX runtime used by the transformers.js tools (~21 MB), tesseract
|
||||
// cores (~3.7 MB each) and OCR language data. They are fetched when their tool runs and kept by the runtime
|
||||
// caches below once used (OCR language data by tesseract.js itself, in IndexedDB).
|
||||
globIgnores: [
|
||||
'**/node_modules/**/*',
|
||||
'**/ffmpeg-core-*',
|
||||
'**/ort-wasm-simd-threaded*',
|
||||
'**/tesseract-core-*',
|
||||
'**/*.traineddata*',
|
||||
],
|
||||
// Relative, like every other precache entry: workbox resolves them against the
|
||||
// service worker's own URL, so the same sw.js works under any deployment path.
|
||||
navigateFallback: 'index.html',
|
||||
@@ -256,6 +267,10 @@ export default defineConfig({
|
||||
'isolated-vm': fileURLToPath(new URL('./src/_empty.ts', import.meta.url)),
|
||||
'onnxruntime-node': fileURLToPath(new URL('./src/_empty.ts', import.meta.url)),
|
||||
'unpdf/pdfjs': fileURLToPath(new URL('./src/_empty.ts', import.meta.url)),
|
||||
// The ONNX runtime files shipped by src/utils/transformers-env.ts; the package's `exports` don't list them.
|
||||
'@huggingface/transformers/dist': fileURLToPath(
|
||||
new URL('./node_modules/@huggingface/transformers/dist', import.meta.url),
|
||||
),
|
||||
'webcrypto-liner-shim': !process.env.VERCEL
|
||||
? 'webcrypto-liner-shim'
|
||||
: fileURLToPath(new URL('./src/_empty.ts', import.meta.url)),
|
||||
@@ -291,6 +306,12 @@ export default defineConfig({
|
||||
external: ['regex', './out/isolated_vm', 'isolated-vm', 'onnxruntime-node', 'unpdf/pdfjs'],
|
||||
output: {
|
||||
format: 'es',
|
||||
// Tesseract language data is fetched as `<langPath>/<lang>.traineddata.gz`, so the bundled files keep their
|
||||
// name and get the content hash as a directory instead (see src/tools/ocr-image/ocr-image.vue).
|
||||
assetFileNames: ({ names }) =>
|
||||
names.some((name) => name.endsWith('.traineddata.gz'))
|
||||
? 'assets/[hash]/[name][extname]'
|
||||
: 'assets/[name]-[hash][extname]',
|
||||
codeSplitting: {
|
||||
// Tool icons are loaded through per-icon dynamic imports (see src/tools/*/index.ts);
|
||||
// merge them into a single lazy chunk instead of ~450 tiny ones.
|
||||
|
||||
Reference in new issue
Block a user