Repository: gradio-app/gradio
Pull request: Fixes issue 5781: Enables specifying a caching directory for Examples
## Description

Please include a concise summary, in clear English, of the changes in this pull request. If it closes an issue, please mention it here.

Closes: #5781 

## 🎯 PRs Should Target Issues

Before your create a PR, please check to see if there is [an existing issue](https://github.com/gradio-app/gradio/issues) for this change. If not, please create an issue before you create this PR, unless the fix is very small. 

Not adhering to this guideline will result in the PR being closed. 

## Tests

1. PRs will only be merged if tests pass on CI. To run the tests locally, please set up [your Gradio environment locally](https://github.com/gradio-app/gradio/blob/main/CONTRIBUTING.md) and run the tests: `bash scripts/run_all_tests.sh`

2. You may need to run the linters: `bash scripts/format_backend.sh` and `bash scripts/format_frontend.sh`
  


Problem statement:
Specify Example Caching Directory
- [x] I have searched to see if a similar issue already exists.


**Is your feature request related to a problem? Please describe.**  
Would be great to specify the directory gradio uses to cache examples. This would allow developers to save the cache to persistent storage on HF Spaces.

**Describe the solution you'd like**  
Support specifying the cached directory via environment variable and a parameter in the examples class.

**Additional context**  
Add any other context or screenshots about the feature request here.

Hints from issue discussion:
Heya, could you please elaborate your request. I'm interested on working on this but need some guidance. thank you <3
@freddyaboulton Why is this request only examples related? I think Gradio is using Python's `tempfile` module. You can actual specifiy the directory via ENV if this is what you are looking for. From my understanding, there is no concept of Gradio know much about the temporary files - could be solved via namespaces but general not an easy task.

> On Windows modify the TEMP environment variable, while on Unix based operating systems, alter the TMPDIR instead.
For files created during the application's lifetime, users can determine the directory where those files are created via `GRADIO_TEMP_DIR` variable. That can't be done for examples. They always get cached to `gradio_cached_examples` in the directory where the python launch command was called from.
Hello @freddyaboulton, I would love to work on this issue actively, but I am not able to understand what is required to be done.
And like from which folder am I supposed to work on, I'm not able to understand.
Kindly guide me through.
Thanks :)

Commit message: backend formatted

Patch to review:
diff --git a/gradio/helpers.py b/gradio/helpers.py
index 53fdc799e2..cd8c56aee8 100644
--- a/gradio/helpers.py
+++ b/gradio/helpers.py
@@ -33,7 +33,10 @@
 if TYPE_CHECKING:  # Only import for type checking (to avoid circular imports).
     from gradio.components import Component
 
-CACHED_FOLDER = "gradio_cached_examples"
+CACHED_FOLDER = str(
+    Path(os.environ.get("GRADIO_EXAMPLES_CACHE") or "gradio_cached_examples").resolve()
+)
+
 LOG_FILE = "log.csv"
 
 set_documentation_group("helpers")
diff --git a/guides/02_building-interfaces/03_more-on-examples.md b/guides/02_building-interfaces/03_more-on-examples.md
index 2860dc903f..0a4a30620a 100644
--- a/guides/02_building-interfaces/03_more-on-examples.md
+++ b/guides/02_building-interfaces/03_more-on-examples.md
@@ -34,7 +34,7 @@ Sometimes your app has many input components, but you would only like to provide
 ## Caching examples
 
 You may wish to provide some cached examples of your model for users to quickly try out, in case your model takes a while to run normally.
-If `cache_examples=True`, the `Interface` will run all of your examples through your app and save the outputs when you call the `launch()` method. This data will be saved in a directory called `gradio_cached_examples`.
+If `cache_examples=True`, the `Interface` will run all of your examples through your app and save the outputs when you call the `launch()` method. This data will be saved in a directory called `gradio_cached_examples` by default. You can also set this directory with the `GRADIO_EXAMPLES_CACHE` environment variable.
 
 Whenever a user clicks on an example, the output will automatically be populated in the app now, using data from this cached directory instead of actually running the function. This is useful so users can quickly try out your model without adding any load!
 


Code review comments:
diff_hunk: @@ -33,7 +33,10 @@
 if TYPE_CHECKING:  # Only import for type checking (to avoid circular imports).
     from gradio.components import Component
 
-CACHED_FOLDER = "gradio_cached_examples"
+CACHED_FOLDER = str(
+    Path(os.environ.get("GRADIO_EXAMPLES_CACHE") or "gradio_cached_examples").resolve()
+)
path: gradio/helpers.py
text: @user1:
I think we can just do:
```suggestion
CACHED_FOLDER = os.environ.get("GRADIO_EXAMPLES_CACHE", "gradio_cached_examples")
```
since we resolve it to an absolute path later when we use this.