Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
317 lines (288 loc) · 10.2 KB
/
Copy pathscript.js
File metadata and controls
317 lines (288 loc) · 10.2 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
313
314
315
316
const messages = document.getElementById("messages");
const userInput = document.getElementById("userInput");
const synth = window.speechSynthesis;
const voiceModeBtn = document.getElementById("voiceModeBtn");
const textModeBtn = document.getElementById("textModeBtn");
const chatbot = document.getElementById("chatbot");
const chatbox = document.getElementById("chatbox");
const blinkBox = document.getElementById("blinkBox");
const recordBtn = document.getElementById("recordBtn");
const sendBtn = document.getElementById("sendBtn");
// const blinkTable = document.getElementById("blinkTable");
const gameBox = document.getElementById("gameBox");
const exitBtn = document.getElementById("exitBtn");
var _currentIndex = 1;
const r = new rive.Rive({
src: "./assets/py_chatbot.riv",
canvas: document.getElementById("chatbot"),
artboard: "chatbot",
fit: rive.Fit.Cover,
autoplay: true,
stateMachines: "State Machine",
// animations: ["blink"],
onLoad: () => {
r.setTextRunValue("message", "Hello");
const inputs = r.stateMachineInputs("State Machine");
// inputs.forEach((input) => {
// console.log(input.name); //print total inputs from stateMachine
// });
const isDefault = inputs.find((input) => input.name === "isDefault");
const isListening = inputs.find((input) => input.name === "isListening");
const isSpeaking = inputs.find((input) => input.name === "isSpeaking");
const exitSpeaking = inputs.find((input) => input.name === "exitSpeaking");
const isBlink = inputs.find((input) => input.name === "isBlink");
const isHappy = inputs.find((input) => input.name === "isHappy");
const isSad = inputs.find((input) => input.name === "isSad");
const isError = inputs.find((input) => input.name === "isError");
class riveBoolean {
static setFalse() {
isDefault.value = false;
isListening.value = false;
isSpeaking.value = false;
isBlink.value = false;
isHappy.value = false;
isSad.value = false;
isError.value = false;
}
static setTrue(name) {
name.value = true;
}
}
async function sendMessage() {
window.speechSynthesis.cancel();
const message = userInput.value.trim();
if (!message) return;
// Display user message
addMessage("user", message);
userInput.value = ""; // Clear the input field
// Set the focus back to the input field for the next message
userInput.focus();
try {
// Send message to the backend
const response = await fetch("http://127.0.0.1:5000/chat", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ message }),
});
const data = await response.json();
console.log(data);
if (data.mode == "game") {
riveBoolean.setFalse();
riveBoolean.setTrue(isBlink);
addMessage("PyChat", formatResponse(data.response));
speakText(data);
setTimeout(() => {
gameBox.removeAttribute("hidden");
}, 1000);
} else if (data.mode == "code") {
_currentIndex = 2;
textModeBtn.classList.add("actived");
voiceModeBtn.classList.remove("actived");
chatbot.setAttribute("hidden", "true");
chatbox.removeAttribute("hidden");
recordBtn.setAttribute("hidden", "true");
riveBoolean.setFalse();
addMessage("PyChat", formatResponse(data.response));
} else {
addMessage("PyChat", formatResponse(data.response));
speakText(data);
}
} catch (error) {
addMessage("PyChat", "I'm having trouble connecting to the server.");
r.setTextRunValue("message", "ERROR!");
riveBoolean.setFalse();
riveBoolean.setTrue(isError);
setTimeout(() => {
riveBoolean.setFalse();
}, 2000);
} finally {
// Set loading state to false after the API call completes (success or failure)
isLoading = false;
console.log("API call completed.");
}
// Keep the chat scrolled to the bottom after each new message
messages.scrollTop = messages.scrollHeight;
}
// Show the response in document
function addMessage(sender, text) {
const messageDiv = document.createElement("div");
messageDiv.className = sender;
if (sender === "user") {
// Replace newline characters with <br> for line breaks in HTML
messageDiv.innerHTML = text.replace(/\n/g, "<br>");
messages.appendChild(messageDiv);
} else {
messageDiv.setAttribute("id", "typedtext");
messages.appendChild(messageDiv);
// Typewriter animation
let aText = [text];
let iSpeed = 80; // time delay of print out (adjust as needed)
let sContents = "";
let words = aText[0].split(" "); // Split text into words
let iWordIndex = 0;
function typewriter() {
if (iWordIndex < words.length) {
sContents += words[iWordIndex] + " ";
messageDiv.innerHTML = sContents + "_"; // Cursor effect
iWordIndex++;
// Scroll to the bottom while typing
messages.scrollTop = messages.scrollHeight;
setTimeout(typewriter, iSpeed);
} else {
messageDiv.innerHTML = sContents; // Remove cursor when done
// Scroll to the bottom once typing is done
messages.scrollTop = messages.scrollHeight;
}
}
setTimeout(typewriter, 1000);
}
}
// Format response
function formatResponse(response) {
// Replace newline characters with <br> for line breaks in HTML
return response.replace(/\n/g, "<br>");
}
userInput.addEventListener("keydown", (event) => {
if (event.key === "Enter") {
event.preventDefault(); // Prevent the default "Enter" behavior (e.g., creating a new line)
sendMessage(); // Call the sendMessage function when Enter is pressed
}
});
// STT
function speechRecognition() {
window.speechSynthesis.cancel();
const recognition = new (window.SpeechRecognition ||
window.webkitSpeechRecognition)();
recognition.lang = "en-US";
//onStart
recognition.onstart = () => {
riveBoolean.setFalse();
riveBoolean.setTrue(isListening);
console.log("onStart");
};
//onEnd
recognition.onend = () => {
riveBoolean.setTrue(isDefault);
console.log("onFinish");
console.log(isListening.value);
};
//onResult
recognition.onresult = (event) => {
console.log("onResult");
const speechResult = event.results[0][0].transcript.trim();
if (speechResult) {
riveBoolean.setTrue(isSpeaking);
userInput.value = speechResult;
sendMessage();
} else {
riveBoolean.setFalse();
riveBoolean.setTrue(isDefault);
}
};
//onError
recognition.onerror = (e) => {
console.log(e);
r.setTextRunValue("message", "ERROR");
riveBoolean.setFalse();
riveBoolean.setTrue(isError);
setTimeout(() => {
riveBoolean.setFalse();
}, 2000);
addMessage(
"PyChat",
"Sorry, there was an issue with speech recognition."
);
};
recognition.start();
}
//TTS
function speakText(data) {
const responseText = data.response;
// Count the number of words in the response
const wordCount = responseText.split(/\s+/).length;
// Check if the word count exceeds 100
if (wordCount > 250) {
console.log("Response too long for speech synthesis.");
return; // Skip speech synthesis if the response is too long
}
const utterance = new SpeechSynthesisUtterance(data.response);
const voices = synth.getVoices();
// utterance.voice =
// (voices.find((voice) => voice.name.includes("Female"))) ||
// voices[1];
// Ensure text-to-speech speaks in a natural pace
utterance.rate = 1;
utterance.pitch = 1;
//onStart
utterance.onboundary = () => {
console.log("Speech started");
if (data.mode == "game" || data.mode == "blink") {
riveBoolean.setFalse();
riveBoolean.setTrue(isBlink);
} else {
riveBoolean.setTrue(isSpeaking);
}
};
//onEnd
utterance.onend = () => {
window.currentUtterance = null; // Clear the reference
if (data.mode == "happiness") {
riveBoolean.setFalse();
riveBoolean.setTrue(isHappy);
setTimeout(() => {
riveBoolean.setFalse();
}, 2000);
} else if (data.mode == "sadness") {
riveBoolean.setFalse();
riveBoolean.setTrue(isSad);
setTimeout(() => {
riveBoolean.setFalse();
}, 2000);
}
// else if (data.mode == "table") {
// setTimeout(() => {
// blinkTable.setAttribute("hidden", "true");
// setTimeout(() => {
// riveBoolean.setFalse();
// }, 500);
// }, 3000);
// }
else if (data.mode == "game") {
} else {
riveBoolean.setFalse();
}
console.log("Speech finished");
};
synth.speak(utterance);
}
recordBtn.addEventListener("click", () => {
speechRecognition();
});
sendBtn.addEventListener("click", () => {
sendMessage();
});
exitBtn.addEventListener("click", () => {
gameBox.setAttribute("hidden", "true");
setTimeout(() => {
riveBoolean.setFalse();
}, 500);
});
},
});
function onMenuClicked(index) {
if (index == 1) {
_currentIndex = 1;
voiceModeBtn.classList.add("actived");
textModeBtn.classList.remove("actived");
chatbox.setAttribute("hidden", "true");
chatbot.removeAttribute("hidden");
recordBtn.removeAttribute("hidden");
} else {
_currentIndex = 2;
textModeBtn.classList.add("actived");
voiceModeBtn.classList.remove("actived");
chatbot.setAttribute("hidden", "true");
chatbox.removeAttribute("hidden");
recordBtn.setAttribute("hidden", "true");
}
}