diff --git a/package-lock.json b/package-lock.json index a450a34cc..ec1093e3d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7,6 +7,7 @@ "": { "name": "sillytavern", "version": "1.10.3", + "hasInstallScript": true, "license": "AGPL-3.0", "dependencies": { "@agnai/sentencepiece-js": "^1.1.1", diff --git a/package.json b/package.json index bcdf7a8f0..929e6dc8f 100644 --- a/package.json +++ b/package.json @@ -57,7 +57,8 @@ "scripts": { "start": "node server.js", "start-multi": "node server.js --disableCsrf", - "pkg": "pkg --compress Gzip --no-bytecode --public ." + "pkg": "pkg --compress Gzip --no-bytecode --public .", + "postinstall": "node post-install.js" }, "bin": { "sillytavern": "./server.js" diff --git a/post-install.js b/post-install.js new file mode 100644 index 000000000..2797812e7 --- /dev/null +++ b/post-install.js @@ -0,0 +1,55 @@ +/** + * Scripts to be done before starting the server for the first time. + */ +const fs = require('fs'); +const crypto = require('crypto'); + +/** + * Returns the MD5 hash of the given data. + * @param {Buffer} data Input data + * @returns {string} MD5 hash of the input data + */ +function getMd5Hash(data) { + return crypto + .createHash('md5') + .update(data) + .digest('hex'); +} + +/** + * Copies the WASM binaries from the sillytavern-transformers package to the dist folder. + */ +function copyWasmFiles() { + if (!fs.existsSync('./dist')) { + fs.mkdirSync('./dist'); + } + + const listDir = fs.readdirSync('./node_modules/sillytavern-transformers/dist'); + + for (const file of listDir) { + if (file.endsWith('.wasm')) { + const sourcePath = `./node_modules/sillytavern-transformers/dist/${file}`; + const targetPath = `./dist/${file}`; + + // Don't copy if the file already exists and is the same checksum + if (fs.existsSync(targetPath)) { + const sourceChecksum = getMd5Hash(fs.readFileSync(sourcePath)); + const targetChecksum = getMd5Hash(fs.readFileSync(targetPath)); + + if (sourceChecksum === targetChecksum) { + continue; + } + } + + fs.copyFileSync(sourcePath, targetPath); + console.log(`${file} successfully copied to ./dist/${file}`); + } + } +} + +try { + // 1. Copy transformers WASM binaries from node_modules + copyWasmFiles(); +} catch (error) { + console.error(error); +} diff --git a/src/caption.mjs b/src/caption.mjs index 08bcd25b5..ab8241f42 100644 --- a/src/caption.mjs +++ b/src/caption.mjs @@ -4,6 +4,8 @@ import { getConfig } from './util.js'; // Limit the number of threads to 1 to avoid issues on Android env.backends.onnx.wasm.numThreads = 1; +// Use WASM from a local folder to avoid CDN connections +env.backends.onnx.wasm.wasmPaths = path.join(process.cwd(), 'dist') + path.sep; class PipelineAccessor { /** diff --git a/src/classify.mjs b/src/classify.mjs index df2baca75..b35965116 100644 --- a/src/classify.mjs +++ b/src/classify.mjs @@ -4,6 +4,8 @@ import { getConfig } from './util.js'; // Limit the number of threads to 1 to avoid issues on Android env.backends.onnx.wasm.numThreads = 1; +// Use WASM from a local folder to avoid CDN connections +env.backends.onnx.wasm.wasmPaths = path.join(process.cwd(), 'dist') + path.sep; class PipelineAccessor { /**