Skip to content

Commit 9285dd9

Browse files
Fix typing for components in gr.Interface and docstring in image.py (#10235)
* changes * add changeset * changes * changes * change * add changeset * image.py * revert msg --------- Co-authored-by: gradio-pr-bot <[email protected]>
1 parent 1be31c1 commit 9285dd9

File tree

4 files changed

+32
-23
lines changed

4 files changed

+32
-23
lines changed

.changeset/twenty-waves-reply.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"gradio": patch
3+
---
4+
5+
fix:Fix typing for components in `gr.Interface` and docstring in `image.py`

gradio/components/image.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def __init__(
117117
every: Continously calls `value` to recalculate it if `value` is a function (has no effect otherwise). Can provide a Timer whose tick resets `value`, or a float that provides the regular interval for the reset Timer.
118118
inputs: Components that are used as inputs to calculate `value` if `value` is a function (has no effect otherwise). `value` is recalculated any time the inputs change.
119119
show_label: if True, will display label.
120-
show_download_button: If True, will display button to download image.
120+
show_download_button: If True, will display button to download image. Only applies if interactive is False (e.g. if the component is used as an output).
121121
container: If True, will place the component in a container - providing some extra padding around the border.
122122
scale: relative size compared to adjacent Components. For example if Components A and B are in a Row, and A has scale=2, and B has scale=1, A will be twice as wide as B. Should be an integer. scale applies in Rows, and to top-level Components in Blocks where fill_height=True.
123123
min_width: minimum pixel width, will wrap if not sufficient screen space to satisfy this value. If a certain scale value results in this Component being narrower than min_width, the min_width parameter will be respected first.

gradio/interface.py

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -222,28 +222,43 @@ def __init__(
222222
get_component_instance(i, unrender=True) for i in additional_inputs
223223
]
224224

225-
if not isinstance(inputs, (str, list, Component)):
225+
if not isinstance(inputs, (Sequence, Component)):
226226
raise TypeError(
227227
f"inputs must be a string, list, or Component, not {inputs}"
228228
)
229-
if not isinstance(outputs, (str, list, Component)):
229+
if not isinstance(outputs, (Sequence, Component)):
230230
raise TypeError(
231231
f"outputs must be a string, list, or Component, not {outputs}"
232232
)
233233

234-
if not isinstance(inputs, list):
234+
if isinstance(inputs, (str, Component)):
235235
inputs = [inputs]
236-
if not isinstance(outputs, list):
236+
if isinstance(outputs, (str, Component)):
237237
outputs = [outputs]
238238

239239
self.cache_examples = cache_examples
240240
self.cache_mode: Literal["eager", "lazy"] | None = cache_mode
241241

242+
self.main_input_components = [
243+
get_component_instance(i, unrender=True) for i in inputs
244+
]
245+
self.input_components = (
246+
self.main_input_components + self.additional_input_components
247+
)
248+
self.output_components = [
249+
get_component_instance(o, unrender=True)
250+
for o in outputs # type: ignore
251+
]
252+
242253
state_input_indexes = [
243-
idx for idx, i in enumerate(inputs) if i == "state" or isinstance(i, State)
254+
idx
255+
for idx, i in enumerate(self.input_components)
256+
if i == "state" or isinstance(i, State)
244257
]
245258
state_output_indexes = [
246-
idx for idx, o in enumerate(outputs) if o == "state" or isinstance(o, State)
259+
idx
260+
for idx, o in enumerate(self.output_components)
261+
if o == "state" or isinstance(o, State)
247262
]
248263

249264
if len(state_input_indexes) == 0 and len(state_output_indexes) == 0:
@@ -255,14 +270,14 @@ def __init__(
255270
else:
256271
state_input_index = state_input_indexes[0]
257272
state_output_index = state_output_indexes[0]
258-
if inputs[state_input_index] == "state":
273+
if self.input_components[state_input_index] == "state":
259274
default = utils.get_default_args(fn)[state_input_index]
260275
state_variable = State(value=default)
261276
else:
262-
state_variable = inputs[state_input_index]
277+
state_variable = self.input_components[state_input_index]
263278

264-
inputs[state_input_index] = state_variable
265-
outputs[state_output_index] = state_variable
279+
self.input_components[state_input_index] = state_variable
280+
self.output_components[state_output_index] = state_variable
266281

267282
if cache_examples:
268283
warnings.warn(
@@ -271,10 +286,6 @@ def __init__(
271286
)
272287
self.cache_examples = False
273288

274-
self.main_input_components = [
275-
get_component_instance(i, unrender=True) for i in inputs
276-
]
277-
278289
if additional_inputs_accordion is None:
279290
self.additional_inputs_accordion_params = {
280291
"label": "Additional Inputs",
@@ -294,13 +305,6 @@ def __init__(
294305
raise ValueError(
295306
f"The `additional_inputs_accordion` parameter must be a string or gr.Accordion, not {type(additional_inputs_accordion)}"
296307
)
297-
self.input_components = (
298-
self.main_input_components + self.additional_input_components
299-
)
300-
self.output_components = [
301-
get_component_instance(o, unrender=True)
302-
for o in outputs # type: ignore
303-
]
304308

305309
for component in self.input_components + self.output_components:
306310
if not (isinstance(component, Component)):

test/test_interfaces.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def test(parameter_name1, parameter_name2):
6969

7070
t = Textbox()
7171
i = Image()
72-
Interface(test, [t, i], "text")
72+
Interface(test, (t, i), "text")
7373
assert t.label == "parameter_name1"
7474
assert i.label == "parameter_name2"
7575

0 commit comments

Comments
 (0)