feat: prettier and eslint

This commit is contained in:
GramingFoxTeam
2022-11-16 13:59:55 +03:00
parent 0ad5ece085
commit 1c27ccb17c
22 changed files with 2359 additions and 324 deletions

View File

@@ -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": "..."
}
```
```

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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