Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -75,41 +75,44 @@ html_code = """
|
|
| 75 |
startListening();
|
| 76 |
}
|
| 77 |
|
| 78 |
-
function startListening() {
|
| 79 |
-
navigator.mediaDevices.getUserMedia({ audio: true })
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
isListening = false;
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
status.textContent = 'Microphone access denied.';
|
| 111 |
-
isListening = false;
|
| 112 |
-
});
|
| 113 |
}
|
| 114 |
</script>
|
| 115 |
</body>
|
|
|
|
| 75 |
startListening();
|
| 76 |
}
|
| 77 |
|
| 78 |
+
async function startListening() {
|
| 79 |
+
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
| 80 |
+
const mediaRecorder = new MediaRecorder(stream, { mimeType: 'audio/webm;codecs=opus' });
|
| 81 |
+
const audioChunks = [];
|
| 82 |
+
|
| 83 |
+
mediaRecorder.ondataavailable = event => audioChunks.push(event.data);
|
| 84 |
+
mediaRecorder.onstop = async () => {
|
| 85 |
+
const audioBlob = new Blob(audioChunks, { type: 'audio/webm' });
|
| 86 |
+
const formData = new FormData();
|
| 87 |
+
formData.append('audio', audioBlob);
|
| 88 |
+
|
| 89 |
+
status.textContent = 'Processing...';
|
| 90 |
+
try {
|
| 91 |
+
const result = await fetch('/process-audio', { method: 'POST', body: formData });
|
| 92 |
+
const data = await result.json();
|
| 93 |
+
response.textContent = data.response;
|
| 94 |
+
response.style.display = 'block';
|
| 95 |
+
|
| 96 |
+
const utterance = new SpeechSynthesisUtterance(data.response);
|
| 97 |
+
speechSynthesis.speak(utterance);
|
| 98 |
+
utterance.onend = () => {
|
| 99 |
+
if (!data.response.includes("Goodbye") && !data.response.includes("final order")) {
|
| 100 |
+
status.textContent = 'Listening...';
|
| 101 |
+
startListening(); // Continue listening after response
|
| 102 |
+
} else {
|
| 103 |
+
status.textContent = 'Conversation ended.';
|
| 104 |
isListening = false;
|
| 105 |
+
}
|
| 106 |
+
};
|
| 107 |
+
} catch (error) {
|
| 108 |
+
response.textContent = 'Error processing your request. Please try again.';
|
| 109 |
+
status.textContent = 'Press the mic button to restart.';
|
| 110 |
+
isListening = false;
|
| 111 |
+
}
|
| 112 |
+
};
|
| 113 |
+
|
| 114 |
+
mediaRecorder.start();
|
| 115 |
+
setTimeout(() => mediaRecorder.stop(), 5000); // Stop recording after 5 seconds
|
|
|
|
|
|
|
|
|
|
| 116 |
}
|
| 117 |
</script>
|
| 118 |
</body>
|