Skip to content

Commit 8abf0d2

Browse files
committed
changes
1 parent 963e774 commit 8abf0d2

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

demo/cache_manual_demo/run.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
"""Demo showcasing simple manual caching with gr.Cache()."""
2+
3+
import time
4+
5+
import gradio as gr
6+
7+
WEATHER_BY_CITY = {
8+
"san francisco": ("Foggy", 61),
9+
"new york": ("Cloudy", 72),
10+
"tokyo": ("Sunny", 78),
11+
"london": ("Rainy", 58),
12+
"nairobi": ("Clear", 75),
13+
}
14+
15+
16+
def normalize_city(city: str) -> str:
17+
return " ".join(city.lower().strip().split())
18+
19+
20+
def lookup_weather(city: str, c=gr.Cache()):
21+
if not city.strip():
22+
return "", "Enter a city name.", ""
23+
24+
cache_key = normalize_city(city)
25+
cached = c.get(cache_key)
26+
if cached is not None:
27+
return cached["forecast"], "Cache hit", cache_key
28+
29+
time.sleep(2)
30+
condition, temperature = WEATHER_BY_CITY.get(cache_key, ("Windy", 68))
31+
forecast = (
32+
f"{city.strip()}: {condition}, {temperature} degF.\n"
33+
f"Normalized cache key: {cache_key}"
34+
)
35+
c.set(cache_key, forecast=forecast)
36+
return forecast, "Computed and stored", cache_key
37+
38+
39+
with gr.Blocks(title="gr.Cache() Demo") as demo:
40+
gr.Markdown(
41+
"# `gr.Cache()` Demo\n"
42+
"This demo manually caches a normalized city lookup. "
43+
"Try the same city twice, or vary capitalization and spacing "
44+
"to reuse the same cached result."
45+
)
46+
47+
city = gr.Textbox(label="City", value=" San Francisco ")
48+
forecast = gr.Textbox(label="Forecast", lines=3)
49+
status = gr.Textbox(label="Status")
50+
cache_key = gr.Textbox(label="Cache key used")
51+
52+
gr.Button("Lookup").click(lookup_weather, city, [forecast, status, cache_key])
53+
54+
55+
if __name__ == "__main__":
56+
demo.launch()

guides/04_additional-features/17_caching.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
ML inference is often expensive: image classification, text generation, and audio synthesis can each take seconds or more. If a user submits the same inputs twice, there's no reason to re-run the model. Gradio provides two caching mechanisms: `@gr.cache` for automatic exact-match caching, and `gr.Cache()` for manual cache control inside your functions.
44

5+
## Demo Apps
6+
7+
Try the caching patterns in these demos:
8+
9+
- [`@gr.cache()` function types demo](https://github.com/gradio-app/gradio/blob/main/demo/cache_demo/run.py) - sync, async, generator, and async generator caching
10+
- [`gr.Cache()` manual cache demo](https://github.com/gradio-app/gradio/blob/main/demo/cache_manual_demo/run.py) - normalized manual cache keys with explicit `get` / `set`
11+
- [`gr.Cache()` KV cache demo](https://github.com/gradio-app/gradio/blob/main/demo/cache_kv_demo/run.py) - transformer prefix reuse with cached KV state
12+
513
## `@gr.cache` — Automatic Caching
614

715
Add `@gr.cache` to any function to automatically cache its results. The decorator hashes inputs by their content — two different numpy arrays with the same pixel values will produce a cache hit. Cache hits bypass the Gradio queue entirely.
@@ -81,6 +89,8 @@ def my_function(prompt, c=gr.Cache()):
8189

8290
If a queued function gets a successful hit from `c.get(...)`, Gradio also shows a timing badge in the UI. This badge says `used cache` instead of `from cache`, because the request still ran, but part of its work was reused from `gr.Cache()`.
8391

92+
A minimal example is available in the [`gr.Cache()` manual cache demo](https://github.com/gradio-app/gradio/blob/main/demo/cache_manual_demo/run.py).
93+
8494
### Why use `gr.Cache()` over a plain dict?
8595

8696
- **Thread-safe** — built-in locking for concurrent requests
@@ -112,6 +122,8 @@ def generate(prompt, c=gr.Cache(per_session=True)):
112122
return output.text
113123
```
114124

125+
For a full runnable version, see the [`gr.Cache()` KV cache demo](https://github.com/gradio-app/gradio/blob/main/demo/cache_kv_demo/run.py).
126+
115127

116128
## When to Use Caching
117129

0 commit comments

Comments
 (0)