Spaces:
Sleeping
Sleeping
Update templates/index.html
Browse files- templates/index.html +25 -40
templates/index.html
CHANGED
|
@@ -189,7 +189,7 @@
|
|
| 189 |
</div>
|
| 190 |
|
| 191 |
<div id="audio-recording-container">
|
| 192 |
-
<button id="audio-record-button"
|
| 193 |
</div>
|
| 194 |
|
| 195 |
<script>
|
|
@@ -259,58 +259,43 @@
|
|
| 259 |
document.getElementById('menu-content').style.display = 'none';
|
| 260 |
}
|
| 261 |
|
| 262 |
-
let isRecording = false;
|
| 263 |
let mediaRecorder;
|
| 264 |
let audioChunks = [];
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
if (isRecording) {
|
| 270 |
-
// Stop recording
|
| 271 |
mediaRecorder.stop();
|
| 272 |
-
recordButton.
|
| 273 |
-
recordButton.
|
| 274 |
-
isRecording = false;
|
| 275 |
} else {
|
| 276 |
-
// Start recording
|
| 277 |
navigator.mediaDevices.getUserMedia({ audio: true })
|
| 278 |
.then(stream => {
|
| 279 |
mediaRecorder = new MediaRecorder(stream);
|
| 280 |
mediaRecorder.start();
|
| 281 |
-
mediaRecorder.
|
| 282 |
audioChunks.push(event.data);
|
| 283 |
-
};
|
| 284 |
-
mediaRecorder.
|
| 285 |
const audioBlob = new Blob(audioChunks, { type: 'audio/wav' });
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 293 |
});
|
| 294 |
}
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
function sendAudioToServer(audioBlob) {
|
| 298 |
-
const formData = new FormData();
|
| 299 |
-
formData.append('audio', audioBlob);
|
| 300 |
-
|
| 301 |
-
fetch('/process_audio', {
|
| 302 |
-
method: 'POST',
|
| 303 |
-
body: formData
|
| 304 |
-
})
|
| 305 |
-
.then(response => response.text())
|
| 306 |
-
.then(text => {
|
| 307 |
-
console.log("Transcribed Text: ", text);
|
| 308 |
-
// You can display the transcribed text in the chat if needed
|
| 309 |
-
})
|
| 310 |
-
.catch(error => {
|
| 311 |
-
console.error("Error:", error);
|
| 312 |
-
});
|
| 313 |
-
}
|
| 314 |
|
| 315 |
</script>
|
| 316 |
</body>
|
|
|
|
| 189 |
</div>
|
| 190 |
|
| 191 |
<div id="audio-recording-container">
|
| 192 |
+
<button id="audio-record-button">Start</button>
|
| 193 |
</div>
|
| 194 |
|
| 195 |
<script>
|
|
|
|
| 259 |
document.getElementById('menu-content').style.display = 'none';
|
| 260 |
}
|
| 261 |
|
|
|
|
| 262 |
let mediaRecorder;
|
| 263 |
let audioChunks = [];
|
| 264 |
+
const recordButton = document.getElementById('audio-record-button');
|
| 265 |
+
let isRecording = false;
|
| 266 |
+
recordButton.addEventListener('click', () => {
|
|
|
|
| 267 |
if (isRecording) {
|
|
|
|
| 268 |
mediaRecorder.stop();
|
| 269 |
+
recordButton.textContent = 'Start Recording';
|
| 270 |
+
recordButton.classList.remove('recording');
|
|
|
|
| 271 |
} else {
|
|
|
|
| 272 |
navigator.mediaDevices.getUserMedia({ audio: true })
|
| 273 |
.then(stream => {
|
| 274 |
mediaRecorder = new MediaRecorder(stream);
|
| 275 |
mediaRecorder.start();
|
| 276 |
+
mediaRecorder.addEventListener('dataavailable', event => {
|
| 277 |
audioChunks.push(event.data);
|
| 278 |
+
});
|
| 279 |
+
mediaRecorder.addEventListener('stop', () => {
|
| 280 |
const audioBlob = new Blob(audioChunks, { type: 'audio/wav' });
|
| 281 |
+
const formData = new FormData();
|
| 282 |
+
formData.append('file', audioBlob, 'audio.wav');
|
| 283 |
+
fetch('/upload', {
|
| 284 |
+
method: 'POST',
|
| 285 |
+
body: formData
|
| 286 |
+
})
|
| 287 |
+
.then(response => response.json())
|
| 288 |
+
.catch(error => {
|
| 289 |
+
console.error('Error:', error);
|
| 290 |
+
});
|
| 291 |
+
audioChunks = [];
|
| 292 |
+
});
|
| 293 |
+
recordButton.textContent = 'Stop Recording';
|
| 294 |
+
recordButton.classList.add('recording');
|
| 295 |
});
|
| 296 |
}
|
| 297 |
+
isRecording = !isRecording;
|
| 298 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 299 |
|
| 300 |
</script>
|
| 301 |
</body>
|