Use transformers WASM binaries from a local folder

This commit is contained in:
Cohee 2023-09-14 14:11:37 +03:00
parent c4c962aeb9
commit 179de92231
5 changed files with 62 additions and 1 deletions

1
package-lock.json generated
View File

@ -7,6 +7,7 @@
"": {
"name": "sillytavern",
"version": "1.10.3",
"hasInstallScript": true,
"license": "AGPL-3.0",
"dependencies": {
"@agnai/sentencepiece-js": "^1.1.1",

View File

@ -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"

55
post-install.js Normal file
View File

@ -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);
}

View File

@ -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 {
/**

View File

@ -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 {
/**