js exec py and return str

This commit is contained in:
array-in-a-matrix 2022-08-16 02:36:47 -04:00
parent 99b70ab839
commit 55b9a2693e
2 changed files with 16 additions and 3 deletions

View File

@ -1,5 +1,6 @@
import config from './config.json' assert {type: "json"};
import { MatrixClient, SimpleFsStorageProvider, AutojoinRoomsMixin } from "matrix-bot-sdk";
import { spawn } from 'node:child_process';
import fs from "fs";
const storage = new SimpleFsStorageProvider("storage.json");
@ -25,7 +26,15 @@ client.on("room.message", (roomId, event) => {
// ? send message every N messages using the training data
if (!(messageCounter % config.frequency)) {
console.log("Generating message...");
client.sendText(roomId, "Hello, World!"); // TODO: exec py function to gen message str
const python = spawn('python', ["textgen.py"]);
python.stdout.on('data', function (message) {
message = message.toString();
console.log("bot:\t" + message);
client.sendText(roomId, message);
});
python.on('close'); // ? close python process when finished
};
});

View File

@ -2,18 +2,20 @@ from aitextgen.TokenDataset import TokenDataset
from aitextgen.tokenizers import train_tokenizer
from aitextgen.utils import GPT2ConfigCPU
from aitextgen import aitextgen
import json
import json, sys
with open('config.json', 'r') as file:
json_object = json.load(file)
file_name = json_object['file']
# ? generate message using trained model
def generate_message():
ai = aitextgen(model_folder="trained_model",
tokenizer_file="aitextgen.tokenizer.json")
ai.generate()
# ? train model using text file
def train_ai():
train_tokenizer(file_name)
tokenizer_file = "aitextgen.tokenizer.json"
@ -23,4 +25,6 @@ def train_ai():
ai.train(data, batch_size=8, num_steps=50000, generate_every=5000, save_every=5000)
print("AI has been trained!")
print(generate_message())
# ? send message to parent JS process
print(generate_message())
sys.stdout.flush()