Spaces:
Sleeping
Sleeping
Update templates/index.html
Browse files- templates/index.html +48 -32
templates/index.html
CHANGED
@@ -16,24 +16,44 @@
|
|
16 |
<label for="email">Email</label>
|
17 |
<input type="text" id="email" placeholder="Your email will appear here..." readonly>
|
18 |
|
19 |
-
<
|
20 |
-
🎙️ Click to Speak
|
21 |
-
</button>
|
22 |
|
23 |
-
<
|
24 |
-
|
25 |
-
<audio id="audio-player" controls></audio>
|
26 |
</div>
|
27 |
|
28 |
<script>
|
29 |
-
async function
|
30 |
-
|
31 |
-
|
32 |
-
document.getElementById("audio-player")
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
}
|
35 |
|
36 |
-
async function
|
37 |
let stream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
38 |
let mediaRecorder = new MediaRecorder(stream);
|
39 |
let audioChunks = [];
|
@@ -42,31 +62,27 @@
|
|
42 |
audioChunks.push(event.data);
|
43 |
};
|
44 |
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
|
|
49 |
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
// Prompt for Email after Name
|
59 |
-
setTimeout(() => {
|
60 |
-
let emailPrompt = new Audio("/static/email_prompt.mp3");
|
61 |
-
emailPrompt.play();
|
62 |
-
}, 2000);
|
63 |
-
};
|
64 |
|
65 |
-
|
66 |
-
|
|
|
67 |
}
|
68 |
|
69 |
-
window.onload =
|
70 |
</script>
|
71 |
</body>
|
72 |
</html>
|
|
|
16 |
<label for="email">Email</label>
|
17 |
<input type="text" id="email" placeholder="Your email will appear here..." readonly>
|
18 |
|
19 |
+
<p class="instructions">Voice interaction in progress...</p>
|
|
|
|
|
20 |
|
21 |
+
<audio id="audio-player" autoplay></audio>
|
|
|
|
|
22 |
</div>
|
23 |
|
24 |
<script>
|
25 |
+
async function startVoiceInteraction() {
|
26 |
+
await fetch("/start_interaction");
|
27 |
+
|
28 |
+
let audioPlayer = document.getElementById("audio-player");
|
29 |
+
|
30 |
+
// Step 1: Welcome Message
|
31 |
+
audioPlayer.src = "/static/welcome.mp3";
|
32 |
+
audioPlayer.play();
|
33 |
+
await new Promise(resolve => setTimeout(resolve, 3000));
|
34 |
+
|
35 |
+
// Step 2: Ask for Name
|
36 |
+
audioPlayer.src = "/static/ask_name.mp3";
|
37 |
+
audioPlayer.play();
|
38 |
+
await new Promise(resolve => setTimeout(resolve, 3000));
|
39 |
+
|
40 |
+
let name = await recordAndProcessAudio();
|
41 |
+
document.getElementById("name").value = name;
|
42 |
+
|
43 |
+
// Step 3: Ask for Email
|
44 |
+
audioPlayer.src = "/static/ask_email.mp3";
|
45 |
+
audioPlayer.play();
|
46 |
+
await new Promise(resolve => setTimeout(resolve, 3000));
|
47 |
+
|
48 |
+
let email = await recordAndProcessAudio();
|
49 |
+
document.getElementById("email").value = email;
|
50 |
+
|
51 |
+
// Step 4: Thank You Message
|
52 |
+
audioPlayer.src = "/static/thank_you.mp3";
|
53 |
+
audioPlayer.play();
|
54 |
}
|
55 |
|
56 |
+
async function recordAndProcessAudio() {
|
57 |
let stream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
58 |
let mediaRecorder = new MediaRecorder(stream);
|
59 |
let audioChunks = [];
|
|
|
62 |
audioChunks.push(event.data);
|
63 |
};
|
64 |
|
65 |
+
return new Promise(resolve => {
|
66 |
+
mediaRecorder.onstop = async () => {
|
67 |
+
let audioBlob = new Blob(audioChunks, { type: 'audio/wav' });
|
68 |
+
let formData = new FormData();
|
69 |
+
formData.append("audio", audioBlob, "input.wav");
|
70 |
|
71 |
+
let response = await fetch("/process_audio", {
|
72 |
+
method: "POST",
|
73 |
+
body: formData
|
74 |
+
});
|
75 |
|
76 |
+
let result = await response.json();
|
77 |
+
resolve(result.text || "Error capturing speech");
|
78 |
+
};
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
|
80 |
+
mediaRecorder.start();
|
81 |
+
setTimeout(() => mediaRecorder.stop(), 4000);
|
82 |
+
});
|
83 |
}
|
84 |
|
85 |
+
window.onload = startVoiceInteraction;
|
86 |
</script>
|
87 |
</body>
|
88 |
</html>
|