Skip to content

Commit

Permalink
Fix: Move to cache in init postprocess + Fallback Fixes (#6107)
Browse files Browse the repository at this point in the history
* Add code and test

* restore

* add changeset

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
  • Loading branch information
freddyaboulton and gradio-pr-bot committed Oct 26, 2023
1 parent fadc057 commit 9a40de7
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 15 deletions.
8 changes: 8 additions & 0 deletions .changeset/loose-ducks-feel.md
@@ -0,0 +1,8 @@
---
"@gradio/app": minor
"@gradio/fallback": minor
"@gradio/slider": minor
"gradio": minor
---

feat:Fix: Move to cache in init postprocess + Fallback Fixes
12 changes: 8 additions & 4 deletions gradio/component_meta.py
Expand Up @@ -11,6 +11,7 @@
from gradio.data_classes import GradioModel, GradioRootModel
from gradio.events import EventListener
from gradio.exceptions import ComponentDefinitionError
from gradio.utils import no_raise_exception

INTERFACE_TEMPLATE = '''
{{ contents }}
Expand Down Expand Up @@ -115,17 +116,20 @@ def create_or_modify_pyi(
+ ["from gradio.events import Dependency"]
+ lines[last_empty_line_before_class:]
)
pyi_file.write_text("\n".join(lines))
with no_raise_exception():
pyi_file.write_text("\n".join(lines))
current_interface, _ = extract_class_source_code(pyi_file.read_text(), class_name)
if not current_interface:
with open(str(pyi_file), mode="a") as f:
f.write(new_interface)
with no_raise_exception():
with open(str(pyi_file), mode="a") as f:
f.write(new_interface)
else:
contents = pyi_file.read_text()
contents = contents.replace(current_interface, new_interface.strip())
current_contents = pyi_file.read_text()
if current_contents != contents:
pyi_file.write_text(contents)
with no_raise_exception():
pyi_file.write_text(contents)


def in_event_listener():
Expand Down
7 changes: 5 additions & 2 deletions gradio/components/base.py
Expand Up @@ -25,6 +25,7 @@
from gradio.data_classes import GradioDataModel
from gradio.events import EventListener
from gradio.layouts import Form
from gradio.processing_utils import move_files_to_cache

if TYPE_CHECKING:
from typing import TypedDict
Expand Down Expand Up @@ -133,8 +134,8 @@ class Component(ComponentBase, Block):

def __init__(
self,
*,
value: Any = None,
*,
label: str | None = None,
info: str | None = None,
show_label: bool | None = None,
Expand Down Expand Up @@ -206,11 +207,13 @@ def __init__(
self.load_event: None | dict[str, Any] = None
self.load_event_to_attach: None | tuple[Callable, float | None] = None
load_fn, initial_value = self.get_load_fn_and_initial_value(value)
self.value = (
initial_value = (
initial_value
if self._skip_init_processing
else self.postprocess(initial_value)
)
self.value = move_files_to_cache(initial_value, self) # type: ignore

if callable(load_fn):
self.attach_load_event(load_fn, every)

Expand Down
9 changes: 9 additions & 0 deletions gradio/utils.py
Expand Up @@ -532,6 +532,15 @@ def set_directory(path: Path | str):
os.chdir(origin)


@contextmanager
def no_raise_exception():
"""Context manager that suppresses exceptions."""
try:
yield
except Exception:
pass


def sanitize_value_for_csv(value: str | Number) -> str | Number:
"""
Sanitizes a value that is being written to a CSV file to prevent CSV injection attacks.
Expand Down
6 changes: 3 additions & 3 deletions js/app/src/types.ts
Expand Up @@ -26,9 +26,9 @@ export interface DependencyTypes {
}

export interface Payload {
fn_index: number,
data: unknown[],
event_data: unknown | null,
fn_index: number;
data: unknown[];
event_data: unknown | null;
}

export interface Dependency {
Expand Down
12 changes: 7 additions & 5 deletions js/fallback/Index.svelte
Expand Up @@ -23,11 +23,13 @@
</script>

<Block {visible} {elem_id} {elem_classes} {container} {scale} {min_width}>
<StatusTracker
autoscroll={gradio.autoscroll}
i18n={gradio.i18n}
{...loading_status}
/>
{#if loading_status}
<StatusTracker
autoscroll={gradio.autoscroll}
i18n={gradio.i18n}
{...loading_status}
/>
{/if}

<JsonView json={value} />
</Block>
2 changes: 1 addition & 1 deletion js/slider/Index.svelte
Expand Up @@ -202,7 +202,7 @@
height: 400%;
}
input[type=range]::-moz-range-track {
input[type="range"]::-moz-range-track {
height: 12px;
}
</style>
5 changes: 5 additions & 0 deletions test/test_components.py
Expand Up @@ -820,6 +820,11 @@ def test_component_functions(self, gradio_temp_dir):
output2 = audio_output.postprocess(Path(y_audio.name))
assert output1 == output2

def test_default_value_postprocess(self):
x_wav = deepcopy(media_data.BASE64_AUDIO)
audio = gr.Audio(value=x_wav["name"])
assert processing_utils.is_in_or_equal(audio.value["name"], audio.GRADIO_CACHE)

def test_in_interface(self):
def reverse_audio(audio):
sr, data = audio
Expand Down

0 comments on commit 9a40de7

Please sign in to comment.