Add OCR, add configuration

This commit is contained in:
reis
2022-11-11 17:29:15 +00:00
parent 9962e4fe68
commit d70fcde227
7 changed files with 146 additions and 71 deletions

View File

@@ -1,31 +1,50 @@
import { readFileSync } from 'node:fs';
// Fix __dirname not being defined in ES modules. (https://stackoverflow.com/a/64383997)
import { fileURLToPath } from 'node:url';
import { dirname, join } from 'node:path';
const __filename = fileURLToPath(import.meta.url);
global.__dirname = dirname(__filename);
const configJSON = readFileSync('./config.json', 'utf-8');
const config = JSON.parse(configJSON);
global.config = config;
console.log(config);
import { createServer } from 'node:net';
import { deserialize } from 'bson';
import FastText from 'fasttext.js';
import { runAI, trainAI } from './events/index.js';
import { runAI, trainAI, runOCR } from './events/index.js';
const ft = new FastText({
loadModel: './model/model.bin'
});
const ft = new FastText(global.config.fasttext);
ft.load();
// I'm sorry. This is probably the only solution.
global.ft = ft;
const server = createServer(async (client) => {
client.on('data', async (data) => {
const eventData = deserialize(data);
switch(eventData.event) {
case 'ai': {
runAI(client, eventData, ft.predict);
switch(eventData.op) {
case 1: {
runAI(client, eventData);
break;
}
};
case 'train_ai': {
trainAI(ft.unload, ft.load);
case 4: {
trainAI();
break;
}
}
};
case 5: {
runOCR(client, eventData);
break;
};
};
});
});
server.listen(process.env.PORT || 3000);
server.listen(global.config.server.port || 3000);