From 66752a1c5db82602e4c6059323752bf1f9428a9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96mer=20Faruk=20=C3=96zdemir?= Date: Thu, 12 May 2022 22:12:01 +0300 Subject: [PATCH 1/9] update-shortcut-syntax - fix&update gr.component - create a demo introducing shortcuts within Blocks --- demo/blocks_component_shortcut/run.py | 21 +++++++++++++++++++++ demo/blocks_textarea/run.py | 14 -------------- gradio/__init__.py | 2 ++ gradio/components.py | 13 ++++++------- test/test_components.py | 2 +- 5 files changed, 30 insertions(+), 22 deletions(-) create mode 100644 demo/blocks_component_shortcut/run.py delete mode 100644 demo/blocks_textarea/run.py diff --git a/demo/blocks_component_shortcut/run.py b/demo/blocks_component_shortcut/run.py new file mode 100644 index 0000000000000..2446e6b6893f0 --- /dev/null +++ b/demo/blocks_component_shortcut/run.py @@ -0,0 +1,21 @@ +import gradio as gr + + +def greet(video): + return video + + +with gr.Blocks() as demo: + """ + You can make use of str shortcuts you use in Interface within Blocks as well. + 1. You can use gr.component() or + 2. gr.templates.Template() or gr.Template() + All the templates are listed in gradio/templates.py + """ + with gr.Column(): + input = gr.component("textarea") + with gr.Column(): + output = gr.TextArea() + output2 = gr.templates.TextArea() + input.change(greet, input, output) + demo.launch() diff --git a/demo/blocks_textarea/run.py b/demo/blocks_textarea/run.py deleted file mode 100644 index 09c70d05b25b8..0000000000000 --- a/demo/blocks_textarea/run.py +++ /dev/null @@ -1,14 +0,0 @@ -import gradio as gr - - -def greet(name): - return "Hello " + name + "!!" - - -demo = gr.Interface( - fn=greet, inputs=gr.component("textarea"), outputs=gr.component("textarea") -) - - -if __name__ == "__main__": - demo.launch() diff --git a/gradio/__init__.py b/gradio/__init__.py index 52e57b368975b..e3029280e2762 100644 --- a/gradio/__init__.py +++ b/gradio/__init__.py @@ -39,6 +39,8 @@ component, update, ) +from gradio.templates import * +import gradio.templates from gradio.flagging import ( CSVLogger, FlaggingCallback, diff --git a/gradio/components.py b/gradio/components.py index 935d153742143..16a5b778a0b1a 100644 --- a/gradio/components.py +++ b/gradio/components.py @@ -3719,8 +3719,7 @@ def update( "__type__": "update", } - -def component(cls_name: str): +def component(cls_name: str) -> Component: """ Returns a component or template with the given class name, or raises a ValueError if not found. @param cls_name: lower-case string class name of a component @@ -3740,18 +3739,18 @@ def component(cls_name: str): ] for name, cls in components + templates: if name.lower() == cls_name.replace("_", "") and issubclass(cls, Component): - return cls + obj = cls() + return obj raise ValueError(f"No such Component: {cls_name}") def get_component_instance(comp: str | dict | Component): if isinstance(comp, str): - component_cls = component(comp) - return component_cls() + return component(comp) elif isinstance(comp, dict): name = comp.pop("name") - component_cls = component(name) - return component_cls(**comp) + component_obj = component(name).__class__ + return component_obj.__init__(**comp) elif isinstance(comp, Component): return comp else: diff --git a/test/test_components.py b/test/test_components.py index 69586fc1d3414..dff0d92d71372 100644 --- a/test/test_components.py +++ b/test/test_components.py @@ -27,7 +27,7 @@ def test_component_functions(self): """ component """ - assert isinstance(gr.components.component("text")(), gr.templates.Text) + assert isinstance(gr.components.component("text"), gr.templates.Text) class TestTextbox(unittest.TestCase): From 030953d403ce03a26c65fa7815925c6b9a270b8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96mer=20Faruk=20=C3=96zdemir?= Date: Thu, 12 May 2022 22:18:50 +0300 Subject: [PATCH 2/9] update-shortcut-syntax - tweaks --- demo/blocks_component_shortcut/run.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/demo/blocks_component_shortcut/run.py b/demo/blocks_component_shortcut/run.py index 2446e6b6893f0..1f9cd8349674a 100644 --- a/demo/blocks_component_shortcut/run.py +++ b/demo/blocks_component_shortcut/run.py @@ -12,10 +12,14 @@ def greet(video): 2. gr.templates.Template() or gr.Template() All the templates are listed in gradio/templates.py """ - with gr.Column(): - input = gr.component("textarea") - with gr.Column(): - output = gr.TextArea() - output2 = gr.templates.TextArea() - input.change(greet, input, output) + with gr.Row(): + with gr.Column(): + text1 = gr.component("textarea") + with gr.Column(): + text2 = gr.TextArea() + with gr.Column(): + text3 = gr.templates.TextArea() + text1.change(greet, text1, text2) + text2.change(greet, text2, text3) + text3.change(greet, text3, text1) demo.launch() From 788c4aa743419f2c9d26d71cb45d48ff9af6bc0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96mer=20Faruk=20=C3=96zdemir?= Date: Thu, 12 May 2022 22:19:10 +0300 Subject: [PATCH 3/9] update-shortcut-syntax - tweaks --- gradio/__init__.py | 4 ++-- gradio/components.py | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/gradio/__init__.py b/gradio/__init__.py index e3029280e2762..b3e1c92ea737c 100644 --- a/gradio/__init__.py +++ b/gradio/__init__.py @@ -4,6 +4,7 @@ import gradio.inputs as inputs import gradio.outputs as outputs import gradio.processing_utils +import gradio.templates from gradio.blocks import Blocks, Column, Row, TabItem, Tabs from gradio.components import ( HTML, @@ -39,8 +40,6 @@ component, update, ) -from gradio.templates import * -import gradio.templates from gradio.flagging import ( CSVLogger, FlaggingCallback, @@ -49,6 +48,7 @@ ) from gradio.interface import Interface, TabbedInterface, close_all from gradio.mix import Parallel, Series +from gradio.templates import * current_pkg_version = pkg_resources.require("gradio")[0].version __version__ = current_pkg_version diff --git a/gradio/components.py b/gradio/components.py index 16a5b778a0b1a..a5a2c9d1800a4 100644 --- a/gradio/components.py +++ b/gradio/components.py @@ -3719,6 +3719,7 @@ def update( "__type__": "update", } + def component(cls_name: str) -> Component: """ Returns a component or template with the given class name, or raises a ValueError if not found. From e2d19a5e25db21dc67abf71f2d0f9ee913be7a2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96mer=20Faruk=20=C3=96zdemir?= Date: Thu, 12 May 2022 22:20:49 +0300 Subject: [PATCH 4/9] update-shortcut-syntax - fix formatting --- .circleci/config.yml | 2 +- scripts/format_backend.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 5c56bca57152d..6858051c9844e 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -55,7 +55,7 @@ jobs: - run: command: | . venv/bin/activate - python -m flake8 --ignore=E731,E501,E722,W503,E126,F401,E203 gradio test + python -m flake8 --ignore=E731,E501,E722,W503,E126,F401,E203,F403 gradio test - run: command: | . venv/bin/activate diff --git a/scripts/format_backend.sh b/scripts/format_backend.sh index 78584104285c8..c047ee8a78f9e 100755 --- a/scripts/format_backend.sh +++ b/scripts/format_backend.sh @@ -6,5 +6,5 @@ else echo "Formatting backend and tests with black and isort, also checking for standards with flake8" python -m black gradio test python -m isort --profile=black gradio test - python -m flake8 --ignore=E731,E501,E722,W503,E126,F401,E203 gradio test + python -m flake8 --ignore=E731,E501,E722,W503,E126,F401,E203,F403 gradio test fi From a9ba0f22e088302b891fcb0cb239912cd6a8332d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96mer=20Faruk=20=C3=96zdemir?= Date: Fri, 13 May 2022 08:49:13 +0300 Subject: [PATCH 5/9] update-shortcut-syntax - tweaks - fix tests --- demo/blocks_component_shortcut/run.py | 3 --- gradio/components.py | 4 ++-- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/demo/blocks_component_shortcut/run.py b/demo/blocks_component_shortcut/run.py index 1f9cd8349674a..e68f6b7fed206 100644 --- a/demo/blocks_component_shortcut/run.py +++ b/demo/blocks_component_shortcut/run.py @@ -13,11 +13,8 @@ def greet(video): All the templates are listed in gradio/templates.py """ with gr.Row(): - with gr.Column(): text1 = gr.component("textarea") - with gr.Column(): text2 = gr.TextArea() - with gr.Column(): text3 = gr.templates.TextArea() text1.change(greet, text1, text2) text2.change(greet, text2, text3) diff --git a/gradio/components.py b/gradio/components.py index a5a2c9d1800a4..ee34c7b644f37 100644 --- a/gradio/components.py +++ b/gradio/components.py @@ -3750,8 +3750,8 @@ def get_component_instance(comp: str | dict | Component): return component(comp) elif isinstance(comp, dict): name = comp.pop("name") - component_obj = component(name).__class__ - return component_obj.__init__(**comp) + component_cls = component(name).__class__ + return component_cls(**comp) elif isinstance(comp, Component): return comp else: From 3e3efa7084d4a52156c3a6ec341e7095fa3a2289 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96mer=20Faruk=20=C3=96zdemir?= Date: Fri, 13 May 2022 09:13:31 +0300 Subject: [PATCH 6/9] update-shortcut-syntax - tweaks - fix tests --- gradio/__init__.py | 1 + gradio/components.py | 23 ++++++++++++++--------- test/test_external.py | 2 +- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/gradio/__init__.py b/gradio/__init__.py index b3e1c92ea737c..8dfaf89ffe954 100644 --- a/gradio/__init__.py +++ b/gradio/__init__.py @@ -40,6 +40,7 @@ component, update, ) +from gradio.external import load_interface from gradio.flagging import ( CSVLogger, FlaggingCallback, diff --git a/gradio/components.py b/gradio/components.py index ee34c7b644f37..5292acb31827c 100644 --- a/gradio/components.py +++ b/gradio/components.py @@ -12,7 +12,7 @@ import warnings from copy import deepcopy from types import ModuleType -from typing import Any, Callable, Dict, List, Optional, Tuple +from typing import Any, Callable, Dict, List, Optional, Tuple, Type import matplotlib.figure import numpy as np @@ -3212,7 +3212,7 @@ class Chatbot(Changeable, IOComponent): def __init__( self, value="", - color_map: Tuple(str, str) = None, + color_map: Tuple[str, str] = None, *, label: Optional[str] = None, show_label: bool = True, @@ -3249,7 +3249,7 @@ def get_config(self): @staticmethod def update( value: Optional[Any] = None, - color_map: Optional[Tuple(str, str)] = None, + color_map: Optional[Tuple[str, str]] = None, label: Optional[str] = None, show_label: Optional[bool] = None, visible: Optional[bool] = None, @@ -3720,9 +3720,9 @@ def update( } -def component(cls_name: str) -> Component: +def component_class(cls_name: str) -> Type[Component]: """ - Returns a component or template with the given class name, or raises a ValueError if not found. + Returns the component class with the given class name, or raises a ValueError if not found. @param cls_name: lower-case string class name of a component @return cls: the component class """ @@ -3740,18 +3740,23 @@ def component(cls_name: str) -> Component: ] for name, cls in components + templates: if name.lower() == cls_name.replace("_", "") and issubclass(cls, Component): - obj = cls() - return obj + return cls raise ValueError(f"No such Component: {cls_name}") +def component(cls_name: str) -> Component: + obj = component_class(cls_name)() + return obj + + def get_component_instance(comp: str | dict | Component): if isinstance(comp, str): return component(comp) elif isinstance(comp, dict): name = comp.pop("name") - component_cls = component(name).__class__ - return component_cls(**comp) + component_cls = component_class(name) + component_obj = component_cls(**comp) + return component_obj elif isinstance(comp, Component): return comp else: diff --git a/test/test_external.py b/test/test_external.py index ee9f163b746a3..434b533a502e2 100644 --- a/test/test_external.py +++ b/test/test_external.py @@ -206,7 +206,7 @@ def test_translation_model(self): self.assertEqual(output, "Mein Name ist Sarah und ich lebe in London") def test_numerical_to_label_space(self): - interface_info = gr.external.load_interface("spaces/abidlabs/titanic-survival") + interface_info = gr.load_interface("spaces/abidlabs/titanic-survival") io = gr.Interface(**interface_info) io.api_mode = True output = io("male", 77, 10) From 01ec6c263ec9a0f17f2f9f36136e00453c687011 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96mer=20Faruk=20=C3=96zdemir?= Date: Fri, 13 May 2022 09:29:30 +0300 Subject: [PATCH 7/9] update-shortcut-syntax - tweaks - fix tests --- demo/blocks_component_shortcut/run.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/demo/blocks_component_shortcut/run.py b/demo/blocks_component_shortcut/run.py index e68f6b7fed206..9afb7eaa10e0f 100644 --- a/demo/blocks_component_shortcut/run.py +++ b/demo/blocks_component_shortcut/run.py @@ -1,15 +1,21 @@ import gradio as gr -def greet(video): - return video +def greet(str): + return str with gr.Blocks() as demo: """ You can make use of str shortcuts you use in Interface within Blocks as well. - 1. You can use gr.component() or - 2. gr.templates.Template() or gr.Template() + + Interface shortcut example: + Interface(greet, "textarea", "textarea") + + You can use + 1. gr.component() + 2. gr.templates.Template() + 3. gr.Template() All the templates are listed in gradio/templates.py """ with gr.Row(): From 375c0e5d068342ee0c2e70070a71a90f5fe539d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96mer=20Faruk=20=C3=96zdemir?= Date: Fri, 13 May 2022 10:11:48 +0300 Subject: [PATCH 8/9] button-default-name - add a default name in Button --- gradio/components.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradio/components.py b/gradio/components.py index 5292acb31827c..6a40053644d7e 100644 --- a/gradio/components.py +++ b/gradio/components.py @@ -3538,7 +3538,7 @@ class Button(Clickable, Component): def __init__( self, - value: str = "", + value: str = "Click", *, variant: str = "primary", visible: bool = True, From 429a2339e77423e90c53211316cd89c3efe9bf65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96mer=20Faruk=20=C3=96zdemir?= Date: Fri, 13 May 2022 10:12:42 +0300 Subject: [PATCH 9/9] button-default-name - add a default name in Button --- gradio/components.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradio/components.py b/gradio/components.py index 6a40053644d7e..dfe31b3db8ce2 100644 --- a/gradio/components.py +++ b/gradio/components.py @@ -3538,7 +3538,7 @@ class Button(Clickable, Component): def __init__( self, - value: str = "Click", + value: str = "Run", *, variant: str = "primary", visible: bool = True,