SillyTavern/src/transformers.mjs

148 lines
4.8 KiB
JavaScript
Raw Permalink Normal View History

2024-10-10 21:37:22 +02:00
import path from 'node:path';
import fs from 'node:fs';
2024-10-11 09:43:29 +02:00
import process from 'node:process';
import { Buffer } from 'node:buffer';
2024-10-10 21:37:22 +02:00
2024-10-11 20:33:36 +02:00
import { pipeline, env, RawImage } from 'sillytavern-transformers';
import { getConfigValue } from './util.js';
2023-09-14 22:12:33 +02:00
configureTransformers();
function configureTransformers() {
// Limit the number of threads to 1 to avoid issues on Android
2024-07-17 17:02:27 +02:00
env.backends.onnx.wasm.numThreads = 1;
2023-09-14 22:12:33 +02:00
// Use WASM from a local folder to avoid CDN connections
env.backends.onnx.wasm.wasmPaths = path.join(process.cwd(), 'dist') + path.sep;
}
const tasks = {
'text-classification': {
defaultModel: 'Cohee/distilbert-base-uncased-go-emotions-onnx',
pipeline: null,
configField: 'extras.classificationModel',
2024-02-02 00:51:02 +01:00
quantized: true,
2023-09-14 22:12:33 +02:00
},
'image-to-text': {
defaultModel: 'Xenova/vit-gpt2-image-captioning',
pipeline: null,
configField: 'extras.captioningModel',
2024-02-02 00:51:02 +01:00
quantized: true,
2023-09-14 22:12:33 +02:00
},
'feature-extraction': {
defaultModel: 'Xenova/all-mpnet-base-v2',
pipeline: null,
configField: 'extras.embeddingModel',
2024-02-02 00:51:02 +01:00
quantized: true,
2023-09-14 22:12:33 +02:00
},
'automatic-speech-recognition': {
defaultModel: 'Xenova/whisper-small',
pipeline: null,
configField: 'extras.speechToTextModel',
2024-02-02 00:51:02 +01:00
quantized: true,
},
'text-to-speech': {
defaultModel: 'Xenova/speecht5_tts',
pipeline: null,
configField: 'extras.textToSpeechModel',
quantized: false,
},
};
2023-09-14 22:12:33 +02:00
2023-10-20 14:03:26 +02:00
/**
* Gets a RawImage object from a base64-encoded image.
* @param {string} image Base64-encoded image
* @returns {Promise<RawImage|null>} Object representing the image
*/
2024-10-10 21:37:22 +02:00
export async function getRawImage(image) {
2023-09-22 22:04:26 +02:00
try {
const buffer = Buffer.from(image, 'base64');
const byteArray = new Uint8Array(buffer);
const blob = new Blob([byteArray]);
2023-09-14 22:12:33 +02:00
2023-09-22 22:04:26 +02:00
const rawImage = await RawImage.fromBlob(blob);
return rawImage;
} catch {
return null;
}
2023-09-14 22:12:33 +02:00
}
2023-10-20 14:03:26 +02:00
/**
* Gets the model to use for a given transformers.js task.
* @param {string} task The task to get the model for
* @returns {string} The model to use for the given task
*/
2023-09-14 22:12:33 +02:00
function getModelForTask(task) {
const defaultModel = tasks[task].defaultModel;
try {
const model = getConfigValue(tasks[task].configField, null);
2023-09-14 22:12:33 +02:00
return model || defaultModel;
} catch (error) {
2023-11-25 22:45:33 +01:00
console.warn('Failed to read config.yaml, using default classification model.');
2023-09-14 22:12:33 +02:00
return defaultModel;
}
}
async function migrateCacheToDataDir() {
const oldCacheDir = path.join(process.cwd(), 'cache');
const newCacheDir = path.join(global.DATA_ROOT, '_cache');
if (!fs.existsSync(newCacheDir)) {
fs.mkdirSync(newCacheDir, { recursive: true });
}
if (fs.existsSync(oldCacheDir) && fs.statSync(oldCacheDir).isDirectory()) {
const files = fs.readdirSync(oldCacheDir);
if (files.length === 0) {
return;
}
console.log('Migrating model cache files to data directory. Please wait...');
for (const file of files) {
try {
const oldPath = path.join(oldCacheDir, file);
const newPath = path.join(newCacheDir, file);
fs.cpSync(oldPath, newPath, { recursive: true, force: true });
fs.rmSync(oldPath, { recursive: true, force: true });
} catch (error) {
console.warn('Failed to migrate cache file. The model will be re-downloaded.', error);
}
}
}
}
2023-10-20 14:03:26 +02:00
/**
* Gets the transformers.js pipeline for a given task.
* @param {import('sillytavern-transformers').PipelineType} task The task to get the pipeline for
* @param {string} forceModel The model to use for the pipeline, if any
2024-10-11 20:33:36 +02:00
* @returns {Promise<import('sillytavern-transformers').Pipeline>} The transformers.js pipeline
2023-10-20 14:03:26 +02:00
*/
2024-10-10 21:37:22 +02:00
export async function getPipeline(task, forceModel = '') {
await migrateCacheToDataDir();
2023-09-14 22:12:33 +02:00
if (tasks[task].pipeline) {
2024-07-08 20:09:42 +02:00
if (forceModel === '' || tasks[task].currentModel === forceModel) {
return tasks[task].pipeline;
}
console.log('Disposing transformers.js pipeline for for task', task, 'with model', tasks[task].currentModel);
await tasks[task].pipeline.dispose();
2023-09-14 22:12:33 +02:00
}
const cacheDir = path.join(global.DATA_ROOT, '_cache');
const model = forceModel || getModelForTask(task);
const localOnly = getConfigValue('extras.disableAutoDownload', false);
2023-09-14 22:12:33 +02:00
console.log('Initializing transformers.js pipeline for task', task, 'with model', model);
const instance = await pipeline(task, model, { cache_dir: cacheDir, quantized: tasks[task].quantized ?? true, local_files_only: localOnly });
2023-09-14 22:12:33 +02:00
tasks[task].pipeline = instance;
2024-07-08 20:09:42 +02:00
tasks[task].currentModel = model;
2024-10-11 20:33:36 +02:00
// @ts-ignore
2023-09-14 22:12:33 +02:00
return instance;
}
export default {
getRawImage,
2024-10-10 21:37:22 +02:00
getPipeline,
};