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

Get Intermediate Results from Python Client #3694

Merged
merged 39 commits into from
Apr 4, 2023

Conversation

freddyaboulton
Copy link
Collaborator

@freddyaboulton freddyaboulton commented Mar 29, 2023

Description

Summary

  1. Adds an outputs() method to Job that returns a list with all of the intermediate outputs received from the job at the time it is called.
  2. If the endpoint is a generator, result() will only block until the first result is returned

We will add status callbacks in the near future to enable event-based updates as well.

Usage

With the simple count_generator space

generator_client = grc.Client(space="gradio/count_generator")
job = generator_client.predict(10, fn_index=0)
while not job.done():
    time.sleep(1)
    print(job.outputs())
[]
[]
[]
[]
[]
[]
[['0'], ['1']]
[['0'], ['1'], ['2'], ['3']]
[['0'], ['1'], ['2'], ['3'], ['4'], ['5']]
[['0'], ['1'], ['2'], ['3'], ['4'], ['5'], ['6'], ['7']]
[['0'], ['1'], ['2'], ['3'], ['4'], ['5'], ['6'], ['7'], ['8'], ['9']]
[['0'], ['1'], ['2'], ['3'], ['4'], ['5'], ['6'], ['7'], ['8'], ['9']]

With a fake diffusion space

stable_diff = grc.Client(space="freddyaboulton/fake_diffusion")
job = stable_diff.predict(5, fn_index=0)
while not job.done():
    time.sleep(1)
print(job.outputs())

image

Checklist:

  • I have performed a self-review of my own code
  • I have added a short summary of my change to the CHANGELOG.md
  • My code follows the style guidelines of this project
  • I have commented my code in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

A note about the CHANGELOG

Hello 👋 and thank you for contributing to Gradio!

All pull requests must update the change log located in CHANGELOG.md, unless the pull request is labeled with the "no-changelog-update" label.

Please add a brief summary of the change to the Upcoming Release > Full Changelog section of the CHANGELOG.md file and include
a link to the PR (formatted in markdown) and a link to your github profile (if you like). For example, "* Added a cool new feature by [@myusername](link-to-your-github-profile) in [PR 11111](https://github.com/gradio-app/gradio/pull/11111)".

If you would like to elaborate on your change further, feel free to include a longer explanation in the other sections.
If you would like an image/gif/video showcasing your feature, it may be best to edit the CHANGELOG file using the
GitHub web UI since that lets you upload files directly via drag-and-drop.

@freddyaboulton freddyaboulton changed the base branch from main to python-client-job-status March 29, 2023 16:13
@gradio-pr-bot
Copy link
Collaborator

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

Base automatically changed from python-client-job-status to main March 29, 2023 22:41
self.ws_url,
open_timeout=10,
extra_headers=self.headers,
max_size=1024 * 1024 * 1024,
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Otherwise the websocket connection would close when sending large files

@freddyaboulton freddyaboulton marked this pull request as ready for review March 30, 2023 01:02
@freddyaboulton freddyaboulton requested a review from abidlabs March 30, 2023 01:02
def result(self, timeout: float | None = None):
if self.is_generator:
# Coerce to inf here because the parent class can't handle inf
timeout = timeout or math.inf
Copy link
Member

Choose a reason for hiding this comment

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

Tiniest of nits: using float("inf") will allow you to not have to import math

Comment on lines 386 to 388
@property
def is_generator(self):
return self.dependency.get("types", {}).get("generator", False)
Copy link
Member

Choose a reason for hiding this comment

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

So technically, this wouldn't work with older versions of Gradio library that are running generators, right? Is there a fallback we can do based on the message we get in WS?

Copy link
Member

Choose a reason for hiding this comment

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

For example, I don't think it works with this Space: abidlabs/test-yield which is running 3.23.1b1

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@abidlabs I think it's working for me

image

@abidlabs
Copy link
Member

Hi @freddyaboulton this looks very cool. My main concern is compatibility with older Spaces. I think we should be able to know if we're calling a generator simply by looking at the messages returned from the websocket, just like the frontend does it, right?

