Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* This works * Add code * Final touches * Lint * Fix bug in other dirs * add changeset * Reload * lint + test * Load from frontend * add changeset * Use key * tweak frontend config generation * tweak * WIP ipython * Fix robust * fix * Fix for jupyter notebook * Add checks * Lint frontend * Undo demo changes * add changeset * Use is_in_or_equal * python 3.11 changes and no if __name__ * Forward sys.argv + guide * lint --------- Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com> Co-authored-by: pngwn <hello@pngwn.io> Co-authored-by: Abubakar Abid <abubakar@huggingface.co>
- Loading branch information
1 parent
63b7a3c
commit 119c834
Showing
15 changed files
with
581 additions
and
194 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"@gradio/app": minor | ||
"@gradio/client": minor | ||
"gradio": minor | ||
--- | ||
|
||
feat:Faster reload mode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,89 @@ | ||
try: | ||
from IPython.core.magic import needs_local_scope, register_cell_magic | ||
from IPython.core.magic import ( | ||
needs_local_scope, | ||
register_cell_magic, | ||
) | ||
from IPython.core.magic_arguments import argument, magic_arguments, parse_argstring | ||
except ImportError: | ||
pass | ||
|
||
import warnings | ||
|
||
import gradio as gr | ||
from gradio.networking import App | ||
from gradio.utils import BaseReloader | ||
|
||
|
||
class CellIdTracker: | ||
"""Determines the most recently run cell in the notebook. | ||
Needed to keep track of which demo the user is updating. | ||
""" | ||
|
||
def __init__(self, ipython): | ||
ipython.events.register("pre_run_cell", self.pre_run_cell) | ||
self.shell = ipython | ||
self.current_cell: str = "" | ||
|
||
def pre_run_cell(self, info): | ||
self._current_cell = info.cell_id | ||
|
||
|
||
class JupyterReloader(BaseReloader): | ||
"""Swap a running blocks class in a notebook with the latest cell contents.""" | ||
|
||
def __init__(self, ipython) -> None: | ||
super().__init__() | ||
self._cell_tracker = CellIdTracker(ipython) | ||
self._running: dict[str, gr.Blocks] = {} | ||
|
||
@property | ||
def current_cell(self): | ||
return self._cell_tracker.current_cell | ||
|
||
@property | ||
def running_app(self) -> App: | ||
assert self.running_demo.server | ||
return self.running_demo.server.running_app | ||
|
||
@property | ||
def running_demo(self): | ||
return self._running[self.current_cell] | ||
|
||
def demo_tracked(self) -> bool: | ||
return self.current_cell in self._running | ||
|
||
def track(self, demo: gr.Blocks): | ||
self._running[self.current_cell] = demo | ||
|
||
|
||
def load_ipython_extension(ipython): | ||
__demo = gr.Blocks() | ||
reloader = JupyterReloader(ipython) | ||
|
||
@magic_arguments() | ||
@argument("--demo-name", default="demo", help="Name of gradio blocks instance.") | ||
@argument( | ||
"--share", | ||
default=False, | ||
const=True, | ||
nargs="?", | ||
help="Whether to launch with sharing. Will slow down reloading.", | ||
) | ||
@register_cell_magic | ||
@needs_local_scope | ||
def blocks(line, cell, local_ns=None): | ||
if "gr.Interface" in cell: | ||
warnings.warn( | ||
"Usage of gradio.Interface with %%blocks may result in errors." | ||
) | ||
with __demo.clear(): | ||
exec(cell, None, local_ns) | ||
__demo.launch(quiet=True) | ||
def blocks(line, cell, local_ns): | ||
"""Launch a demo defined in a cell in reload mode.""" | ||
|
||
args = parse_argstring(blocks, line) | ||
|
||
exec(cell, None, local_ns) | ||
demo: gr.Blocks = local_ns[args.demo_name] | ||
if not reloader.demo_tracked(): | ||
demo.launch(share=args.share) | ||
reloader.track(demo) | ||
elif reloader.queue_changed(demo): | ||
print("Queue got added or removed. Restarting demo.") | ||
reloader.running_demo.close() | ||
demo.launch() | ||
reloader.track(demo) | ||
else: | ||
reloader.swap_blocks(demo) | ||
return reloader.running_demo.artifact |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.