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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Control which files get moved to cache with gr.set_static_paths #7618
Changes from 9 commits
b335399
c361d28
a7198cd
522fe19
52cfe47
90df93f
7e62d10
437891b
33f7e5f
d0b054c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"gradio": patch | ||
"gradio_client": patch | ||
--- | ||
|
||
fix:Control which files get moved to cache with gr.set_static_paths |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -18,7 +18,7 @@ | |||||
from gradio_client import utils as client_utils | ||||||
from PIL import Image, ImageOps, PngImagePlugin | ||||||
|
||||||
from gradio import wasm_utils | ||||||
from gradio import utils, wasm_utils | ||||||
from gradio.data_classes import FileData, GradioModel, GradioRootModel | ||||||
from gradio.utils import abspath, get_upload_folder, is_in_or_equal | ||||||
|
||||||
|
@@ -262,6 +262,8 @@ def _move_to_cache(d: dict): | |||||
# This makes it so that the URL is not downloaded and speeds up event processing | ||||||
if payload.url and postprocess and client_utils.is_http_url_like(payload.url): | ||||||
payload.path = payload.url | ||||||
elif utils.is_static_file(payload): | ||||||
payload.path = payload.path | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done! |
||||||
elif not block.proxy_url: | ||||||
# If the file is on a remote server, do not move it to cache. | ||||||
if check_in_upload_folder and not client_utils.is_http_url_like( | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,10 +40,12 @@ | |
|
||
import anyio | ||
import httpx | ||
from gradio_client.documentation import document | ||
from typing_extensions import ParamSpec | ||
|
||
import gradio | ||
from gradio.context import Context | ||
from gradio.data_classes import FileData | ||
from gradio.strings import en | ||
|
||
if TYPE_CHECKING: # Only import for type checking (is False at runtime). | ||
|
@@ -958,6 +960,62 @@ def is_in_or_equal(path_1: str | Path, path_2: str | Path): | |
return True | ||
|
||
|
||
@document() | ||
def set_static_paths(paths: list[str | Path]) -> None: | ||
""" | ||
Set the static paths to be served by the gradio app. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From what I understand (and testing the code), this method sets the static_paths for all Gradio apps defined until that Python session ends (or There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yea good call! |
||
|
||
Static files are not moved to the gradio cache and are served directly from the file system. | ||
This function is useful when you want to serve files that you know will not be modified during the lifetime of the gradio app (like files used in gr.Examples). | ||
By setting static paths, your app will launch faster and it will consume less disk space. | ||
|
||
Parameters: | ||
paths: List of filepaths or directory names to be served by the gradio app. If it is a directory name, ALL files located within that directory will be considered static and not moved to the gradio cache. This also means that ALL files in that directory will be accessible over the network. | ||
Example: | ||
import gradio as gr | ||
|
||
# Paths can be a list of strings or pathlib.Path objects | ||
# corresponding to filenames or directories. | ||
gr.set_static_paths(paths=["test/test_files/"]) | ||
|
||
# The example files and the default value of the input | ||
# will not be copied to the gradio cache and will be served directly. | ||
demo = gr.Interface( | ||
lambda s: s.rotate(45), | ||
gr.Image(value="test/test_files/cheetah1.jpg", type="pil"), | ||
gr.Image(), | ||
examples=["test/test_files/bus.png"], | ||
) | ||
|
||
demo.launch() | ||
""" | ||
from gradio.data_classes import _StaticFiles | ||
|
||
_StaticFiles.all_paths.extend([Path(p).resolve() for p in paths]) | ||
|
||
|
||
def is_static_file(file_path: Any): | ||
"""Returns True if the file is a static file (and not moved to cache)""" | ||
from gradio.data_classes import _StaticFiles | ||
|
||
return _is_static_file(file_path, _StaticFiles.all_paths) | ||
|
||
|
||
def _is_static_file(file_path: Any, static_files: list[Path]) -> bool: | ||
""" | ||
Returns True if the file is a static file (i.e. is is in the static files list). | ||
""" | ||
if not isinstance(file_path, (str, Path, FileData)): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unless I missed something, |
||
return False | ||
if isinstance(file_path, FileData): | ||
file_path = file_path.path | ||
if isinstance(file_path, str): | ||
file_path = Path(file_path) | ||
if not file_path.exists(): | ||
return False | ||
return any(is_in_or_equal(file_path, static_file) for static_file in static_files) | ||
|
||
|
||
HTML_TAG_RE = re.compile("<.*?>") | ||
|
||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I find this
.extend()
behavior a little unintuitive: I would have expected:to clear the static paths. Especially since we call the method
set...
, I think it should replace the previous paths, not extend the previous paths.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea good point! The reason I decided to do it this way is in the case you're nesting various blocks to make a single demo. I think we can have it replace the previous paths and add an api to extend previous paths later based on user request.