ar08 commited on
Commit
e255e64
Β·
verified Β·
1 Parent(s): 4acb3ed

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -40
app.py CHANGED
@@ -120,7 +120,7 @@ def create_demo():
120
 
121
  with gr.Column(scale=1):
122
  chat_output = gr.Textbox(label="πŸ’¬ AI Response", elem_id="chat-output", lines=5, interactive=False)
123
- audio_output = gr.Audio(label="πŸ”Š AI Voice Response", elem_id="audio-output")
124
 
125
  # Add some spacing and a divider
126
  gr.Markdown("---")
@@ -129,63 +129,39 @@ def create_demo():
129
  def process_audio(audio, volume):
130
  logging.info(f"Received audio: {audio}")
131
  if audio is None:
132
- return "No audio detected. Please try recording again.", None
133
  response, audio_path = transcribe_and_chat(audio)
134
  # Adjust volume for the response audio
135
  adjusted_audio_path = asyncio.run(text_to_speech_stream(response, volume))
136
  logging.info(f"Response: {response}, Audio path: {adjusted_audio_path}")
137
- return response, adjusted_audio_path
138
 
139
- audio_input.change(process_audio, inputs=[audio_input, voice_volume], outputs=[chat_output, audio_output])
140
  clear_button.click(lambda: (None, None, None), None, [chat_output, audio_output, audio_input])
141
 
142
- # JavaScript to handle autoplay and automatic clearing
143
  demo.load(None, js="""
144
  function() {
145
- let isPlaying = false;
146
-
 
 
 
 
147
  function playAssistantAudio() {
148
  var audioElements = document.querySelectorAll('audio');
149
  if (audioElements.length > 1) {
150
  var assistantAudio = audioElements[1];
151
  if (assistantAudio) {
152
- isPlaying = true;
153
  assistantAudio.play();
154
- assistantAudio.onended = function() {
155
- isPlaying = false;
156
- clearAllInputs();
157
- };
158
  }
159
  }
160
  }
161
 
162
- function clearAllInputs() {
163
- if (!isPlaying) {
164
- document.querySelector('#clear-button').click();
165
- }
166
- }
167
-
168
- // Automatically play the assistant's audio when it's loaded
169
- const audioOutputObserver = new MutationObserver((mutations) => {
170
- mutations.forEach((mutation) => {
171
- if (mutation.type === 'childList') {
172
- const addedNodes = mutation.addedNodes;
173
- addedNodes.forEach((node) => {
174
- if (node.tagName === 'AUDIO') {
175
- setTimeout(() => {
176
- node.play();
177
- }, 100);
178
- }
179
- });
180
- }
181
- });
182
  });
183
 
184
- const audioOutputElement = document.querySelector('#audio-output');
185
- if (audioOutputElement) {
186
- audioOutputObserver.observe(audioOutputElement, { childList: true, subtree: true });
187
- }
188
-
189
  document.addEventListener('gradioUpdated', function(event) {
190
  setTimeout(playAssistantAudio, 100);
191
  });
@@ -194,9 +170,7 @@ def create_demo():
194
  document.addEventListener("visibilitychange", function() {
195
  var audioElements = document.querySelectorAll('audio');
196
  audioElements.forEach(function(audio) {
197
- if (isPlaying) {
198
- audio.play();
199
- }
200
  });
201
  });
202
  }
 
120
 
121
  with gr.Column(scale=1):
122
  chat_output = gr.Textbox(label="πŸ’¬ AI Response", elem_id="chat-output", lines=5, interactive=False)
123
+ audio_output = gr.Audio(label="πŸ”Š AI Voice Response", autoplay=True, elem_id="audio-output")
124
 
125
  # Add some spacing and a divider
126
  gr.Markdown("---")
 
129
  def process_audio(audio, volume):
130
  logging.info(f"Received audio: {audio}")
131
  if audio is None:
132
+ return "No audio detected. Please try recording again.", None, None
133
  response, audio_path = transcribe_and_chat(audio)
134
  # Adjust volume for the response audio
135
  adjusted_audio_path = asyncio.run(text_to_speech_stream(response, volume))
136
  logging.info(f"Response: {response}, Audio path: {adjusted_audio_path}")
137
+ return response, adjusted_audio_path, None # Return None to clear the audio input
138
 
139
+ audio_input.change(process_audio, inputs=[audio_input, voice_volume], outputs=[chat_output, audio_output, audio_input])
140
  clear_button.click(lambda: (None, None, None), None, [chat_output, audio_output, audio_input])
141
 
142
+ # JavaScript to handle autoplay and automatic submission
143
  demo.load(None, js="""
144
  function() {
145
+ document.querySelector("audio").addEventListener("stop", function() {
146
+ setTimeout(function() {
147
+ document.querySelector('button[title="Submit"]').click();
148
+ }, 500);
149
+ });
150
+
151
  function playAssistantAudio() {
152
  var audioElements = document.querySelectorAll('audio');
153
  if (audioElements.length > 1) {
154
  var assistantAudio = audioElements[1];
155
  if (assistantAudio) {
 
156
  assistantAudio.play();
 
 
 
 
157
  }
158
  }
159
  }
160
 
161
+ document.addEventListener('gradioAudioLoaded', function(event) {
162
+ playAssistantAudio();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
163
  });
164
 
 
 
 
 
 
165
  document.addEventListener('gradioUpdated', function(event) {
166
  setTimeout(playAssistantAudio, 100);
167
  });
 
170
  document.addEventListener("visibilitychange", function() {
171
  var audioElements = document.querySelectorAll('audio');
172
  audioElements.forEach(function(audio) {
173
+ audio.play();
 
 
174
  });
175
  });
176
  }