dschandra commited on
Commit
283764f
·
verified ·
1 Parent(s): 0347f6c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -8
app.py CHANGED
@@ -84,13 +84,24 @@ html_code = """
84
  const response = document.getElementById('response');
85
  let mediaRecorder;
86
  let audioChunks = [];
 
87
 
88
  micButton.addEventListener('click', () => {
 
 
 
 
 
 
 
 
 
 
 
 
89
  navigator.mediaDevices.getUserMedia({ audio: true }).then(stream => {
90
  mediaRecorder = new MediaRecorder(stream, { mimeType: 'audio/webm;codecs=opus' });
91
  mediaRecorder.start();
92
- status.textContent = 'Listening...';
93
- status.classList.add('listening');
94
  audioChunks = [];
95
  mediaRecorder.ondataavailable = event => audioChunks.push(event.data);
96
  mediaRecorder.onstop = async () => {
@@ -99,28 +110,35 @@ html_code = """
99
  formData.append('audio', audioBlob);
100
 
101
  status.textContent = 'Processing...';
102
- status.classList.remove('listening');
103
  try {
104
  const result = await fetch('/process-audio', { method: 'POST', body: formData });
105
  const data = await result.json();
106
  response.textContent = data.response;
107
  response.style.display = 'block';
108
- status.textContent = 'Press the mic button to continue...';
109
- // Speak the response
110
  const utterance = new SpeechSynthesisUtterance(data.response);
111
  speechSynthesis.speak(utterance);
 
112
  if (data.response.includes("Goodbye")) {
113
  status.textContent = 'Conversation ended. Press the mic button to start again.';
 
 
 
 
114
  }
115
  } catch (error) {
116
  response.textContent = 'Error occurred. Please try again.';
117
  response.style.display = 'block';
118
- status.textContent = 'Press the mic button to try again.';
 
119
  }
120
  };
121
  setTimeout(() => mediaRecorder.stop(), 5000); // Stop recording after 5 seconds
122
- }).catch(() => status.textContent = 'Microphone access denied.');
123
- });
 
 
 
124
  </script>
125
  </body>
126
  </html>
 
84
  const response = document.getElementById('response');
85
  let mediaRecorder;
86
  let audioChunks = [];
87
+ let isConversationActive = false;
88
 
89
  micButton.addEventListener('click', () => {
90
+ if (!isConversationActive) {
91
+ isConversationActive = true;
92
+ startConversation();
93
+ }
94
+ });
95
+
96
+ function startConversation() {
97
+ status.textContent = 'Listening...';
98
+ startListening();
99
+ }
100
+
101
+ function startListening() {
102
  navigator.mediaDevices.getUserMedia({ audio: true }).then(stream => {
103
  mediaRecorder = new MediaRecorder(stream, { mimeType: 'audio/webm;codecs=opus' });
104
  mediaRecorder.start();
 
 
105
  audioChunks = [];
106
  mediaRecorder.ondataavailable = event => audioChunks.push(event.data);
107
  mediaRecorder.onstop = async () => {
 
110
  formData.append('audio', audioBlob);
111
 
112
  status.textContent = 'Processing...';
 
113
  try {
114
  const result = await fetch('/process-audio', { method: 'POST', body: formData });
115
  const data = await result.json();
116
  response.textContent = data.response;
117
  response.style.display = 'block';
118
+
 
119
  const utterance = new SpeechSynthesisUtterance(data.response);
120
  speechSynthesis.speak(utterance);
121
+
122
  if (data.response.includes("Goodbye")) {
123
  status.textContent = 'Conversation ended. Press the mic button to start again.';
124
+ isConversationActive = false;
125
+ } else {
126
+ status.textContent = 'Listening...';
127
+ setTimeout(startListening, 1000); // Continue listening
128
  }
129
  } catch (error) {
130
  response.textContent = 'Error occurred. Please try again.';
131
  response.style.display = 'block';
132
+ status.textContent = 'Press the mic button to restart the conversation.';
133
+ isConversationActive = false;
134
  }
135
  };
136
  setTimeout(() => mediaRecorder.stop(), 5000); // Stop recording after 5 seconds
137
+ }).catch(() => {
138
+ status.textContent = 'Microphone access denied.';
139
+ isConversationActive = false;
140
+ });
141
+ }
142
  </script>
143
  </body>
144
  </html>