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

Do not reload code inside gr.NO_RELOAD context #7684

Merged
merged 32 commits into from Mar 21, 2024
Merged

Conversation

freddyaboulton
Copy link
Collaborator

@freddyaboulton freddyaboulton commented Mar 12, 2024

Description

Adds a gr.NO_RELOAD symbol. Any code in a if gr.NO_RELOAD code-block will not be re-evaluated when the source file is reloaded. Helpful for importing modules that do not like to be reloaded (tiktoken, numpy) as well as database connections and long running set up code.

Sample usage:

import gradio as gr
import os

if gr.NO_RELOAD:
    print("IMPORTING")
    import tiktoken
    import numpy as np

def image_mod(image):
    print(np.zeros(5))
    return image.rotate(45)


cheetah = os.path.join(os.path.dirname(__file__), "images/lion.jpg")

demo = gr.Interface(image_mod, gr.Image(type="pil", value=cheetah, label="Input"), "image",
    flagging_options=["blurry", "incorrect", "other"], title="Testing a title!", examples=[
        os.path.join(os.path.dirname(__file__), "images/lion.jpg"),
        os.path.join(os.path.dirname(__file__), "images/cheetah1.jpg")
        ])

if __name__ == "__main__":
    demo.launch()

Attached gif below, note that the "IMPORTING" statement is not printed during reloads (only when first running the file as expected).

reload_no_import

Also tested:

  • modifying a file imported in the main demo file. Works as expected.
  • gradio cc dev. Works as expected
  • Also adds a reload mode test

Closes: #5402
Closes: #7123
Closes #6214

🎯 PRs Should Target Issues

Before your create a PR, please check to see if there is an existing issue for this change. If not, please create an issue before you create this PR, unless the fix is very small.

Not adhering to this guideline will result in the PR being closed.

Tests

  1. PRs will only be merged if tests pass on CI. To run the tests locally, please set up your Gradio environment locally and run the tests: bash scripts/run_all_tests.sh

  2. You may need to run the linters: bash scripts/format_backend.sh and bash scripts/format_frontend.sh

@gradio-pr-bot
Copy link
Contributor

gradio-pr-bot commented Mar 12, 2024

🪼 branch checks and previews

Name Status URL
Spaces ready! Spaces preview
Website ready! Website preview
Storybook ready! Storybook preview
🦄 Changes detected! Details

Install Gradio from this PR

pip install https://gradio-builds.s3.amazonaws.com/79f17950ca3192cebd3a5da3e416ca644ddcefd7/gradio-4.22.0-py3-none-any.whl

Install Gradio Python Client from this PR

pip install "gradio-client @ git+https://github.com/gradio-app/gradio@79f17950ca3192cebd3a5da3e416ca644ddcefd7#subdirectory=client/python"

@gradio-pr-bot
Copy link
Contributor

gradio-pr-bot commented Mar 12, 2024

🦄 change detected

This Pull Request includes changes to the following packages.

Package Version
@gradio/app minor
gradio minor
website minor
  • Maintainers can select this checkbox to manually select packages to update.

With the following changelog entry.

Do not reload code inside gr.NO_RELOAD context

Maintainers or the PR author can modify the PR title to modify this entry.

Something isn't right?

  • Maintainers can change the version label to modify the version bump.
  • If the bot has failed to detect any changes, or if this pull request needs to update multiple packages to different versions or requires a more comprehensive changelog entry, maintainers can update the changelog file directly.

Comment on lines 109 to 114
temp_dir = tempfile.mkdtemp()
for file in Path(s).rglob("*.py"):
src = file
dst = Path(temp_dir) / file.relative_to(s)
dst.parent.mkdir(parents=True, exist_ok=True)
shutil.copy(src, dst)
Copy link
Contributor

Choose a reason for hiding this comment

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

This will lead to interesting performance characteristics if the the tempdir ends up on a different file system, I'm sure.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I realized we don't actually need to do the copying

@freddyaboulton freddyaboulton changed the title Do not reload code inside gr.no_reload() context Do not reload code inside gr.NO_RELOAD context Mar 14, 2024
@abidlabs
Copy link
Member

Would be good add gr.NO_RELOAD here: https://www.gradio.app/guides/developing-faster-with-reload-mode

@freddyaboulton
Copy link
Collaborator Author

Open for review - can wait until the release goes out!

gradio/utils.py Outdated
node.body = [ast.Pass()]

# convert tree to string
code_removed = ast.unparse(tree)
Copy link
Member

Choose a reason for hiding this comment

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

It looks like ast.unparse was added in Python 3.9: https://docs.python.org/3/library/ast.html#ast.unparse

If we absolutely need this method, we could use this third-party implementation: https://github.com/simonpercivall/astunparse

Copy link
Member

Choose a reason for hiding this comment

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

Btw we should confirm that the reload tests are using 3.8 (otherwise, should not have passed)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Only 7 months until 3.8 EOL 🤩

I think we can manually modify the source code string so will look into it

Copy link
Member

Choose a reason for hiding this comment

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

Thanks @freddyaboulton, ping me whenever I can take another pass at the pr

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@abidlabs this should be good for review now

demo/calculator/run.py Outdated Show resolved Hide resolved
gradio/networking.py Outdated Show resolved Hide resolved
@abidlabs
Copy link
Member

abidlabs commented Mar 21, 2024

Very cool @freddyaboulton and easy to use!

As I was testing, I noticed two things:

  1. Now, when I run a file using gradio app.py, the entire contents of the file get executed twice initially. To repro, create a file called app.py with these contents:
import gradio as gr
import numpy as np

print("2") 

if gr.NO_RELOAD:
    print("1")

demo = gr.Interface(
    lambda s: s, gr.Image(np.random.random((255, 255, 3))), gr.Image()
)

if __name__ == "__main__":
    demo.launch()

You'll see that this is printed to the terminal:
2
1
2
1

  1. I had expected that if gr.NO_RELOAD: is present in a file that is imported from the main file, it would also work -- since not all heavy imports are necessarily included in the main file. However, that does not seem to be the case. For example, if you have a second file called run.py and you import it from app.py with:
import gradio as gr

if gr.NO_RELOAD:
    print("3")

You'll noticed that "3" will be printed to the terminal every time that the original file is changed.

@freddyaboulton
Copy link
Collaborator Author

Thanks for the review @abidlabs ! I've addressed all of the comments you left in the PR. For point 1, that's expected. In order to reload the modules in the thread running the reloader, the modules need to be first imported. Point 2 should be fixed now!

Copy link
Member

@abidlabs abidlabs left a comment

Choose a reason for hiding this comment

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

Good stuff @freddyaboulton! Love the fact that we now have a test for reload mode

@freddyaboulton
Copy link
Collaborator Author

Thanks for the review @abidlabs ! Excited to release this

@freddyaboulton freddyaboulton merged commit 755157f into main Mar 21, 2024
8 checks passed
@freddyaboulton freddyaboulton deleted the reload-mode-symbols branch March 21, 2024 19:59
@pngwn pngwn mentioned this pull request Mar 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants