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
30.56k stars 2.27k forks source link

Improve file handling in JS Client #8462

Closed hannahblair closed 2 weeks ago

hannahblair commented 3 weeks ago

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 commented 3 weeks ago

🪼 branch checks and previews

• Name Status URL
Spaces ready! Spaces preview
Website failed! Details
:unicorn: 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 commented 3 weeks ago

🦄 change detected

This Pull Request includes changes to the following packages.

Package Version
@gradio/client patch
gradio patch

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](https://github.com/gradio-app/gradio/edit/js-client-files/.changeset/violet-pans-doubt.md).
pngwn commented 3 weeks ago

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)]
  }
})