Skip to content

Prevent api requests from skipping queue#2493

Merged
freddyaboulton merged 4 commits into
mainfrom
2452-prevent-api-from-jumping-queue
Oct 25, 2022
Merged

Prevent api requests from skipping queue#2493
freddyaboulton merged 4 commits into
mainfrom
2452-prevent-api-from-jumping-queue

Conversation

@freddyaboulton
Copy link
Copy Markdown
Collaborator

@freddyaboulton freddyaboulton commented Oct 18, 2022

Description

Fixes #2452

Summary:

  • Requests made to /api endpoint will be blocked if the queue is enabled for that event, unless a special auth token is passed in the header. This is to allow the queue to make requests.
  • Developers can keep the api "open" by setting queue(api_open=True).

Added some unit tests but you can test yourself as well:

import gradio as gr
import time

def iteration(steps):
    for i in range(steps):
       time.sleep(0.5)
       yield i

gr.Interface(iteration,
             inputs=gr.Slider(minimum=1, maximum=10, step=1, value=5),
             outputs=gr.Number()).queue(api_open=False).launch()

In a separate process

assert requests.post(
"http://127.0.0.1:7861/api/predict/",
json={"fn_index": 0, "data": [5], "session_hash": 0}).reason == 'Unauthorized'

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 title Prevent api from skipping queue Prevent api requests from skipping queue Oct 18, 2022
@freddyaboulton freddyaboulton force-pushed the 2452-prevent-api-from-jumping-queue branch from 3c597bc to 6a50df4 Compare October 18, 2022 20:23
@github-actions
Copy link
Copy Markdown
Contributor

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

@freddyaboulton freddyaboulton force-pushed the 2452-prevent-api-from-jumping-queue branch from 6a50df4 to 5f6b984 Compare October 18, 2022 20:59
Comment thread gradio/blocks.py
utils.run_coro_in_background(self._queue.start)
utils.run_coro_in_background(self.create_limiter)

def queue_enabled_for_fn(self, fn_index: int):
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Just a refactor to reduce dupe code

Comment thread gradio/routes.py
if not app.blocks.api_open and app.blocks.queue_enabled_for_fn(
body.fn_index
):
if f"Bearer {app.queue_token}" != request.headers.get("Authorization"):
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I think Bearer {token} in the Authorization header is the standard way of providing these tokens.

@freddyaboulton freddyaboulton marked this pull request as ready for review October 18, 2022 21:15
@freddyaboulton freddyaboulton requested review from abidlabs, aliabd, aliabid94, dawoodkhan82 and pngwn and removed request for abidlabs October 18, 2022 21:26
Comment thread gradio/blocks.py Outdated
self.share_url = None
self.width = None
self.height = None
self.api_open = False
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

My feeling is that the API should be open by default to preserve the current behavior.

Comment thread gradio/blocks.py
client_position_to_load_data: Once a client's position in Queue is less that this value, the Queue will collect the input data from the client. You may make this smaller if clients can send large volumes of data, such as video, since the queued data is stored in memory.
default_enabled: If True, all event listeners will use queueing by default.
api_open: If True, the REST routes of the backend will be open, allowing requests made directly to those endpoints to skip the queue.
max_size: The maximum number of events the queue will store at any given moment.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for adding!

@abidlabs
Copy link
Copy Markdown
Member

Thanks @freddyaboulton, this looks really good to me. One addition: can we hide the "view API" page link in the footer if queue is enabled and api_open is False?

@abidlabs
Copy link
Copy Markdown
Member

abidlabs commented Oct 23, 2022

This is also very timely as we currently have issues with an app on Spaces whose API endpoint is being called from an external endpoint, degrading the experience for regular users of the Space. I'm going to release a beta version so that we can try this out on the Space itself. cc @AK391 @osanseviero

Internal discussion: https://huggingface.slack.com/archives/C02136Y252P/p1666447456974249

Also did some more testing and confirmedthat if you have a Space that sets api_open=False, you can still gr.Interface.load() it. The loaded Interface's requests are still correctly added to the end of the original Space's queue ✅

@freddyaboulton freddyaboulton force-pushed the 2452-prevent-api-from-jumping-queue branch from f862b9f to ac1de7d Compare October 24, 2022 21:53
@freddyaboulton
Copy link
Copy Markdown
Collaborator Author

@abidlabs I made it so that the api is open by default and api_open will take precedence over show_api if the queue is enabled! Should be good for another look now!

Comment thread gradio/blocks.py Outdated
@abidlabs
Copy link
Copy Markdown
Member

LGTM @freddyaboulton very nice PR!

@freddyaboulton freddyaboulton force-pushed the 2452-prevent-api-from-jumping-queue branch from 3444743 to 11c008b Compare October 25, 2022 15:33
@freddyaboulton
Copy link
Copy Markdown
Collaborator Author

Thank you for the review @abidlabs !

@freddyaboulton freddyaboulton merged commit 7f9223f into main Oct 25, 2022
@freddyaboulton freddyaboulton deleted the 2452-prevent-api-from-jumping-queue branch October 25, 2022 15:45
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.

API call not stack with the queue from the website.

2 participants