geethareddy commited on
Commit
e9eac4a
·
verified ·
1 Parent(s): eca5014

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -34
app.py CHANGED
@@ -75,41 +75,44 @@ html_code = """
75
  startListening();
76
  }
77
 
78
- function startListening() {
79
- navigator.mediaDevices.getUserMedia({ audio: true }).then(stream => {
80
- const mediaRecorder = new MediaRecorder(stream, { mimeType: 'audio/webm;codecs=opus' });
81
- const audioChunks = [];
82
- mediaRecorder.ondataavailable = event => audioChunks.push(event.data);
83
- mediaRecorder.onstop = async () => {
84
- const audioBlob = new Blob(audioChunks, { type: 'audio/webm' });
85
- const formData = new FormData();
86
- formData.append('audio', audioBlob);
87
-
88
- status.textContent = 'Processing...';
89
- try {
90
- const result = await fetch('/process-audio', { method: 'POST', body: formData });
91
- const data = await result.json();
92
- response.textContent = data.response;
93
- response.style.display = 'block';
94
-
95
- const utterance = new SpeechSynthesisUtterance(data.response);
96
- speechSynthesis.speak(utterance);
97
- utterance.onend = () => {
 
 
 
 
 
 
98
  isListening = false;
99
- status.textContent = 'Press the mic button to continue ordering.';
100
- };
101
- } catch (error) {
102
- response.textContent = 'Error processing your request. Please try again.';
103
- status.textContent = 'Press the mic button to restart.';
104
- isListening = false;
105
- }
106
- };
107
- setTimeout(() => mediaRecorder.stop(), 5000);
108
- mediaRecorder.start();
109
- }).catch(() => {
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>