Fix loading interface with examples (#2640)

* fix examples

* Fix test

* Add unit test

* Add CHANGELOG

* Add unit test where load external

* fix for older demos

* Add comment
This commit is contained in:
Freddy Boulton 2022-11-15 17:20:59 +01:00 committed by GitHub
parent a8f961ec7e
commit ebb65eb9ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 21 additions and 0 deletions

View File

@ -5,6 +5,7 @@ No changes to highlight.
## Bug Fixes:
* Updated the minimum FastApi used in tests to version 0.87 [@freddyaboulton](https://github.com/freddyaboulton) in [PR 2647](https://github.com/gradio-app/gradio/pull/2647)
* Fixed bug where interfaces with examples could not be loaded with `gr.Interface.load` by [@freddyaboulton](https://github.com/freddyaboulton) [PR 2640](https://github.com/gradio-app/gradio/pull/2640)
## Documentation Changes:
No changes to highlight.

View File

@ -622,6 +622,13 @@ class Blocks(BlockContext):
# add the event triggers
for dependency, fn in zip(config["dependencies"], fns):
# We used to add a "fake_event" to the config to cache examples
# without removing it. This was causing bugs in calling gr.Interface.load
# We fixed the issue by removing "fake_event" from the config in examples.py
# but we still need to skip these events when loading the config to support
# older demos
if dependency["trigger"] == "fake_event":
continue
targets = dependency.pop("targets")
trigger = dependency.pop("trigger")
dependency.pop("backend_fn")

View File

@ -296,6 +296,9 @@ class Examples:
if self.batch:
output = [value[0] for value in output]
cache_logger.flag(output)
# Remove the "fake_event" to prevent bugs in loading interfaces from spaces
Context.root_block.dependencies.remove(dependency)
Context.root_block.fns.pop(fn_index)
async def load_from_cache(self, example_id: int) -> List[Any]:
"""Loads a particular cached example for the interface.

View File

@ -179,6 +179,7 @@ class TestProcessExamples:
)
prediction = await io.examples_handler.load_from_cache(0)
assert prediction == [{"lines": 4, "__type__": "update"}, {"label": "lion"}]
assert not any(d["trigger"] == "fake_event" for d in io.config["dependencies"])
def test_raise_helpful_error_message_if_providing_partial_examples(self, tmp_path):
def foo(a, b):

View File

@ -274,6 +274,15 @@ class TestLoadInterfaceWithExamples:
]
)
def test_interface_with_examples(self):
# This demo has the "fake_event" correctly removed
demo = gr.Interface.load("spaces/freddyaboulton/calculator")
assert demo(2, "add", 3) == 5
# This demo still has the "fake_event". both should work
demo = gr.Interface.load("spaces/abidlabs/test-calculator-2")
assert demo(2, "add", 4) == 6
def test_get_tabular_examples_replaces_nan_with_str_nan():
readme = """