Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -104,42 +104,79 @@ def transcribe_and_chat(audio):
|
|
104 |
return response, audio_path
|
105 |
|
106 |
def create_demo():
|
107 |
-
with gr.Blocks() as demo:
|
108 |
gr.Markdown(
|
109 |
"""
|
110 |
-
#
|
111 |
-
Welcome to your personal voice assistant!
|
|
|
112 |
"""
|
113 |
)
|
114 |
|
115 |
with gr.Row():
|
116 |
with gr.Column(scale=1):
|
117 |
-
audio_input = gr.Audio(
|
118 |
-
|
119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
120 |
|
121 |
with gr.Column(scale=1):
|
122 |
-
chat_output = gr.
|
123 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
124 |
|
125 |
-
|
126 |
-
|
|
|
127 |
|
128 |
# Processing the audio input
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
141 |
|
142 |
-
# JavaScript to handle autoplay
|
143 |
demo.load(None, js="""
|
144 |
function() {
|
145 |
var recordButton;
|
@@ -163,24 +200,17 @@ def create_demo():
|
|
163 |
}
|
164 |
}
|
165 |
|
166 |
-
document.querySelector("audio").addEventListener("ended", function() {
|
167 |
setTimeout(startListening, 500);
|
168 |
});
|
169 |
|
170 |
function playAssistantAudio() {
|
171 |
-
var
|
172 |
-
if (
|
173 |
-
|
174 |
-
if (assistantAudio) {
|
175 |
-
assistantAudio.play();
|
176 |
-
}
|
177 |
}
|
178 |
}
|
179 |
|
180 |
-
document.addEventListener('gradioAudioLoaded', function(event) {
|
181 |
-
playAssistantAudio();
|
182 |
-
});
|
183 |
-
|
184 |
document.addEventListener('gradioUpdated', function(event) {
|
185 |
setTimeout(playAssistantAudio, 100);
|
186 |
});
|
|
|
104 |
return response, audio_path
|
105 |
|
106 |
def create_demo():
|
107 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
108 |
gr.Markdown(
|
109 |
"""
|
110 |
+
# ποΈ AI Voice Assistant
|
111 |
+
Welcome to your personal voice assistant! Have a natural conversation with an AI-powered assistant.
|
112 |
+
Simply record your voice, and I'll respond with both text and speech.
|
113 |
"""
|
114 |
)
|
115 |
|
116 |
with gr.Row():
|
117 |
with gr.Column(scale=1):
|
118 |
+
audio_input = gr.Audio(
|
119 |
+
type="filepath",
|
120 |
+
label="π€ Speak your message",
|
121 |
+
elem_id="audio-input",
|
122 |
+
source="microphone"
|
123 |
+
)
|
124 |
+
voice_volume = gr.Slider(
|
125 |
+
minimum=0,
|
126 |
+
maximum=2,
|
127 |
+
value=1,
|
128 |
+
step=0.1,
|
129 |
+
label="π Assistant Voice Volume",
|
130 |
+
elem_id="voice-volume"
|
131 |
+
)
|
132 |
|
133 |
with gr.Column(scale=1):
|
134 |
+
chat_output = gr.Chatbot(
|
135 |
+
label="Conversation",
|
136 |
+
elem_id="chat-output",
|
137 |
+
height=400
|
138 |
+
)
|
139 |
+
audio_output = gr.Audio(
|
140 |
+
label="π AI Voice Response",
|
141 |
+
autoplay=True,
|
142 |
+
elem_id="audio-output"
|
143 |
+
)
|
144 |
|
145 |
+
with gr.Row():
|
146 |
+
clear_button = gr.Button("ποΈ Clear Chat", variant="secondary", elem_id="clear-button")
|
147 |
+
submit_button = gr.Button("π Submit", variant="primary", elem_id="submit-button")
|
148 |
|
149 |
# Processing the audio input
|
150 |
+
def process_audio(audio, volume, history):
|
151 |
logging.info(f"Received audio: {audio}")
|
152 |
if audio is None:
|
153 |
+
return history + [("No audio detected. Please try recording again.", None)], None
|
154 |
+
|
155 |
+
transcribed_text = whisper_speech_to_text(audio)
|
156 |
+
if not transcribed_text:
|
157 |
+
return history + [("Sorry, I couldn't understand the audio. Please try again.", None)], None
|
158 |
+
|
159 |
+
response, audio_path = asyncio.run(chat_with_ai(transcribed_text))
|
160 |
+
|
161 |
# Adjust volume for the response audio
|
162 |
adjusted_audio_path = asyncio.run(text_to_speech_stream(response, volume))
|
163 |
logging.info(f"Response: {response}, Audio path: {adjusted_audio_path}")
|
164 |
+
|
165 |
+
new_history = history + [(transcribed_text, response)]
|
166 |
+
return new_history, adjusted_audio_path
|
167 |
+
|
168 |
+
submit_button.click(
|
169 |
+
process_audio,
|
170 |
+
inputs=[audio_input, voice_volume, chat_output],
|
171 |
+
outputs=[chat_output, audio_output]
|
172 |
+
)
|
173 |
+
|
174 |
+
clear_button.click(
|
175 |
+
lambda: ([], None),
|
176 |
+
outputs=[chat_output, audio_output]
|
177 |
+
)
|
178 |
|
179 |
+
# JavaScript to handle autoplay and auto-listen
|
180 |
demo.load(None, js="""
|
181 |
function() {
|
182 |
var recordButton;
|
|
|
200 |
}
|
201 |
}
|
202 |
|
203 |
+
document.querySelector("#audio-output").addEventListener("ended", function() {
|
204 |
setTimeout(startListening, 500);
|
205 |
});
|
206 |
|
207 |
function playAssistantAudio() {
|
208 |
+
var audioElement = document.querySelector('#audio-output audio');
|
209 |
+
if (audioElement) {
|
210 |
+
audioElement.play();
|
|
|
|
|
|
|
211 |
}
|
212 |
}
|
213 |
|
|
|
|
|
|
|
|
|
214 |
document.addEventListener('gradioUpdated', function(event) {
|
215 |
setTimeout(playAssistantAudio, 100);
|
216 |
});
|