now using pythonshell lib
This commit is contained in:
parent
b16bffb130
commit
fa853dbd86
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.
|
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
|
```sh
|
||||||
> pnpm add matrix-bot-sdk
|
> 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
|
```sh
|
||||||
> pip3 install aitextgen
|
> pip3 install aitextgen
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### Configurations
|
### 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.
|
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.
|
||||||
|
|
22
index.js
22
index.js
|
@ -1,7 +1,7 @@
|
||||||
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";
|
||||||
|
import { PythonShell } from 'python-shell';
|
||||||
|
|
||||||
const storage = new SimpleFsStorageProvider("storage.json");
|
const storage = new SimpleFsStorageProvider("storage.json");
|
||||||
const client = new MatrixClient(config.homeserver, config.token, storage);
|
const client = new MatrixClient(config.homeserver, config.token, storage);
|
||||||
|
@ -33,30 +33,24 @@ client.on("room.message", (roomId, event) => {
|
||||||
console.log("Generating message...");
|
console.log("Generating message...");
|
||||||
|
|
||||||
userMessage.shift()
|
userMessage.shift()
|
||||||
const python = spawn('python', [pyFile, "generate", userMessage.join(' ')]);
|
const options = { args: ['generate'] };
|
||||||
|
PythonShell.run(pyFile, options, (err, message) => {
|
||||||
python.stdout.on('data', (message) => {
|
if (err) throw err;
|
||||||
message = message.toString();
|
client.sendText(roomId, message.toString());
|
||||||
client.sendText(roomId, message); // ? send generated message to room
|
|
||||||
});
|
|
||||||
console.log("Message sent!");
|
console.log("Message sent!");
|
||||||
python.on('close'); // ? close python process when finished
|
}); // ? send generated message to room
|
||||||
};
|
};
|
||||||
|
|
||||||
if (trainCounter >= config.retrain || userMessage[0] === "train") {
|
if (trainCounter >= config.retrain || userMessage[0] === "train") {
|
||||||
console.log("Retraining the AI...");
|
console.log("Retraining the AI...");
|
||||||
|
|
||||||
trainCounter = 0;
|
trainCounter = 0;
|
||||||
const python = spawn('python', [pyFile, "train"]);
|
// TODO: exec training function
|
||||||
|
|
||||||
python.stdout.on('data', function (message) {
|
|
||||||
console.log(message.toString());
|
|
||||||
});
|
|
||||||
console.log("Training finished!");
|
console.log("Training finished!");
|
||||||
python.on('close'); // ? close python process when finished
|
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
function lineCount(text) {
|
function lineCount(text) {
|
||||||
return fs.readFileSync(text).toString().split("\n").length - 1;
|
return fs.readFileSync(text).toString().split("\n").length - 1;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
{
|
{
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"matrix-bot-sdk": "^0.6.1"
|
"matrix-bot-sdk": "^0.6.1",
|
||||||
|
"python-shell": "^3.0.1"
|
||||||
},
|
},
|
||||||
"type": "module"
|
"type": "module"
|
||||||
}
|
}
|
7
pnpm-lock.yaml
generated
7
pnpm-lock.yaml
generated
|
@ -2,9 +2,11 @@ lockfileVersion: 5.4
|
||||||
|
|
||||||
specifiers:
|
specifiers:
|
||||||
matrix-bot-sdk: ^0.6.1
|
matrix-bot-sdk: ^0.6.1
|
||||||
|
python-shell: ^3.0.1
|
||||||
|
|
||||||
dependencies:
|
dependencies:
|
||||||
matrix-bot-sdk: 0.6.1
|
matrix-bot-sdk: 0.6.1
|
||||||
|
python-shell: 3.0.1
|
||||||
|
|
||||||
packages:
|
packages:
|
||||||
|
|
||||||
|
@ -843,6 +845,11 @@ packages:
|
||||||
engines: {node: '>=6'}
|
engines: {node: '>=6'}
|
||||||
dev: false
|
dev: false
|
||||||
|
|
||||||
|
/python-shell/3.0.1:
|
||||||
|
resolution: {integrity: sha512-TWeotuxe1auhXa5bGRScxnc2J+0r41NBntSa6RYZtMBLtAEsvCboKrEbW6DvASosWQepVkhZZlT3B5Ei766G+Q==}
|
||||||
|
engines: {node: '>=0.10'}
|
||||||
|
dev: false
|
||||||
|
|
||||||
/qs/6.10.3:
|
/qs/6.10.3:
|
||||||
resolution: {integrity: sha512-wr7M2E0OFRfIfJZjKGieI8lBKb7fRCH4Fv5KNPEs7gJ8jadvotdsS08PzOKR7opXhZ/Xkjtt3WF9g38drmyRqQ==}
|
resolution: {integrity: sha512-wr7M2E0OFRfIfJZjKGieI8lBKb7fRCH4Fv5KNPEs7gJ8jadvotdsS08PzOKR7opXhZ/Xkjtt3WF9g38drmyRqQ==}
|
||||||
engines: {node: '>=0.6'}
|
engines: {node: '>=0.6'}
|
||||||
|
|
Loading…
Add table
Reference in a new issue