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

Return final output for generators in Client.predict #5057

Merged
merged 16 commits into from Aug 9, 2023

Conversation

freddyaboulton
Copy link
Collaborator

@freddyaboulton freddyaboulton commented Aug 1, 2023

Description

Closes #4887
Closes #4888

Adds a finish method to the client jobs to make it easier to wait for job completion.

Also installs the client locally for CI as discussed in #4888

Python

image

JS

image

🎯 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

@vercel
Copy link

vercel bot commented Aug 1, 2023

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

Name Status Preview Updated (UTC)
gradio ✅ Ready (Inspect) Visit Preview Aug 8, 2023 7:22pm

@gradio-pr-bot
Copy link
Contributor

gradio-pr-bot commented Aug 1, 2023

🎉 Chromatic build completed!

There are 0 visual changes to review.
There are 0 failed tests to fix.

@gradio-pr-bot
Copy link
Contributor

gradio-pr-bot commented Aug 1, 2023

🦄 change detected

This Pull Request includes changes to the following packages.

Package Version
@gradio/app minor
gradio minor
gradio_client minor

With the following changelog entry.

Client.predict will now return the final output for streaming endpoints

This is a breaking change (for gradio_client only)!

Previously, Client.predict would only return the first output of an endpoint that streamed results. This was causing confusion for developers that wanted to call these streaming demos via the client.

We realize that developers using the client don't know the internals of whether a demo streams or not, so we're changing the behavior of predict to match developer expectations.

Using Client.predict will now return the final output of a streaming endpoint. This will make it even easier to use gradio apps via the client.

⚠️ The changeset file for this pull request has been modified manually, so the changeset generation bot has been disabled. To go back into automatic mode, delete the changeset file.

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 1, 2023

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


You can install the changes in this PR by running:

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

return { on, off, cancel, destroy, finish };
}

async function finish() {
Copy link
Member

@abidlabs abidlabs Aug 2, 2023

Choose a reason for hiding this comment

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

Nice js!

@abidlabs
Copy link
Member

abidlabs commented Aug 2, 2023

Looks good @freddyaboulton! I tested the Python client and works great. Trying to figure out how to test the JS client right now.

A thought in the meantime -- I know we discussed this before and I was initially against it -- but should we just have the .result() method actually return the final result from .outputs() instead of the first result? 🙈

Pros:

  1. People only need to remember one method and we don't need to have different code snippets
  2. Its much simpler syntax than finish() and indexing the final result from outputs()
  3. Its what people expect -- we've had a number of people asking why they are only getting the first response from generative outputs

Cons:

  1. It would be a breaking change (but we are pre-release anyways)
  2. We would have to handle every separately (but these endpoints are pretty rare anyways). Maybe we don't even expose endpoints that are every via API

@freddyaboulton
Copy link
Collaborator Author

I agree it's what people expect! Ok let's do that.

@freddyaboulton freddyaboulton changed the title Use submit in View API page for generators Return final output for generators in Client.predict Aug 7, 2023
Comment on lines 360 to 376
let result;

app
.on("data", (d) => {
data_returned = true;
if (status_complete) {
app.destroy();
data_returned = true;
res(d);
}
res(d);
result = d;
})
.on("status", (status) => {
if (status.stage === "error") rej(status);
if (status.stage === "complete" && data_returned) {
app.destroy();
}
if (status.stage === "complete") {
status_complete = true;
app.destroy();
res(result);
Copy link
Member

Choose a reason for hiding this comment

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

Whats this stuff?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Change to returning the final output of a generator.

Works in my tests but let me know if there’s a better way.

Copy link
Member

Choose a reason for hiding this comment

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

oh of course, my bad.

The only problem is it was implemented this way because i was having an issue receiving message out of order, which isn't technically supposed to be possible but it still happened. So the original logic allowed either message to resolve the promise whereas this only allows the status function to finish it, which if it is processed before the final data message means that the return value of predict would be the second to last value.

Maybe we could use the data.completed property and add the dual handling back?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ok I added the dual handling back

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.

Looks good to me! Thanks @freddyaboulton!

@freddyaboulton
Copy link
Collaborator Author

Thank you @pngwn !!

@freddyaboulton freddyaboulton merged commit 35856f8 into main Aug 9, 2023
15 checks passed
@freddyaboulton freddyaboulton deleted the 4887-use-via-api-code-snippets branch August 9, 2023 17:03
@pngwn pngwn mentioned this pull request Aug 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
t: highlight A change that we wish to highlight
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Install gradio_client locally in CI Provide a better code snippet for generators in the "view API" page
4 participants