-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using arbitrary paths for gr.Audio
element
#9867
Comments
Tried to dynamically add the paths to import os
import webview
import gradio as gr
_WINDOW: webview.Window
def user_select_directory():
dirname = _WINDOW.create_file_dialog()
if dirname:
allowed_paths = os.getenv("GRADIO_ALLOWED_PATHS")
if allowed_paths:
os.environ["GRADIO_ALLOWED_PATHS"] = f"{allowed_paths},{dirname[0]}"
else:
os.environ["GRADIO_ALLOWED_PATHS"] = dirname[0]
return gr.Audio(value=dirname[0], visible=True)
return None
with gr.Blocks(analytics_enabled=False) as demo:
selection_btn = gr.Button("Select file")
audio_element = gr.Audio(visible=False)
selection_btn.click(user_select_directory, outputs=audio_element)
url = demo.queue(api_open=False).launch(prevent_thread_lock=True, quiet=True, show_api=False)[1]
_WINDOW = webview.create_window("Gradio-App", url)
webview.start(private_mode=False) |
Yes if you wanted to be able to process any possible file I think this is what you would have to do |
That is easier said than done unfortunately, as paths to drives differ in Win and Mac/Linux. But if that's how it's supposed to be then I'll come up with something. Thanks for the feedback |
Thank you for the patience @Josef-Haupt . |
This is what I came up with. Should work on win, darwin and linux. import os
import sys
import webview
import gradio as gr
_WINDOW: webview.Window
def user_select_directory():
filename = _WINDOW.create_file_dialog()
if filename:
return gr.Audio(value=filename[0], visible=True)
return None
def get_win_drives():
from string import ascii_uppercase as UPPER_CASE
return [f"{drive}:\\" for drive in UPPER_CASE]
with gr.Blocks(analytics_enabled=False) as demo:
selection_btn = gr.Button("Select file")
audio_element = gr.Audio(visible=False)
selection_btn.click(user_select_directory, outputs=audio_element)
allowed_drives = get_win_drives() if sys.platform == "win32" else ["/"]
url = demo.queue(api_open=False).launch(
prevent_thread_lock=True,
quiet=True,
show_api=False,
allowed_paths=allowed_drives
)[1]
_WINDOW = webview.create_window("Gradio-App", url)
webview.start(private_mode=False) |
Actually, the docs could probably benefit from a note, concerning this behaviour and how to work around it, just a suggestion though. |
Describe the bug
I am building an app with gradio that runs locally using pywebview. Until now, the user would select a directory and I'd collect all audio files inside and then feed them to the
gr.Audio
element, which worked perfectly before updating to Gradio5.4.0
.I've read the Migration Guide and the post about File Access and Security and I do get it in the context of a web app. But since my app runs locally I do not care for the files selected by the user. If I run the app now with Gradio 5, I'll get this error:
So to get the behaviour I used in Gradio 4, I would have to add all possible paths (C, D, ...) to
allowed_paths
which would defeat the purpose of the feature. Or I could move the file to the specified locations, basically cache it, for gradio to cache it again, which is also pretty roundabout way of doing things.Is there a way to get the behavior of gradio 4 or disable the caching in general? If not, I'll probably go back to Gradio 4, for the time being.
Have you searched existing issues? 🔎
Reproduction
Screenshot
No response
Logs
No response
System Info
Severity
I can work around it
The text was updated successfully, but these errors were encountered: