Spaces:
Build error
Build error
Update Gradio_UI.py
Browse files- Gradio_UI.py +49 -42
Gradio_UI.py
CHANGED
|
@@ -295,52 +295,59 @@ class GradioUI:
|
|
| 295 |
)
|
| 296 |
|
| 297 |
def launch(self, **kwargs):
|
| 298 |
-
|
| 299 |
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 314 |
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
# output_text = gr.Textbox(label="Jokes Found") #added
|
| 318 |
-
# output_audio = gr.Audio(label="Audio Pronunciation", type="filepath") #added
|
| 319 |
-
# btn = gr.Button("Get Jokes") #added
|
| 320 |
-
# btn.click(self.gradio_search_jokes, inputs=input_box, outputs=[output_text, output_audio]) #added
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
# If an upload folder is provided, enable the upload feature
|
| 325 |
-
if self.file_upload_folder is not None:
|
| 326 |
-
upload_file = gr.File(label="Upload a file")
|
| 327 |
-
upload_status = gr.Textbox(label="Upload Status", interactive=False, visible=False)
|
| 328 |
-
upload_file.change(
|
| 329 |
-
self.upload_file,
|
| 330 |
-
[upload_file, file_uploads_log],
|
| 331 |
-
[upload_status, file_uploads_log],
|
| 332 |
-
)
|
| 333 |
-
text_input = gr.Textbox(lines=1, label="Chat Message")
|
| 334 |
-
text_input.submit(
|
| 335 |
-
self.log_user_message,
|
| 336 |
-
[text_input, file_uploads_log],
|
| 337 |
-
[stored_messages, text_input],
|
| 338 |
-
).then(self.interact_with_agent, [stored_messages, chatbot], [chatbot])
|
| 339 |
|
| 340 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 341 |
|
| 342 |
-
|
| 343 |
-
|
|
|
|
| 344 |
|
| 345 |
|
| 346 |
__all__ = ["stream_to_gradio", "GradioUI"]
|
|
|
|
| 295 |
)
|
| 296 |
|
| 297 |
def launch(self, **kwargs):
|
| 298 |
+
import gradio as gr
|
| 299 |
|
| 300 |
+
# NEW: Add a helper function to extract audio file path from stored messages
|
| 301 |
+
def extract_audio(messages):
|
| 302 |
+
if not messages:
|
| 303 |
+
return None
|
| 304 |
+
last_message = messages[-1]
|
| 305 |
+
if isinstance(last_message.content, dict):
|
| 306 |
+
mime = last_message.content.get("mime_type", "")
|
| 307 |
+
if mime.startswith("audio"):
|
| 308 |
+
return last_message.content.get("path")
|
| 309 |
+
return None
|
| 310 |
+
|
| 311 |
+
with gr.Blocks(fill_height=True) as demo:
|
| 312 |
+
stored_messages = gr.State([])
|
| 313 |
+
file_uploads_log = gr.State([])
|
| 314 |
+
chatbot = gr.Chatbot(
|
| 315 |
+
label="Agent",
|
| 316 |
+
type="messages",
|
| 317 |
+
avatar_images=(
|
| 318 |
+
None,
|
| 319 |
+
"https://huggingface.co/datasets/agents-course/course-images/resolve/main/en/communication/Alfred.png",
|
| 320 |
+
),
|
| 321 |
+
resizeable=True,
|
| 322 |
+
scale=1,
|
| 323 |
+
)
|
| 324 |
|
| 325 |
+
# NEW: Add a dedicated audio player component below the chatbot.
|
| 326 |
+
audio_player = gr.Audio(label="Audio Pronunciation", type="filepath")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 327 |
|
| 328 |
+
if self.file_upload_folder is not None:
|
| 329 |
+
upload_file = gr.File(label="Upload a file")
|
| 330 |
+
upload_status = gr.Textbox(label="Upload Status", interactive=False, visible=False)
|
| 331 |
+
upload_file.change(
|
| 332 |
+
self.upload_file,
|
| 333 |
+
[upload_file, file_uploads_log],
|
| 334 |
+
[upload_status, file_uploads_log],
|
| 335 |
+
)
|
| 336 |
+
text_input = gr.Textbox(lines=1, label="Chat Message")
|
| 337 |
+
# Chain the functions: log user message -> interact with agent -> extract audio for player
|
| 338 |
+
text_input.submit(
|
| 339 |
+
self.log_user_message,
|
| 340 |
+
[text_input, file_uploads_log],
|
| 341 |
+
[stored_messages, text_input],
|
| 342 |
+
).then(
|
| 343 |
+
self.interact_with_agent, [stored_messages, chatbot], [chatbot]
|
| 344 |
+
).then(
|
| 345 |
+
fn=extract_audio, inputs=[stored_messages], outputs=[audio_player]
|
| 346 |
+
)
|
| 347 |
|
| 348 |
+
# Optionally, you can arrange the components as you like, for example:
|
| 349 |
+
# gr.Column([chatbot, audio_player, text_input])
|
| 350 |
+
demo.launch(debug=True, share=True, **kwargs)
|
| 351 |
|
| 352 |
|
| 353 |
__all__ = ["stream_to_gradio", "GradioUI"]
|