Optional-Input-Clarification

- when the component is optional, label it as optional
This commit is contained in:
Ömer Faruk Özdemir 2022-02-12 15:22:27 +03:00
parent 6f8afa0eb5
commit 56ce2dcc60
3 changed files with 26 additions and 12 deletions

View File

@ -1,6 +1,6 @@
import os
import shutil
from typing import Any, Dict
from typing import Any, Dict, Optional
from gradio import processing_utils
@ -49,7 +49,7 @@ class Component:
def save_flagged_file(
self, dir: str, label: str, data: Any, encryption_key: bool
) -> str:
) -> Optional[str]:
"""
Saved flagged data (e.g. image or audio) as a file and returns filepath
"""

View File

@ -30,11 +30,15 @@ class InputComponent(Component):
Input Component. All input components subclass this.
"""
def __init__(self, label: str, requires_permissions: bool = False):
def __init__(
self, label: str, requires_permissions: bool = False, optional: bool = False
):
"""
Constructs an input component.
"""
self.set_interpret_parameters()
if optional is True:
label = InputComponent.label_as_optional(label)
super().__init__(label, requires_permissions)
def preprocess(self, x: Any) -> Any:
@ -97,6 +101,10 @@ class InputComponent(Component):
"""
pass
@staticmethod
def label_as_optional(label: str) -> str:
return f"{label}(Optional)"
class Textbox(InputComponent):
"""
@ -248,16 +256,22 @@ class Number(InputComponent):
Demos: tax_calculator, titanic_survival
"""
def __init__(self, default: Optional[float] = None, label: Optional[str] = None):
def __init__(
self,
default: Optional[float] = None,
label: Optional[str] = None,
optional: bool = False,
):
"""
Parameters:
default (float): default value.
label (str): component name in interface.
optional (bool):
"""
self.default = default
self.test_input = default if default is not None else 1
self.interpret_by_tokens = False
super().__init__(label)
super().__init__(label, optional=optional)
def get_template_context(self):
return {"default": self.default, **super().get_template_context()}
@ -590,7 +604,7 @@ class Radio(InputComponent):
def __init__(
self,
choices: List(str),
choices: List[str],
type: str = "value",
default: Optional[str] = None,
label: Optional[str] = None,
@ -772,7 +786,7 @@ class Image(InputComponent):
self.invert_colors = invert_colors
self.test_input = test_data.BASE64_IMAGE
self.interpret_by_tokens = True
super().__init__(label, requires_permissions)
super().__init__(label, requires_permissions, optional=optional)
@classmethod
def get_shortcut_implementations(cls):
@ -994,7 +1008,7 @@ class Video(InputComponent):
self.type = type
self.source = source
self.optional = optional
super().__init__(label)
super().__init__(label, optional=optional)
@classmethod
def get_shortcut_implementations(cls):
@ -1084,7 +1098,7 @@ class Audio(InputComponent):
self.optional = optional
self.test_input = test_data.BASE64_AUDIO
self.interpret_by_tokens = True
super().__init__(label, requires_permissions)
super().__init__(label, requires_permissions, optional=optional)
def get_template_context(self):
return {
@ -1296,7 +1310,7 @@ class File(InputComponent):
self.type = type
self.test_input = None
self.optional = optional
super().__init__(label)
super().__init__(label, optional=optional)
def get_template_context(self):
return {
@ -1509,7 +1523,7 @@ class Timeseries(InputComponent):
y = [y]
self.y = y
self.optional = optional
super().__init__(label)
super().__init__(label, optional=optional)
def get_template_context(self):
return {

View File

@ -3,7 +3,7 @@ if [ -z "$(ls | grep CONTRIBUTING.md)" ]; then
echo "Please run the script from repo directory"
exit -1
else
echo "Installing formatting with black and isort, also checking for standards with flake8"
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