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

Allow users to add a custom API route #10332

Merged
merged 13 commits into from
Jan 10, 2025
Merged

Allow users to add a custom API route #10332

merged 13 commits into from
Jan 10, 2025

Conversation

aliabid94
Copy link
Collaborator

@aliabid94 aliabid94 commented Jan 10, 2025

Now users can use gr.api to add custom API routes to their apps. For example:

    with gr.Blocks() as demo:
        def fn(a: int, b: int, c: list[str]) -> tuple[int, str]:
            return a + b, c[a:b]
        
        gr.api(fn, api_name="addition")

would add an API endpoint and API page that looks like this:

Screenshot 2025-01-10 at 9 25 30 AM

Note that it picks up type from type-hints. This endpoint will use the queue just like a normal endpoint.

It however does not yet pick up parameter and return type descriptions from the docstrings. This was because there's no consistent docstring format in python, especially for multiple return types - usually they are documented as a single return tuple which makes it hard to specify the indiviual types within the tuple. Can document in a follow up PR.

Closes: #10217

@gradio-pr-bot
Copy link
Collaborator

gradio-pr-bot commented Jan 10, 2025

🪼 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-pypi-previews.s3.amazonaws.com/bce06dd34658be9bf9f5cc66ed5001d003ae473c/gradio-5.11.0-py3-none-any.whl

Install Gradio Python Client from this PR

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

Install Gradio JS Client from this PR

npm install https://gradio-npm-previews.s3.amazonaws.com/bce06dd34658be9bf9f5cc66ed5001d003ae473c/gradio-client-1.9.0.tgz

Use Lite from this PR

<script type="module" src="https://gradio-lite-previews.s3.amazonaws.com/bce06dd34658be9bf9f5cc66ed5001d003ae473c/dist/lite.js""></script>

@gradio-pr-bot
Copy link
Collaborator

gradio-pr-bot commented Jan 10, 2025

🦄 change detected

This Pull Request includes changes to the following packages.

Package Version
@gradio/client patch
@gradio/core patch
gradio patch
gradio_client patch
  • Maintainers can select this checkbox to manually select packages to update.

With the following changelog entry.

Allow users to add a custom API route

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.

demo/hello_blocks/run.py Outdated Show resolved Hide resolved
gradio/queueing.py Outdated Show resolved Hide resolved
@@ -638,6 +638,7 @@ export function get_component(
} {
let example_component_map: Map<ComponentMeta["type"], LoadingComponent> =
new Map();
if (type === "api") type = "state";
Copy link
Member

@abidlabs abidlabs Jan 10, 2025

Choose a reason for hiding this comment

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

instead of setting the type to "state" which could have repercussions for other code, perhaps better to have a list of non-rendered components and include both "state" and "api"

@abidlabs
Copy link
Member

abidlabs commented Jan 10, 2025

It however does not yet pick up parameter and return type descriptions from the docstrings. This was because there's no consistent docstring format in python, especially for multiple return types - usually they are documented as a single return tuple which makes it hard to specify the indiviual types within the tuple. Can document in a follow up PR.

In that case, perhaps better to add explicit parameters parameter_descriptions and return_type_descriptions, but agreed this can be a separate pr. Could also add support for example_inputs which are currently missing in the view api page

@abidlabs
Copy link
Member

Great PR @aliabid94! Generally works great. Just two suggestions:

  1. Have a better error (or graceful fallback) if some of the type-hint is not specified. E.g. if you do this:
  def fn2(a: int, b: int, c):
      return a + b, c[a:b]
  
  gr.api(fn2, api_name="addition2")

the gradio app fails to launch with a messy error

  1. (suggestion) use gr.api inside of gr.ChatInterface as a further test of robustness.

@aliabid94
Copy link
Collaborator Author

suggestion) use gr.api inside of gr.ChatInterface as a further test of robustness

Will do in a followup that also allows editing API description

@aliabid94 aliabid94 merged commit e742dcc into main Jan 10, 2025
23 checks passed
@aliabid94 aliabid94 deleted the custom_api branch January 10, 2025 19:46
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.

IndexError when creating a pure API endpoint
3 participants