mirror of
https://github.com/ReVanced/revanced-bots.git
synced 2026-01-11 13:56:15 +00:00
feat: prettier and eslint
This commit is contained in:
18
.eslintrc.cjs
Normal file
18
.eslintrc.cjs
Normal file
@@ -0,0 +1,18 @@
|
||||
module.exports = {
|
||||
env: {
|
||||
es2021: true,
|
||||
node: true
|
||||
},
|
||||
extends: 'eslint:recommended',
|
||||
overrides: [],
|
||||
parserOptions: {
|
||||
ecmaVersion: 'latest',
|
||||
sourceType: 'module'
|
||||
},
|
||||
rules: {
|
||||
indent: ['error', 'tab'],
|
||||
'linebreak-style': ['error', 'windows'],
|
||||
quotes: ['error', 'single'],
|
||||
semi: ['error', 'always']
|
||||
}
|
||||
};
|
||||
18
.eslintrc.js
Normal file
18
.eslintrc.js
Normal file
@@ -0,0 +1,18 @@
|
||||
module.exports = {
|
||||
env: {
|
||||
es2021: true,
|
||||
node: true
|
||||
},
|
||||
extends: 'eslint:recommended',
|
||||
overrides: [],
|
||||
parserOptions: {
|
||||
ecmaVersion: 'latest',
|
||||
sourceType: 'module'
|
||||
},
|
||||
rules: {
|
||||
indent: ['error', 'tab'],
|
||||
'linebreak-style': ['error', 'windows'],
|
||||
quotes: ['error', 'single'],
|
||||
semi: ['error', 'always']
|
||||
}
|
||||
};
|
||||
4
.prettierrc.json
Normal file
4
.prettierrc.json
Normal file
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"singleQuote": true,
|
||||
"trailingComma": "none"
|
||||
}
|
||||
@@ -1,19 +1,19 @@
|
||||
{
|
||||
"discord": {
|
||||
"token": "YOUR-BOT-TOKEN-HERE",
|
||||
"id": "1038762591805247518"
|
||||
},
|
||||
|
||||
"server": {
|
||||
"port": 3000,
|
||||
"host": "192.168.1.6"
|
||||
},
|
||||
"discord": {
|
||||
"token": "YOUR-BOT-TOKEN-HERE",
|
||||
"id": "1038762591805247518"
|
||||
},
|
||||
|
||||
"responses": [
|
||||
{
|
||||
"label": "DOWNLOAD",
|
||||
"threshold": 0.85,
|
||||
"text": "you wanted peevanced"
|
||||
}
|
||||
]
|
||||
}
|
||||
"server": {
|
||||
"port": 3000,
|
||||
"host": "192.168.1.6"
|
||||
},
|
||||
|
||||
"responses": [
|
||||
{
|
||||
"label": "DOWNLOAD",
|
||||
"threshold": 0.85,
|
||||
"text": "you wanted peevanced"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
@@ -1,35 +1,49 @@
|
||||
import { ContextMenuCommandBuilder, ApplicationCommandType, ActionRowBuilder, SelectMenuBuilder, ComponentType } from 'discord.js';
|
||||
import {
|
||||
ContextMenuCommandBuilder,
|
||||
ApplicationCommandType,
|
||||
ActionRowBuilder,
|
||||
SelectMenuBuilder,
|
||||
ComponentType
|
||||
} from 'discord.js';
|
||||
export default {
|
||||
data: new ContextMenuCommandBuilder()
|
||||
.setName('Train Message')
|
||||
.setType(ApplicationCommandType.Message),
|
||||
async execute(interaction) {
|
||||
const options = [];
|
||||
data: new ContextMenuCommandBuilder()
|
||||
.setName('Train Message')
|
||||
.setType(ApplicationCommandType.Message),
|
||||
async execute(interaction) {
|
||||
const options = [];
|
||||
|
||||
for (const { label } of global.config.responses) {
|
||||
options.push({
|
||||
label: label,
|
||||
description: `The ${label} label.`,
|
||||
value: label.toLowerCase()
|
||||
});
|
||||
};
|
||||
for (const { label } of global.config.responses) {
|
||||
options.push({
|
||||
label: label,
|
||||
description: `The ${label} label.`,
|
||||
value: label.toLowerCase()
|
||||
});
|
||||
}
|
||||
|
||||
const row = new ActionRowBuilder()
|
||||
.addComponents(
|
||||
new SelectMenuBuilder()
|
||||
.setCustomId('select')
|
||||
.setPlaceholder('Nothing selected')
|
||||
.addOptions(options),
|
||||
);
|
||||
const reply = await interaction.reply({ content: 'Please select the corresponding label to train the bot.', components: [row], ephemeral: true });
|
||||
const row = new ActionRowBuilder().addComponents(
|
||||
new SelectMenuBuilder()
|
||||
.setCustomId('select')
|
||||
.setPlaceholder('Nothing selected')
|
||||
.addOptions(options)
|
||||
);
|
||||
const reply = await interaction.reply({
|
||||
content: 'Please select the corresponding label to train the bot.',
|
||||
components: [row],
|
||||
ephemeral: true
|
||||
});
|
||||
|
||||
const collector = reply.createMessageComponentCollector({ componentType: ComponentType.StringSelect, time: 15000 });
|
||||
const collector = reply.createMessageComponentCollector({
|
||||
componentType: ComponentType.StringSelect,
|
||||
time: 15000
|
||||
});
|
||||
|
||||
collector.on('collect', i => {
|
||||
i.client.helper.sendTrainData(interaction.targetMessage.content, i.values[0].toUpperCase());
|
||||
collector.on('collect', (i) => {
|
||||
i.client.helper.sendTrainData(
|
||||
interaction.targetMessage.content,
|
||||
i.values[0].toUpperCase()
|
||||
);
|
||||
|
||||
i.reply({ content: 'Sent train data to server.', ephemeral: true });
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
i.reply({ content: 'Sent train data to server.', ephemeral: true });
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,23 +1,28 @@
|
||||
import { Events } from 'discord.js';
|
||||
|
||||
export default {
|
||||
name: Events.InteractionCreate,
|
||||
once: false,
|
||||
async execute(interaction) {
|
||||
if (!interaction.isMessageContextMenuCommand()) return;
|
||||
name: Events.InteractionCreate,
|
||||
once: false,
|
||||
async execute(interaction) {
|
||||
if (!interaction.isMessageContextMenuCommand()) return;
|
||||
|
||||
const command = interaction.client.commands.get(interaction.commandName);
|
||||
const command = interaction.client.commands.get(interaction.commandName);
|
||||
|
||||
if (!command) {
|
||||
console.error(`No command matching ${interaction.commandName} was found.`);
|
||||
return;
|
||||
}
|
||||
if (!command) {
|
||||
console.error(
|
||||
`No command matching ${interaction.commandName} was found.`
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await command.execute(interaction);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
|
||||
}
|
||||
}
|
||||
}
|
||||
try {
|
||||
await command.execute(interaction);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
await interaction.reply({
|
||||
content: 'There was an error while executing this command!',
|
||||
ephemeral: true
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import { Events } from 'discord.js';
|
||||
|
||||
export default {
|
||||
name: Events.MessageCreate,
|
||||
once: false,
|
||||
execute(msg) {
|
||||
msg.client.helper.scanText(msg.content, `${msg.channelId}/${msg.id}`);
|
||||
}
|
||||
}
|
||||
name: Events.MessageCreate,
|
||||
once: false,
|
||||
execute(msg) {
|
||||
msg.client.helper.scanText(msg.content, `${msg.channelId}/${msg.id}`);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,30 +1,31 @@
|
||||
export default {
|
||||
name: 'aiResponse',
|
||||
once: false,
|
||||
async execute(aiRes) {
|
||||
console.log(aiRes);
|
||||
const response = config.responses.find(res => res.label === aiRes.predictions[0].label);
|
||||
if (!response) return;
|
||||
name: 'aiResponse',
|
||||
once: false,
|
||||
async execute(aiRes) {
|
||||
const response = global.config.responses.find(
|
||||
(res) => res.label === aiRes.predictions[0].label
|
||||
);
|
||||
if (!response) return;
|
||||
|
||||
if (Number(aiRes.predictions[0].score) >= response.threshold) {
|
||||
const ids = aiRes.id.split('/');
|
||||
let channel = client.channels.cache.get(ids[0]);
|
||||
if (Number(aiRes.predictions[0].score) >= response.threshold) {
|
||||
const ids = aiRes.id.split('/');
|
||||
let channel = global.client.channels.cache.get(ids[0]);
|
||||
|
||||
if (!channel) {
|
||||
await client.channels.fetch(ids[0]);
|
||||
channel = client.channels.cache.get(ids[0]);
|
||||
}
|
||||
if (!channel) {
|
||||
await global.client.channels.fetch(ids[0]);
|
||||
channel = global.client.channels.cache.get(ids[0]);
|
||||
}
|
||||
|
||||
let message = channel.messages.cache.get(ids[1]);
|
||||
let message = channel.messages.cache.get(ids[1]);
|
||||
|
||||
if (!message) {
|
||||
await channel.messages.fetch(ids[1]);
|
||||
message = channel.messages.cache.get(ids[1]);
|
||||
}
|
||||
if (!message) {
|
||||
await channel.messages.fetch(ids[1]);
|
||||
message = channel.messages.cache.get(ids[1]);
|
||||
}
|
||||
|
||||
message.reply(response.text);
|
||||
message.reply(response.text);
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
export default {
|
||||
name: 'ocrResponse',
|
||||
once: false,
|
||||
execute(client, ocrRes) {
|
||||
// TODO
|
||||
}
|
||||
}
|
||||
name: 'ocrResponse',
|
||||
once: false,
|
||||
execute() {
|
||||
// TODO
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Client, Events, GatewayIntentBits, Collection } from 'discord.js';
|
||||
import { Client, GatewayIntentBits, Collection } from 'discord.js';
|
||||
import { readFileSync, readdirSync } from 'node:fs';
|
||||
// Fix __dirname not being defined in ES modules. (https://stackoverflow.com/a/64383997)
|
||||
import { fileURLToPath } from 'node:url';
|
||||
@@ -11,45 +11,59 @@ import HelperClient from '../../client/index.js';
|
||||
const configJSON = readFileSync('../config.json', 'utf-8');
|
||||
global.config = JSON.parse(configJSON);
|
||||
|
||||
const helper = new HelperClient(config);
|
||||
const helper = new HelperClient(global.config);
|
||||
|
||||
helper.connect();
|
||||
|
||||
global.client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent] });
|
||||
global.client = new Client({
|
||||
intents: [
|
||||
GatewayIntentBits.Guilds,
|
||||
GatewayIntentBits.GuildMessages,
|
||||
GatewayIntentBits.MessageContent
|
||||
]
|
||||
});
|
||||
|
||||
client.commands = new Collection();
|
||||
client.helper = helper;
|
||||
global.client.commands = new Collection();
|
||||
global.client.helper = helper;
|
||||
|
||||
const commandsPath = join(__dirname, 'commands');
|
||||
const commandFiles = readdirSync(commandsPath).filter(file => file.endsWith('.js'));
|
||||
const commandFiles = readdirSync(commandsPath).filter((file) =>
|
||||
file.endsWith('.js')
|
||||
);
|
||||
|
||||
for (const file of commandFiles) {
|
||||
const filePath = join(commandsPath, file);
|
||||
const command = (await import(`file://${filePath}`)).default;
|
||||
if ('data' in command && 'execute' in command) {
|
||||
client.commands.set(command.data.name, command);
|
||||
global.client.commands.set(command.data.name, command);
|
||||
} else {
|
||||
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
|
||||
console.log(
|
||||
`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const discordEventsPath = join(__dirname, 'events/discord');
|
||||
const discordEventFiles = readdirSync(discordEventsPath).filter(file => file.endsWith('.js'));
|
||||
const discordEventFiles = readdirSync(discordEventsPath).filter((file) =>
|
||||
file.endsWith('.js')
|
||||
);
|
||||
|
||||
for (const file of discordEventFiles) {
|
||||
const filePath = join(discordEventsPath, file);
|
||||
const event = (await import(`file://${filePath}`)).default;
|
||||
if (event.once) {
|
||||
client.once(event.name, (...args) => event.execute(...args));
|
||||
global.client.once(event.name, (...args) => event.execute(...args));
|
||||
} else {
|
||||
client.on(event.name, (...args) => event.execute(...args));
|
||||
global.client.on(event.name, (...args) => event.execute(...args));
|
||||
}
|
||||
}
|
||||
|
||||
// The ReVanced Helper events.
|
||||
|
||||
const helperEventsPath = join(__dirname, 'events/helper');
|
||||
const helperEventFiles = readdirSync(helperEventsPath).filter(file => file.endsWith('.js'));
|
||||
const helperEventFiles = readdirSync(helperEventsPath).filter((file) =>
|
||||
file.endsWith('.js')
|
||||
);
|
||||
|
||||
for (const file of helperEventFiles) {
|
||||
const filePath = join(helperEventsPath, file);
|
||||
@@ -61,4 +75,4 @@ for (const file of helperEventFiles) {
|
||||
}
|
||||
}
|
||||
|
||||
client.login(config.discord.token);
|
||||
global.client.login(global.config.discord.token);
|
||||
|
||||
@@ -5,7 +5,9 @@ const config = JSON.parse(configJSON);
|
||||
|
||||
const commands = [];
|
||||
// Grab all the command files from the commands directory you created earlier
|
||||
const commandFiles = readdirSync('./commands').filter(file => file.endsWith('.js'));
|
||||
const commandFiles = readdirSync('./commands').filter((file) =>
|
||||
file.endsWith('.js')
|
||||
);
|
||||
|
||||
// Grab the SlashCommandBuilder#toJSON() output of each command's data for deployment
|
||||
for (const file of commandFiles) {
|
||||
@@ -17,16 +19,17 @@ for (const file of commandFiles) {
|
||||
const rest = new REST({ version: '10' }).setToken(config.discord.token);
|
||||
|
||||
try {
|
||||
console.log(`Started refreshing ${commands.length} application (/) commands.`);
|
||||
console.log(
|
||||
`Started refreshing ${commands.length} application (/) commands.`
|
||||
);
|
||||
|
||||
// The put method is used to fully refresh all commands in the guild with the current set
|
||||
const data = await rest.put(
|
||||
Routes.applicationCommands(config.discord.id),
|
||||
{ body: commands },
|
||||
);
|
||||
// The put method is used to fully refresh all commands in the guild with the current set
|
||||
const data = await rest.put(Routes.applicationCommands(config.discord.id), {
|
||||
body: commands
|
||||
});
|
||||
|
||||
console.log(`Successfully reloaded ${data.length} application (/) commands.`);
|
||||
console.log(`Successfully reloaded ${data.length} application (/) commands.`);
|
||||
} catch (error) {
|
||||
// And of course, make sure you catch and log any errors!
|
||||
console.error(error);
|
||||
}
|
||||
// And of course, make sure you catch and log any errors!
|
||||
console.error(error);
|
||||
}
|
||||
|
||||
122
client/index.js
122
client/index.js
@@ -3,76 +3,84 @@ import { serialize, deserialize } from 'bson';
|
||||
import EventEmitter from 'node:events';
|
||||
|
||||
class HelperClient extends EventEmitter {
|
||||
constructor({ server }) {
|
||||
super();
|
||||
if (!server?.port) throw new Error('You did not specify the server port.');
|
||||
this.server = server;
|
||||
};
|
||||
constructor({ server }) {
|
||||
super();
|
||||
if (!server?.port) throw new Error('You did not specify the server port.');
|
||||
this.server = server;
|
||||
}
|
||||
|
||||
connect() {
|
||||
this.client = createConnection(this.server.port, this.server.host ? this.server.host : 'localhost', () => {
|
||||
this.emit('connect');
|
||||
});
|
||||
connect() {
|
||||
this.client = createConnection(
|
||||
this.server.port,
|
||||
this.server.host ? this.server.host : 'localhost',
|
||||
() => {
|
||||
this.emit('connect');
|
||||
}
|
||||
);
|
||||
|
||||
this.client.on('data', (data) => {
|
||||
const eventData = deserialize(data, { allowObjectSmallerThanBufferSize: true });
|
||||
this.client.on('data', (data) => {
|
||||
const eventData = deserialize(data, {
|
||||
allowObjectSmallerThanBufferSize: true
|
||||
});
|
||||
|
||||
switch (eventData.op) {
|
||||
case 2: {
|
||||
// The 'aiResponse' event.
|
||||
switch (eventData.op) {
|
||||
case 2: {
|
||||
// The 'aiResponse' event.
|
||||
|
||||
this.emit('aiResponse', eventData);
|
||||
};
|
||||
this.emit('aiResponse', eventData);
|
||||
break;
|
||||
}
|
||||
|
||||
case 6: {
|
||||
// The 'ocrResponse' event.
|
||||
case 6: {
|
||||
// The 'ocrResponse' event.
|
||||
|
||||
this.emit('ocrResponse', eventData);
|
||||
};
|
||||
};
|
||||
});
|
||||
};
|
||||
this.emit('ocrResponse', eventData);
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
sendData(data) {
|
||||
this.client.write(serialize(data));
|
||||
return;
|
||||
};
|
||||
sendData(data) {
|
||||
this.client.write(serialize(data));
|
||||
return;
|
||||
}
|
||||
|
||||
scanText(text, id) {
|
||||
this.sendData({
|
||||
op: 1,
|
||||
id,
|
||||
text
|
||||
});
|
||||
scanText(text, id) {
|
||||
this.sendData({
|
||||
op: 1,
|
||||
id,
|
||||
text
|
||||
});
|
||||
|
||||
return;
|
||||
};
|
||||
return;
|
||||
}
|
||||
|
||||
scanImage(url, id) {
|
||||
this.sendData({
|
||||
op: 5,
|
||||
id,
|
||||
url
|
||||
});
|
||||
scanImage(url, id) {
|
||||
this.sendData({
|
||||
op: 5,
|
||||
id,
|
||||
url
|
||||
});
|
||||
|
||||
return;
|
||||
};
|
||||
return;
|
||||
}
|
||||
|
||||
sendTrainData(text, label) {
|
||||
this.sendData({
|
||||
op: 3,
|
||||
label,
|
||||
text
|
||||
});
|
||||
sendTrainData(text, label) {
|
||||
this.sendData({
|
||||
op: 3,
|
||||
label,
|
||||
text
|
||||
});
|
||||
|
||||
return;
|
||||
};
|
||||
return;
|
||||
}
|
||||
|
||||
trainAI() {
|
||||
this.sendData({ op: 4 });
|
||||
trainAI() {
|
||||
this.sendData({ op: 4 });
|
||||
|
||||
return;
|
||||
};
|
||||
};
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
export default HelperClient;
|
||||
export default HelperClient;
|
||||
|
||||
1939
package-lock.json
generated
Normal file
1939
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
@@ -11,5 +11,12 @@
|
||||
"bugs": {
|
||||
"url": "https://github.com/reisxd/revanced-helper/issues"
|
||||
},
|
||||
"homepage": "https://github.com/reisxd/revanced-helper#readme"
|
||||
"homepage": "https://github.com/reisxd/revanced-helper#readme",
|
||||
"scripts": {
|
||||
"lint": "prettier --write . && eslint --fix ."
|
||||
},
|
||||
"devDependencies": {
|
||||
"eslint": "^8.27.0",
|
||||
"prettier": "2.7.1"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,9 +8,9 @@ Sending the server this JSON (BSON) will send you back the AI predictions.
|
||||
|
||||
```json
|
||||
{
|
||||
"op": 1,
|
||||
"id": "String",
|
||||
"text": "How do i download ReVanced?"
|
||||
"op": 1,
|
||||
"id": "String",
|
||||
"text": "How do i download ReVanced?"
|
||||
}
|
||||
```
|
||||
|
||||
@@ -18,14 +18,14 @@ And the server would return something like this:
|
||||
|
||||
```json
|
||||
{
|
||||
"op": 2,
|
||||
"id": "String",
|
||||
"predictions": [
|
||||
{
|
||||
"label": "DOWNLOAD",
|
||||
"score": "1"
|
||||
}
|
||||
]
|
||||
"op": 2,
|
||||
"id": "String",
|
||||
"predictions": [
|
||||
{
|
||||
"label": "DOWNLOAD",
|
||||
"score": "1"
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
@@ -35,9 +35,9 @@ To add data to the train data, send a BSON (JSON) like this:
|
||||
|
||||
```json
|
||||
{
|
||||
"op": 3,
|
||||
"label": "FALSEPOSITIVE",
|
||||
"text": "how"
|
||||
"op": 3,
|
||||
"label": "FALSEPOSITIVE",
|
||||
"text": "how"
|
||||
}
|
||||
```
|
||||
|
||||
@@ -45,7 +45,7 @@ To train the AI and to re-load it, send this BSON (JSON):
|
||||
|
||||
```json
|
||||
{
|
||||
"event": 4
|
||||
"event": 4
|
||||
}
|
||||
```
|
||||
|
||||
@@ -55,9 +55,9 @@ Sending the server this JSON (BSON) will send you back the read text.
|
||||
|
||||
```json
|
||||
{
|
||||
"op": 5,
|
||||
"id": "String",
|
||||
"url": "https://cdn.discordapp.com/attachments/1033338556493606963/1033338557231796224/Screenshot_20221022-121318.jpg"
|
||||
"op": 5,
|
||||
"id": "String",
|
||||
"url": "https://cdn.discordapp.com/attachments/1033338556493606963/1033338557231796224/Screenshot_20221022-121318.jpg"
|
||||
}
|
||||
```
|
||||
|
||||
@@ -65,8 +65,8 @@ And the server would return something like this:
|
||||
|
||||
```json
|
||||
{
|
||||
"op": 6,
|
||||
"id": "String",
|
||||
"ocrText": "..."
|
||||
"op": 6,
|
||||
"id": "String",
|
||||
"ocrText": "..."
|
||||
}
|
||||
```
|
||||
```
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
{
|
||||
"server": {
|
||||
"port": 3000
|
||||
},
|
||||
"server": {
|
||||
"port": 3000
|
||||
},
|
||||
|
||||
"fasttext": {
|
||||
"bin": "./model/fastText/fasttext",
|
||||
"loadModel": "./model/model.bin",
|
||||
"trainFile": "./model/train.tsv",
|
||||
"debug": true
|
||||
}
|
||||
}
|
||||
"fasttext": {
|
||||
"bin": "./model/fastText/fasttext",
|
||||
"loadModel": "./model/model.bin",
|
||||
"trainFile": "./model/train.tsv",
|
||||
"debug": true
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,15 +2,21 @@ import { readFileSync, writeFileSync } from 'node:fs';
|
||||
import { join } from 'node:path';
|
||||
|
||||
export default function addTrainData(eventData) {
|
||||
const file = readFileSync(join(global.__dirname, global.config.fasttext.trainFile), 'utf-8');
|
||||
const data = file.split('\n');
|
||||
const { label, text } = eventData;
|
||||
const file = readFileSync(
|
||||
join(global.__dirname, global.config.fasttext.trainFile),
|
||||
'utf-8'
|
||||
);
|
||||
const data = file.split('\n');
|
||||
const { label, text } = eventData;
|
||||
|
||||
const labelIndex = data.findIndex((data) => data.startsWith(label));
|
||||
const labelIndex = data.findIndex((data) => data.startsWith(label));
|
||||
|
||||
data.splice(labelIndex === -1 ? 0 : labelIndex, 0, `${label} ${text}`);
|
||||
data.splice(labelIndex === -1 ? 0 : labelIndex, 0, `${label} ${text}`);
|
||||
|
||||
writeFileSync(join(global.__dirname, global.config.fasttext.trainFile), data.join('\n'));
|
||||
writeFileSync(
|
||||
join(global.__dirname, global.config.fasttext.trainFile),
|
||||
data.join('\n')
|
||||
);
|
||||
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
import { serialize } from 'bson';
|
||||
|
||||
export default async function runAI(client, data) {
|
||||
const predictions = await global.ft.predict(data.text);
|
||||
const jsonData = {
|
||||
op: 2,
|
||||
id: data.id,
|
||||
predictions
|
||||
};
|
||||
const predictions = await global.ft.predict(data.text);
|
||||
const jsonData = {
|
||||
op: 2,
|
||||
id: data.id,
|
||||
predictions
|
||||
};
|
||||
|
||||
const bsonData = serialize(jsonData);
|
||||
client.write(bsonData);
|
||||
|
||||
return;
|
||||
}
|
||||
const bsonData = serialize(jsonData);
|
||||
client.write(bsonData);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -3,9 +3,4 @@ import trainAI from './trainAI.js';
|
||||
import runOCR from './ocr.js';
|
||||
import addTrainData from './addTrainData.js';
|
||||
|
||||
export {
|
||||
runAI,
|
||||
trainAI,
|
||||
runOCR,
|
||||
addTrainData
|
||||
}
|
||||
export { runAI, trainAI, runOCR, addTrainData };
|
||||
|
||||
@@ -2,22 +2,22 @@ import { recognize } from 'node-tesseract-ocr';
|
||||
import { serialize } from 'bson';
|
||||
|
||||
export default async function runOCR(client, eventData) {
|
||||
const config = {
|
||||
lang: 'eng',
|
||||
oem: 3,
|
||||
psm: 3,
|
||||
};
|
||||
const config = {
|
||||
lang: 'eng',
|
||||
oem: 3,
|
||||
psm: 3
|
||||
};
|
||||
|
||||
const ocrText = await recognize(eventData.url, config);
|
||||
const ocrText = await recognize(eventData.url, config);
|
||||
|
||||
const jsonData = {
|
||||
op: 6,
|
||||
id: eventData.id,
|
||||
ocrText
|
||||
};
|
||||
const jsonData = {
|
||||
op: 6,
|
||||
id: eventData.id,
|
||||
ocrText
|
||||
};
|
||||
|
||||
const bsonData = serialize(jsonData);
|
||||
client.write(bsonData);
|
||||
const bsonData = serialize(jsonData);
|
||||
client.write(bsonData);
|
||||
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -2,53 +2,56 @@ import FastText from 'fasttext.js';
|
||||
import { join } from 'node:path';
|
||||
|
||||
export default async function trainAI() {
|
||||
const ft = new FastText({
|
||||
train: {
|
||||
// number of concurrent threads
|
||||
thread: 8,
|
||||
// verbosity level [2]
|
||||
verbose: 4,
|
||||
// number of negatives sampled [5]
|
||||
neg: 7,
|
||||
// loss function {ns, hs, softmax} [ns]
|
||||
loss: 'ns',
|
||||
// learning rate [0.05]
|
||||
lr: 1,
|
||||
// change the rate of updates for the learning rate [100]
|
||||
lrUpdateRate: 1000,
|
||||
// max length of word ngram [1]
|
||||
wordNgrams: 5,
|
||||
// minimal number of word occurences
|
||||
minCount: 1,
|
||||
// minimal number of word occurences
|
||||
minCountLabel: 1,
|
||||
// size of word vectors [100]
|
||||
dim: 100,
|
||||
// size of the context window [5]
|
||||
ws: 5,
|
||||
// number of epochs [5]
|
||||
epoch: 20,
|
||||
// number of buckets [2000000]
|
||||
bucket: 2000000,
|
||||
// min length of char ngram [3]
|
||||
minn: process.env.TRAIN_MINN || 3,
|
||||
// max length of char ngram [6]
|
||||
maxn: process.env.TRAIN_MAXN || 6,
|
||||
// sampling threshold [0.0001]
|
||||
t: 0.0001,
|
||||
// load pre trained word vectors from unsupervised model
|
||||
pretrainedVectors: ''
|
||||
},
|
||||
serializeTo: join(global.__dirname, global.config.fasttext.loadModel).replace('.bin', ''),
|
||||
trainFile: join(global.__dirname, global.config.fasttext.trainFile),
|
||||
bin: join(global.__dirname, global.config.fasttext.bin)
|
||||
});
|
||||
|
||||
global.ft.unload();
|
||||
const ft = new FastText({
|
||||
train: {
|
||||
// number of concurrent threads
|
||||
thread: 8,
|
||||
// verbosity level [2]
|
||||
verbose: 4,
|
||||
// number of negatives sampled [5]
|
||||
neg: 7,
|
||||
// loss function {ns, hs, softmax} [ns]
|
||||
loss: 'ns',
|
||||
// learning rate [0.05]
|
||||
lr: 1,
|
||||
// change the rate of updates for the learning rate [100]
|
||||
lrUpdateRate: 1000,
|
||||
// max length of word ngram [1]
|
||||
wordNgrams: 5,
|
||||
// minimal number of word occurences
|
||||
minCount: 1,
|
||||
// minimal number of word occurences
|
||||
minCountLabel: 1,
|
||||
// size of word vectors [100]
|
||||
dim: 100,
|
||||
// size of the context window [5]
|
||||
ws: 5,
|
||||
// number of epochs [5]
|
||||
epoch: 20,
|
||||
// number of buckets [2000000]
|
||||
bucket: 2000000,
|
||||
// min length of char ngram [3]
|
||||
minn: process.env.TRAIN_MINN || 3,
|
||||
// max length of char ngram [6]
|
||||
maxn: process.env.TRAIN_MAXN || 6,
|
||||
// sampling threshold [0.0001]
|
||||
t: 0.0001,
|
||||
// load pre trained word vectors from unsupervised model
|
||||
pretrainedVectors: ''
|
||||
},
|
||||
serializeTo: join(
|
||||
global.__dirname,
|
||||
global.config.fasttext.loadModel
|
||||
).replace('.bin', ''),
|
||||
trainFile: join(global.__dirname, global.config.fasttext.trainFile),
|
||||
bin: join(global.__dirname, global.config.fasttext.bin)
|
||||
});
|
||||
|
||||
await ft.train()
|
||||
global.ft.unload();
|
||||
|
||||
global.ft.load();
|
||||
await ft.train();
|
||||
|
||||
return;
|
||||
}
|
||||
global.ft.load();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
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';
|
||||
import { dirname } from 'node:path';
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
global.__dirname = dirname(__filename);
|
||||
@@ -22,33 +22,33 @@ ft.load();
|
||||
global.ft = ft;
|
||||
|
||||
const server = createServer(async (client) => {
|
||||
client.on('data', async (data) => {
|
||||
const eventData = deserialize(data, { allowObjectSmallerThanBufferSize: true });
|
||||
client.on('data', async (data) => {
|
||||
const eventData = deserialize(data, {
|
||||
allowObjectSmallerThanBufferSize: true
|
||||
});
|
||||
|
||||
switch(eventData.op) {
|
||||
case 1: {
|
||||
runAI(client, eventData);
|
||||
break;
|
||||
};
|
||||
switch (eventData.op) {
|
||||
case 1: {
|
||||
runAI(client, eventData);
|
||||
break;
|
||||
}
|
||||
|
||||
case 3: {
|
||||
addTrainData(eventData);
|
||||
break;
|
||||
};
|
||||
case 3: {
|
||||
addTrainData(eventData);
|
||||
break;
|
||||
}
|
||||
|
||||
case 4: {
|
||||
trainAI();
|
||||
break;
|
||||
|
||||
};
|
||||
case 4: {
|
||||
trainAI();
|
||||
break;
|
||||
}
|
||||
|
||||
case 5: {
|
||||
runOCR(client, eventData);
|
||||
break;
|
||||
};
|
||||
};
|
||||
|
||||
});
|
||||
case 5: {
|
||||
runOCR(client, eventData);
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
server.listen(global.config.server.port || 3000);
|
||||
server.listen(global.config.server.port || 3000);
|
||||
|
||||
Reference in New Issue
Block a user