Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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",
|
124 |
|
125 |
# Add some spacing and a divider
|
126 |
gr.Markdown("---")
|
@@ -129,35 +129,42 @@ 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
|
143 |
demo.load(None, js="""
|
144 |
function() {
|
145 |
-
|
146 |
-
|
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 |
});
|
@@ -170,7 +177,9 @@ def create_demo():
|
|
170 |
document.addEventListener("visibilitychange", function() {
|
171 |
var audioElements = document.querySelectorAll('audio');
|
172 |
audioElements.forEach(function(audio) {
|
173 |
-
|
|
|
|
|
174 |
});
|
175 |
});
|
176 |
}
|
|
|
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 |
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 |
document.addEventListener('gradioAudioLoaded', function(event) {
|
169 |
playAssistantAudio();
|
170 |
});
|
|
|
177 |
document.addEventListener("visibilitychange", function() {
|
178 |
var audioElements = document.querySelectorAll('audio');
|
179 |
audioElements.forEach(function(audio) {
|
180 |
+
if (isPlaying) {
|
181 |
+
audio.play();
|
182 |
+
}
|
183 |
});
|
184 |
});
|
185 |
}
|