now using pythonshell lib

This commit is contained in:
array-in-a-matrix 2022-08-22 01:55:10 -04:00
parent b16bffb130
commit fa853dbd86
4 changed files with 22 additions and 21 deletions

View File

@ -37,20 +37,19 @@ List of directories and files created by the project and its dependencies:
The project is split into 2 parts `index.js` and `textgen.py`. The `index.js` file contains the code that interacts with the user on Matrix and sends text generated by the `textgen.py` file.
Install [JavaSript SDK](https://github.com/turt2live/matrix-bot-sdk):
Install [matrix-bot-sdk](https://github.com/turt2live/matrix-bot-sdk) and [python-shell](https://github.com/extrabacon/python-shell) (JS):
```sh
> pnpm add matrix-bot-sdk
> pnpm add python-shell
```
Install [Python module](https://github.com/minimaxir/aitextgen):
Install [aitextgen](https://github.com/minimaxir/aitextgen) (PY):
```sh
> pip3 install aitextgen
```
### Configurations
Before a bot can be used the fields in the `config.json` file must be populated with valid information. Values in angled brackets (stared below) must be supplied before usage.

View File

@ -1,7 +1,7 @@
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";
import { PythonShell } from 'python-shell';
const storage = new SimpleFsStorageProvider("storage.json");
const client = new MatrixClient(config.homeserver, config.token, storage);
@ -33,30 +33,24 @@ client.on("room.message", (roomId, event) => {
console.log("Generating message...");
userMessage.shift()
const python = spawn('python', [pyFile, "generate", userMessage.join(' ')]);
python.stdout.on('data', (message) => {
message = message.toString();
client.sendText(roomId, message); // ? send generated message to room
});
const options = { args: ['generate'] };
PythonShell.run(pyFile, options, (err, message) => {
if (err) throw err;
client.sendText(roomId, message.toString());
console.log("Message sent!");
python.on('close'); // ? close python process when finished
}); // ? send generated message to room
};
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());
});
// TODO: exec training function
console.log("Training finished!");
python.on('close'); // ? close python process when finished
};
});
function lineCount(text) {
return fs.readFileSync(text).toString().split("\n").length - 1;
};

View File

@ -1,6 +1,7 @@
{
"dependencies": {
"matrix-bot-sdk": "^0.6.1"
"matrix-bot-sdk": "^0.6.1",
"python-shell": "^3.0.1"
},
"type": "module"
}

View File

@ -2,9 +2,11 @@ lockfileVersion: 5.4
specifiers:
matrix-bot-sdk: ^0.6.1
python-shell: ^3.0.1
dependencies:
matrix-bot-sdk: 0.6.1
python-shell: 3.0.1
packages:
@ -843,6 +845,11 @@ packages:
engines: {node: '>=6'}
dev: false
/python-shell/3.0.1:
resolution: {integrity: sha512-TWeotuxe1auhXa5bGRScxnc2J+0r41NBntSa6RYZtMBLtAEsvCboKrEbW6DvASosWQepVkhZZlT3B5Ei766G+Q==}
engines: {node: '>=0.10'}
dev: false
/qs/6.10.3:
resolution: {integrity: sha512-wr7M2E0OFRfIfJZjKGieI8lBKb7fRCH4Fv5KNPEs7gJ8jadvotdsS08PzOKR7opXhZ/Xkjtt3WF9g38drmyRqQ==}
engines: {node: '>=0.6'}