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

Improve file handling in JS Client #8462

Merged
merged 17 commits into from
Jun 6, 2024
Merged

Improve file handling in JS Client #8462

merged 17 commits into from
Jun 6, 2024

Conversation

hannahblair
Copy link
Collaborator

@hannahblair hannahblair commented Jun 5, 2024

Description

This PR adds a handle_file function, which converts a URL or File to FileData, or if it receives a Blob, it will just return the Blob as the client handles that anyway. This PR also fixes an issue with image uploading.

Methods of file handling that should work with this PR:

Passing a blob directly

const response = await fetch("https://raw.githubusercontent.com/gradio-app/gradio/main/test/test_files/bus.png");
const image_blob = await response_0.blob();
const result = client.submit("/lambda", { x: image_blob })

Passing a blob to handle_file

const response = await fetch("https://raw.githubusercontent.com/gradio-app/gradio/main/test/test_files/bus.png");
const image_blob = await response_0.blob();
const result = client.submit("/lambda", { x: handle_file(image_blob) })

Passing an image URL to handle_file

const image_url = "https://raw.githubusercontent.com/gradio-app/gradio/main/test/test_files/bus.png";
const result = client.submit("/lambda", { x: handle_file(image_url) })

Passing an image URL as an object

const image_url = "https://raw.githubusercontent.com/gradio-app/gradio/main/test/test_files/bus.png";
const result = client.submit("/lambda", { x: { path: image_url })

Passing a local file path to handle_file

const image_path = "./bus.png";
const result = client.submit("/lambda", { x: handle_file(image_path) )

Passing a Buffer to handle_file

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const imagePath = path.resolve(__dirname, "./bus.png");
const imageBuffer = fs.readFileSync(imagePath);

const result = client.submit("/lambda", { x: handle_file(imageBuffer) )

Passing an array of local image files to handle_file

const client = await Client.connect("hmb/image-simple-2"); // example space requiring two imgs
const result = await client.predict("/process_images", { image_list: [handle_file("./dog.png"), handle_file("./dog.png")] });

Closes: #6623
Closes: #8082
Closes: #7834
Closes: #8402
Clsoes: #8256

🎯 PRs Should Target Issues

Before your create a PR, please check to see if there is an existing issue for this change. If not, please create an issue before you create this PR, unless the fix is very small.

Not adhering to this guideline will result in the PR being closed.

Tests

  1. PRs will only be merged if tests pass on CI. To run the tests locally, please set up your Gradio environment locally and run the tests: bash scripts/run_all_tests.sh

  2. You may need to run the linters: bash scripts/format_backend.sh and bash scripts/format_frontend.sh

@gradio-pr-bot
Copy link
Collaborator

gradio-pr-bot commented Jun 5, 2024

🪼 branch checks and previews

Name Status URL
Spaces ready! Spaces preview
Website failed! Details
🦄 Changes detected! Details

Install Gradio from this PR

pip install https://gradio-builds.s3.amazonaws.com/c4265462b96bbf2dc247f9dd2b55fcf190dc3037/gradio-4.33.0-py3-none-any.whl

Install Gradio Python Client from this PR

pip install "gradio-client @ git+https://github.com/gradio-app/gradio@c4265462b96bbf2dc247f9dd2b55fcf190dc3037#subdirectory=client/python"

Install Gradio JS Client from this PR

npm install https://gradio-builds.s3.amazonaws.com/c4265462b96bbf2dc247f9dd2b55fcf190dc3037/gradio-client-0.20.1.tgz

@gradio-pr-bot
Copy link
Collaborator

gradio-pr-bot commented Jun 5, 2024

🦄 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.

Improve file handling in JS Client

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.

client/js/src/types.ts Outdated Show resolved Hide resolved
@pngwn
Copy link
Member

pngwn commented Jun 5, 2024

Another case that should work is when the handle_file is passed as part of a nested object or array:

const image_url = "https://raw.githubusercontent.com/gradio-app/gradio/main/test/test_files/bus.png";
const result = client.submit("/lambda", { 
  x: {
    files: [handle_file(image_url), handle_file(image_url)]
  }
})

@hannahblair hannahblair marked this pull request as ready for review June 5, 2024 23:02
throw new Error(NODEJS_FS_ERROR_MSG);
}

const file = new File([fileBuffer], cmd_item.meta.name, {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't use File in node, it was only added in v20, we can just use Blob here instead. In theory we lose the filename but we lose it anyway. We can improve the filename handling later on.

Copy link
Member

@pngwn pngwn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apart from that File comment, it works perfectly. Awesome stuff, thanks @hannahblair!

@hannahblair hannahblair mentioned this pull request Jun 6, 2024
1 task
@hannahblair hannahblair merged commit 6447dfa into main Jun 6, 2024
8 checks passed
@hannahblair hannahblair deleted the js-client-files branch June 6, 2024 11:56
@pngwn pngwn mentioned this pull request Jun 6, 2024
@pngwn pngwn mentioned this pull request Jun 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants