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

Enable streaming audio in python client #5248

Merged
merged 12 commits into from Aug 21, 2023
Merged

Conversation

freddyaboulton
Copy link
Collaborator

Description

Update the FileSerializable so that it can handle streaming responses introduced in #5077

import gradio_client as grc
client = grc.Client("<local-url-of-streaming-audio-demo>")
job = client.submit("/Users/freddy/Pictures/bark_demo.mp4", fn_index=1)

# The entire audio stream
job.result()

# Each individual chunk
job.outputs()

🎯 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
Contributor

gradio-pr-bot commented Aug 16, 2023

🪼 branch checks and previews

Name Status URL
Spaces ready! Spaces preview
Website ready! Website preview
Storybook ready! Storybook preview
Visual tests all good! Build review
🦄 Changes detected! Details

Install Gradio from this PR

pip install https://gradio-builds.s3.amazonaws.com/387da3b5846278da4ddbb56e0f2b669fa1925129/gradio-3.40.1-py3-none-any.whl

Install Gradio Python Client from this PR

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

@gradio-pr-bot
Copy link
Contributor

gradio-pr-bot commented Aug 16, 2023

🦄 change detected

This Pull Request includes changes to the following packages.

Package Version
gradio minor
gradio_client minor

With the following changelog entry.

Enable streaming audio in python client

The gradio_client now supports streaming file outputs 🌊

No new syntax! Connect to a gradio demo that supports streaming file outputs and call predict or submit as you normally would.

import gradio_client as grc
client = grc.Client("gradio/stream_audio_out")

# Get the entire generated audio as a local file
client.predict("/Users/freddy/Pictures/bark_demo.mp4", api_name="/predict")

job = client.submit("/Users/freddy/Pictures/bark_demo.mp4", api_name="/predict")

# Get the entire generated audio as a local file
job.result()

# Each individual chunk
job.outputs()

⚠️ 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.

@freddyaboulton freddyaboulton added t: feat A change that implements a feature t: highlight A change that we wish to highlight labels Aug 16, 2023
@freddyaboulton freddyaboulton removed the t: feat A change that implements a feature label Aug 16, 2023
"is_stream": True,
}
if data[i]:
data[i]["is_file"] = False
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Preserve the orig_name so that clients have an easier time downloading the byte stream

Copy link
Member

@abidlabs abidlabs Aug 17, 2023

Choose a reason for hiding this comment

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

But isn't ["name"] assigned to the same value as before in the next line?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yea we're using "name" to mean the url from which to download the stream so I'm keeping that the same. Just preserving orig_name (if it's there) so that we can give a meaningful name when we go to download the byte stream to a file.

}
if data[i]:
data[i]["is_file"] = False
data[i]["name"] = f"{session_hash}/{run}/{output_id}"
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@aliabid94 Thoughts on including the stream endpoint name in the route so that it doesn't have to be hardcoded in the client?

Comment on lines 312 to 315
self.stream = self._setup_stream(
root_url + "stream/" + x["name"], hf_token=hf_token
)
chunk = next(self.stream)
Copy link
Member

Choose a reason for hiding this comment

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

Sorry can you explain why these two lines are repeated from above?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It was to determine when the stream changed but there's a better/clearer way. Will push up now!

for data in r.iter_bytes():
arr += data
yield data
yield arr
Copy link
Member

Choose a reason for hiding this comment

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

Am I understanding correctly that what's happening is that we are iterating chunk by chunk and then finally yielding the full array so that .predict() returns the full data array? 2 questions:

(1) How come outputs() doesn't include this final full data array?
(2) Will r.iter_bytes() work if the streaming response returns binary data (as in #5238) instead of file data?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

(1) outputs only includes the values returned from the server during the process_generating event
(2) yea the stream enpoint only serves bytes so it should work when #5238 is merged

@abidlabs
Copy link
Member

Really nice @freddyaboulton! Just a few questions to understand the implementation -- will test more thoroughly tomorrow.

@freddyaboulton freddyaboulton self-assigned this Aug 21, 2023
Copy link
Member

@abidlabs abidlabs left a comment

Choose a reason for hiding this comment

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

Very nice @freddyaboulton! LGTM

@abidlabs
Copy link
Member

I'll go ahead and merge this in so that we can consolidate things before the release

@abidlabs abidlabs merged commit 390624d into main Aug 21, 2023
20 checks passed
@abidlabs abidlabs deleted the enable-streaming-in-client branch August 21, 2023 19:15
@pngwn pngwn mentioned this pull request Aug 21, 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.

None yet

3 participants