gradio-app / gradio

Build and share delightful machine learning apps, all in Python. 🌟 Star to support our work!
http://www.gradio.app
Apache License 2.0
33.24k stars 2.51k forks source link

Gradio File raise InvalidPathError() gradio.exceptions.InvalidPathError #9715

Open russosalv opened 2 days ago

russosalv commented 2 days ago

Describe the bug

Using Gradio File in Windows, if you try to go down in folder (more then 1 step) receive raise InvalidPathError() gradio.exceptions.InvalidPathError

filesystem is: Folder1 -> Folder2 -> Folder3 -> File

Have you searched existing issues? 🔎

Reproduction

import gradio as gr

        with gr.Blocks() as suggester_ui:
            with gr.Row(): 
                file_explorer = gr.FileExplorer(
                        interactive=True,
                        glob="*",
                    )

Screenshot

No response

Logs

No response

System Info

gradio 5.1.0

Severity

Blocking usage of gradio

freddyaboulton commented 1 day ago

Hi @russosalv - this is expected with gradio 5. We won't serve files located outside your current working directory unless you specify them in allowed_paths

russosalv commented 1 day ago

Hi @freddyaboulton thx for reply how to declare that it's allowed to go down in the directory?

freddyaboulton commented 1 day ago

Can you try something like

with gr.Blocks() as demo:
    ...
demo.launch(allowed_paths=["Folder1/"])
russosalv commented 1 day ago

@freddyaboulton alo adding

current_path = os.path.dirname(os.path.abspath(__file__))
...
ui.launch(allowed_paths=[current_path])

still ahve same error image

freddyaboulton commented 21 hours ago

I can't repro this actually @russosalv . I just tried with the file_explorer demo in the repo

import gradio as gr
from pathlib import Path

current_file_path = Path(__file__).resolve()
absolute_path = (current_file_path.parent / ".." / ".." / "gradio").resolve()

with gr.Blocks() as demo:
    gr.Markdown('### `FileExplorer` to `FileExplorer` -- `file_count="multiple"`')
    submit_btn = gr.Button("Select")
    with gr.Row():
        file = gr.FileExplorer(
            glob="**/components/*.py",
            # value=["themes/utils"],
            root_dir=absolute_path,
            ignore_glob="**/__init__.py",
        )

        file2 = gr.File()
    submit_btn.click(lambda x: x, file, file2)

if __name__ == "__main__":
    demo.launch(allowed_paths=[str(absolute_path)])

If the playgrounds directory is in allowed_paths then you should be able to process those files.

russosalv commented 44 minutes ago

@freddyaboulton try to execute this code and inspect Folder 2

import gradio as gr
from pathlib import Path

current_file_path = Path(__file__).resolve()
absolute_path = (current_file_path.parent).resolve()

# create a Folder called Folder1 in the same directory as this file
new_folder = absolute_path / "Folder1"
new_folder.mkdir(exist_ok=True)

# insert a fake txt file into the new folder
fake_txt = new_folder / "fake.txt"
fake_txt.write_text("Hello World")

# create a Folder called Folder2 in the directory new_folder
new_folder2 = new_folder / "Folder2"
new_folder2.mkdir(exist_ok=True)

# insert a fake txt file into the new folder
fake_txt2 = new_folder2 / "fake2.txt"
fake_txt2.write_text("Hello World 2")

with gr.Blocks() as demo:
    gr.Markdown('### `FileExplorer` to `FileExplorer` -- `file_count="multiple"`')
    submit_btn = gr.Button("Select")
    with gr.Row():
        file = gr.FileExplorer(
            glob="**/components/*.py",
            # value=["themes/utils"],
            root_dir=absolute_path,
            ignore_glob="**/__init__.py",
        )

        file2 = gr.File()
    submit_btn.click(lambda x: x, file, file2)

if __name__ == "__main__":
    demo.launch(allowed_paths=[str(absolute_path)])

then open folder1, and Folder 2 => you will have error

image