Skip to content
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

Closed
1 task done
Josef-Haupt opened this issue Oct 29, 2024 · 6 comments
Closed
1 task done

Using arbitrary paths for gr.Audio element #9867

Josef-Haupt opened this issue Oct 29, 2024 · 6 comments
Labels
bug Something isn't working

Comments

@Josef-Haupt
Copy link

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 Gradio 5.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:

gradio.exceptions.InvalidPathError: Cannot move some_path\XC525205_37.flac to the gradio cache 
dir because it was not created by the application or it is not located in either the current working 
directory or your system's temp directory. To fix this error, please ensure your function returns files 
located in either the current working directory, your system's temp directory or add parent_directory 
to the allowed_paths parameter of launch().

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? 🔎

  • I have searched and found no existing issues

Reproduction

import webview
import gradio as gr

_WINDOW: webview.Window

def user_select_directory():
    dirname = _WINDOW.create_file_dialog()

    return gr.Audio(value=dirname[0], visible=True) if dirname else 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)

Screenshot

No response

Logs

No response

System Info

Python: 3.11
Gradio: 5.4.0
Win 11

Severity

I can work around it

@Josef-Haupt Josef-Haupt added the bug Something isn't working label Oct 29, 2024
@Josef-Haupt
Copy link
Author

Tried to dynamically add the paths to GRADIO_ALLOWED_PATHS during execution, but doesn't seem to work

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)

@freddyaboulton
Copy link
Collaborator

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

Yes if you wanted to be able to process any possible file I think this is what you would have to do

@Josef-Haupt
Copy link
Author

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

@freddyaboulton
Copy link
Collaborator

Thank you for the patience @Josef-Haupt .

@Josef-Haupt
Copy link
Author

Josef-Haupt commented Nov 1, 2024

This is what I came up with. Should work on win, darwin and linux.
I just blindly add all possible drives on windows and the root / on non-win machines, which should account for drives mounted during execution. Seems to be working so far.

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)

@Josef-Haupt
Copy link
Author

Actually, the docs could probably benefit from a note, concerning this behaviour and how to work around it, just a suggestion though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants