-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatgpt main.js
More file actions
312 lines (270 loc) · 11.1 KB
/
Chatgpt main.js
File metadata and controls
312 lines (270 loc) · 11.1 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
alert("AI Chat Bot By Esvanth");
alert("click yes or No if you are satisfied with your answer");
alert("Train Your AI");
window.addEventListener('load', function(){
var chat = document.getElementById("chatButton");
var no = document.getElementById("noButton");
var yes = document.getElementById("yesButton");
var txt = document.getElementById("textBox");
var confirm = document.getElementById("confirmation");
var help = document.getElementById("helpBox");
var helpBtn = document.getElementById("helpButton");
var trainingArea = document.getElementById("trainArea");
var botTalk = ["Hello! I hope you have a good day!","I am fine, thanks!","I have no name, but my creators name is Esvanth!","20","I was created in december, 2018.","I am not human, I am a robot."];
var divArr=[];
var delayVar=0;
function newDiv(COLOR, TEXT){
var newdiv = document.createElement("div");
newdiv.style.width = "90%";
newdiv.style.height = "10%";
newdiv.style.background = COLOR;
if(COLOR=="green"){
newdiv.style.left="53%";
}
else{
newdiv.style.left="47%";
}
newdiv.style.bottom="15%";
newdiv.style.position="fixed";
newdiv.style.borderRadius="10px";
newdiv.style.transform="translate(-50%,0)";
newdiv.style.paddingLeft ="10px";
newdiv.style.paddingTop ="5px";
newdiv.style.fontFamily=" Verdana, Times, serif";
newdiv.innerHTML = TEXT;
newdiv.style.border = "1px solid black";
newdiv.style.color="white";
document.body.appendChild(newdiv);
divArr.push(newdiv);
for (y=0;y<divArr.length-1;y++){
if (divArr[y].style.bottom=="15%"){
divArr[y].style.bottom="28%";
}
else if (divArr[y].style.bottom=="28%"){
divArr[y].style.bottom="41%";
}
else if (divArr[y].style.bottom=="41%"){
divArr[y].style.bottom="54%";
}
else if (divArr[y].style.bottom=="54%"){
divArr[y].style.bottom="67%";
}
else if (divArr[y].style.bottom=="67%"){
divArr[y].style.bottom="80%";
}
else if (divArr[y].style.bottom=="80%"){
divArr[y].style.bottom="93%";
}
else if (divArr[y].style.bottom=="93%"){
divArr[y].style.bottom="106%";
}
else if(divArr[y].style.bottom=="106%"){
divArr[y].style.display="none";
}
}
}
/*
newDiv("green","Who are you?");
newDiv("orange","I am a bot.");
*/
//***********Machine learning**************
var net = new brain.NeuralNetwork();
var trainData = [];
var maxLength = 0;
var remainingLength = 0;
var newInput;
var commands = 7;
//Greeting
trainData.push({ input: [1,0,0,0,1,1,1,1,0,0,1,0,0,0], output: {[1]: 1} }); //HI
trainData.push({ input: [1,0,0,0,1,1,1,1,0,0,0,1,0,0,1,0,1,1,0,0,0], output: {[1]: 1} }); //HEY
trainData.push({ input: [1,0,0,0,1,1,1,1,0,0,0,1,0,0,1,0,0,1,0,1,1,1,0,0,1,0,1,1,1,0,0,1,1,1,0], output: {[1]: 1} }); //HELLO
trainData.push({ input: [1,0,1,1,0,0,0,1,0,0,1,1,1,0], output: {[1]: 1} }); //Yo
//How are you?
trainData.push({ input: [1,0,0,0,1,1,1,1,0,0,1,1,1,0,1,0,1,0,1,1,0,1,0,0,0,0,0,0,1,0,1,0,0,0,1,1,0,0,0,1,0,0,1,0,1,1,0,0,0,1,0,0,1,1,1,0,1,0,1,0,1,0,0,1,1,1,1,1,1,1], output: {[2]: 1} }); //How are you?
trainData.push({ input: [1,0,0,0,0,0,0,1,0,1,0,0,0,1,1,0,0,0,1,0,0,1,0,1,1,0,0,0,1,0,0,1,1,1,0,1,0,1,0,1,0,0,1,0,0,1,1,1,0,1,0,0,1,0,1,0,1,1,1,1,1,1,1], output: {[2]: 1} }); //Are you ok?
//What is your name?
trainData.push({ input: [1,0,1,0,1,1,0,1,0,0,0,1,1,1,1,0,0,0,0,0,0,1,0,1,0,0,1,1,1,0,0,1,0,0,0,1,0,1,0,0,1,0,1,0,1,1,0,0,0,1,0,0,1,1,1,0,1,0,1,0,1,0,0,1,0,1,0,0,0,1,1,0,0,1,1,0,1,1,0,0,0,0,0,0,1,0,0,1,1,0,0,1,0,0,0,1,0,0,1,1,1,1,1,1,1], output: {[3]: 1} }); //What is your name?
trainData.push({ input: [1,0,1,0,1,1,0,1,0,0,0,1,1,1,1,0,0,0,0,0,0,1,0,1,0,0,1,1,1,0,1,0,0,1,0,1,0,1,1,0,0,0,1,0,0,1,1,1,0,1,0,1,0,1,0,0,1,0,1,0,0,0,1,1,0,0,1,1,0,1,1,0,0,0,0,0,0,1,0,0,1,1,0,0,1,0,0,0,1,0,0,1,1,1,1,1,1,1], output: {[3]: 1} }); //Whats your name?
trainData.push({ input: [1,0,1,0,1,1,0,1,0,0,0,1,1,1,1,0,0,0,0,0,0,1,0,1,0,0,1,1,1,0,1,0,0,1,0,1,0,1,0,1,0,0,1,0,1,0,0,0,1,1,0,0,1,1,0,1,1,0,0,0,0,0,0,1,0,0,1,1,0,0,1,0,0,0,1,0,0,1,1,1,1,1,1,1], output: {[3]: 1} }); //Whats ur name?
trainData.push({ input: [1,0,1,1,0,0,0,1,0,0,1,1,1,0,1,0,1,0,1,0,0,1,0,1,0,0,0,1,1,0,0,1,1,0,1,1,0,0,0,0,0,0,1,0,0,1,1,0,0,1,0,0,0,1,0,0,1,1,1,1,1,1,1], output: {[3]: 1} }); //Your name?
trainData.push({ input: [1,0,1,0,1,1,0,1,0,0,0,1,1,1,1,0,0,1,1,1,0,1,0,0,0,0,0,0,1,0,1,0,0,0,1,1,0,0,0,1,0,0,1,0,1,1,0,0,0,1,0,0,1,1,1,0,1,0,1,0,1,0,0,1,1,1,1,1,1,1], output: {[3]: 1} }); //Who are you?
trainData.push({ input: [1,0,0,1,1,0,1,1,0,0,0,0,0,0,1,0,0,1,1,0,0,1,0,0,0,1,0,0,1,1,1,1,1,1,1], output: {[3]: 1} }); //Name?
//Meaning of life?
trainData.push({ input: [1,0,1,0,1,1,0,1,0,0,0,1,1,1,1,0,0,0,0,0,0,1,0,1,0,0,1,1,1,0,0,1,0,0,0,1,0,1,0,0,1,0,1,0,1,0,0,1,1,1,0,0,0,1,1,1,1,0,0,0,1,0,0,1,0,0,1,1,0,0,1,0,0,0,1,0,0,1,0,0,0,0,0,0,1,0,0,1,1,0,1,1,0,0,1,0,0,0,1,0,0,1,1,0,1,1,0,0,0,1,1,0,1,0,0,1,1,1,0,1,0,0,0,1,0,1,1,0,0,1,0,1,1,1,0,0,1,0,0,0,1,0,0,0,1,0,1,1,0,0,0,1,0,0,1,1,1,1,1,1,1], output: {[4]: 1} }); //What is the meaning of life?
trainData.push({ input: [1,0,0,1,1,0,0,1,0,0,0,1,0,0,1,0,0,0,0,0,0,1,0,0,1,1,0,1,1,0,0,1,0,0,0,1,0,0,1,1,0,1,1,0,0,0,1,1,0,1,0,0,1,1,1,0,1,0,0,0,1,0,1,1,0,0,1,0,1,1,1,0,0,1,0,0,0,1,0,0,0,1,0,1,1,0,0,0,1,0,0,1,1,1,1,1,1,1], output: {[4]: 1} }); //Meaning of life?
//How old are you?
trainData.push({ input: [1,0,0,0,1,1,1,1,0,0,1,1,1,0,1,0,1,0,1,1,0,1,0,0,1,1,1,0,1,0,0,1,0,1,1,1,0,0,0,0,1,1,1,0,0,0,0,0,0,1,0,1,0,0,0,1,1,0,0,0,1,0,0,1,0,1,1,0,0,0,1,0,0,1,1,1,0,1,0,1,0,1,0,0,1,1,1,1,1,1,1], output: {[5]: 1} }); //How old are you?
trainData.push({ input: [1,0,1,0,1,1,0,1,0,0,0,1,1,1,1,0,0,0,0,0,0,1,0,1,0,0,1,1,1,0,0,1,0,0,0,1,0,1,0,0,1,0,1,0,1,1,0,0,0,1,0,0,1,1,1,0,1,0,1,0,1,0,0,1,0,1,0,0,0,1,1,0,0,0,0,0,0,1,0,0,0,1,1,0,1,0,0,0,1,0,0,1,1,1,1,1,1,1], output: {[5]: 1} }); //What is your age?
trainData.push({ input: [1,0,1,0,1,1,0,1,0,0,0,1,1,1,1,0,0,0,0,0,0,1,0,1,0,0,1,1,1,0,1,0,0,1,0,1,0,1,1,0,0,0,1,0,0,1,1,1,0,1,0,1,0,1,0,0,1,0,1,0,0,0,1,1,0,0,0,0,0,0,1,0,0,0,1,1,0,1,0,0,0,1,0,0,1,1,1,1,1,1,1], output: {[5]: 1} }); //Whats your age?
trainData.push({ input: [1,0,1,0,1,1,0,1,0,0,0,1,1,1,1,0,0,0,0,0,0,1,0,1,0,0,1,1,1,0,1,0,0,1,0,1,0,1,0,1,0,0,1,0,1,0,0,0,1,1,0,0,0,0,0,0,1,0,0,0,1,1,0,1,0,0,0,1,0,0,1,1,1,1,1,1,1], output: {[5]: 1} }); //Whats ur age?
//Are you human?
trainData.push({ input: [1,0,0,0,0,0,0,1,0,1,0,0,0,1,1,0,0,0,1,0,0,1,0,1,1,0,0,0,1,0,0,1,1,1,0,1,0,1,0,1,0,0,1,0,0,0,1,1,1,1,0,1,0,1,0,0,1,0,0,1,1,0,0,1,0,0,0,0,0,0,1,0,0,1,1,0,1,1,1,1,1,1,1,1], output: {[6]: 1} }); //Are you human?
trainData.push({ input: [1,0,0,0,1,1,1,1,0,1,0,1,0,0,1,0,0,1,1,0,0,1,0,0,0,0,0,0,1,0,0,1,1,0,1,1,1,1,1,1,1,1], output: {[6]: 1} }); //human?
//Commands to fill up the arrays with zeros. All arrays must be of same length
for (j=0;j<trainData.length;j++){
if (trainData[j].input.length > maxLength){
maxLength = trainData[j].input.length;
}
}
for (q=0;q<trainData.length;q++){
if (trainData[q].input.length < maxLength){
remainingLength = maxLength - trainData[q].input.length;
zeroArray = Array(remainingLength).fill(0);
trainData[q].input = trainData[q].input.concat(zeroArray);
}
}
//Training
net.train(trainData, {
log: false,
logPeriod: 10,
errorThresh: 0.0005,
}); //Using all the training data to train the AI
//Chat button
chat.addEventListener("click",function(){
if (txt.value != ""){
newDiv("green",txt.value);
var data = textToBinary(txt.value);
var result = brain.likely(data, net);
for (k=1;k<=botTalk.length;k++){
if (result == k){
delayVar=k;
setTimeout(function(){
newDiv("orange",botTalk[delayVar-1]);
trainingArea.style.display="inline";
},800);
}
}
help.style.display = "none";
helpBtn.style.display = "none";
}
});
yes.addEventListener("click", function(){
alert("Sweet!");
txt.value="";
help.style.display = "none";
helpBtn.style.display = "none";
trainingArea.style.display="none";
})
no.addEventListener("click", function(){
alert("Oh, I am sorry! What would be a good response to your input?");
divArr[divArr.length-1].style.backgroundColor="#ff6666"
help.style.display = "inline";
helpBtn.style.display = "inline";
})
helpBtn.addEventListener("click", function(){
trainingArea.style.display="none";
botTalk.push(help.value);
newInput = textToBinary(txt.value);
trainData.push({ input: newInput, output: {[commands]: 1} }); //user training data
commands = commands+1;
net = new brain.NeuralNetwork();
//Training the AI
net.train(trainData, {
log: false,
logPeriod: 10,
errorThresh: 0.0005,
});
alert("Alright! Thanks for making me smarter!");
txt.value="";
help.value="";
help.style.display = "none";
helpBtn.style.display = "none";
})
function textToBinary(text){
//Storing all letters as binary numbers for AI
text = text.toUpperCase();
var data = [];
for (i=0;i<text.length;i++){
if ( text[i]=="A"){
data = data.concat([1,0,0,0,0,0,0]);
}
else if (text[i]=="B"){
data = data.concat([1,0,0,0,0,0,1]);
}
else if (text[i]=="C"){
data = data.concat([1,0,0,0,0,1,0]);
}
else if (text[i]=="D"){
data = data.concat([1,0,0,0,0,1,1]);
}
else if (text[i]=="E"){
data = data.concat([1,0,0,0,1,0,0]);
}
else if (text[i]=="F"){
data = data.concat([1,0,0,0,1,0,1]);
}
else if (text[i]=="G"){
data = data.concat([1,0,0,0,1,1,0]);
}
else if (text[i]=="H"){
data = data.concat([1,0,0,0,1,1,1]);
}
else if (text[i]=="I"){
data = data.concat([1,0,0,1,0,0,0]);
}
else if (text[i]=="J"){
data = data.concat([1,0,0,1,0,0,1]);
}
else if (text[i]=="K"){
data = data.concat([1,0,0,1,0,1,0]);
}
else if (text[i]=="L"){
data = data.concat([1,0,0,1,0,1,1]);
}
else if (text[i]=="M"){
data = data.concat([1,0,0,1,1,0,0]);
}
else if (text[i]=="N"){
data = data.concat([1,0,0,1,1,0,1]);
}
else if (text[i]=="O"){
data = data.concat([1,0,0,1,1,1,0]);
}
else if (text[i]=="P"){
data = data.concat([1,0,0,1,1,1,1]);
}
else if (text[i]=="Q"){
data = data.concat([1,0,1,0,0,0,0]);
}
else if (text[i]=="R"){
data = data.concat([1,0,1,0,0,0,1]);
}
else if (text[i]=="S"){
data = data.concat([1,0,1,0,0,1,0]);
}
else if (text[i]=="T"){
data = data.concat([1,0,1,0,0,1,1]);
}
else if (text[i]=="U"){
data = data.concat([1,0,1,0,1,0,0]);
}
else if (text[i]=="V"){
data = data.concat([1,0,1,0,1,0,1]);
}
else if (text[i]=="W"){
data = data.concat([1,0,1,0,1,1,0]);
}
else if (text[i]=="X"){
data = data.concat([1,0,1,0,1,1,1]);
}
else if (text[i]=="Y"){
data = data.concat([1,0,1,1,0,0,0]);
}
else if (text[i]=="Z"){
data = data.concat([1,0,1,1,0,0,1]);
}
else if (text[i]=="?"){
data = data.concat([1,1,1,1,1,1,1]);
}
}
//Used the code below to be able to read long arrays
//console.log(data.toString());
//Fill user input array with zeros to get correct length
if (data.length < maxLength){
remainingLength = maxLength - data.length;
zeroArray = Array(remainingLength).fill(0);
data = data.concat(zeroArray);
}
return data;
}
});