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

Add open in colab buttons to demos in docs and /demos #2608

Merged
merged 45 commits into from
Nov 23, 2022
Merged

Conversation

aliabd
Copy link
Collaborator

@aliabd aliabd commented Nov 5, 2022

Adds buttons to open a demo in colab:

Untitled.mov

If a contributor wants to change a demo in the /demo dir, they just have to run /demo/generate_notebooks.py to regenerate all the .ipynb notebooks. I could create a github action for this but seems like overkill.

Note that not all the demos will work on colab, the ones that reference another file in the dir won't work (for example). I could detect this and not show a colab button for those, but I feel like it would still be useful for a reader to be able to play with the code (so don't know if it's the right idea to skip them), and usually you can easily replace the file (I could link to the dir in github so they can move the files they need?)

Closes: #2278

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

@aliabd aliabd requested review from abidlabs, aliabid94 and freddyaboulton and removed request for abidlabs and aliabid94 November 5, 2022 02:55
@github-actions
Copy link
Contributor

github-actions bot commented Nov 5, 2022

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

@abidlabs
Copy link
Member

abidlabs commented Nov 6, 2022

Wow this is great @aliabd! LGTM with a few suggestions on the points you raised:

If a contributor wants to change a demo in the /demo dir, they just have to run /demo/generate_notebooks.py to regenerate all the .ipynb notebooks. I could create a github action for this but seems like overkill.

I took a look at the /demo directory and we do edit it relatively frequently so I actually think it would be good to have a GitHub action to run to make sure no demos and notebooks go out of sync.

Note that not all the demos will work on colab, the ones that reference another file in the dir won't work

I think that's fine. I think it's pretty clear to users when this happens and how to fix it. As a general note, perhaps we should switch to using URLs in our demos instead as much as possible

@aliabid94
Copy link
Collaborator

This is great.

I think there should be a github action that opens a commit if a demo changes and the notebook hasn't.
As for demos with files in the folder, add a cell block before the demo that curls and downloads the files into the colab environment, so the paths resolve the same way.

@aliabd
Copy link
Collaborator Author

aliabd commented Nov 20, 2022

Ok I updated this:

  • There's now a github check that will run on every PR that changes something in the /demo path. If the demo run.py has changed but not the generated notebook, the check fails. (check works by running python generate_notebooks.py and asserting that there's nothing to commit). You can see an example of it failing in this fake commit here, and an example of it passing once I regenerated the notebooks here.
  • When the notebooks are generated, we automatically add a cell to wget the necessary files from the demo folder (and put them in the correct structure) so every demo should run immediately.

import random

GRADIO_DEMO_DIR = os.getcwd()
DEMOS_TO_SKIP = {"all_demos", "reset_components", "custom_path", "kitchen_sink_random"}
Copy link
Member

Choose a reason for hiding this comment

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

Why are we skipping "reset_components", "custom_path", "kitchen_sink_random"

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

reset_components and kitchen_sink_random import from other demos so can't be used on colab alone. We also skip them when uploading to spaces because they don't work there either. They're only used for the PR demos. (along with all_demos)

custom_path is a demo of launching gradio using uvicorn from the terminal, so not really useful on colab either (also skipped on spaces)

@abidlabs
Copy link
Member

Awesome PR @aliabd! Left a couple of nits above but otherwise LGTM.

One suggestion -- if the CI check fails, would it be possible to print the command that needs to be run to regenerate the notebooks?

@github-actions
Copy link
Contributor

The demo notebooks don't match the run.py files. Please run this command from the root of the repo and then commit the changes:

pip install nbformat && cd demo && python generate_notebooks.py

@aliabd
Copy link
Collaborator Author

aliabd commented Nov 21, 2022

@abidlabs thanks for the suggestion. The github action will now comment on the PR if the check fails with the command that needs to be run. The last two commits show an example of this.

@github-actions
Copy link
Contributor

The demo notebooks don't match the run.py files. Please run this command from the root of the repo and then commit the changes:

pip install nbformat && cd demo && python generate_notebooks.py

- name: Assert Notebooks Match
id: assertNotebooksMatch
run: git status | grep "nothing to commit, working tree clean"
- name: Comment On PR
Copy link
Collaborator

Choose a reason for hiding this comment

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

Does this run on forked repos?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good question, I just checked on this PR #2691

The check fails as expected, but it can't comment. Do you know how to fix that? Apparently it's an open issue.

Copy link
Collaborator

Choose a reason for hiding this comment

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

We need to follow this approach I think: https://securitylab.github.com/research/github-actions-preventing-pwn-requests/#:~:text=The%20main%20differences%20between%20the,but%20not%20from%20external%20forks. I think it may also be possible to comment on the pr if the action is defined in a separate repo? I think that's what doc-builder does.

@pngwn May now some best practices for commenting on PRs from forks.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ah nice

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.

Super cool @aliabd !

content = json.loads(content)
for i, cell in enumerate(content["cells"]):
random.seed(i)
cell["id"] = random.getrandbits(128)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Just wondering why this is neccessary?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good question...

The way the github check works is by running python generate_notebooks.py and seeing if there's anything to commit. If there is, it must mean that run.py files and run.ipynb notebooks don't match.

The problem is that jupyter notebooks create a random id for every cell that's different each time, and creates a huge mess on git diffs even when the notebook is functionally the same. So everytime python generate_notebooks.py is run it will show that every notebook has changed. There's no way to set the id using nbformat so instead I just manually set them in the file from python and define a seed to make sure they're always the same.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Very cool - thanks for explaining!

@aliabd
Copy link
Collaborator Author

aliabd commented Nov 22, 2022

@freddyaboulton I updated this to use pull_request_target and the action now works as expected for both branches and forks. Thanks for the suggestion! Tested here in a branch and a fork.

Copy link
Collaborator

@aliabid94 aliabid94 left a comment

Choose a reason for hiding this comment

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

looks great!

@aliabd aliabd merged commit 67275ec into main Nov 23, 2022
@aliabd aliabd deleted the aliabd/colab-buttons branch November 23, 2022 00:56
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.

Add "Run in Colab" Links to demos on the website Demos Page
4 participants