Skip to content

Allow gradio embeds in colab without share#2455

Merged
aliabid94 merged 57 commits into
mainfrom
rename_api_for_colab
Nov 3, 2022
Merged

Allow gradio embeds in colab without share#2455
aliabid94 merged 57 commits into
mainfrom
rename_api_for_colab

Conversation

@aliabid94
Copy link
Copy Markdown
Contributor

@aliabid94 aliabid94 commented Oct 13, 2022

Embeds Gradio apps directly into using colabs output.serve_kernel_port_as_iframe. Had to change default api endpoint name to /run (though /api is still supported for backwards compatibility.).

Note: websockets with queueing are not supported, so will still use sharing in that case.

Example colab: https://colab.research.google.com/drive/1oOyMGV0cxziK_ViSmh0y2c2FxPOHPFMs?usp=sharing

@aliabid94 aliabid94 requested review from abidlabs and pngwn October 13, 2022 07:27
@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-2455-all-demos

@aliabid94
Copy link
Copy Markdown
Contributor Author

Still a draft because the colab.html code I copied from your colab @pngwn but I assumed that might not be the final version of that code. feel free to edit this PR directly

Comment thread gradio/routes.py Outdated
return JSONResponse(
content={
"error": f"This app has no endpoint /api/{api_name}/."
"error": f"This app has no endpoint /run/{api_name}/."
Copy link
Copy Markdown
Member

@abidlabs abidlabs Oct 13, 2022

Choose a reason for hiding this comment

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

It looks like we are migrating from "/api" to "/run" as the official API endpoint. Let's make sure to update the "view API" page as well.

Also this is a bit of an edge case but what happens if a user-supplied api_name includes the string "api". Perhaps we can print a warning to users letting them know that their app will not work inside Colab

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Does colab block any call with a substring "api"? Anyways, even if they create a custom named API, the frontend does not use the names API's, so gradio UI will still work fine.

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

This is really cool stuff @aliabid94 and @pngwn!

I released a new version gradio==3.0.1b123 to test with the latest changes. However, this version is not working for me. Anytime I try to launch an Interface, I get "ValueError: When using queueing in Colab or when localhost is not accessible, a shareable link must be created. Please set share=True." I think I know the culprit (it's the if condition that I commented on above).

In the meantime, I'm testing with gradio==3.0.1b121 which seems to work

@abidlabs
Copy link
Copy Markdown
Member

Tested a bunch of different Gradio demos with gradio==3.0.1b121and generally everything seems to be working! Some feedback:

  • When you launch a demo on colab, some text is printed (the URL for localhost, etc.) before the demo overrides the text. The experience is a little disorienting, we should try to leave the text there or hide the text altogether

image

  • If you launch a demo, and have a few minutes of inactivity, the following error happens:

image

I thought this error only happens if you have queuing enabled, but I experienced with a very simple UI without the demo enabled. We should investigate what's going on here.

  • queueing() does not work. You may have fixed it after gradio==3.0.1b121 was released, but this needs to be tested

Copy link
Copy Markdown
Member

@pngwn pngwn left a comment

Choose a reason for hiding this comment

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

This is great. I think most of the frontend code is fine, left a few comments but I can't think of anything else we really need to cover.

Comment thread gradio/templates/colab.html Outdated
Comment thread gradio/templates/colab.html Outdated
Comment thread gradio/templates/colab.html Outdated
@abidlabs abidlabs mentioned this pull request Oct 14, 2022
@aliabid94 aliabid94 marked this pull request as ready for review October 18, 2022 08:12
@aliabid94 aliabid94 marked this pull request as draft October 18, 2022 08:16
Comment thread gradio/templates/colab.html Outdated
@aliabid94
Copy link
Copy Markdown
Contributor Author

Okay everything should be fixed!

  • Copied logic of internal google.colab.output.serve_kernel_port_as_iframe and modified it to allow iframe permissions
  • Window sizes now respect height and width
  • File downloads do not respect the port proxying, so implemented something that detects colab, and if so, opens file downloads in a new tab (which works)
  • Updated language and comments as suggested

I think it's good to go!

@abidlabs
Copy link
Copy Markdown
Member

abidlabs commented Nov 2, 2022

LGTM, I can confirm that the issues above have been fixed 🚀 🚀

Made a very small tweak to the printed message to link to the GitHub repo

@freddyaboulton
Copy link
Copy Markdown
Collaborator

What's the right version to test with? 3.0.1b121?

@abidlabs
Copy link
Copy Markdown
Member

abidlabs commented Nov 2, 2022

What's the right version to test with? 3.0.1b121?

It looks like @aliabid94 modified the original Colab to install from this branch instead of pypi

@aliabid94 aliabid94 merged commit 91abb3a into main Nov 3, 2022
@aliabid94 aliabid94 deleted the rename_api_for_colab branch November 3, 2022 22:51
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.

4 participants