fix regarding callable function error (#6294)

* fix regarding callable function error

* add changeset

* fix

* push

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
Co-authored-by: Abubakar Abid <abubakar@huggingface.co>
This commit is contained in:
Srijan Sahay Srivastava 2023-11-07 01:42:28 +05:30 committed by GitHub
parent 5b5af1899d
commit 7ab73df48e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 42 additions and 10 deletions

View File

@ -0,0 +1,5 @@
---
"gradio": patch
---
fix: fix regarding callable function error

View File

@ -734,7 +734,10 @@ def special_args(
Returns:
updated inputs, progress index, event data index.
"""
signature = inspect.signature(fn)
try:
signature = inspect.signature(fn)
except ValueError:
return inputs or [], None, None
type_hints = utils.get_type_hints(fn)
positional_args = []
for param in signature.parameters.values():

View File

@ -322,12 +322,17 @@ class Interface(Blocks):
Interface.instances.add(self)
param_types = utils.get_type_hints(self.fn)
param_names = inspect.getfullargspec(self.fn)[0]
if len(param_names) > 0 and inspect.ismethod(self.fn):
param_names = param_names[1:]
for param_name in param_names.copy():
if utils.is_special_typed_parameter(param_name, param_types):
param_names.remove(param_name)
# param_names = inspect.getfullargspec(self.fn)[0]
param_names = []
try:
param_names = inspect.getfullargspec(self.fn)[0]
if len(param_names) > 0 and inspect.ismethod(self.fn):
param_names = param_names[1:]
for param_name in param_names.copy():
if utils.is_special_typed_parameter(param_name, param_types):
param_names.remove(param_name)
except (TypeError, ValueError):
param_names = utils.default_input_labels()
for component, param_name in zip(self.input_components, param_names):
assert isinstance(component, Component)
if component.label is None:

View File

@ -792,10 +792,13 @@ def is_special_typed_parameter(name, parameter_types):
def check_function_inputs_match(fn: Callable, inputs: list, inputs_as_dict: bool):
"""
Checks if the input component set matches the function
Returns: None if valid, a string error message if mismatch
Returns: None if valid or if the function does not have a signature (e.g. is a built in),
or a string error message if mismatch
"""
signature = inspect.signature(fn)
try:
signature = inspect.signature(fn)
except ValueError:
return None
parameter_types = get_type_hints(fn)
min_args = 0
max_args = 0
@ -948,3 +951,14 @@ class NamedString(str):
def __init__(self, *args):
super().__init__()
self.name = str(self) if args else ""
def default_input_labels():
"""
A generator that provides default input labels for components when the user's function
does not have named parameters. The labels are of the form "input 0", "input 1", etc.
"""
n = 0
while True:
yield f"input {n}"
n += 1

View File

@ -172,6 +172,11 @@ class TestInterface:
with Blocks():
Interface(fn=lambda x: x, inputs=Textbox(), outputs=Image())
def test_interface_with_built_ins(self):
t = Textbox()
Interface(fn=str, inputs=t, outputs=Textbox())
assert t.label == "input 0"
class TestTabbedInterface:
def test_tabbed_interface_config_matches_manual_tab(self):