Skip to content

Translate and OCR in the browser

This guide adds the Sakutto SDK to a browser app built with a tool such as Vite, then runs local PLaMo translation and optional PP-OCRv6 OCR.

  • Node.js 20.3 or later for dependency installation and builds
  • A modern browser. WebGPU is used when available, with CPU/WASM fallback
  • Network access for the first download of the translation model (about 546 MB) and standard OCR models (about 23 MB)
  • User review and acceptance of the PLaMo Community License

For translation only, install these three packages.

ターミナルウィンドウ
npm install @sakutto/core @sakutto/models @sakutto/runtime-wllama

For OCR as well, add @sakutto/ocr and its onnxruntime-web peer dependency.

ターミナルウィンドウ
npm install @sakutto/ocr onnxruntime-web

2. Serve workers, WASM, and dictionaries from your origin

Section titled “2. Serve workers, WASM, and dictionaries from your origin”

Do not load the wllama or ONNX Runtime files from a CDN. Serve them from the same origin as your app. The repository’s examples/web-vite/prepare-assets.mjs is a complete script that copies the required files into public/.

import { cp, mkdir } from 'node:fs/promises';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
const publicDir = join(import.meta.dirname, 'public');
await mkdir(join(publicDir, 'vendor'), { recursive: true });
await cp(
dirname(fileURLToPath(import.meta.resolve('@sakutto/runtime-wllama/assets/wllama.js'))),
join(publicDir, 'vendor', 'wllama'),
{ recursive: true },
);

For OCR, also copy pp-ocrv6-chars.txt and the ONNX Runtime WASM files as shown in the same example. The default OCR runtime path is /ocr/ort/; pass assetBase if you serve them somewhere else.

Threaded WASM requires COOP and COEP headers. In Vite, configure the development and preview servers like this:

import { defineConfig } from 'vite';
const headers = {
'Cross-Origin-Opener-Policy': 'same-origin',
'Cross-Origin-Embedder-Policy': 'require-corp',
};
export default defineConfig({
server: { headers },
preview: { headers },
optimizeDeps: { exclude: ['@sakutto/models'] },
});

The compatibility build can run without shared memory, but enable the headers when you can.

import { createTranslator } from '@sakutto/core';
import {
createModelManager,
DEFAULT_MODEL_ID,
} from '@sakutto/models';
import { openOpfsModelStore } from '@sakutto/models/browser';
import { createWllamaRuntime } from '@sakutto/runtime-wllama';
const store = await openOpfsModelStore('my-app');
const models = createModelManager({ store });
// In a real app, call this only after the user accepts the license.
await models.download(DEFAULT_MODEL_ID, {
acceptLicense: 'plamo-community',
onProgress: ({ received, total }) => {
console.log(`${received} / ${total} bytes`);
},
});
const runtime = await createWllamaRuntime(await models.open());
try {
const translator = createTranslator({ runtime });
const translated = await translator.translateText('こんにちは', {
src: 'ja',
tgt: 'en',
});
console.log(translated);
} finally {
await runtime.dispose();
}

Cold model loading is more expensive than translation requests. Reuse the runtime in your app and call dispose() when finished. The default backend: 'auto' tries WebGPU and falls back to CPU/WASM. Use backend: 'cpu-wasm' to select WASM explicitly.

OCR model files are downloaded separately from the translation model. OCR_TIER_CATALOG and AssetManager apply pinned revisions and SHA-256 verification.

import { AssetManager, OCR_TIER_CATALOG } from '@sakutto/models';
import { createWebOcr } from '@sakutto/ocr/web';
const ocrModels = new AssetManager({ store, bundles: OCR_TIER_CATALOG });
await ocrModels.download('standard', {
signal: new AbortController().signal,
});
const response = await fetch('/ocr/pp-ocrv6-chars.txt');
if (!response.ok) throw new Error('OCR dictionary was not found');
const ocr = await createWebOcr({
assets: ocrModels,
dictionary: await response.text(),
});
try {
const result = await ocr.recognize(imageData);
console.log(result.text);
} finally {
await ocr.release();
}

imageData is an ImageData object, usually read from a canvas. Images and OCR output stay local. WebGPU is used opportunistically, with a single-threaded WASM fallback.

The repository’s examples/web-vite demonstrates license acceptance, model and OCR downloads, translation, and image recognition end to end. Its npm run dev and npm run build scripts are the quickest way to inspect a working setup.