One other suggestion: it would be more elegant imo if we didn't have to pass in the deserialize function all the way down through the communicator. Maybe we instead pass it to the Job constructor and run it when we call outputs()? Just a suggestion

if output and status_update.code == Status.ITERATING:
try:
result = helper.deserialize(*output)
except Exception as e:
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's possible for the deserialization to error so I'm catching that here and adding the exception string but I'm open to other solutions

Comment on lines 386 to 388
@property
def is_generator(self):
return self.dependency.get("types", {}).get("generator", 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.

@abidlabs I think it's working for me

image

@freddyaboulton
Copy link
Collaborator Author

@abidlabs This should be good for another look! I refactored it so that we no longer check from config if the function is a generator.

self.future = future
self.communicator = communicator

def status(self) -> StatusUpdate:
def outputs(
Copy link
Member

Choose a reason for hiding this comment

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

Would add a docstring for this method, along with the expected signature

@abidlabs
Copy link
Member

abidlabs commented Apr 4, 2023

Hey @freddyaboulton,

Very nice PR. Looks good to me, but a couple of clarification questions:

  • outputs() returns each of the intermediate outputs as lists, but the final output not enclosed in a list. Concretely, running
import gradio_client as grc

generator_client = grc.Client("gradio/count_generator")
job = generator_client.predict(10, fn_index=0)
while not job.done():
    pass
job.outputs()

Produces:

[['0'], ['1'], ['2'], ['3'], ['4'], ['5'], ['6'], ['7'], ['8'], ['9'], '9']

Why is the 9 repeated? And why is it not enclosed in a list the final 9? It feels to me that the expected output should be:

['0', '1', ... '8', '9']
  • For gr.Interface in which an iterator function is passed in, we see an interesting value returned:
['',
  {'visible': False, '__type__': 'update'},
  {'visible': True, '__type__': 'update'}]

That's because clicking the submit button causes not only the inner function to run, but also the submit button to turn into a stop button, etc. All of these updates are returned by the same API endpoint. I think we should refactor gr.Interface to avoid this from happening. We can do this now using .then(), although I think it's fine to do this in another PR.

Abubakar

@freddyaboulton
Copy link
Collaborator Author

@abidlabs Following our synch earlier today, I made the necessary modifications so the output is not stored in a list if there is only one output component.

For count_generator, you should see ['0', ..., '9'] and this I've added a new endpoint to that space called /gen_images that will generate pairs of random images to test a case with more than one output component:

image

Should be good for a re-review! Thanks again

@abidlabs
Copy link
Member

abidlabs commented Apr 4, 2023

Tested and works great, thanks @freddyaboulton! Awesome PR

@freddyaboulton
Copy link
Collaborator Author

Thank you for the review @abidlabs !

@freddyaboulton freddyaboulton merged commit 9325cba into main Apr 4, 2023
@freddyaboulton freddyaboulton deleted the python-client-intermediate-results branch April 4, 2023 21:58
dawoodkhan82 pushed a commit that referenced this pull request Apr 5, 2023
* Add status + unit test (flaky) for now

* Install client

* Fix tests

* Lint backend + tests

* Add non-queue test

* Fix name

* Use lock instead

* Add simplify implementation + fix tests

* Restore changes to scripts

* Fix README typo

* Fix CI

* Add intermediate results to python client

* Type check

* Typecheck again

* Catch exception:

* Thinking

* Dont read generator from config

* add no queue test

* Remove unused method

* Fix types

* Remove breakpoint

* Fix code

* Fix test

* Fix tests

* Unpack list

* Add docstring

---------

Co-authored-by: Abubakar Abid <[email protected]>
dawoodkhan82 added a commit that referenced this pull request Apr 6, 2023
* update gallery styles

* Set theme name from load (#3595)

* Add name + test

* Add theme names

* CHANGELOG

* Delete theme in interface

* Trigger event when Slider number input is released (#3589)

* Add event

* Add unit test

* CHANGELOG

* Sets up the Python `gradio` client (#3300)

* placeholder

* changelog

* added to readme

* client

* implement futures

* utils

* scripts

* lint

* reorg

* scripts

* serialization

* cleanup

* fns

* serialize

* cache

* callbacks

* updates

* formatting

* packaging

* requirements

* remove changelog

* client

* access token

* formatting

* deprecate

* format backend

* client replace

* updates

* moving from utils

* remove code duplication

* rm duplicates

* simplify

* galleryserializer

* serializable

* load serializers

* fixing errors

* errors

* typing

* tests

* changelog

* lint

* fix lint

* fixing files

* formatting

* type

* fix type checking

* changelog

* changelog

* Update client/python/gradio_client/client.py

Co-authored-by: Lucain <[email protected]>

* formatting, tests

* formatting, tests

* gr.load

* refactoring

* refactoring'

* formatting

* formatting

* tests

* tests

* fix tests

* cleanup

* added tests

* adding scripts

* formatting

* address review comments

* readme

* serialize info

* remove from changelog

* version 0.0.2 released

* lint

* type fix

* check

* type issues

* hf_token

* update hf token

* telemetry

* docs, circle dependency

* hf token

* formatting

* updates

* sort

* script

* external

* docs

* formatting

* fixes

* scripts

* requirements

* fix tests

* context

* changes

* formatting

* fixes

* format fix

---------

Co-authored-by: Lucain <[email protected]>

* Translate "or" for i18n (#3599)

* Translate or for i18n

* CHANGELOG

* Fixes Blocks exit issue (#3600)

* fix

* changelog

* blocks

* formatting

* Use gradio-api-server for telemetry (#3488)

* analytics

* changelog

* remove interface analytics

* ip

* remove import

* format

* theme name

* theme analytics

* format

* changelog

* fixes

* format

* remove unused param

---------

Co-authored-by: Abubakar Abid <[email protected]>

* Simplify tests (#3608)

* simplify tests

* imports

* imports

* formatting

* removed cometml typing

* simplify

* changelog

* fix wc error in dev mode (#3572)

Co-authored-by: Abubakar Abid <[email protected]>

* A few small fixes to docs / demos (#3611)

* fixes

* remove binaries

* doc

* changelog

* typing

* run on windows

* cancels

* added clarifications

* Add docs for HF Json saver (#3604)

* Add docs for flagging

* Fix params

* CHANGELOG

---------

Co-authored-by: freddyaboulton <[email protected]>

* ensure css loads before mounting app (#3573)

* ensure css loads before mounting app

* changelog

* fix tests

* change?

* changelog

* fix issue with missing version (#3632)

Co-authored-by: Freddy Boulton <[email protected]>

* Fixes chatbot autoscroll + Textbox lines > 20 issue (#3637)

* fixes

* changelog

* Update gradio/components.py

Co-authored-by: Abubakar Abid <[email protected]>

* Update CHANGELOG.md

* Update CHANGELOG.md

---------

Co-authored-by: Abubakar Abid <[email protected]>

* Fix embedded demos (#3638)

Co-authored-by: Abubakar Abid <[email protected]>

* Add Windows CI (#3628)

* Add Windows CI

* Update changelog

* fix

* Skip one test on Windows

* Preserve virtualenv path

* Skip another test on Windows

* Make conditional flaky

* Requested changes

* consistent os

* cleanup

* fix test for windows

* remove unnecessary check

* lint

* lint

---------

Co-authored-by: Abubakar Abid <[email protected]>

* move files (#3605)

* move files

* commit the rest of the files

* fix lockfile

* fix workflow

* fix type errors

* fix tests

* only run ci when certain files change

* run correct test command in ci

* version

* fix pypi script

---------

Co-authored-by: Abubakar Abid <[email protected]>

* Support empty lists being used in `gr.Dataframe` (#3646)

* Support empty lists being used in `gr.Dataframe`

* Update changelog

* Add empty dataframe test

---------

Co-authored-by: Abubakar Abid <[email protected]>

* Fix windows flake (#3650)

* fix windows flake

* format backend

---------

Co-authored-by: Freddy Boulton <[email protected]>

* Raise errror if event queued but queue is not enabled (#3640)

* Raise Error

* CHANGELOG

* Add progress tracking validate_queue_settings

* Update gradio/blocks.py

Co-authored-by: Abubakar Abid <[email protected]>

* Fix test

---------

Co-authored-by: Abubakar Abid <[email protected]>

* Copy everything in website Dockerfile, fix build issues (#3659)

* dockerfile

* copy everything from repo

* correct dir

* changelog

* Correct the documentation of `gr.File` component (#3660)

This closes #3658.

Co-authored-by: Abubakar Abid <[email protected]>

* Nit in ValueError (#3669)

* Nit in ValueError

* CHANGELOG

---------

Co-authored-by: freddyaboulton <[email protected]>

* Load upstream theme (#3641)

* theme loading

* upstream theme

* version

* format themes

* fixes

* tests

* one more test

* fix test

* address review

* Add job for python client ci (#3674)

* Add job + lint

* Fix path

* Fix path

* Fix path

* Checkout

* Add test requirements

* Fix syntax

* Fix test

* Lint

* Fix deps + README

* Move dependency

* Hide dropdown if in single-select mode (#3678)

* Hide dropdown if in single-select mode

* Update changelog

---------

Co-authored-by: Abubakar Abid <[email protected]>

* Fix default parameters value and gr.Progress in same function (#3671)

* Fix default parameters value and gr.Progress in same function

* Update changelog

* Fix tests

* Format

* Expand tests for other types of special function arguments

* Augment SelectData tests

---------

Co-authored-by: Abubakar Abid <[email protected]>

* warning

* changelog

* ghangelog

* changelog

* object fit optional

* Add status for Python Client Jobs (#3645)

* Add status + unit test (flaky) for now

* Install client

* Fix tests

* Lint backend + tests

* Add non-queue test

* Fix name

* Use lock instead

* Add simplify implementation + fix tests

* Restore changes to scripts

* Fix README typo

* Fix CI

* Add two concurrent test

* Fix broken spaces in docs (#3698)

* fix examples in sentence_builder

* fix sklearn error in titanic demo

* regenerate notebooks

* changelgo

* Add download button for video (#3581)

* Add download buttom

* Add missing imports

* CHANGELOG

* Update CHANGELOG.md

* Trigger CI

* Fix visibility

* Try to fix ci

* Fix deps

* download button change

* Lint

---------

Co-authored-by: Dawood <[email protected]>
Co-authored-by: Abubakar Abid <[email protected]>

* Fix outdated sharing your app guide (#3699)

* fix embed this space screenshot

* fix use via api

* changelog

---------

Co-authored-by: Freddy Boulton <[email protected]>

* Add orig_name field to video outputs (#3700)

* Add orig_name to video

* Fix test

* CHANGELOG

* Lint

* Theme builder (#3664)

* changes

* changes

* changes

* changes

* changes

* changes

* changes

* changes

* changes

* changes

* changes

* changes

* Update CHANGELOG.md

Co-authored-by: Abubakar Abid <[email protected]>

* Update gradio/themes/builder.py

Co-authored-by: Abubakar Abid <[email protected]>

* changes

* changes

* changes

* changes

* changes

* changes

* changes

* changes

* changes

* changes

* changes

---------

Co-authored-by: Abubakar Abid <[email protected]>

* fix dropdowns, release 3.24 (#3713)

* changes

* changes

* Update version.txt

* New Version Docs (#3715)

* [create-pull-request] automated change

* fix changelog

---------

Co-authored-by: abidlabs <[email protected]>
Co-authored-by: Abubakar Abid <[email protected]>

* Fix chatbot newline issue (#3717)

* changes

* changes

* changes

* changelog

---------

Co-authored-by: Abubakar Abid <[email protected]>

* New Version Docs (#3720)

* [create-pull-request] automated change

* Trigger Build

---------

Co-authored-by: aliabd <[email protected]>
Co-authored-by: aliabd <[email protected]>

* Fix Serializer Mapping  (#3722)

* Fix mapping and test

* Bump gradio version

* Revert gradio version bump

* Fix some bugs related to Python client (#3721)

* client format

* docs

* formatting

* fix tests

* fixed bug

* api endpoint changes

* fix tests

* fix tests

* formatting

* Add support for sessions [python client] (#3731)

* client

* add state and tests

* remove session param

* node support for js client (#3692)

* bundle js client + gen types

* changeset

* changeset

* fix bugs

* fix deps

* fix deps

* format

* fix ci

* fix types

* Support IPv6 addresses for --server-name (#3695)

* Support IPv6 addresses for --server-name

* Update changelog now that I have a PR number.

---------

Co-authored-by: freddyaboulton <[email protected]>

* Increase timeout for analytics request + remove exception print (#3647)

* increase timeout

* merge

* Add changelog

---------

Co-authored-by: Freddy Boulton <[email protected]>

* Switch linting to Ruff (#3710)

* Sort requirements.in

* Switch flake8 + isort to ruff

* Apply ruff import order fixes

* Fix ruff complaints in demo/

* Fix ruff complaints in test/

* Use `x is not y`, not `not x is y`

* Remove unused listdir from website generator

* Clean up duplicate dict keys

* Add changelog entry

* Clean up unused imports (except in gradio/__init__.py)

* add space

---------

Co-authored-by: Abubakar Abid <[email protected]>

* Fix missing docstrings (new PR) (#3740)

* Move documentation for _js into docstring, not a stray comment

* Add missing argument docstrings (and remove non-existent ones)

* Add changelog entry

* updated docstrings

* changelog

* contributors

* changelog

* formatting

* removed _js

---------

Co-authored-by: Aarni Koskela <[email protected]>

* import (#3742)

* Import Literal from typing extensions in client (#3741)

* Fix typing extensions

* Import typing_extensions

---------

Co-authored-by: Abubakar Abid <[email protected]>

* Access http token for ws connection (#3735)

* Access unsecure token

* CHANGELOG

---------

Co-authored-by: Abubakar Abid <[email protected]>

* Add root_url to serializers in gradio_client (#3736)

* Add root_url to serializers

* Add url fix

* Respect fn parameter

* Fix docstring

* Add other test

* Pass to method

* CI tweaks (#3752)

* Adds a pypi release action for the gradio python client (#3743)

* release action

* fixes

* name

* Update version.txt

* Update version.txt

* update

* fixes

* version

* rename

* action

* fix token

* custom dir

* fixes

* change password

* revert back to token

* scripts

* remove twine

* Get Intermediate Results from Python Client (#3694)

* Add status + unit test (flaky) for now

* Install client

* Fix tests

* Lint backend + tests

* Add non-queue test

* Fix name

* Use lock instead

* Add simplify implementation + fix tests

* Restore changes to scripts

* Fix README typo

* Fix CI

* Add intermediate results to python client

* Type check

* Typecheck again

* Catch exception:

* Thinking

* Dont read generator from config

* add no queue test

* Remove unused method

* Fix types

* Remove breakpoint

* Fix code

* Fix test

* Fix tests

* Unpack list

* Add docstring

---------

Co-authored-by: Abubakar Abid <[email protected]>

* changes (#3760)

* Make Client Jobs Iterable (#3762)

* Add iterator

* Break if done

* Add test for early termination

* changelog

* notebooks

---------

Co-authored-by: Freddy Boulton <[email protected]>
Co-authored-by: Abubakar Abid <[email protected]>
Co-authored-by: Lucain <[email protected]>
Co-authored-by: pngwn <[email protected]>
Co-authored-by: Omar Sanseviero <[email protected]>
Co-authored-by: Ali Abdalla <[email protected]>
Co-authored-by: space-nuko <[email protected]>
Co-authored-by: Luo Peng <[email protected]>
Co-authored-by: aliabid94 <[email protected]>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: abidlabs <[email protected]>
Co-authored-by: aliabd <[email protected]>
Co-authored-by: Dan Sully <[email protected]>
Co-authored-by: Aarni Koskela <[email protected]>
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.

3 participants