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

Support gr.load()-ing Gradio apps with Blocks.load() events #10324

Merged
merged 10 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tall-geese-hammer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"gradio": patch
---

fix:Support `gr.load()`-ing Gradio apps with `Blocks.load()` events
46 changes: 35 additions & 11 deletions gradio/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
utils,
wasm_utils,
)
from gradio.blocks_events import BlocksEvents, BlocksMeta
from gradio.blocks_events import BLOCKS_EVENTS, BlocksEvents, BlocksMeta
from gradio.context import (
Context,
LocalContext,
Expand Down Expand Up @@ -1315,21 +1315,14 @@ def iterate_over_children(children_list):
)
dependency["no_target"] = True
else:
targets = [
getattr(
original_mapping[
target if isinstance(target, int) else target[0]
],
trigger if isinstance(target, int) else target[1],
)
for target in _targets
]
targets = [
EventListenerMethod(
t.__self__ if t.has_trigger else None,
t.event_name, # type: ignore
)
for t in targets
for t in Blocks.get_event_targets(
original_mapping, _targets, trigger
)
]
dependency = root_block.default_config.set_event_trigger(
targets=targets, fn=fn, **dependency
Expand Down Expand Up @@ -1433,6 +1426,11 @@ def render(self):
]
for dependency in self.fns.values():
dependency._id += dependency_offset
# Any event -- e.g. Blocks.load() -- that is triggered by this Blocks
# should now be triggered by the root Blocks instead.
for target in dependency.targets:
if target[0] == self._id:
target = (Context.root_block._id, target[1])
api_name = dependency.api_name
if isinstance(api_name, str):
api_name_ = utils.append_unique_suffix(
Expand Down Expand Up @@ -3006,3 +3004,29 @@ def get_api_info(self, all_endpoints: bool = False) -> dict[str, Any] | None:
api_info["named_endpoints"][f"/{fn.api_name}"] = dependency_info

return api_info

@staticmethod
def get_event_targets(
original_mapping: dict[int, Block], _targets: list, trigger: str
) -> list:
target_events = []
for target in _targets:
# If target is just an integer (old format), use it directly with the trigger
# Otherwise target is a tuple and we use its components
target_id = target if isinstance(target, int) else target[0]
event_name = trigger if isinstance(target, int) else target[1]
block = original_mapping.get(target_id)
# Blocks events are a special case because they are not stored in the blocks list in the config
if block is None:
if event_name in [
event.event_name if isinstance(event, EventListener) else event
for event in BLOCKS_EVENTS
]:
block = Context.root_block
else:
raise ValueError(
f"Cannot find Block with id: {target_id} but is present as a target in the config"
)
event = getattr(block, event_name)
target_events.append(event)
return target_events
41 changes: 26 additions & 15 deletions gradio/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ def safe_deepcopy(obj: Any) -> Any:


def assert_configs_are_equivalent_besides_ids(
config1: dict, config2: dict, root_keys: tuple = ("mode",)
config1: BlocksConfigDict, config2: BlocksConfigDict, root_keys: tuple = ("mode",)
):
"""Allows you to test if two different Blocks configs produce the same demo.

Expand Down Expand Up @@ -563,20 +563,31 @@ def same_children_recursive(children1, chidren2):
if "children" in child1 or "children" in child2:
same_children_recursive(child1["children"], child2["children"])

children1 = config1["layout"]["children"]
children2 = config2["layout"]["children"]
same_children_recursive(children1, children2)

for d1, d2 in zip(config1["dependencies"], config2["dependencies"], strict=False):
for t1, t2 in zip(d1.pop("targets"), d2.pop("targets"), strict=False):
assert_same_components(t1[0], t2[0])
for i1, i2 in zip(d1.pop("inputs"), d2.pop("inputs"), strict=False):
assert_same_components(i1, i2)
for o1, o2 in zip(d1.pop("outputs"), d2.pop("outputs"), strict=False):
assert_same_components(o1, o2)

if d1 != d2:
raise ValueError(f"{d1} does not match {d2}")
if "layout" in config1:
if "layout" not in config2:
raise ValueError(
"The first config has a layout key, but the second does not"
)
children1 = config1["layout"]["children"]
children2 = config2["layout"]["children"]
same_children_recursive(children1, children2)

if "dependencies" in config1:
if "dependencies" not in config2:
raise ValueError(
"The first config has a dependencies key, but the second does not"
)
for d1, d2 in zip(
config1["dependencies"], config2["dependencies"], strict=False
):
for t1, t2 in zip(d1.pop("targets"), d2.pop("targets"), strict=False):
assert_same_components(t1[0], t2[0])
for i1, i2 in zip(d1.pop("inputs"), d2.pop("inputs"), strict=False):
assert_same_components(i1, i2)
for o1, o2 in zip(d1.pop("outputs"), d2.pop("outputs"), strict=False):
assert_same_components(o1, o2)
if d1 != d2:
raise ValueError(f"{d1} does not match {d2}")

return True

Expand Down
15 changes: 14 additions & 1 deletion test/test_blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,20 @@ def update(name):
for component in config1["components"]:
component["props"]["proxy_url"] = f"{fake_url}/"
config2 = demo2.get_config_file()
assert assert_configs_are_equivalent_besides_ids(config1, config2) # type: ignore
assert assert_configs_are_equivalent_besides_ids(config1, config2)

def test_load_from_config_with_blocks_events(self):
fake_url = "https://fake.hf.space"

def fn():
return "Hello"

with gr.Blocks() as demo:
t = gr.Textbox()
demo.load(fn, None, t)

config = demo.get_config_file()
gr.Blocks.from_config(config, [fn], fake_url) # Should not raise

def test_partial_fn_in_config(self):
def greet(name, formatter):
Expand Down
Loading