Create tbbot.js
Browse files
tbbot.js
ADDED
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
const io = require('socket.io-client');
|
2 |
+
const tf = require('@tensorflow/tfjs-node');
|
3 |
+
|
4 |
+
// Connect to the existing Socket.IO server
|
5 |
+
const socket = io('https://box.km.mk/socket.io'); // Replace with your server URL
|
6 |
+
socket.emit("user joined", "AITrainer", "green")
|
7 |
+
// Simple in-memory dataset for training
|
8 |
+
let trainingData = [];
|
9 |
+
let labels = [];
|
10 |
+
|
11 |
+
// Listen for incoming messages
|
12 |
+
socket.on('data', (data) => {
|
13 |
+
console.log('Received message:', data);
|
14 |
+
processMessage(data); // Process the incoming message
|
15 |
+
});
|
16 |
+
|
17 |
+
// Function to process incoming messages and prepare them for training
|
18 |
+
function processMessage(message) {
|
19 |
+
// Here we assume the message is an object with text and label properties
|
20 |
+
if (message.text && message.label) {
|
21 |
+
trainingData.push(message.text);
|
22 |
+
labels.push(message.label);
|
23 |
+
console.log('Added to training data:', message.text);
|
24 |
+
|
25 |
+
// Optionally train the AI every N messages or on demand
|
26 |
+
if (trainingData.length >= 10) { // Example: train after 10 messages
|
27 |
+
trainAI();
|
28 |
+
}
|
29 |
+
}
|
30 |
+
}
|
31 |
+
|
32 |
+
// Function to train the AI model
|
33 |
+
async function trainAI() {
|
34 |
+
console.log('Training AI with collected data...');
|
35 |
+
|
36 |
+
// Convert texts to tensors (this is a placeholder; implement your own preprocessing)
|
37 |
+
const xs = tf.tensor2d(trainingData.map(text => text.split('').map(char => char.charCodeAt(0))), [trainingData.length, trainingData[0].length]);
|
38 |
+
const ys = tf.tensor2d(labels.map(label => label === 'positive' ? [1] : [0]), [labels.length, 1]); // Binary classification example
|
39 |
+
|
40 |
+
// Define a simple model (this is just an example)
|
41 |
+
const model = tf.sequential();
|
42 |
+
model.add(tf.layers.dense({ units: 5, activation: 'relu', inputShape: [trainingData[0].length] }));
|
43 |
+
model.add(tf.layers.dense({ units: 1, activation: 'sigmoid' }));
|
44 |
+
|
45 |
+
model.compile({ optimizer: 'adam', loss: 'binaryCrossentropy', metrics: ['accuracy'] });
|
46 |
+
|
47 |
+
// Train the model
|
48 |
+
await model.fit(xs, ys, { epochs: 10 });
|
49 |
+
|
50 |
+
console.log('Training complete.');
|
51 |
+
|
52 |
+
// Clear training data after training (optional)
|
53 |
+
trainingData = [];
|
54 |
+
labels = [];
|
55 |
+
}
|
56 |
+
|
57 |
+
// Handle errors
|
58 |
+
socket.on('connect_error', (err) => {
|
59 |
+
console.error('Connection error:', err);
|
60 |
+
});
|
61 |
+
|
62 |
+
// Keep the process alive indefinitely
|
63 |
+
process.on('SIGINT', () => {
|
64 |
+
console.log("Shutting down gracefully...");
|
65 |
+
socket.disconnect();
|
66 |
+
process.exit();
|
67 |
+
});
|
68 |
+
|
69 |
+
console.log("Listening for messages...");
|
70 |
+
|
71 |
+
// This will keep the script running indefinitely.
|
72 |
+
setInterval(() => {}, 1000); // Keeps the event loop active
|