dschandra commited on
Commit
717f735
·
verified ·
1 Parent(s): f73965a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -1
app.py CHANGED
@@ -89,32 +89,42 @@ html_code = """
89
  let mediaRecorder;
90
  let audioChunks = [];
91
  let isConversationActive = false;
 
92
  micButton.addEventListener('click', () => {
93
  if (!isConversationActive) {
94
  isConversationActive = true;
95
  startConversation();
96
  }
97
  });
 
98
  function startConversation() {
 
99
  status.textContent = 'Listening...';
100
  startListening();
101
  }
 
102
  function startListening() {
103
  navigator.mediaDevices.getUserMedia({ audio: true }).then(stream => {
104
  mediaRecorder = new MediaRecorder(stream, { mimeType: 'audio/webm;codecs=opus' });
105
  mediaRecorder.start();
106
  audioChunks = [];
 
107
  mediaRecorder.ondataavailable = event => audioChunks.push(event.data);
108
  mediaRecorder.onstop = async () => {
 
 
 
109
  const audioBlob = new Blob(audioChunks, { type: 'audio/webm' });
110
  const formData = new FormData();
111
  formData.append('audio', audioBlob);
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
  try {
119
  const utterance = new SpeechSynthesisUtterance(data.response);
120
  speechSynthesis.speak(utterance);
@@ -129,6 +139,8 @@ html_code = """
129
  console.error("Speech synthesis not supported or failed:", speechError);
130
  response.textContent = "Speech output unavailable. Please check your browser.";
131
  }
 
 
132
  if (data.response.includes("Goodbye")) {
133
  status.textContent = 'Conversation ended. Press the mic button to start again.';
134
  isConversationActive = false;
 
89
  let mediaRecorder;
90
  let audioChunks = [];
91
  let isConversationActive = false;
92
+
93
  micButton.addEventListener('click', () => {
94
  if (!isConversationActive) {
95
  isConversationActive = true;
96
  startConversation();
97
  }
98
  });
99
+
100
  function startConversation() {
101
+ // Show "Listening..." before recording
102
  status.textContent = 'Listening...';
103
  startListening();
104
  }
105
+
106
  function startListening() {
107
  navigator.mediaDevices.getUserMedia({ audio: true }).then(stream => {
108
  mediaRecorder = new MediaRecorder(stream, { mimeType: 'audio/webm;codecs=opus' });
109
  mediaRecorder.start();
110
  audioChunks = [];
111
+
112
  mediaRecorder.ondataavailable = event => audioChunks.push(event.data);
113
  mediaRecorder.onstop = async () => {
114
+ // Once the recording stops, update status to "Processing..."
115
+ status.textContent = 'Processing...';
116
+
117
  const audioBlob = new Blob(audioChunks, { type: 'audio/webm' });
118
  const formData = new FormData();
119
  formData.append('audio', audioBlob);
120
+
121
  try {
122
  const result = await fetch('/process-audio', { method: 'POST', body: formData });
123
  const data = await result.json();
124
  response.textContent = data.response;
125
  response.style.display = 'block';
126
+
127
+ // Handle Speech synthesis
128
  try {
129
  const utterance = new SpeechSynthesisUtterance(data.response);
130
  speechSynthesis.speak(utterance);
 
139
  console.error("Speech synthesis not supported or failed:", speechError);
140
  response.textContent = "Speech output unavailable. Please check your browser.";
141
  }
142
+
143
+ // Continue the conversation after a short delay
144
  if (data.response.includes("Goodbye")) {
145
  status.textContent = 'Conversation ended. Press the mic button to start again.';
146
  isConversationActive = false;