Spaces:
Sleeping
Sleeping
Update static/script.js
Browse files- static/script.js +33 -17
static/script.js
CHANGED
|
@@ -1,8 +1,11 @@
|
|
| 1 |
-
window.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
if (!window.SpeechRecognition) {
|
| 4 |
-
alert("Your browser does not support Speech Recognition. Please use Chrome.");
|
| 5 |
-
} else {
|
| 6 |
const recognition = new SpeechRecognition();
|
| 7 |
recognition.continuous = true;
|
| 8 |
recognition.lang = "en-US";
|
|
@@ -10,17 +13,25 @@ if (!window.SpeechRecognition) {
|
|
| 10 |
|
| 11 |
function startListening() {
|
| 12 |
recognition.start();
|
| 13 |
-
document.getElementById("status")
|
|
|
|
|
|
|
|
|
|
| 14 |
}
|
| 15 |
|
| 16 |
-
recognition.onresult = function(event) {
|
| 17 |
let transcript = event.results[event.results.length - 1][0].transcript.toLowerCase();
|
| 18 |
console.log("You said:", transcript);
|
| 19 |
|
| 20 |
if (!listeningForCommands && transcript.includes("hi biriyani hub")) {
|
| 21 |
console.log("Wake word detected! Now listening for commands...");
|
| 22 |
listeningForCommands = true;
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
speak("Hello! What can I do for you?");
|
| 25 |
} else if (listeningForCommands) {
|
| 26 |
console.log("Command received:", transcript);
|
|
@@ -28,7 +39,7 @@ if (!window.SpeechRecognition) {
|
|
| 28 |
}
|
| 29 |
};
|
| 30 |
|
| 31 |
-
recognition.onerror = function(event) {
|
| 32 |
console.error("Speech recognition error:", event.error);
|
| 33 |
};
|
| 34 |
|
|
@@ -40,14 +51,18 @@ if (!window.SpeechRecognition) {
|
|
| 40 |
},
|
| 41 |
body: JSON.stringify({ command: command }),
|
| 42 |
})
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
}
|
| 52 |
|
| 53 |
function speak(text) {
|
|
@@ -58,4 +73,5 @@ if (!window.SpeechRecognition) {
|
|
| 58 |
}
|
| 59 |
|
| 60 |
startListening();
|
| 61 |
-
}
|
|
|
|
|
|
| 1 |
+
window.onload = function () {
|
| 2 |
+
window.SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
|
| 3 |
+
|
| 4 |
+
if (!window.SpeechRecognition) {
|
| 5 |
+
alert("Your browser does not support Speech Recognition. Please use Chrome.");
|
| 6 |
+
return;
|
| 7 |
+
}
|
| 8 |
|
|
|
|
|
|
|
|
|
|
| 9 |
const recognition = new SpeechRecognition();
|
| 10 |
recognition.continuous = true;
|
| 11 |
recognition.lang = "en-US";
|
|
|
|
| 13 |
|
| 14 |
function startListening() {
|
| 15 |
recognition.start();
|
| 16 |
+
let statusElement = document.getElementById("status");
|
| 17 |
+
if (statusElement) {
|
| 18 |
+
statusElement.innerText = "Listening...";
|
| 19 |
+
}
|
| 20 |
}
|
| 21 |
|
| 22 |
+
recognition.onresult = function (event) {
|
| 23 |
let transcript = event.results[event.results.length - 1][0].transcript.toLowerCase();
|
| 24 |
console.log("You said:", transcript);
|
| 25 |
|
| 26 |
if (!listeningForCommands && transcript.includes("hi biriyani hub")) {
|
| 27 |
console.log("Wake word detected! Now listening for commands...");
|
| 28 |
listeningForCommands = true;
|
| 29 |
+
|
| 30 |
+
let statusElement = document.getElementById("status");
|
| 31 |
+
if (statusElement) {
|
| 32 |
+
statusElement.innerText = "Listening for command...";
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
speak("Hello! What can I do for you?");
|
| 36 |
} else if (listeningForCommands) {
|
| 37 |
console.log("Command received:", transcript);
|
|
|
|
| 39 |
}
|
| 40 |
};
|
| 41 |
|
| 42 |
+
recognition.onerror = function (event) {
|
| 43 |
console.error("Speech recognition error:", event.error);
|
| 44 |
};
|
| 45 |
|
|
|
|
| 51 |
},
|
| 52 |
body: JSON.stringify({ command: command }),
|
| 53 |
})
|
| 54 |
+
.then(response => response.json())
|
| 55 |
+
.then(data => {
|
| 56 |
+
console.log("Server response:", data.response);
|
| 57 |
+
speak(data.response);
|
| 58 |
+
listeningForCommands = false;
|
| 59 |
+
|
| 60 |
+
let statusElement = document.getElementById("status");
|
| 61 |
+
if (statusElement) {
|
| 62 |
+
statusElement.innerText = "Listening for wake word...";
|
| 63 |
+
}
|
| 64 |
+
})
|
| 65 |
+
.catch(error => console.error("Error sending command:", error));
|
| 66 |
}
|
| 67 |
|
| 68 |
function speak(text) {
|
|
|
|
| 73 |
}
|
| 74 |
|
| 75 |
startListening();
|
| 76 |
+
};
|
| 77 |
+
|