mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
Use Map for caches instead of objects
This commit is contained in:
@@ -5,7 +5,10 @@ const TASK = 'text-classification';
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
const cacheObject = {};
|
||||
/**
|
||||
* @type {Map<string, object>} Cache for classification results
|
||||
*/
|
||||
const cacheObject = new Map();
|
||||
|
||||
router.post('/labels', jsonParser, async (req, res) => {
|
||||
try {
|
||||
@@ -23,15 +26,20 @@ router.post('/', jsonParser, async (req, res) => {
|
||||
try {
|
||||
const { text } = req.body;
|
||||
|
||||
/**
|
||||
* Get classification result for a given text
|
||||
* @param {string} text Text to classify
|
||||
* @returns {Promise<object>} Classification result
|
||||
*/
|
||||
async function getResult(text) {
|
||||
if (Object.hasOwn(cacheObject, text)) {
|
||||
return cacheObject[text];
|
||||
if (cacheObject.has(text)) {
|
||||
return cacheObject.get(text);
|
||||
} else {
|
||||
const module = await import('../transformers.mjs');
|
||||
const pipe = await module.default.getPipeline(TASK);
|
||||
const result = await pipe(text, { topk: 5 });
|
||||
result.sort((a, b) => b.score - a.score);
|
||||
cacheObject[text] = result;
|
||||
cacheObject.set(text, result);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user