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

Allow supporting >1000 files in gr.File() and gr.UploadButton() #5075

Merged
merged 4 commits into from Aug 3, 2023

Conversation

abidlabs
Copy link
Member

@abidlabs abidlabs commented Aug 2, 2023

A few months ago, starlette decided to limit the number of files that could be uploaded via a single request to 1,000 by default (though configurable). FastAPI does not support configuring this yet, though there is an open PR.

This has prevented Gradio users from uploading more than 1,000 files through our UploadButton and File components. This PR modifies the upload_files() method used in the frontend to upload files in batches of 1,000 to get around this limitation.

Fixes: #4361

To test:

  1. First create >1000 files in a directory:
import os

def create_tiny_files(folder_name, num_files):
    if not os.path.exists(folder_name):
        os.makedirs(folder_name)
    
    for i in range(num_files):
        file_name = f"{folder_name}/file_{i + 1}.txt"
        with open(file_name, "w") as file:
            file.write(str(i + 1))

if __name__ == "__main__":
    folder_name = "tiny_files2"
    num_files = 100
    create_tiny_files(folder_name, num_files)
    print(f"{num_files} tiny files have been created in the '{folder_name}' folder.")
  1. Then try uploading them:
import gradio as gr

def upload_file(files):
    file_paths = [file.name for file in files]
    return file_paths

with gr.Blocks() as demo:
    full_button = gr.Files(file_count="directory")
    textbox = gr.Textbox()
    button = gr.Button()
    
    button.click(upload_file, full_button, textbox)
    
demo.launch()

@vercel
Copy link

vercel bot commented Aug 2, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
gradio ✅ Ready (Inspect) Visit Preview Aug 3, 2023 5:12pm

@gradio-pr-bot
Copy link
Contributor

gradio-pr-bot commented Aug 2, 2023

🦄 change detected

This Pull Request includes changes to the following packages.

Package Version
@gradio/client patch
gradio patch
  • Maintainers can select this checkbox to manually select packages to update.

With the following changelog entry.

Allow supporting >1000 files in gr.File() and gr.UploadButton()

Maintainers or the PR author can modify the PR title to modify this entry.

Something isn't right?

  • Maintainers can change the version label to modify the version bump.
  • If the bot has failed to detect any changes, or if this pull request needs to update multiple packages to different versions or requires a more comprehensive changelog entry, maintainers can update the changelog file directly.

@gradio-pr-bot
Copy link
Contributor

gradio-pr-bot commented Aug 2, 2023

All the demos for this PR have been deployed at https://huggingface.co/spaces/gradio-pr-deploys/pr-5075-all-demos


You can install the changes in this PR by running:

pip install https://gradio-builds.s3.amazonaws.com/097b7caff6bc97f563a37afb50707976b5606933/gradio-3.39.0-py3-none-any.whl

client/js/src/client.ts Outdated Show resolved Hide resolved
client/js/src/client.ts Outdated Show resolved Hide resolved
@hannahblair
Copy link
Collaborator

Looks good and have tested that it works! Thanks for addressing this!

Screenshot 2023-08-03 at 20 18 03

@abidlabs
Copy link
Member Author

abidlabs commented Aug 3, 2023

Thanks @hannahblair for the review! @freddyaboulton raised a good point internally. I'll address that in Slack more fully and then will merge this in.

@abidlabs abidlabs merged commit 67265a5 into main Aug 3, 2023
9 checks passed
@abidlabs abidlabs deleted the max-files branch August 3, 2023 19:29
@pngwn pngwn mentioned this pull request Aug 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

An error happened when upload a directory more than 1000 files
3 participants