Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -106,18 +106,14 @@ def transcribe_and_chat(audio):
|
|
106 |
def create_demo():
|
107 |
with gr.Blocks(css="""
|
108 |
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;700&display=swap');
|
109 |
-
body { font-family: 'Poppins', sans-serif; margin: 0; padding: 0;
|
110 |
-
#
|
111 |
-
|
112 |
-
#audio-
|
113 |
-
#
|
114 |
-
#
|
115 |
-
|
116 |
-
|
117 |
-
button {font-size: 16px; padding: 10px 20px; border-radius: 5px; cursor: pointer;}
|
118 |
-
button:hover { background-color: #ff6b6b; color: white;}
|
119 |
-
audio {border-radius: 10px; background-color: #9b51e0; padding: 10px;}
|
120 |
-
hr { border: 1px solid #9b51e0; margin-top: 20px; }
|
121 |
footer {display: none;}
|
122 |
@media (max-width: 768px) {
|
123 |
#audio-input, #chat-output, #audio-output { width: 100%; }
|
@@ -126,8 +122,9 @@ def create_demo():
|
|
126 |
""") as demo:
|
127 |
gr.Markdown(
|
128 |
"""
|
129 |
-
<div
|
130 |
-
<p style='text-align:center;'>Talk to your personal AI! Record your voice, and get a response in both text and audio.</p>
|
|
|
131 |
""",
|
132 |
elem_id="header"
|
133 |
)
|
@@ -142,13 +139,16 @@ def create_demo():
|
|
142 |
chat_output = gr.Textbox(label="π¬ AI Response", elem_id="chat-output", lines=5, interactive=False)
|
143 |
audio_output = gr.Audio(label="π AI Voice Response", autoplay=True, elem_id="audio-output")
|
144 |
|
145 |
-
|
|
|
146 |
|
|
|
147 |
def process_audio(audio, volume):
|
148 |
logging.info(f"Received audio: {audio}")
|
149 |
if audio is None:
|
150 |
return "No audio detected. Please try recording again.", None
|
151 |
response, audio_path = transcribe_and_chat(audio)
|
|
|
152 |
adjusted_audio_path = asyncio.run(text_to_speech_stream(response, volume))
|
153 |
logging.info(f"Response: {response}, Audio path: {adjusted_audio_path}")
|
154 |
return response, adjusted_audio_path
|
@@ -160,18 +160,21 @@ def create_demo():
|
|
160 |
demo.load(None, js="""
|
161 |
function() {
|
162 |
var recordButton;
|
163 |
-
|
164 |
function findRecordButton() {
|
165 |
var buttons = document.querySelectorAll('button');
|
166 |
-
for (var i = 0; buttons.length; i++) {
|
167 |
if (buttons[i].textContent.includes('Record from microphone')) {
|
168 |
return buttons[i];
|
169 |
}
|
170 |
}
|
|
|
171 |
}
|
172 |
|
173 |
function startListening() {
|
174 |
-
|
|
|
|
|
175 |
if (recordButton) {
|
176 |
recordButton.click();
|
177 |
}
|
@@ -199,6 +202,7 @@ def create_demo():
|
|
199 |
setTimeout(playAssistantAudio, 100);
|
200 |
});
|
201 |
|
|
|
202 |
document.addEventListener("visibilitychange", function() {
|
203 |
var audioElements = document.querySelectorAll('audio');
|
204 |
audioElements.forEach(function(audio) {
|
@@ -213,4 +217,4 @@ def create_demo():
|
|
213 |
# Launch the Gradio app
|
214 |
if __name__ == "__main__":
|
215 |
demo = create_demo()
|
216 |
-
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
106 |
def create_demo():
|
107 |
with gr.Blocks(css="""
|
108 |
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;700&display=swap');
|
109 |
+
body { font-family: 'Poppins', sans-serif; margin: 0; padding: 0; box-sizing: border-box;}
|
110 |
+
#audio-input {border: 2px solid #ffb703; padding: 10px;}
|
111 |
+
#chat-output {background-color: #023047; color: #ffffff; font-size: 1.2em;}
|
112 |
+
#audio-output {border: 2px solid #8ecae6;}
|
113 |
+
#clear-button {background-color: #fb8500; color: white;}
|
114 |
+
#voice-volume {background-color: #219ebc;}
|
115 |
+
button {font-size: 16px;}
|
116 |
+
audio {background-color: #ffb703; border-radius: 10px;}
|
|
|
|
|
|
|
|
|
117 |
footer {display: none;}
|
118 |
@media (max-width: 768px) {
|
119 |
#audio-input, #chat-output, #audio-output { width: 100%; }
|
|
|
122 |
""") as demo:
|
123 |
gr.Markdown(
|
124 |
"""
|
125 |
+
<div style='text-align:center; color:#023047; font-size: 28px; font-weight: bold;'>π£οΈ AI Voice Assistant</div>
|
126 |
+
<p style='text-align:center; color:#8ecae6; font-size: 18px;'>Talk to your personal AI! Record your voice, and get a response in both text and audio.</p>
|
127 |
+
<p style='text-align:center; color:#8ecae6;'>Powered by advanced AI models for real-time interaction.</p>
|
128 |
""",
|
129 |
elem_id="header"
|
130 |
)
|
|
|
139 |
chat_output = gr.Textbox(label="π¬ AI Response", elem_id="chat-output", lines=5, interactive=False)
|
140 |
audio_output = gr.Audio(label="π AI Voice Response", autoplay=True, elem_id="audio-output")
|
141 |
|
142 |
+
# Add some spacing and a divider
|
143 |
+
gr.Markdown("<hr style='border: 1px solid #8ecae6;'/>")
|
144 |
|
145 |
+
# Processing the audio input
|
146 |
def process_audio(audio, volume):
|
147 |
logging.info(f"Received audio: {audio}")
|
148 |
if audio is None:
|
149 |
return "No audio detected. Please try recording again.", None
|
150 |
response, audio_path = transcribe_and_chat(audio)
|
151 |
+
# Adjust volume for the response audio
|
152 |
adjusted_audio_path = asyncio.run(text_to_speech_stream(response, volume))
|
153 |
logging.info(f"Response: {response}, Audio path: {adjusted_audio_path}")
|
154 |
return response, adjusted_audio_path
|
|
|
160 |
demo.load(None, js="""
|
161 |
function() {
|
162 |
var recordButton;
|
163 |
+
|
164 |
function findRecordButton() {
|
165 |
var buttons = document.querySelectorAll('button');
|
166 |
+
for (var i = 0; i < buttons.length; i++) {
|
167 |
if (buttons[i].textContent.includes('Record from microphone')) {
|
168 |
return buttons[i];
|
169 |
}
|
170 |
}
|
171 |
+
return null;
|
172 |
}
|
173 |
|
174 |
function startListening() {
|
175 |
+
if (!recordButton) {
|
176 |
+
recordButton = findRecordButton();
|
177 |
+
}
|
178 |
if (recordButton) {
|
179 |
recordButton.click();
|
180 |
}
|
|
|
202 |
setTimeout(playAssistantAudio, 100);
|
203 |
});
|
204 |
|
205 |
+
// Prevent audio from stopping when switching tabs
|
206 |
document.addEventListener("visibilitychange", function() {
|
207 |
var audioElements = document.querySelectorAll('audio');
|
208 |
audioElements.forEach(function(audio) {
|
|
|
217 |
# Launch the Gradio app
|
218 |
if __name__ == "__main__":
|
219 |
demo = create_demo()
|
220 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|