Start queue when gradio is a sub application#2319
Conversation
1853b1d to
e3da8e3
Compare
|
All the demos for this PR have been deployed at https://huggingface.co/spaces/gradio-pr-deploys/pr-2319-all-demos |
|
Nice PR @freddyaboulton! I had a question about supplying the Do we really need to know the absolute path for the local server? It seems like that is used inside the |
|
@abidlabs I agree it's not ideal to have to specify the server and port in two places. I think Do you have other solutions in mind? |
|
websocket part LGTM. |
I might be missing something but can't the queue just use the relative URL like all of the other route? Basically here: Line 217 in 581fbab Why can't just be: url="/api/predict", |
|
@abidlabs The queue is running on a separate thread than the main gradio app so it needs the full url path to the predict endpoint to access the gradio api. Just tried out your suggestion and I got an error with the |
|
Gotcha, testing the PR now |
| return type(val) is dict and "update" in val.get("__type__", "") | ||
|
|
||
|
|
||
| def mount_gradio_app( |
There was a problem hiding this comment.
Nit/suggestion: it feels more natural to me for this method to belong in routes.py with all of the other fastapi-related stuff
|
|
||
| io = gr.Interface(lambda x: "Hello, " + x + "!", "textbox", "textbox") | ||
| gradio_app = gr.routes.App.create_app(io) | ||
| app = gr.mount_gradio_app(app, gradio_app, server_name="localhost", port="8000", path="/gradio") |
There was a problem hiding this comment.
A suggestion for this function: can we have it take a Gradio Blocks object instead of a FastAPI app as its second parameter? And then internally it calls it gr.routes.App.create_app(io). This saves our users a line of code and needing to reference two different methods both of which are deep in our code base
There was a problem hiding this comment.
Sure thing! Good point.
|
@freddyaboulton I tested and this PR works great, and allows a FastAPI app to mount a Gradio app with or without queue enabled. My main suggestion would be to try to simplify the usage for our users. I noticed that there is actually a way to get the server URL without needing the user to specify it. In any FastAPI route, if you add the So this suggests an idea:
This should eliminate the need for the |
363131b to
7af2f04
Compare
|
Thanks for the awesome suggestion @abidlabs ! I agree that it's much better to not have users specify the same information twice. I pushed up the suggestion you made - we can now set the gradio api endpoint dynamically from the queue join route if it's not specified. This works! I deployed a fastapi app with gradio as a sub application here: https://gradio-sub-app.onrender.com/ If you go to https://gradio-sub-app.onrender.com/gradio/ you will see a gradio app! You can also access the lion image here: https://gradio-sub-app.onrender.com/lion.jpg (I mounted a static file app on the root path). The problem with this approach is that it won't work on spaces because spaces only expose websockets on a specific endpoint that's different from the one gradio is running on, e.g. What if we keep an optional parameter called With that approach, I was able to get gradio sub-applications to work on spaces here: https://huggingface.co/spaces/freddyaboulton/gradio-subapp |
|
Thank you so much for trying out the suggestions @freddyaboulton!
Sounds good! Let's add and document this parameter :) |
|
@abidlabs Just pushed up the doc changes! Documentation here: Updated guide here: Should be good for a final look now! |
| app: fastapi.FastAPI, | ||
| blocks: gradio.Blocks, | ||
| path: str, | ||
| gradio_api_url: Optional[str], |
There was a problem hiding this comment.
| gradio_api_url: Optional[str], | |
| gradio_api_url: Optional[str] = None, |
|
Works great @freddyaboulton. I'm going to push some small tweaks to the documentation (easier than commenting in all of the places) if you don't mind! Feel free to take or leave |
…adio into fix-queue-when-app-mounted
|
Awesome @freddyaboulton! LGTM |
|
Thanks for the review @abidlabs ! |




Description
Closes: #2292
There were two problems as noted on the original issue:
startup-eventsroute that's called duringBlocks.launch.I think I figured out the ws path issue although I'd like some feedback from @aliabid94 .
The queue thread not starting automatically is an interesting problem. I think the cleanest thing would be to use fastapi startup events to start the queue however there are two issues with that:
I opted for adding a helper method called
mount_gradio_appthat will add a startup event to the parent application that runs the queue. This is not as nice as using the built-in FastAPIapp.mount(gradio_app, ...)syntax but it's the best solution I see now.Changes to the guide
Checklist: