Spaces:
Running
Running
Update Gradio_UI.py
Browse files- Gradio_UI.py +25 -4
Gradio_UI.py
CHANGED
@@ -26,6 +26,15 @@ from smolagents.utils import _is_package_available
|
|
26 |
|
27 |
from Code_Functions import speak_text
|
28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
#from jokes import gradio_search_jokes
|
31 |
|
@@ -185,11 +194,16 @@ def stream_to_gradio(
|
|
185 |
content={"path": final_answer.to_string(), "mime_type": "image/png"},
|
186 |
)
|
187 |
elif isinstance(final_answer, AgentAudio):
|
|
|
|
|
|
|
|
|
188 |
yield gr.ChatMessage(
|
189 |
role="assistant",
|
190 |
-
content={"
|
191 |
-
#content={"data": final_answer.value, "mime_type": "audio/mpeg"},
|
192 |
)
|
|
|
|
|
193 |
else:
|
194 |
yield gr.ChatMessage(role="assistant", content=f"**Final answer:** {str(final_answer)}")
|
195 |
|
@@ -313,7 +327,14 @@ class GradioUI:
|
|
313 |
if isinstance(msg.content, dict):
|
314 |
mime = msg.content.get("mime_type", "")
|
315 |
if mime.startswith("audio"):
|
316 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
317 |
return None
|
318 |
|
319 |
with gr.Blocks(fill_height=True) as demo:
|
@@ -331,7 +352,7 @@ class GradioUI:
|
|
331 |
)
|
332 |
|
333 |
# NEW: Add a dedicated audio player component below the chatbot.
|
334 |
-
audio_player = gr.Audio(label="Audio Pronunciation", type="
|
335 |
|
336 |
if self.file_upload_folder is not None:
|
337 |
upload_file = gr.File(label="Upload a file")
|
|
|
26 |
|
27 |
from Code_Functions import speak_text
|
28 |
|
29 |
+
import io
|
30 |
+
import librosa
|
31 |
+
import numpy as np
|
32 |
+
|
33 |
+
def mp3_bytes_to_numpy(audio_bytes, sr=None):
|
34 |
+
# Load audio from the MP3 bytes; sr=None preserves the original sample rate.
|
35 |
+
audio_np, sr = librosa.load(io.BytesIO(audio_bytes), sr=sr)
|
36 |
+
return audio_np
|
37 |
+
|
38 |
|
39 |
#from jokes import gradio_search_jokes
|
40 |
|
|
|
194 |
content={"path": final_answer.to_string(), "mime_type": "image/png"},
|
195 |
)
|
196 |
elif isinstance(final_answer, AgentAudio):
|
197 |
+
# Assuming your AgentAudio object stores the raw MP3 bytes in an attribute called "value"
|
198 |
+
audio_bytes = final_answer.value
|
199 |
+
# Convert MP3 bytes to a numpy array using our helper function
|
200 |
+
audio_np = mp3_bytes_to_numpy(audio_bytes)
|
201 |
yield gr.ChatMessage(
|
202 |
role="assistant",
|
203 |
+
content={"data": audio_np, "mime_type": "audio/mpeg"},
|
|
|
204 |
)
|
205 |
+
print("DEBUG AgentAudio attributes:", vars(final_answer))
|
206 |
+
|
207 |
else:
|
208 |
yield gr.ChatMessage(role="assistant", content=f"**Final answer:** {str(final_answer)}")
|
209 |
|
|
|
327 |
if isinstance(msg.content, dict):
|
328 |
mime = msg.content.get("mime_type", "")
|
329 |
if mime.startswith("audio"):
|
330 |
+
# If the audio data is already provided under "data", return it.
|
331 |
+
if "data" in msg.content:
|
332 |
+
return msg.content["data"]
|
333 |
+
# Otherwise, if a file path is provided (fallback), load and convert it.
|
334 |
+
elif "path" in msg.content:
|
335 |
+
with open(msg.content["path"], "rb") as f:
|
336 |
+
audio_bytes = f.read()
|
337 |
+
return mp3_bytes_to_numpy(audio_bytes)
|
338 |
return None
|
339 |
|
340 |
with gr.Blocks(fill_height=True) as demo:
|
|
|
352 |
)
|
353 |
|
354 |
# NEW: Add a dedicated audio player component below the chatbot.
|
355 |
+
audio_player = gr.Audio(label="Audio Pronunciation", type="numpy")
|
356 |
|
357 |
if self.file_upload_folder is not None:
|
358 |
upload_file = gr.File(label="Upload a file")
|