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

Adding ability to access in Fastapi request object into your function #2641

Merged
merged 28 commits into from
Nov 19, 2022

Conversation

abidlabs
Copy link
Member

@abidlabs abidlabs commented Nov 12, 2022

Adds ability to access the underlying FastAPI request from within your Python function. Simple test function:

import gradio as gr

def test(name, request: gr.Request):
    print(request.headers)
    return name

io = gr.Interface(test, "textbox", "textbox").launch()

Fixes: #2379

@github-actions
Copy link
Contributor

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

@abidlabs abidlabs marked this pull request as ready for review November 15, 2022 17:58
@freddyaboulton
Copy link
Collaborator

This is really cool @abidlabs !

One thing I noticed is that the Request object is different depending on whether or not the function is being run on the queue. That is because when the queue is active, requests are made from the server and not the client. If the purpose of this feature is to allow users to extract info from requests from the client, should we update the implementation to work better with the queue?

You can see what I'm talking about here:

https://huggingface.co/spaces/freddyaboulton/fastapi-request

@abidlabs
Copy link
Member Author

abidlabs commented Nov 15, 2022

Really good catch, thanks @freddyaboulton! Yes let me fix that

@abidlabs
Copy link
Member Author

abidlabs commented Nov 15, 2022

Ok so this is a little tricky. The most obvious approach would be to pass along the Websocket object to your function from the user's original request to the function if queueing is enabled, just like we currently pass along the Request object.

However, now there are two separate things that a function could read from:

  • A fastapi.Websocket if queuing is enabled
  • A fastapi.Request if queuing is disabled

Those two objects are not the same, so basically what this means is now you might to change your function depending on whether queuing is enabled. I.e. if you upload your code to Spaces, it will break. That's not good...

The only solution I can think of is only to support the queueing-enabled case. In other words, if you want to access request headers, etc. you have to enable queuing and then access the Websockets object from within your function. Something like:

import gradio as gr
import fastapi

def test(name, websocket: fastapi.Websockets):
    print(websocket.headers)
    return name

io = gr.Interface(test, "textbox", "textbox").queue().launch()

@abidlabs
Copy link
Member Author

Update: @freddyaboulton suggested a wrapper gr.Request over both websockets and regular requests, and I like it!

@abidlabs
Copy link
Member Author

abidlabs commented Nov 16, 2022

Thanks for the suggestion @freddyaboulton. I've implemented it so that queuing passes the parameters from the original request, instead of the request from the server. We now utilize our own gr.Request class, which is also added to the docs.

Ready for another look!

@freddyaboulton
Copy link
Collaborator

@abidlabs I lost this notification in my inbox! Will take a look today

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.

Awesome PR @abidlabs ! Left mostly nits. Tested and works well!

demo/audio_debugger/run.py Outdated Show resolved Hide resolved
gradio/utils.py Show resolved Hide resolved
gradio/routes.py Show resolved Hide resolved
gradio/blocks.py Show resolved Hide resolved
gradio/examples.py Show resolved Hide resolved
guides/01_getting_started/03_sharing_your_app.md Outdated Show resolved Hide resolved
gradio/blocks.py Show resolved Hide resolved
@abidlabs
Copy link
Member Author

Thanks for the great review @freddyaboulton!

@abidlabs
Copy link
Member Author

All righty, this PR should be ready to go. I'll merge later tonight

@abidlabs abidlabs merged commit 5e148c3 into main Nov 19, 2022
@abidlabs abidlabs deleted the fastapi-requests branch November 19, 2022 08:52
@abidlabs
Copy link
Member Author

Thanks again for the detailed review @freddyaboulton. Merging!

@Juenjie
Copy link

Juenjie commented Apr 12, 2023

@abidlabs The feature is really useful. Can gradio=3.11 work without internet connection?

@abidlabs
Copy link
Member Author

Unfortunately, I don't think any 3.x works without internet connection

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.

How can I get the request Headers in the Gradio background?
3 participants