Skip to content

Improve UI for lazy caching #10873

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

Merged
merged 7 commits into from
Mar 25, 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/puny-jokes-clap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"gradio": patch
---

fix:Improve UI for lazy caching
23 changes: 16 additions & 7 deletions gradio/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,20 +348,29 @@ def create(self) -> None:

if self.cache_examples:

def load_example_with_output(example_tuple):
example_id, example_value = example_tuple
processed_example = self._get_processed_example(
example_value
) + self.load_from_cache(example_id)
def load_example_input(example_tuple):
_, example_value = example_tuple
processed_example = self._get_processed_example(example_value)
return utils.resolve_singleton(processed_example)

def load_example_output(example_tuple):
example_id, _ = example_tuple
cached_outputs = self.load_from_cache(example_id)
return utils.resolve_singleton(cached_outputs)

self.cache_event = self.load_input_event = self.dataset.click(
load_example_with_output,
load_example_input,
inputs=[self.dataset],
outputs=self.inputs + self.outputs, # type: ignore
outputs=self.inputs,
show_progress="hidden",
postprocess=False,
queue=False,
show_api=False,
).then(
load_example_output,
inputs=[self.dataset],
outputs=self.outputs,
postprocess=False,
api_name=self.api_name,
show_api=False,
)
Expand Down
34 changes: 8 additions & 26 deletions test/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,21 +213,13 @@ def combine(a, b):
)

with connect(demo) as client:
assert client.predict(1, api_name="/examples") == (
"hello",
"Eve",
"hello Eve",
)
assert client.predict(1, api_name="/examples") == "hello Eve"

# Let the server shut down
time.sleep(1)

with connect(demo) as client:
assert client.predict(1, api_name="/examples") == (
"hello",
"Eve",
"hello Eve",
)
assert client.predict(1, api_name="/examples") == "hello Eve"


@patch("gradio.utils.get_cache_folder", return_value=Path(tempfile.mkdtemp()))
Expand Down Expand Up @@ -264,18 +256,10 @@ def combine(a, b):
)

with connect(demo) as client:
assert client.predict(1, api_name="/examples") == (
"hello",
"Eve",
"hello Eve",
)
assert client.predict(1, api_name="/examples") == "hello Eve"

with connect(demo) as client:
assert client.predict(1, api_name="/examples") == (
"hello",
"Eve",
"hello Eve",
)
assert client.predict(1, api_name="/examples") == "hello Eve"

def test_caching_image(self, patched_cache_folder, connect):
io = gr.Interface(
Expand Down Expand Up @@ -616,10 +600,10 @@ def concatenate(str1, str2):
client = TestClient(app)

response = client.post(f"{API_PREFIX}/api/load_example/", json={"data": [0]})
assert response.json()["data"] == ["Hello,", "World", "Hello, World"]
assert response.json()["data"] == ["Hello, World"]

response = client.post(f"{API_PREFIX}/api/load_example/", json={"data": [1]})
assert response.json()["data"] == ["Michael", "Jordan", "Michael Jordan"]
assert response.json()["data"] == ["Michael Jordan"]

def test_end_to_end_lazy_cache_examples(self, patched_cache_folder):
def image_identity(image, string):
Expand Down Expand Up @@ -648,13 +632,11 @@ def image_identity(image, string):

response = client.post(f"{API_PREFIX}/api/load_example/", json={"data": [0]})
data = response.json()["data"]
assert data[0]["path"].endswith("cheetah1.jpg")
assert data[1] == "cheetah"
assert data[0]["path"].endswith("image.webp")

response = client.post(f"{API_PREFIX}/api/load_example/", json={"data": [1]})
data = response.json()["data"]
assert data[0]["path"].endswith("bus.png")
assert data[1] == "bus"
assert data[0]["path"].endswith("image.webp")


def test_multiple_file_flagging(tmp_path, connect):
Expand Down
Loading