Skip to content

Commit c7108a9

Browse files
author
Dipam Sen
committed
update CC80 voice chatbot
1 parent 3b2988d commit c7108a9

File tree

2 files changed

+48
-39
lines changed

2 files changed

+48
-39
lines changed
+31-26
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,35 @@
11
<!DOCTYPE html>
22
<html>
3-
4-
<head>
5-
<meta charset="UTF-8">
6-
<meta http-equiv="X-UA-Compatible" content="IE=edge">
7-
<meta name="viewport" content="width=device-width, initial-scale=1">
8-
9-
<title>chatbot1</title>
10-
<script type="text/javascript" src="https://unpkg.com/rivescript@latest/dist/rivescript.min.js"></script>
11-
<script
12-
src="https://code.jquery.com/jquery-3.2.1.min.js"
13-
integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
14-
crossorigin="anonymous"></script>
15-
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/p5@1.4.1/lib/p5.min.js"></script>
16-
17-
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/p5@1.4.1/lib/addons/p5.sound.min.js"></script>
18-
<script type="text/javascript" src="libraries/p5.speech.js"></script>
19-
<script type="text/javascript" src="sketch.js"></script>
20-
</head>
21-
22-
<body>
23-
24-
<!-- <h1>Chatbot</h1>
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1" />
7+
8+
<title>Voice Chatbot</title>
9+
<script
10+
type="text/javascript"
11+
src="https://unpkg.com/rivescript@latest/dist/rivescript.min.js"
12+
></script>
13+
<script
14+
src="https://code.jquery.com/jquery-3.2.1.min.js"
15+
integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
16+
crossorigin="anonymous"
17+
></script>
18+
<script
19+
type="text/javascript"
20+
src="https://cdn.jsdelivr.net/npm/p5@1.4.1/lib/p5.min.js"
21+
></script>
22+
23+
<script
24+
type="text/javascript"
25+
src="https://cdn.jsdelivr.net/npm/p5@1.4.1/lib/addons/p5.sound.min.js"
26+
></script>
27+
<script type="text/javascript" src="libraries/p5.speech.js"></script>
28+
<script type="text/javascript" src="sketch.js"></script>
29+
</head>
30+
31+
<body>
32+
<!-- <h1>Chatbot</h1>
2533
2634
<p>
2735
say: <input id="user_input"></input>
@@ -31,8 +39,5 @@
3139
<p>
3240
reply: <span id="output"></span>
3341
</p> -->
34-
35-
36-
</body>
37-
42+
</body>
3843
</html>

080_Voice_Chatbot_with_p5.Speech/P5/sketch.js

+17-13
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
1-
// Daniel Shiffman
2-
// http://codingtra.in
3-
// http://patreon.com/codingtrain
1+
// Chat Bot
2+
// The Coding Train / Daniel Shiffman
3+
// https://thecodingtrain.com/challenges/80-voice-chatbot-with-p5speech
4+
// https://youtu.be/iFTgphKCP9U
45

5-
// Voice Chatbot with p5.Speech
6-
// Edited Video: https://youtu.be/iFTgphKCP9U
6+
// Code from Challenge: https://editor.p5js.org/codingtrain/sketches/QcY2Z36mJ
77

8+
// This code has been adapted to use promises instead of callbacks since callbacks are deprecated in RiveScript.
89
function setup() {
910
noCanvas();
1011
let speech = new p5.Speech();
11-
let speechRec = new p5.SpeechRec('en-US', gotSpeech);
12+
let speechRec = new p5.SpeechRec("en-US", gotSpeech);
1213
let continuous = true;
1314
let interim = false;
1415
speechRec.start(continuous, interim);
1516

1617
let bot = new RiveScript();
17-
bot.loadFile('brain.rive', brainReady, brainError);
18+
19+
// changed callbacks to .then and .catch
20+
bot.loadFile("brain.txt").then(brainReady).catch(brainError);
1821

1922
function brainReady() {
20-
console.log('Chatbot ready!');
23+
console.log("Chatbot ready!");
2124
bot.sortReplies();
2225
}
2326

2427
function brainError() {
25-
console.log('Chatbot error!');
28+
console.log("Chatbot error!");
2629
}
2730

2831
// let button = select('#submit');
@@ -31,13 +34,14 @@ function setup() {
3134

3235
// button.mousePressed(chat);
3336

34-
function gotSpeech() {
37+
// Using async and await
38+
async function gotSpeech() {
3539
if (speechRec.resultValue) {
3640
let input = speechRec.resultString;
37-
//user_input.value(input);
38-
let reply = bot.reply('local-user', input);
41+
// user_input.value(input);
42+
let reply = await bot.reply("local-user", input);
3943
speech.speak(reply);
40-
//output.html(reply);
44+
// output.html(reply);
4145
}
4246
}
4347

0 commit comments

Comments
 (0)