Spaces:
Runtime error
Runtime error
Update templates/index.html
Browse files- templates/index.html +23 -13
templates/index.html
CHANGED
@@ -19,6 +19,7 @@
|
|
19 |
<p id="status">Initializing...</p>
|
20 |
<audio id="audio-player" autoplay></audio>
|
21 |
</div>
|
|
|
22 |
<script>
|
23 |
async function playAudio(src) {
|
24 |
const audioPlayer = document.getElementById('audio-player');
|
@@ -32,22 +33,31 @@
|
|
32 |
async function recordAudio() {
|
33 |
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
34 |
const mediaRecorder = new MediaRecorder(stream);
|
35 |
-
|
36 |
-
|
37 |
-
mediaRecorder.ondataavailable = event => {
|
38 |
-
audioChunks.push(event.data);
|
39 |
-
};
|
40 |
-
|
41 |
-
mediaRecorder.start();
|
42 |
-
|
43 |
-
await new Promise(resolve => setTimeout(resolve, 4000)); // Record for 4 seconds
|
44 |
-
mediaRecorder.stop();
|
45 |
|
46 |
return new Promise(resolve => {
|
|
|
|
|
|
|
|
|
47 |
mediaRecorder.onstop = () => {
|
48 |
const audioBlob = new Blob(audioChunks, { type: 'audio/wav' });
|
49 |
resolve(audioBlob);
|
50 |
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
});
|
52 |
}
|
53 |
|
@@ -61,7 +71,7 @@
|
|
61 |
});
|
62 |
|
63 |
const result = await response.json();
|
64 |
-
return result.text || '
|
65 |
}
|
66 |
|
67 |
async function startInteraction() {
|
@@ -75,7 +85,7 @@
|
|
75 |
status.textContent = 'Asking for your name...';
|
76 |
await playAudio('{{ url_for("static", filename="ask_name.mp3") }}');
|
77 |
|
78 |
-
status.textContent = '
|
79 |
const nameAudio = await recordAudio();
|
80 |
const nameText = await transcribeAudio(nameAudio);
|
81 |
nameInput.value = nameText;
|
@@ -83,7 +93,7 @@
|
|
83 |
status.textContent = 'Asking for your email...';
|
84 |
await playAudio('{{ url_for("static", filename="ask_email.mp3") }}');
|
85 |
|
86 |
-
status.textContent = '
|
87 |
const emailAudio = await recordAudio();
|
88 |
const emailText = await transcribeAudio(emailAudio);
|
89 |
emailInput.value = emailText;
|
|
|
19 |
<p id="status">Initializing...</p>
|
20 |
<audio id="audio-player" autoplay></audio>
|
21 |
</div>
|
22 |
+
|
23 |
<script>
|
24 |
async function playAudio(src) {
|
25 |
const audioPlayer = document.getElementById('audio-player');
|
|
|
33 |
async function recordAudio() {
|
34 |
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
35 |
const mediaRecorder = new MediaRecorder(stream);
|
36 |
+
let audioChunks = [];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
return new Promise(resolve => {
|
39 |
+
mediaRecorder.ondataavailable = event => {
|
40 |
+
audioChunks.push(event.data);
|
41 |
+
};
|
42 |
+
|
43 |
mediaRecorder.onstop = () => {
|
44 |
const audioBlob = new Blob(audioChunks, { type: 'audio/wav' });
|
45 |
resolve(audioBlob);
|
46 |
};
|
47 |
+
|
48 |
+
mediaRecorder.start();
|
49 |
+
|
50 |
+
// Wait for user to stop speaking (detect silence)
|
51 |
+
let silenceTimeout = setTimeout(() => {
|
52 |
+
mediaRecorder.stop();
|
53 |
+
}, 5000); // Max 5 seconds recording
|
54 |
+
|
55 |
+
navigator.mediaDevices.getUserMedia({ audio: true }).then(stream => {
|
56 |
+
stream.getTracks().forEach(track => {
|
57 |
+
track.onmute = () => clearTimeout(silenceTimeout);
|
58 |
+
track.onunmute = () => silenceTimeout = setTimeout(() => mediaRecorder.stop(), 2000);
|
59 |
+
});
|
60 |
+
});
|
61 |
});
|
62 |
}
|
63 |
|
|
|
71 |
});
|
72 |
|
73 |
const result = await response.json();
|
74 |
+
return result.text || 'Error capturing speech';
|
75 |
}
|
76 |
|
77 |
async function startInteraction() {
|
|
|
85 |
status.textContent = 'Asking for your name...';
|
86 |
await playAudio('{{ url_for("static", filename="ask_name.mp3") }}');
|
87 |
|
88 |
+
status.textContent = 'Listening for your name...';
|
89 |
const nameAudio = await recordAudio();
|
90 |
const nameText = await transcribeAudio(nameAudio);
|
91 |
nameInput.value = nameText;
|
|
|
93 |
status.textContent = 'Asking for your email...';
|
94 |
await playAudio('{{ url_for("static", filename="ask_email.mp3") }}');
|
95 |
|
96 |
+
status.textContent = 'Listening for your email...';
|
97 |
const emailAudio = await recordAudio();
|
98 |
const emailText = await transcribeAudio(emailAudio);
|
99 |
emailInput.value = emailText;
|