Remove IOComponent.add_interactive_to_config (#3476)

* Remove `IOComponent.add_interactive_to_config`

* Move interactive mode conversion into postprocess

* Requested changes

* Update tests

* Fix test

* Fix test

* Fix test

* Fix test

* updating test file

---------

Co-authored-by: Abubakar Abid <abubakar@huggingface.co>
This commit is contained in:
space-nuko 2023-03-18 11:13:19 -04:00 committed by GitHub
parent df7dffca88
commit 52253b8bb1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 47 additions and 59 deletions

View File

@ -25,6 +25,7 @@ By [@aliabid94](https://github.com/aliabid94) in [PR 3466](https://github.com/gr
## Testing and Infrastructure Changes:
- Pinned `pyright==1.1.298` for stability by [@abidlabs](https://github.com/abidlabs) in [PR 3475](https://github.com/gradio-app/gradio/pull/3475)
- Removed `IOComponent.add_interactive_to_config()` by [@space-nuko](https://github.com/space-nuko) in [PR 3476](https://github.com/gradio-app/gradio/pull/3476)
- Removed `IOComponent.generate_sample()` by [@space-nuko](https://github.com/space-nuko) in [PR 3475](https://github.com/gradio-app/gradio/pull/3483)
## Breaking Changes:

View File

@ -400,6 +400,9 @@ def postprocess_update_dict(block: Block, update_dict: Dict, postprocess: bool =
update_dict = block.get_specific_update(update_dict)
if update_dict.get("value") is components._Keywords.NO_VALUE:
update_dict.pop("value")
interactive = update_dict.pop("interactive", None)
if interactive is not None:
update_dict["mode"] = "dynamic" if interactive else "static"
prediction_value = delete_none(update_dict, skip_value=True)
if "value" in prediction_value and postprocess:
assert isinstance(

View File

@ -209,12 +209,6 @@ class IOComponent(Component, Serializable):
config["info"] = self.info
return config
@staticmethod
def add_interactive_to_config(config, interactive):
if interactive is not None:
config["mode"] = "dynamic" if interactive else "static"
return config
@staticmethod
def get_load_fn_and_initial_value(value):
if callable(value):
@ -347,7 +341,7 @@ class Textbox(
interactive: bool | None = None,
type: str | None = None,
):
updated_config = {
return {
"lines": lines,
"max_lines": max_lines,
"placeholder": placeholder,
@ -356,9 +350,9 @@ class Textbox(
"visible": visible,
"value": value,
"type": type,
"interactive": interactive,
"__type__": "update",
}
return IOComponent.add_interactive_to_config(updated_config, interactive)
def preprocess(self, x: str | None) -> str | None:
"""
@ -552,14 +546,14 @@ class Number(
interactive: bool | None = None,
visible: bool | None = None,
):
updated_config = {
return {
"label": label,
"show_label": show_label,
"visible": visible,
"value": value,
"interactive": interactive,
"__type__": "update",
}
return IOComponent.add_interactive_to_config(updated_config, interactive)
def preprocess(self, x: float | None) -> float | None:
"""
@ -745,7 +739,7 @@ class Slider(
interactive: bool | None = None,
visible: bool | None = None,
):
updated_config = {
return {
"minimum": minimum,
"maximum": maximum,
"step": step,
@ -754,9 +748,9 @@ class Slider(
"interactive": interactive,
"visible": visible,
"value": value,
"interactive": interactive,
"__type__": "update",
}
return IOComponent.add_interactive_to_config(updated_config, interactive)
def postprocess(self, y: float | None) -> float | None:
"""
@ -880,15 +874,15 @@ class Checkbox(
interactive: bool | None = None,
visible: bool | None = None,
):
updated_config = {
return {
"label": label,
"show_label": show_label,
"interactive": interactive,
"visible": visible,
"value": value,
"interactive": interactive,
"__type__": "update",
}
return IOComponent.add_interactive_to_config(updated_config, interactive)
def get_interpretation_neighbors(self, x):
return [not x], {}
@ -1000,16 +994,16 @@ class CheckboxGroup(
interactive: bool | None = None,
visible: bool | None = None,
):
updated_config = {
return {
"choices": choices,
"label": label,
"show_label": show_label,
"interactive": interactive,
"visible": visible,
"value": value,
"interactive": interactive,
"__type__": "update",
}
return IOComponent.add_interactive_to_config(updated_config, interactive)
def preprocess(self, x: List[str]) -> List[str] | List[int]:
"""
@ -1182,16 +1176,16 @@ class Radio(
interactive: bool | None = None,
visible: bool | None = None,
):
updated_config = {
return {
"choices": choices,
"label": label,
"show_label": show_label,
"interactive": interactive,
"visible": visible,
"value": value,
"interactive": interactive,
"__type__": "update",
}
return IOComponent.add_interactive_to_config(updated_config, interactive)
def preprocess(self, x: str | None) -> str | int | None:
"""
@ -1351,16 +1345,16 @@ class Dropdown(Changeable, Selectable, IOComponent, SimpleSerializable, FormComp
interactive: bool | None = None,
visible: bool | None = None,
):
updated_config = {
return {
"choices": choices,
"label": label,
"show_label": show_label,
"interactive": interactive,
"visible": visible,
"value": value,
"interactive": interactive,
"__type__": "update",
}
return IOComponent.add_interactive_to_config(updated_config, interactive)
def preprocess(
self, x: str | List[str]
@ -1543,16 +1537,16 @@ class Image(
visible: bool | None = None,
brush_radius: int | None = None,
):
updated_config = {
return {
"label": label,
"show_label": show_label,
"interactive": interactive,
"visible": visible,
"value": value,
"brush_radius": brush_radius,
"interactive": interactive,
"__type__": "update",
}
return IOComponent.add_interactive_to_config(updated_config, interactive)
def _format_image(
self, im: _Image.Image | None
@ -1892,16 +1886,16 @@ class Video(
interactive: bool | None = None,
visible: bool | None = None,
):
updated_config = {
return {
"source": source,
"label": label,
"show_label": show_label,
"interactive": interactive,
"visible": visible,
"value": value,
"interactive": interactive,
"__type__": "update",
}
return IOComponent.add_interactive_to_config(updated_config, interactive)
def preprocess(self, x: Dict[str, str] | None) -> str | None:
"""
@ -2114,16 +2108,16 @@ class Audio(
interactive: bool | None = None,
visible: bool | None = None,
):
updated_config = {
return {
"source": source,
"label": label,
"show_label": show_label,
"interactive": interactive,
"visible": visible,
"value": value,
"interactive": interactive,
"__type__": "update",
}
return IOComponent.add_interactive_to_config(updated_config, interactive)
def preprocess(
self, x: Dict[str, Any] | None
@ -2434,15 +2428,15 @@ class File(
interactive: bool | None = None,
visible: bool | None = None,
):
updated_config = {
return {
"label": label,
"show_label": show_label,
"interactive": interactive,
"visible": visible,
"value": value,
"interactive": interactive,
"__type__": "update",
}
return IOComponent.add_interactive_to_config(updated_config, interactive)
def preprocess(
self, x: List[Dict[str, Any]] | None
@ -2699,7 +2693,7 @@ class Dataframe(Changeable, Selectable, IOComponent, JSONSerializable):
interactive: bool | None = None,
visible: bool | None = None,
):
updated_config = {
return {
"max_rows": max_rows,
"max_cols": max_cols,
"label": label,
@ -2707,9 +2701,9 @@ class Dataframe(Changeable, Selectable, IOComponent, JSONSerializable):
"interactive": interactive,
"visible": visible,
"value": value,
"interactive": interactive,
"__type__": "update",
}
return IOComponent.add_interactive_to_config(updated_config, interactive)
def preprocess(self, x: DataframeData):
"""
@ -2915,16 +2909,16 @@ class Timeseries(Changeable, IOComponent, JSONSerializable):
interactive: bool | None = None,
visible: bool | None = None,
):
updated_config = {
return {
"colors": colors,
"label": label,
"show_label": show_label,
"interactive": interactive,
"visible": visible,
"value": value,
"interactive": interactive,
"__type__": "update",
}
return IOComponent.add_interactive_to_config(updated_config, interactive)
def preprocess(self, x: Dict | None) -> pd.DataFrame | None:
"""
@ -3075,13 +3069,13 @@ class Button(Clickable, IOComponent, SimpleSerializable):
visible: bool | None = None,
interactive: bool | None = None,
):
updated_config = {
return {
"variant": variant,
"visible": visible,
"value": value,
"interactive": interactive,
"__type__": "update",
}
return IOComponent.add_interactive_to_config(updated_config, interactive)
def style(
self,
@ -3179,13 +3173,13 @@ class UploadButton(
interactive: bool | None = None,
visible: bool | None = None,
):
updated_config = {
return {
"interactive": interactive,
"visible": visible,
"value": value,
"interactive": interactive,
"__type__": "update",
}
return IOComponent.add_interactive_to_config(updated_config, interactive)
def preprocess(
self, x: List[Dict[str, Any]] | None
@ -3338,14 +3332,14 @@ class ColorPicker(Changeable, Submittable, IOComponent, SimpleSerializable):
visible: bool | None = None,
interactive: bool | None = None,
):
updated_config = {
return {
"value": value,
"label": label,
"show_label": show_label,
"visible": visible,
"interactive": interactive,
"__type__": "update",
}
return IOComponent.add_interactive_to_config(updated_config, interactive)
def preprocess(self, x: str | None) -> str | None:
"""
@ -3501,7 +3495,7 @@ class Label(Changeable, Selectable, IOComponent, JSONSerializable):
# e.g. no background default state.
elif color is None:
color = "transparent"
updated_config = {
return {
"label": label,
"show_label": show_label,
"visible": visible,
@ -3509,7 +3503,6 @@ class Label(Changeable, Selectable, IOComponent, JSONSerializable):
"color": color,
"__type__": "update",
}
return updated_config
def style(
self,
@ -5614,15 +5607,15 @@ class Code(Changeable, IOComponent, SimpleSerializable):
language: str | None = None,
interactive: bool | None = True,
):
updated_config = {
return {
"label": label,
"show_label": show_label,
"visible": visible,
"value": value,
"language": language,
"interactive": interactive,
"__type__": "update",
}
return IOComponent.add_interactive_to_config(updated_config, interactive)
def style(self):
return self

View File

@ -79,14 +79,14 @@ class ColorPicker(Changeable, Submittable, IOComponent):
visible: Optional[bool] = None,
interactive: Optional[bool] = None,
):
updated_config = {
return {
"value": value,
"label": label,
"show_label": show_label,
"visible": visible,
"interactive": interactive,
"__type__": "update",
}
return IOComponent.add_interactive_to_config(updated_config, interactive)
# Input Functionalities
def preprocess(self, x: str | None) -> Any:

View File

@ -46,16 +46,9 @@ def captured_output():
class TestBlocksMethods:
maxDiff = None
def test_set_share(self):
def test_set_share_is_false_by_default(self):
with gr.Blocks() as demo:
# self.share is False when instantiating the class
assert not demo.share
# default is False, if share is None
demo.share = None
assert not demo.share
# if set to True, it doesn't change
demo.share = True
assert demo.share
@patch("gradio.networking.setup_tunnel")
@patch("gradio.utils.colab_check")
@ -449,6 +442,7 @@ class TestComponentsInBlocks:
class TestBlocksPostprocessing:
def test_blocks_do_not_filter_none_values_from_updates(self, io_components):
io_components = [
c()
for c in io_components
@ -575,7 +569,6 @@ class TestBlocksPostprocessing:
for fn_index in range(2):
output = await demo.process_api(fn_index, [], state={})
assert output["data"][0] == {
"interactive": True,
"__type__": "update",
"mode": "dynamic",
}
@ -888,11 +881,10 @@ class TestSpecificUpdate:
"label": None,
"show_label": None,
"type": None,
"type": None,
"interactive": False,
"visible": None,
"value": gr.components._Keywords.NO_VALUE,
"__type__": "update",
"mode": "static",
"__type__": "update"
}
specific_update = gr.Textbox.get_specific_update(
@ -905,10 +897,10 @@ class TestSpecificUpdate:
"label": None,
"show_label": None,
"type": None,
"interactive": True,
"visible": None,
"value": gr.components._Keywords.NO_VALUE,
"__type__": "update",
"mode": "dynamic",
"__type__": "update"
}
def test_with_generic_update(self):
@ -926,7 +918,6 @@ class TestSpecificUpdate:
"show_label": None,
"visible": True,
"value": "test.mp4",
"mode": "dynamic",
"interactive": True,
"__type__": "update",
}