Merge pull request 'use python shell library' (#4) from python-shell into master
Reviewed-on: https://git.arrayinamatrix.xyz/array-in-a-matrix/text-gen-bot/pulls/4
This commit is contained in:
commit
e85992ffab
4 changed files with 22 additions and 21 deletions
|
@ -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.
|
||||
|
|
26
index.js
26
index.js
|
@ -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);
|
||||
|
@ -32,31 +32,25 @@ client.on("room.message", (roomId, event) => {
|
|||
if ((!(messageCounter % config.frequency) && !(lineCount(config.file) < config.size)) || userMessage[0] === "speak") {
|
||||
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
|
||||
});
|
||||
console.log("Message sent!");
|
||||
python.on('close'); // ? close python process when finished
|
||||
userMessage.shift()
|
||||
const options = { args: ['generate'] };
|
||||
PythonShell.run(pyFile, options, (err, message) => {
|
||||
if (err) throw err;
|
||||
client.sendText(roomId, message.toString());
|
||||
console.log("Message sent!");
|
||||
}); // ? 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;
|
||||
};
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
{
|
||||
"dependencies": {
|
||||
"matrix-bot-sdk": "^0.6.1"
|
||||
"matrix-bot-sdk": "^0.6.1",
|
||||
"python-shell": "^3.0.1"
|
||||
},
|
||||
"type": "module"
|
||||
}
|
7
pnpm-lock.yaml
generated
7
pnpm-lock.yaml
generated
|
@ -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'}
|
||||
|
|
Loading…
Add table
Reference in a new issue