js exec py and return str
This commit is contained in:
parent
99b70ab839
commit
55b9a2693e
11
index.js
11
index.js
|
@ -1,5 +1,6 @@
|
||||||
import config from './config.json' assert {type: "json"};
|
import config from './config.json' assert {type: "json"};
|
||||||
import { MatrixClient, SimpleFsStorageProvider, AutojoinRoomsMixin } from "matrix-bot-sdk";
|
import { MatrixClient, SimpleFsStorageProvider, AutojoinRoomsMixin } from "matrix-bot-sdk";
|
||||||
|
import { spawn } from 'node:child_process';
|
||||||
import fs from "fs";
|
import fs from "fs";
|
||||||
|
|
||||||
const storage = new SimpleFsStorageProvider("storage.json");
|
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
|
// ? send message every N messages using the training data
|
||||||
if (!(messageCounter % config.frequency)) {
|
if (!(messageCounter % config.frequency)) {
|
||||||
console.log("Generating message...");
|
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
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -2,18 +2,20 @@ from aitextgen.TokenDataset import TokenDataset
|
||||||
from aitextgen.tokenizers import train_tokenizer
|
from aitextgen.tokenizers import train_tokenizer
|
||||||
from aitextgen.utils import GPT2ConfigCPU
|
from aitextgen.utils import GPT2ConfigCPU
|
||||||
from aitextgen import aitextgen
|
from aitextgen import aitextgen
|
||||||
import json
|
import json, sys
|
||||||
|
|
||||||
with open('config.json', 'r') as file:
|
with open('config.json', 'r') as file:
|
||||||
json_object = json.load(file)
|
json_object = json.load(file)
|
||||||
|
|
||||||
file_name = json_object['file']
|
file_name = json_object['file']
|
||||||
|
|
||||||
|
# ? generate message using trained model
|
||||||
def generate_message():
|
def generate_message():
|
||||||
ai = aitextgen(model_folder="trained_model",
|
ai = aitextgen(model_folder="trained_model",
|
||||||
tokenizer_file="aitextgen.tokenizer.json")
|
tokenizer_file="aitextgen.tokenizer.json")
|
||||||
ai.generate()
|
ai.generate()
|
||||||
|
|
||||||
|
# ? train model using text file
|
||||||
def train_ai():
|
def train_ai():
|
||||||
train_tokenizer(file_name)
|
train_tokenizer(file_name)
|
||||||
tokenizer_file = "aitextgen.tokenizer.json"
|
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)
|
ai.train(data, batch_size=8, num_steps=50000, generate_every=5000, save_every=5000)
|
||||||
print("AI has been trained!")
|
print("AI has been trained!")
|
||||||
|
|
||||||
print(generate_message())
|
# ? send message to parent JS process
|
||||||
|
print(generate_message())
|
||||||
|
sys.stdout.flush()
|
Loading…
Reference in a new issue