Spaces:
Running
Running
Update index.html
Browse files- index.html +8 -13
index.html
CHANGED
@@ -79,6 +79,7 @@
|
|
79 |
|
80 |
<script>
|
81 |
let recognition = null;
|
|
|
82 |
|
83 |
async function query(data) {
|
84 |
const response = await fetch(
|
@@ -92,8 +93,7 @@
|
|
92 |
}
|
93 |
);
|
94 |
const result = await response.json();
|
95 |
-
|
96 |
-
displayStatus(`API Response: ${result.answer || "No response"}`);
|
97 |
}
|
98 |
|
99 |
function displayStatus(message, isError = false) {
|
@@ -113,7 +113,7 @@
|
|
113 |
const newRecognition = new SpeechRecognition();
|
114 |
|
115 |
newRecognition.continuous = true;
|
116 |
-
newRecognition.interimResults =
|
117 |
newRecognition.maxAlternatives = 1;
|
118 |
|
119 |
newRecognition.lang = 'en-US';
|
@@ -132,23 +132,18 @@
|
|
132 |
displayStatus('Listening for command...');
|
133 |
|
134 |
recognition.onresult = async function(event) {
|
|
|
135 |
const result = event.results[event.results.length - 1][0].transcript.toLowerCase();
|
136 |
document.getElementById('result').innerText = result;
|
137 |
-
};
|
138 |
-
|
139 |
-
recognition.onspeechend = async function() {
|
140 |
-
recognition.stop();
|
141 |
-
};
|
142 |
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
};
|
148 |
|
149 |
recognition.onerror = function(event) {
|
150 |
displayStatus(`Recognition Error: ${event.error}`, true);
|
151 |
-
startCommandRecognition(); // Restart recognition after error
|
152 |
};
|
153 |
|
154 |
recognition.start();
|
|
|
79 |
|
80 |
<script>
|
81 |
let recognition = null;
|
82 |
+
let silenceTimeout = null;
|
83 |
|
84 |
async function query(data) {
|
85 |
const response = await fetch(
|
|
|
93 |
}
|
94 |
);
|
95 |
const result = await response.json();
|
96 |
+
return result;
|
|
|
97 |
}
|
98 |
|
99 |
function displayStatus(message, isError = false) {
|
|
|
113 |
const newRecognition = new SpeechRecognition();
|
114 |
|
115 |
newRecognition.continuous = true;
|
116 |
+
newRecognition.interimResults = true;
|
117 |
newRecognition.maxAlternatives = 1;
|
118 |
|
119 |
newRecognition.lang = 'en-US';
|
|
|
132 |
displayStatus('Listening for command...');
|
133 |
|
134 |
recognition.onresult = async function(event) {
|
135 |
+
clearTimeout(silenceTimeout);
|
136 |
const result = event.results[event.results.length - 1][0].transcript.toLowerCase();
|
137 |
document.getElementById('result').innerText = result;
|
|
|
|
|
|
|
|
|
|
|
138 |
|
139 |
+
silenceTimeout = setTimeout(async () => {
|
140 |
+
const apiResponse = await query({"question": result});
|
141 |
+
console.log(apiResponse);
|
142 |
+
}, 2000);
|
143 |
};
|
144 |
|
145 |
recognition.onerror = function(event) {
|
146 |
displayStatus(`Recognition Error: ${event.error}`, true);
|
|
|
147 |
};
|
148 |
|
149 |
recognition.start();
|