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
34.18k stars 2.6k forks source link

Support examples for gr.Gallery #10034

Open avan06 opened 1 day ago

avan06 commented 1 day ago

Describe the bug

Hi Gradio Development Team,

I suspect there may be an issue with the Examples mechanism when using the gr.Gallery component. The same Examples implementation works perfectly with the gr.Image component. Here's a detailed explanation of the issue:

Recently, I updated my Gradio application by replacing the gr.Image component with gr.Gallery. However, this resulted in a PermissionError: [Errno 13] Permission denied: 'C:\\my\\path'.

Upon investigation, it appears that the issue may be related to the component.as_example(ex) function in gradio\components\dataset.py.

To debug, I added a print statement in the __init__ method of dataset.py. Below are the console logs for comparison:

When using gr.Image, the console log shows:

component: ex:power.jpg component.as_example(ex):path='power.jpg' url=None size=None orig_name='power.jpg' mime_type=None is_stream=False meta={'_type': 'gradio.FileData'}

When using gr.Gallery, the console log shows:

component: ex:power.jpg component.as_example(ex):root=[GalleryImage(image=FileData(path='p', url=None, size=None, orig_name='p', mime_type=None, is_stream=False, meta={'_type': 'gradio.FileData'}), caption=None), GalleryImage(image=FileData(path='o', url=None, size=None, orig_name='o', mime_type=None, is_stream=False, meta={'_type': 'gradio.FileData'}), caption=None), GalleryImage(image=FileData(path='w', url=None, size=None, orig_name='w', mime_type=None, is_stream=False, meta={'_type': 'gradio.FileData'}), caption=None), GalleryImage(image=FileData(path='e', url=None, size=None, orig_name='e', mime_type=None, is_stream=False, meta={'_type': 'gradio.FileData'}), caption=None), GalleryImage(image=FileData(path='r', url=None, size=None, orig_name='r', mime_type=None, is_stream=False, meta={'_type': 'gradio.FileData'}), caption=None), GalleryImage(image=FileData(path='.', url=None, size=None, orig_name='', mime_type=None, is_stream=False, meta={'_type': 'gradio.FileData'}), caption=None), GalleryImage(image=FileData(path='j', url=None, size=None, orig_name='j', mime_type=None, is_stream=False, meta={'_type': 'gradio.FileData'}), caption=None), GalleryImage(image=FileData(path='p', url=None, size=None, orig_name='p', mime_type=None, is_stream=False, meta={'_type': 'gradio.FileData'}), caption=None), GalleryImage(image=FileData(path='g', url=None, size=None, orig_name='g', mime_type=None, is_stream=False, meta={'_type': 'gradio.FileData'}), caption=None)] Traceback (most recent call last): File "C:\my\path\app.py", line 469, in main() File "C:\my\path\app.py", line 449, in main gr.Examples( File "C:\my\path\venv\lib\site-packages\gradio\helpers.py", line 56, in create_examples examples_obj = Examples( File "C:\my\path\venv\lib\site-packages\gradio\helpers.py", line 264, in __init__ self.dataset = components.Dataset( File "C:\my\path\venv\lib\site-packages\gradio\component_meta.py", line 179, in wrapper return fn(self, **kwargs) File "C:\my\path\venv\lib\site-packages\gradio\components\dataset.py", line 117, in __init__ processing_utils.move_files_to_cache( File "C:\my\path\venv\lib\site-packages\gradio\processing_utils.py", line 516, in move_files_to_cache return client_utils.traverse( File "C:\my\path\venv\lib\site-packages\gradio_client\utils.py", line 1009, in traverse new_obj.append(traverse(item, func, is_root)) File "C:\my\path\venv\lib\site-packages\gradio_client\utils.py", line 1004, in traverse new_obj[key] = traverse(value, func, is_root) File "C:\my\path\venv\lib\site-packages\gradio_client\utils.py", line 1000, in traverse return func(json_obj) File "C:\my\path\venv\lib\site-packages\gradio\processing_utils.py", line 490, in _move_to_cache temp_file_path = block.move_resource_to_block_cache(payload.path) File "C:\my\path\venv\lib\site-packages\gradio\blocks.py", line 347, in move_resource_to_block_cache temp_file_path = processing_utils.save_file_to_cache( File "C:\my\path\venv\lib\site-packages\gradio\processing_utils.py", line 277, in save_file_to_cache temp_dir = hash_file(file_path) File "C:\my\path\venv\lib\site-packages\gradio\processing_utils.py", line 206, in hash_file with open(file_path, "rb") as f: PermissionError: [Errno 13] Permission denied: 'C:\\my\\path'

Could you please help investigate and confirm this behavior? Thank you!

Have you searched existing issues? 🔎

Reproduction

import gradio as gr

def main():
    with gr.Blocks() as demo:
        with gr.Column():
            #image = gr.Image(type="pil", image_mode="RGBA", label="Input")
            gallery = gr.Gallery(columns=5, rows=5, show_share_button=False, interactive=True, height="500px", label="Input")

        gr.Examples(
            [["power.jpg"]], 
            inputs=[
                gallery,
            ],
        )

    demo.queue(max_size=10)
    demo.launch(inbrowser=True)

if __name__ == "__main__":
    main()

Screenshot

No response

Logs

No response

System Info

The testing environment is Windows 10 with Python 3.10.9 and Gradio 5.6.0.

Severity

Blocking usage of gradio

freddyaboulton commented 16 hours ago

Hi @avan06 - The gallery component doesn't actually support examples at the moment (it's typically used as an output component)