mirror of
https://github.com/gradio-app/gradio.git
synced 2024-11-27 01:40:20 +08:00
parent
3672e17256
commit
db48356832
@ -134,7 +134,6 @@ No changes to highlight.
|
||||
* Images in the chatbot component are now resized if they exceed a max width by [@abidlabs](https://github.com/abidlabs) in [PR 2748](https://github.com/gradio-app/gradio/pull/2748)
|
||||
* Missing parameters have been added to `gr.Blocks().load()` by [@abidlabs](https://github.com/abidlabs) in [PR 2755](https://github.com/gradio-app/gradio/pull/2755)
|
||||
* Deindex share URLs from search by [@aliabd](https://github.com/aliabd) in [PR 2772](https://github.com/gradio-app/gradio/pull/2772)
|
||||
* Added handling of invalid values in the `Slider`, `Radio` and `CheckgroupBox` components by [@thehimalayanleo](https://github.com/thehimalayanleo) in [PR 2760](https://github.com/gradio-app/gradio/pull/2760).
|
||||
* Redirect old links and fix broken ones by [@aliabd](https://github.com/aliabd) in [PR 2774](https://github.com/gradio-app/gradio/pull/2774)
|
||||
|
||||
## Contributors Shoutout:
|
||||
|
@ -714,24 +714,6 @@ class Slider(Changeable, IOComponent, SimpleSerializable, FormComponent):
|
||||
def generate_sample(self) -> float:
|
||||
return self.maximum
|
||||
|
||||
def preprocess(self, x: float | None) -> float | None:
|
||||
"""
|
||||
Any preprocessing needed to be performed on function input.
|
||||
Parameters:
|
||||
x: numeric input
|
||||
Returns:
|
||||
None if the input is None, else the numeric input.
|
||||
Raises:
|
||||
ValueError: if the input is greater than maximum or less than minimum.
|
||||
"""
|
||||
if x is None:
|
||||
return None
|
||||
if x > self.maximum or x < self.minimum:
|
||||
raise ValueError(
|
||||
f"Slider value {x} is out of range. Minimum is {self.minimum} and maximum is {self.maximum}."
|
||||
)
|
||||
return x
|
||||
|
||||
def postprocess(self, y: float | None) -> float | None:
|
||||
"""
|
||||
Any postprocessing needed to be performed on function output.
|
||||
@ -963,23 +945,17 @@ class CheckboxGroup(Changeable, IOComponent, SimpleSerializable, FormComponent):
|
||||
Parameters:
|
||||
x: list of selected choices
|
||||
Returns:
|
||||
Returns a list of selected choices as strings or indices within choice list
|
||||
depending on `self.type`.
|
||||
Raises:
|
||||
ValueError: if any of elements in `x` are not in `self.choices`, or if `self.type` is not one of "value" or "index".
|
||||
list of selected choices as strings or indices within choice list
|
||||
"""
|
||||
for choice in x:
|
||||
if choice not in self.choices:
|
||||
raise ValueError(
|
||||
f"Invalid choice: {choice}. Select from: {self.choices}."
|
||||
)
|
||||
if self.type == "value":
|
||||
return x
|
||||
elif self.type == "index":
|
||||
return [self.choices.index(choice) for choice in x]
|
||||
else:
|
||||
raise ValueError(
|
||||
f"Unknown type: {self.type}. Please choose from: 'value', 'index'."
|
||||
"Unknown type: "
|
||||
+ str(self.type)
|
||||
+ ". Please choose from: 'value', 'index'."
|
||||
)
|
||||
|
||||
def postprocess(self, y: List[str] | None) -> List[str]:
|
||||
@ -1128,29 +1104,25 @@ class Radio(Changeable, IOComponent, SimpleSerializable, FormComponent):
|
||||
def generate_sample(self):
|
||||
return self.choices[0]
|
||||
|
||||
def preprocess(self, x: str | None) -> str | int | None:
|
||||
def preprocess(self, x: str) -> str | int:
|
||||
"""
|
||||
Parameters:
|
||||
x: selected choice
|
||||
Returns:
|
||||
the input string `x` if `self.type` is "value", otherwise if `self.type` is "index", returns the index of `x` in `self.choices`.
|
||||
Raises:
|
||||
ValueError: if x is not in `self.choices` or if `self.type` is not "value" or "index".
|
||||
selected choice as string or index within choice list
|
||||
"""
|
||||
if x is None:
|
||||
return None
|
||||
if x not in self.choices:
|
||||
raise ValueError(
|
||||
f"Invalid value for value: {x}. Please choose from: {self.choices}."
|
||||
)
|
||||
|
||||
if self.type == "value":
|
||||
return x
|
||||
elif self.type == "index":
|
||||
return self.choices.index(x)
|
||||
if x is None:
|
||||
return None
|
||||
else:
|
||||
return self.choices.index(x)
|
||||
else:
|
||||
raise ValueError(
|
||||
f"Unknown type: {self.type}. Please choose from: 'value', 'index'."
|
||||
"Unknown type: "
|
||||
+ str(self.type)
|
||||
+ ". Please choose from: 'value', 'index'."
|
||||
)
|
||||
|
||||
def set_interpret_parameters(self):
|
||||
|
@ -378,7 +378,7 @@ class TestSlider:
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_in_interface(self):
|
||||
"""
|
||||
""" "
|
||||
Interface, process, interpret
|
||||
"""
|
||||
iface = gr.Interface(lambda x: x**2, "slider", "textbox")
|
||||
@ -398,17 +398,6 @@ class TestSlider:
|
||||
9996.0,
|
||||
]
|
||||
|
||||
def test_slider_invalid_values(self):
|
||||
"""
|
||||
Preprocesses invalid values for slider
|
||||
"""
|
||||
slider = gr.Slider(minimum=20, maximum=30)
|
||||
assert slider.preprocess(25) == 25.0
|
||||
with pytest.raises(ValueError):
|
||||
slider.preprocess(10)
|
||||
with pytest.raises(ValueError):
|
||||
slider.preprocess(40)
|
||||
|
||||
def test_static(self):
|
||||
"""
|
||||
postprocess
|
||||
@ -514,26 +503,6 @@ class TestCheckboxGroup:
|
||||
assert iface([]) == ""
|
||||
_ = gr.CheckboxGroup(["a", "b", "c"], type="index")
|
||||
|
||||
def test_checkboxgroup_invalid_values(self):
|
||||
"""
|
||||
tests handling of invalid checkbox group inputs
|
||||
"""
|
||||
checkboxes_input = gr.CheckboxGroup(["a", "b", "c"], type="index")
|
||||
assert checkboxes_input.preprocess(["c"]) == [2]
|
||||
assert checkboxes_input.preprocess(["a", "c"]) == [0, 2]
|
||||
with pytest.raises(ValueError):
|
||||
checkboxes_input.preprocess(["d"])
|
||||
with pytest.raises(ValueError):
|
||||
checkboxes_input.preprocess(["a", "b", "d"])
|
||||
|
||||
checkboxes_input = gr.CheckboxGroup(["a", "b", "c"], type="value")
|
||||
assert checkboxes_input.preprocess(["c"]) == ["c"]
|
||||
assert checkboxes_input.preprocess(["a", "c"]) == ["a", "c"]
|
||||
with pytest.raises(ValueError):
|
||||
checkboxes_input.preprocess(["d"])
|
||||
with pytest.raises(ValueError):
|
||||
checkboxes_input.preprocess(["a", "b", "d"])
|
||||
|
||||
|
||||
class TestRadio:
|
||||
def test_component_functions(self):
|
||||
@ -580,20 +549,6 @@ class TestRadio:
|
||||
scores = (await iface.interpret(["b"]))[0]["interpretation"]
|
||||
assert scores == [-2.0, None, 2.0]
|
||||
|
||||
def test_radio_invalid_values(self):
|
||||
"""
|
||||
tests handling of invalid radio inputs
|
||||
"""
|
||||
radio_input = gr.Radio(["a", "b", "c"], type="index")
|
||||
assert radio_input.preprocess("c") == 2
|
||||
with pytest.raises(ValueError):
|
||||
radio_input.preprocess("d")
|
||||
|
||||
radio_input = gr.Radio(["a", "b", "c"], type="value")
|
||||
assert radio_input.preprocess("c") == "c"
|
||||
with pytest.raises(ValueError):
|
||||
radio_input.preprocess("d")
|
||||
|
||||
|
||||
class TestImage:
|
||||
@pytest.mark.asyncio
|
||||
|
Loading…
Reference in New Issue
Block a user