bibform / web-src /build.mjs
sasakiy84's picture
書誌メモの抽出に対応したBibform v2.2を公開
e3a9858 verified
Raw History Blame Contribute Delete
4.68 kB
import { createReadStream, createWriteStream } from 'node:fs';
import { cp, mkdir, readFile, stat } from 'node:fs/promises';
import { resolve } from 'node:path';
import { pipeline } from 'node:stream/promises';
import { createGzip } from 'node:zlib';
import { build } from 'esbuild';
const [modelPath, destination = 'dist', standardPath, tokenizerPath] = process.argv.slice(2);
if (!modelPath) throw new Error('Pass the folder produced by s0472_export_lite_onnx.py');
const model = resolve(modelPath);
const output = resolve(destination);
const runtime = resolve(output, 'runtime');
const packedStandard = Boolean(standardPath && await stat(resolve(standardPath, 'encoder.onnx.gz')).catch(() => null));
const rawStandard = Boolean(standardPath && await stat(resolve(standardPath, 'encoder.onnx')).catch(() => null));
const standardV2 = packedStandard || rawStandard;
const liteConfig = JSON.parse(await readFile(resolve(model, 'model.json'), 'utf8'));
const workerV2 = liteConfig.version === 2;
const packageDir = resolve('node_modules', 'onnxruntime-web');
await mkdir(resolve(output, 'lite'), { recursive: true });
await mkdir(runtime, { recursive: true });
async function packModelFile(source, destination, filename) {
const packed = resolve(source, filename + '.gz');
const target = resolve(destination, filename + '.gz');
if (await stat(packed).catch(() => null)) {
await cp(packed, target);
} else {
await pipeline(createReadStream(resolve(source, filename)), createGzip({ level: 9 }), createWriteStream(target));
}
}
const liteFiles = ['byte-network.onnx', 'role-network.onnx'];
if (liteConfig.issuer_contributor_threshold !== undefined) liteFiles.push('issuer-contributor.onnx');
if (liteConfig.compact_name_threshold !== undefined) liteFiles.push('compact-name.onnx');
if (liteConfig.organization_endpoint_threshold !== undefined) liteFiles.push('organization-endpoints.onnx');
if (liteConfig.line_continuation_threshold !== undefined) liteFiles.push('line-continuation.onnx');
if (liteConfig.name_orientation_threshold !== undefined) liteFiles.push('name-orientation.onnx');
if (liteConfig.container_end_threshold !== undefined) liteFiles.push('container-end.onnx');
if (liteConfig.contributor_range_repair !== undefined) liteFiles.push('contributor-ranges.onnx');
for (const filename of liteFiles) {
await packModelFile(model, resolve(output, 'lite'), filename);
}
await cp(resolve(model, 'model.json'), resolve(output, 'lite', 'model.json'));
await cp(resolve(packageDir, 'dist/ort-wasm-simd-threaded.mjs'), resolve(runtime, 'ort-wasm-simd-threaded.mjs'));
await pipeline(createReadStream(resolve(packageDir, 'dist/ort-wasm-simd-threaded.wasm')), createGzip({ level: 9 }),
createWriteStream(resolve(runtime, 'ort-wasm-simd-threaded.wasm.gz')));
for (const filename of ['LICENSE', 'ThirdPartyNotices.txt']) {
const source = resolve(packageDir, filename);
if (await stat(source).catch(() => null)) await cp(source, resolve(runtime, filename));
}
for (const filename of ['index.html', 'style.css']) await cp(filename, resolve(output, filename));
await build({
entryPoints: workerV2 ? { app: 'src/app.ts', 'standard-worker': 'src/standard-v2-worker.ts' }
: standardPath ? ['src/app.ts', 'src/standard-worker.ts'] : ['src/app.ts'],
outdir: output,
define: { __BIBFORM_STANDARD__: JSON.stringify(Boolean(standardPath)),
__BIBFORM_STANDARD_V2__: JSON.stringify(standardV2), __BIBFORM_WORKER_V2__: JSON.stringify(workerV2) },
bundle: true,
format: 'esm',
target: 'es2022',
minify: true,
});
if (standardPath) {
const standard = resolve(output, 'standard');
if (rawStandard && !packedStandard) {
await mkdir(standard, { recursive: true });
for (const filename of ['encoder.onnx', 'features.onnx', 'role-network.onnx', 'tokenizer.json']) {
await packModelFile(standardPath, standard, filename);
}
const standardConfig = JSON.parse(await readFile(resolve(standardPath, 'model.json'), 'utf8'));
if (standardConfig.conditional_name_threshold !== undefined) {
await packModelFile(standardPath, standard, 'conditional-names.onnx');
}
if (standardConfig.private_byte_auxiliary === true) {
const auxiliary = resolve(standard, 'auxiliary');
await mkdir(auxiliary, { recursive: true });
await packModelFile(resolve(standardPath, 'auxiliary'), auxiliary, 'byte-network.onnx');
}
await cp(resolve(standardPath, 'model.json'), resolve(standard, 'model.json'));
await cp(tokenizerPath ?? resolve(standardPath, 'tokenizer'), resolve(standard, 'tokenizer'), { recursive: true });
} else {
await cp(resolve(standardPath), standard, { recursive: true });
}
}