From 55b9a2693ebb65e47f5ec336d287adf279212f46 Mon Sep 17 00:00:00 2001 From: array-in-a-matrix Date: Tue, 16 Aug 2022 02:36:47 -0400 Subject: [PATCH] js exec py and return str --- index.js | 11 ++++++++++- textgen.py | 8 ++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 1cd46b0..8473adb 100644 --- a/index.js +++ b/index.js @@ -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 }; }); diff --git a/textgen.py b/textgen.py index a842992..549b638 100644 --- a/textgen.py +++ b/textgen.py @@ -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()) \ No newline at end of file +# ? send message to parent JS process +print(generate_message()) +sys.stdout.flush() \ No newline at end of file