-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
130 lines (102 loc) · 3.99 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
const express = require('express');
const brain = require('brain.js');
const fs = require('fs');
const app = express();
app.use(express.json());
const net = new brain.recurrent.LSTM();
const trainingData = [
{ input: "hello", output: "greetings.hello" },
{ input: "hi", output: "greetings.hello" },
{ input: "howdy", output: "greetings.hello" },
{ input: "goodbye for now", output: "greetings.bye" },
{ input: "bye bye take care", output: "greetings.bye" },
{ input: "i must go", output: "greetings.bye" },
{ input: "who is your creator", output: "creator.identity" },
{ input: "siapa pembuat mu", output: "creator.identity" },
{ input: "calculate 10 plus 5", output: "math.add" },
{ input: "calculate 10 minus 5", output: "math.subtract" },
{ input: "calculate 10 times 5", output: "math.multiply" },
{ input: "calculate 10 divided by 5", output: "math.divide" }
];
function handleArithmetic(text) {
const parts = text.split(" ");
const num1 = parseFloat(parts[1]);
const num2 = parseFloat(parts[3]);
if (text.includes("plus")) return `The result is ${num1 + num2}`;
if (text.includes("minus")) return `The result is ${num1 - num2}`;
if (text.includes("times")) return `The result is ${num1 * num2}`;
if (text.includes("divided by")) return num2 !== 0 ? `The result is ${num1 / num2}` : 'Cannot divide by zero';
return "I couldn't understand the calculation.";
}
function getResponse(text) {
const output = net.run(text.toLowerCase());
switch (output) {
case "greetings.hello":
return "Hey there!";
case "greetings.bye":
return "Till next time!";
case "creator.identity":
return "I was created by SyahdanDev!";
case "math.add":
case "math.subtract":
case "math.multiply":
case "math.divide":
return handleArithmetic(text);
default:
return "I'm not sure how to respond to that.";
}
}
function trainModel() {
console.log('Starting training...');
net.train(trainingData, {
iterations: 500,
log: (details) => console.log(details),
logPeriod: 10,
errorThresh: 0.011
});
const modelJSON = net.toJSON();
fs.writeFileSync('brain-model.json', JSON.stringify(modelJSON));
console.log('Training completed and model saved to brain-model.json');
}
function loadModel() {
if (fs.existsSync('brain-model.json')) {
const modelJSON = JSON.parse(fs.readFileSync('brain-model.json'));
net.fromJSON(modelJSON);
console.log('Model loaded from brain-model.json');
} else {
console.error('No trained model found. Run with "train" to create a model first.');
process.exit(1);
}
}
(async () => {
const command = process.argv[2];
if (command === 'train') {
trainModel();
} else if (command === 'api') {
loadModel();
app.post('/api/message', (req, res) => {
const { text } = req.body;
if (!text) return res.status(400).json({ error: "Please provide a 'text' input." });
const response = getResponse(text);
res.json({ answer: response });
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
} else if (command === 'simple_qna') {
const input = process.argv[3];
if (!input) {
console.error('Please provide an input text for simple_qna');
process.exit(1);
}
loadModel();
const response = getResponse(input);
console.log(response);
} else {
console.log('Usage:');
console.log(' node index.js train - to train and save the model');
console.log(' node index.js api - to start the API server');
console.log(' node index.js simple_qna <text> - to process a single input');
}
})();