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

Adds support for kwargs and default arguments in the python client, and improves how parameter information is displayed in the "view API" page #7732

Merged
merged 46 commits into from Mar 22, 2024

Conversation

abidlabs
Copy link
Member

@abidlabs abidlabs commented Mar 18, 2024

This PR adds support for kwargs and default arguments in the Client. In other words, suppose you have the following demo:

def calculator(num1, operation, num2):
    if operation == "add":
        return num1 + num2
    elif operation == "subtract":
        return num1 - num2
    elif operation == "multiply":
        return num1 * num2
    elif operation == "divide":
        if num2 == 0:
            raise gr.Error("Cannot divide by zero!")
        return num1 / num2

demo = gr.Interface(
    calculator,
    ["number", gr.Radio(["add", "subtract", "multiply", "divide"], value="add), "number"],
    "number",
    examples=[
        [5, "add", 3],
        [4, "divide", 2],
        [-4, "multiply", 2.5],
        [0, "subtract", 1.2],
    ],
).launch()

When you connect to this app via the client, you can now make queries like this:

from gradio_client import Client

client = Client("http://127.0.0.1:7861/")
result = client.predict(
		num1=3,
		operation="add",
		num2=10,
		api_name="/predict"
)
print(result)

Note that parameters to .predict() are now supplied with key-word arguments. For backwards compatibility, the old approach using positional arguments continues to work. e.g.

result = client.predict(
		3,
		"add",
		33,
		api_name="/predict"
)

but the key-word approach is encouraged because it is more robust and also allows us to take advantage of default arguments. In the example above, we could also do:

from gradio_client import Client

client = Client("http://127.0.0.1:7861/")
result = client.predict(
		num1=33,
		num2=33,
		api_name="/predict"
)
print(result)

taking advantage of the fact that operation is "add" by default, as specified in the initial value of the component.

Closes: #3540
Closes: #5344
Closes: #4498

Finally, I've updated the design of the view API page to showcase all this information about each parameter. (parameter names, whether it is required, or the default fallback value if it is not). Here's a partial screenshot:

image

@abidlabs abidlabs changed the title [WIP] API kwarg params Add support for kwargs in python client Mar 18, 2024
@gradio-pr-bot
Copy link
Contributor

gradio-pr-bot commented Mar 18, 2024

🪼 branch checks and previews

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

Install Gradio from this PR

pip install https://gradio-builds.s3.amazonaws.com/2c59e5609ef78cbe54501f67ccd81d1666e20a08/gradio-4.22.0-py3-none-any.whl

Install Gradio Python Client from this PR

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

@gradio-pr-bot
Copy link
Contributor

gradio-pr-bot commented Mar 18, 2024

🦄 change detected

This Pull Request includes changes to the following packages.

Package Version
@gradio/app minor
@gradio/atoms minor
gradio minor
gradio_client minor
  • Maintainers can select this checkbox to manually select packages to update.

With the following changelog entry.

Adds support for kwargs and default arguments in the python client, and improves how parameter information is displayed in the "view API" page

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.

@abidlabs abidlabs marked this pull request as ready for review March 18, 2024 23:20
@abidlabs abidlabs marked this pull request as draft March 18, 2024 23:21
@abidlabs abidlabs added the flaky-tests This label runs flaky tests (those that use the HF API) on a PR label Mar 19, 2024
@abidlabs
Copy link
Member Author

Ok I've made the change @freddyaboulton. Still working on the documentation, but should otherwise be good for a review

{parameter_name
? parameter_name + "="
: ""}<span class="example-inputs"
>{represent_value(
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: there is an extra space added before the comma

Pasted image 20240321162808

Copy link
Member Author

Choose a reason for hiding this comment

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

It looks like that but I think there's no space actually, maybe its a padding issue

Copy link
Collaborator

Choose a reason for hiding this comment

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

Interesting. I think ideal would be no padding but not a big deal

desc = (
f" ({info['python_type']['description']})"
if info["python_type"].get("description")
else ""
)
default_value = info.get("parameter_default")
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we need to traverse the value here to only pull out the actual filepath or url

Copy link
Member Author

Choose a reason for hiding this comment

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

So the view API page takes care of this automatically:

image

but the .view_api() function does not

image
import gradio as gr

demo = gr.Interface(
    lambda x:x,
    gr.Audio("test.mp3"),
    "audio"
)

_, url, _ = demo.launch()

from gradio_client import Client

client = Client(url)

client.view_api()

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll fix in the .view_api()

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yea sorry that's what I meant!

@abidlabs
Copy link
Member Author

Ok the docs should be ready as well now!

Copy link
Collaborator

@freddyaboulton freddyaboulton left a comment

Choose a reason for hiding this comment

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

Very slick PR @abidlabs ! Tested a variety of demos and they all work well!

one thing that would be nice is to have the URL of the file if it's on spaces otherwise the file upload would fail?

image

@abidlabs
Copy link
Member Author

Thanks @freddyaboulton! Agree will figure out a better solution for files

@abidlabs
Copy link
Member Author

abidlabs commented Mar 21, 2024

Ok that should address everything in the review, thanks so much @freddyaboulton:

image

I'll merge in later today unless @aliabd has any other suggestions

Copy link
Collaborator

@aliabd aliabd left a comment

Choose a reason for hiding this comment

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

Really like this @abidlabs!

I have two small suggestions that you can make the final call on.

  • I feel like the multiple parameters are blending together visually. I know you have the faint orange separator (in light mode), maybe making it more prominent or increasing the space?
  • I think 'Defaults to _' and 'Required' should occupy the same position. Right now 'Required' is after the name and type, where as the default value is in the end of the description. I think it would be better if you maybe put them both at the end of the first line, on the right.

Everything else looks so clean!

@abidlabs
Copy link
Member Author

Thanks for the reviews @freddyaboulton and @aliabd. I agree with your suggestions @aliabd, will make the changes and merge

@abidlabs
Copy link
Member Author

Updated. It now looks like this (more margin between the parameters and defaults on the same line as the required):

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flaky-tests This label runs flaky tests (those that use the HF API) on a PR
Projects
None yet
4 participants