-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Conversation
All the demos for this PR have been deployed at https://huggingface.co/spaces/gradio-pr-deploys/pr-2641-all-demos |
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 |
Really good catch, thanks @freddyaboulton! Yes let me fix that |
Ok so this is a little tricky. The most obvious approach would be to pass along the However, now there are two separate things that a function could read from:
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() |
Update: @freddyaboulton suggested a wrapper |
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 Ready for another look! |
@abidlabs I lost this notification in my inbox! Will take a look today |
There was a problem hiding this 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!
Thanks for the great review @freddyaboulton! |
All righty, this PR should be ready to go. I'll merge later tonight |
Thanks again for the detailed review @freddyaboulton. Merging! |
@abidlabs The feature is really useful. Can gradio=3.11 work without internet connection? |
Unfortunately, I don't think any 3.x works without internet connection |
Adds ability to access the underlying FastAPI request from within your Python function. Simple test function:
Fixes: #2379