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
Conversation
🪼 branch checks and previews
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" |
🦄 change detectedThis Pull Request includes changes to the following packages.
With the following changelog entry.
Maintainers or the PR author can modify the PR title to modify this entry.
|
4667d00
to
e18d258
Compare
gradio/cli/commands/reload.py
Outdated
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) |
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.
This will lead to interesting performance characteristics if the the tempdir ends up on a different file system, I'm sure.
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.
I realized we don't actually need to do the copying
08d5d62
to
b0df3d3
Compare
e2f3472
to
966c2d5
Compare
Would be good add gr.NO_RELOAD here: https://www.gradio.app/guides/developing-faster-with-reload-mode |
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) |
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.
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
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.
Btw we should confirm that the reload tests are using 3.8 (otherwise, should not have passed)
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.
Only 7 months until 3.8 EOL 🤩
I think we can manually modify the source code string so will look into it
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.
Thanks @freddyaboulton, ping me whenever I can take another pass at the pr
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.
@abidlabs this should be good for review now
guides/09_other-tutorials/developing-faster-with-reload-mode.md
Outdated
Show resolved
Hide resolved
js/_website/src/routes/[[version]]/docs/no-reload/+page.server.ts
Outdated
Show resolved
Hide resolved
js/_website/src/routes/[[version]]/docs/no-reload/+page.server.ts
Outdated
Show resolved
Hide resolved
guides/09_other-tutorials/developing-faster-with-reload-mode.md
Outdated
Show resolved
Hide resolved
Very cool @freddyaboulton and easy to use! As I was testing, I noticed two things:
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:
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. |
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! |
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.
Good stuff @freddyaboulton! Love the fact that we now have a test for reload mode
Thanks for the review @abidlabs ! Excited to release this |
Description
Adds a
gr.NO_RELOAD
symbol. Any code in aif 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:
Attached gif below, note that the "IMPORTING" statement is not printed during reloads (only when first running the file as expected).
Also tested:
gradio cc dev
. Works as expectedCloses: #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
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
You may need to run the linters:
bash scripts/format_backend.sh
andbash scripts/format_frontend.sh