alot
This commit is contained in:
parent
1b4b769b7a
commit
30518a0d74
1 changed files with 34 additions and 15 deletions
49
index.js
49
index.js
|
@ -5,39 +5,58 @@ import fs from "fs";
|
|||
|
||||
const storage = new SimpleFsStorageProvider("storage.json");
|
||||
const client = new MatrixClient(config.homeserver, config.token, storage);
|
||||
const pyFile = "textgen.py";
|
||||
|
||||
AutojoinRoomsMixin.setupOnClient(client)
|
||||
AutojoinRoomsMixin.setupOnClient(client);
|
||||
client.start().then(() => console.log(`Client has started!\n`));
|
||||
|
||||
let messageCounter = 0;
|
||||
let trainCounter = 0;
|
||||
|
||||
// ? event listener: logs messages sent into file
|
||||
client.on("room.message", (roomId, event) => {
|
||||
if (!event["content"] || event["sender"] === config.user) return;
|
||||
++messageCounter;
|
||||
fs.appendFile('training-matrix.txt', event["content"]["body"] + "\n", function (err) {
|
||||
if (err) throw err;
|
||||
// console.log(messageCounter + "\t" + event["content"]["body"]);
|
||||
});
|
||||
|
||||
if (lineCount(config.file) < config.size) return; // ? don't start generating messages until a big enough dataset is present
|
||||
// ? send message every N messages using the training data
|
||||
if (!(messageCounter % config.frequency)) {
|
||||
++messageCounter;
|
||||
++trainCounter;
|
||||
let userMessage = event["content"]["body"].split(" ");
|
||||
|
||||
if (userMessage[0].startsWith(config.prefix)) {
|
||||
userMessage[0] = userMessage[0].replace(config.prefix, '').toLowerCase();
|
||||
} else {
|
||||
fs.appendFile('training-matrix.txt', userMessage + "\n", function (err) {
|
||||
if (err) throw err;
|
||||
});
|
||||
};
|
||||
|
||||
// ? send message every N messages if big enough dataset is present
|
||||
if ((!(messageCounter % config.frequency) && !(lineCount(config.file) < config.size)) || userMessage[0] === "speak") {
|
||||
console.log("Generating message...");
|
||||
|
||||
const python = spawn('python', ["textgen.py", "generate"]);
|
||||
userMessage.shift()
|
||||
const python = spawn('python', [pyFile, "generate", userMessage.join(' ')]);
|
||||
|
||||
python.stdout.on('data', function (message) {
|
||||
python.stdout.on('data', (message) => {
|
||||
message = message.toString();
|
||||
client.sendText(roomId, message);
|
||||
client.sendText(roomId, message); // ? send generated message to room
|
||||
});
|
||||
console.log("Message sent!");
|
||||
python.on('close'); // ? close python process when finished
|
||||
};
|
||||
|
||||
// TODO: train AI every Nth message?
|
||||
if (trainCounter >= config.retrain || userMessage[0] === "train") {
|
||||
console.log("Retraining the AI...");
|
||||
|
||||
trainCounter = 0;
|
||||
const python = spawn('python', [pyFile, "train"]);
|
||||
|
||||
python.stdout.on('data', function (message) {
|
||||
console.log(message.toString());
|
||||
});
|
||||
console.log("Training finished!");
|
||||
python.on('close'); // ? close python process when finished
|
||||
};
|
||||
});
|
||||
|
||||
function lineCount(text) {
|
||||
return fs.readFileSync(text).toString().split("\n").length - 1;
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Add table
Reference in a new issue