mirror of
https://github.com/gradio-app/gradio.git
synced 2024-12-03 01:50:59 +08:00
add params to gr.Interface
and gr.ChatInterface
(#7124)
* add params * add changeset * clear button * changes * changes * simplify * fix * changes * revert * fix typing * added params to chatinterface * improve docstrings * improve docstrings --------- Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
This commit is contained in:
parent
45f725f8d0
commit
21a16c60e8
5
.changeset/cold-trees-wash.md
Normal file
5
.changeset/cold-trees-wash.md
Normal file
@ -0,0 +1,5 @@
|
||||
---
|
||||
"gradio": minor
|
||||
---
|
||||
|
||||
feat:add params to `gr.Interface` and `gr.ChatInterface`
|
@ -512,9 +512,9 @@ class Blocks(BlockContext, BlocksEvents, metaclass=BlocksMeta):
|
||||
analytics_enabled: Whether to allow basic telemetry. If None, will use GRADIO_ANALYTICS_ENABLED environment variable or default to True.
|
||||
mode: A human-friendly name for the kind of Blocks or Interface being created. Used internally for analytics.
|
||||
title: The tab title to display when this is opened in a browser window.
|
||||
css: Custom css or path to custom css file to apply to entire Blocks.
|
||||
js: Custom js or path to custom js file to run when demo is first loaded.
|
||||
head: Custom html to insert into the head of the page. This can be used to add custom meta tags, scripts, stylesheets, etc. to the page.
|
||||
css: Custom css as a string or path to a css file. This css will be included in the demo webpage.
|
||||
js: Custom js or path to js file to run when demo is first loaded. This javascript will be included in the demo webpage.
|
||||
head: Custom html to insert into the head of the demo webpage. This can be used to add custom meta tags, scripts, stylesheets, etc. to the page.
|
||||
"""
|
||||
self.limiter = None
|
||||
if theme is None:
|
||||
|
@ -68,6 +68,8 @@ class ChatInterface(Blocks):
|
||||
description: str | None = None,
|
||||
theme: Theme | str | None = None,
|
||||
css: str | None = None,
|
||||
js: str | None = None,
|
||||
head: str | None = None,
|
||||
analytics_enabled: bool | None = None,
|
||||
submit_btn: str | None | Button = "Submit",
|
||||
stop_btn: str | None | Button = "Stop",
|
||||
@ -79,18 +81,20 @@ class ChatInterface(Blocks):
|
||||
):
|
||||
"""
|
||||
Parameters:
|
||||
fn: the function to wrap the chat interface around. Should accept two parameters: a string input message and list of two-element lists of the form [[user_message, bot_message], ...] representing the chat history, and return a string response. See the Chatbot documentation for more information on the chat history format.
|
||||
chatbot: an instance of the gr.Chatbot component to use for the chat interface, if you would like to customize the chatbot properties. If not provided, a default gr.Chatbot component will be created.
|
||||
textbox: an instance of the gr.Textbox component to use for the chat interface, if you would like to customize the textbox properties. If not provided, a default gr.Textbox component will be created.
|
||||
additional_inputs: an instance or list of instances of gradio components (or their string shortcuts) to use as additional inputs to the chatbot. If components are not already rendered in a surrounding Blocks, then the components will be displayed under the chatbot, in an accordion.
|
||||
fn: The function to wrap the chat interface around. Should accept two parameters: a string input message and list of two-element lists of the form [[user_message, bot_message], ...] representing the chat history, and return a string response. See the Chatbot documentation for more information on the chat history format.
|
||||
chatbot: An instance of the gr.Chatbot component to use for the chat interface, if you would like to customize the chatbot properties. If not provided, a default gr.Chatbot component will be created.
|
||||
textbox: An instance of the gr.Textbox component to use for the chat interface, if you would like to customize the textbox properties. If not provided, a default gr.Textbox component will be created.
|
||||
additional_inputs: An instance or list of instances of gradio components (or their string shortcuts) to use as additional inputs to the chatbot. If components are not already rendered in a surrounding Blocks, then the components will be displayed under the chatbot, in an accordion.
|
||||
additional_inputs_accordion_name: Deprecated. Will be removed in a future version of Gradio. Use the `additional_inputs_accordion` parameter instead.
|
||||
additional_inputs_accordion: If a string is provided, this is the label of the `gr.Accordion` to use to contain additional inputs. A `gr.Accordion` object can be provided as well to configure other properties of the container holding the additional inputs. Defaults to a `gr.Accordion(label="Additional Inputs", open=False)`. This parameter is only used if `additional_inputs` is provided.
|
||||
examples: sample inputs for the function; if provided, appear below the chatbot and can be clicked to populate the chatbot input.
|
||||
examples: Sample inputs for the function; if provided, appear below the chatbot and can be clicked to populate the chatbot input.
|
||||
cache_examples: If True, caches examples in the server for fast runtime in examples. The default option in HuggingFace Spaces is True. The default option elsewhere is False.
|
||||
title: a title for the interface; if provided, appears above chatbot in large font. Also used as the tab title when opened in a browser window.
|
||||
description: a description for the interface; if provided, appears above the chatbot and beneath the title in regular font. Accepts Markdown and HTML content.
|
||||
theme: Theme to use, loaded from gradio.themes.
|
||||
css: custom css or path to custom css file to use with interface.
|
||||
css: Custom css as a string or path to a css file. This css will be included in the demo webpage.
|
||||
js: Custom js or path to js file to run when demo is first loaded. This javascript will be included in the demo webpage.
|
||||
head: Custom html to insert into the head of the demo webpage. This can be used to add custom meta tags, scripts, stylesheets, etc. to the page.
|
||||
analytics_enabled: Whether to allow basic telemetry. If None, will use GRADIO_ANALYTICS_ENABLED environment variable if defined, or default to True.
|
||||
submit_btn: Text to display on the submit button. If None, no button will be displayed. If a Button object, that button will be used.
|
||||
stop_btn: Text to display on the stop button, which replaces the submit_btn when the submit_btn or retry_btn is clicked and response is streaming. Clicking on the stop_btn will halt the chatbot response. If set to None, stop button functionality does not appear in the chatbot. If a Button object, that button will be used as the stop button.
|
||||
@ -106,6 +110,8 @@ class ChatInterface(Blocks):
|
||||
css=css,
|
||||
title=title or "Gradio",
|
||||
theme=theme,
|
||||
js=js,
|
||||
head=head,
|
||||
)
|
||||
self.concurrency_limit = concurrency_limit
|
||||
self.fn = fn
|
||||
@ -189,7 +195,7 @@ class ChatInterface(Blocks):
|
||||
scale=7,
|
||||
autofocus=autofocus,
|
||||
)
|
||||
if submit_btn:
|
||||
if submit_btn is not None:
|
||||
if isinstance(submit_btn, Button):
|
||||
submit_btn.render()
|
||||
elif isinstance(submit_btn, str):
|
||||
@ -203,7 +209,7 @@ class ChatInterface(Blocks):
|
||||
raise ValueError(
|
||||
f"The submit_btn parameter must be a gr.Button, string, or None, not {type(submit_btn)}"
|
||||
)
|
||||
if stop_btn:
|
||||
if stop_btn is not None:
|
||||
if isinstance(stop_btn, Button):
|
||||
stop_btn.visible = False
|
||||
stop_btn.render()
|
||||
@ -223,7 +229,7 @@ class ChatInterface(Blocks):
|
||||
|
||||
with Row():
|
||||
for btn in [retry_btn, undo_btn, clear_btn]:
|
||||
if btn:
|
||||
if btn is not None:
|
||||
if isinstance(btn, Button):
|
||||
btn.render()
|
||||
elif isinstance(btn, str):
|
||||
|
@ -119,37 +119,44 @@ class Interface(Blocks):
|
||||
head: str | None = None,
|
||||
additional_inputs: str | Component | list[str | Component] | None = None,
|
||||
additional_inputs_accordion: str | Accordion | None = None,
|
||||
*,
|
||||
submit_btn: str | Button = "Submit",
|
||||
stop_btn: str | Button = "Stop",
|
||||
clear_btn: str | Button = "Clear",
|
||||
**kwargs,
|
||||
):
|
||||
"""
|
||||
Parameters:
|
||||
fn: the function to wrap an interface around. Often a machine learning model's prediction function. Each parameter of the function corresponds to one input component, and the function should return a single value or a tuple of values, with each element in the tuple corresponding to one output component.
|
||||
inputs: a single Gradio component, or list of Gradio components. Components can either be passed as instantiated objects, or referred to by their string shortcuts. The number of input components should match the number of parameters in fn. If set to None, then only the output components will be displayed.
|
||||
outputs: a single Gradio component, or list of Gradio components. Components can either be passed as instantiated objects, or referred to by their string shortcuts. The number of output components should match the number of values returned by fn. If set to None, then only the input components will be displayed.
|
||||
examples: sample inputs for the function; if provided, appear below the UI components and can be clicked to populate the interface. Should be nested list, in which the outer list consists of samples and each inner list consists of an input corresponding to each input component. A string path to a directory of examples can also be provided, but it should be within the directory with the python file running the gradio app. If there are multiple input components and a directory is provided, a log.csv file must be present in the directory to link corresponding inputs.
|
||||
fn: The function to wrap an interface around. Often a machine learning model's prediction function. Each parameter of the function corresponds to one input component, and the function should return a single value or a tuple of values, with each element in the tuple corresponding to one output component.
|
||||
inputs: A single Gradio component, or list of Gradio components. Components can either be passed as instantiated objects, or referred to by their string shortcuts. The number of input components should match the number of parameters in fn. If set to None, then only the output components will be displayed.
|
||||
outputs: A single Gradio component, or list of Gradio components. Components can either be passed as instantiated objects, or referred to by their string shortcuts. The number of output components should match the number of values returned by fn. If set to None, then only the input components will be displayed.
|
||||
examples: Sample inputs for the function; if provided, appear below the UI components and can be clicked to populate the interface. Should be nested list, in which the outer list consists of samples and each inner list consists of an input corresponding to each input component. A string path to a directory of examples can also be provided, but it should be within the directory with the python file running the gradio app. If there are multiple input components and a directory is provided, a log.csv file must be present in the directory to link corresponding inputs.
|
||||
cache_examples: If True, caches examples in the server for fast runtime in examples. If `fn` is a generator function, then the last yielded value will be used as the output. The default option in HuggingFace Spaces is True. The default option elsewhere is False.
|
||||
examples_per_page: If examples are provided, how many to display per page.
|
||||
live: whether the interface should automatically rerun if any of the inputs change.
|
||||
title: a title for the interface; if provided, appears above the input and output components in large font. Also used as the tab title when opened in a browser window.
|
||||
description: a description for the interface; if provided, appears above the input and output components and beneath the title in regular font. Accepts Markdown and HTML content.
|
||||
article: an expanded article explaining the interface; if provided, appears below the input and output components in regular font. Accepts Markdown and HTML content.
|
||||
thumbnail: path or url to image to use as display image when the web demo is shared on social media.
|
||||
live: Whether the interface should automatically rerun if any of the inputs change.
|
||||
title: A title for the interface; if provided, appears above the input and output components in large font. Also used as the tab title when opened in a browser window.
|
||||
description: A description for the interface; if provided, appears above the input and output components and beneath the title in regular font. Accepts Markdown and HTML content.
|
||||
article: An expanded article explaining the interface; if provided, appears below the input and output components in regular font. Accepts Markdown and HTML content.
|
||||
thumbnail: String path or url to image to use as display image when the web demo is shared on social media.
|
||||
theme: Theme to use, loaded from gradio.themes.
|
||||
css: custom css or path to custom css file to use with interface.
|
||||
allow_flagging: one of "never", "auto", or "manual". If "never" or "auto", users will not see a button to flag an input and output. If "manual", users will see a button to flag. If "auto", every input the user submits will be automatically flagged (outputs are not flagged). If "manual", both the input and outputs are flagged when the user clicks flag button. This parameter can be set with environmental variable GRADIO_ALLOW_FLAGGING; otherwise defaults to "manual".
|
||||
flagging_options: if provided, allows user to select from the list of options when flagging. Only applies if allow_flagging is "manual". Can either be a list of tuples of the form (label, value), where label is the string that will be displayed on the button and value is the string that will be stored in the flagging CSV; or it can be a list of strings ["X", "Y"], in which case the values will be the list of strings and the labels will ["Flag as X", "Flag as Y"], etc.
|
||||
flagging_dir: what to name the directory where flagged data is stored.
|
||||
css: Custom css as a string or path to a css file. This css will be included in the demo webpage.
|
||||
allow_flagging: One of "never", "auto", or "manual". If "never" or "auto", users will not see a button to flag an input and output. If "manual", users will see a button to flag. If "auto", every input the user submits will be automatically flagged (outputs are not flagged). If "manual", both the input and outputs are flagged when the user clicks flag button. This parameter can be set with environmental variable GRADIO_ALLOW_FLAGGING; otherwise defaults to "manual".
|
||||
flagging_options: If provided, allows user to select from the list of options when flagging. Only applies if allow_flagging is "manual". Can either be a list of tuples of the form (label, value), where label is the string that will be displayed on the button and value is the string that will be stored in the flagging CSV; or it can be a list of strings ["X", "Y"], in which case the values will be the list of strings and the labels will ["Flag as X", "Flag as Y"], etc.
|
||||
flagging_dir: What to name the directory where flagged data is stored.
|
||||
flagging_callback: None or an instance of a subclass of FlaggingCallback which will be called when a sample is flagged. If set to None, an instance of gradio.flagging.CSVLogger will be created and logs will be saved to a local CSV file in flagging_dir. Default to None.
|
||||
analytics_enabled: Whether to allow basic telemetry. If None, will use GRADIO_ANALYTICS_ENABLED environment variable if defined, or default to True.
|
||||
batch: If True, then the function should process a batch of inputs, meaning that it should accept a list of input values for each parameter. The lists should be of equal length (and be up to length `max_batch_size`). The function is then *required* to return a tuple of lists (even if there is only 1 output component), with each list in the tuple corresponding to one output component.
|
||||
max_batch_size: Maximum number of inputs to batch together if this is called from the queue (only relevant if batch=True)
|
||||
api_name: defines how the endpoint appears in the API docs. Can be a string, None, or False. If set to a string, the endpoint will be exposed in the API docs with the given name. If None, the name of the prediction function will be used as the API endpoint. If False, the endpoint will not be exposed in the API docs and downstream apps (including those that `gr.load` this app) will not be able to use this event.
|
||||
api_name: Defines how the endpoint appears in the API docs. Can be a string, None, or False. If set to a string, the endpoint will be exposed in the API docs with the given name. If None, the name of the prediction function will be used as the API endpoint. If False, the endpoint will not be exposed in the API docs and downstream apps (including those that `gr.load` this app) will not be able to use this event.
|
||||
allow_duplication: If True, then will show a 'Duplicate Spaces' button on Hugging Face Spaces.
|
||||
concurrency_limit: If set, this is the maximum number of this event that can be running simultaneously. Can be set to None to mean no concurrency_limit (any number of this event can be running simultaneously). Set to "default" to use the default concurrency limit (defined by the `default_concurrency_limit` parameter in `.queue()`, which itself is 1 by default).
|
||||
js: Custom js or path to custom js file to run when demo is first loaded.
|
||||
head: Custom html to insert into the head of the page. This can be used to add custom meta tags, scripts, stylesheets, etc. to the page.
|
||||
js: Custom js or path to js file to run when demo is first loaded. This javascript will be included in the demo webpage.
|
||||
head: Custom html to insert into the head of the demo webpage. This can be used to add custom meta tags, scripts, stylesheets, etc. to the page.
|
||||
additional_inputs: A single Gradio component, or list of Gradio components. Components can either be passed as instantiated objects, or referred to by their string shortcuts. These components will be rendered in an accordion below the main input components. By default, no additional input components will be displayed.
|
||||
additional_inputs_accordion: If a string is provided, this is the label of the `gr.Accordion` to use to contain additional inputs. A `gr.Accordion` object can be provided as well to configure other properties of the container holding the additional inputs. Defaults to a `gr.Accordion(label="Additional Inputs", open=False)`. This parameter is only used if `additional_inputs` is provided.
|
||||
submit_btn: The button to use for submitting inputs. Defaults to a `gr.Button("Submit", variant="primary")`. This parameter does not apply if the Interface is output-only, in which case the submit button always displays "Generate". Can be set to a string (which becomes the button label) or a `gr.Button` object (which allows for more customization).
|
||||
stop_btn: The button to use for stopping the interface. Defaults to a `gr.Button("Stop", variant="stop", visible=False)`. Can be set to a string (which becomes the button label) or a `gr.Button` object (which allows for more customization).
|
||||
clear_btn: The button to use for clearing the inputs. Defaults to a `gr.Button("Clear", variant="secondary")`. Can be set to a string (which becomes the button label) or a `gr.Button` object (which allows for more customization).
|
||||
"""
|
||||
super().__init__(
|
||||
analytics_enabled=analytics_enabled,
|
||||
@ -298,6 +305,43 @@ class Interface(Blocks):
|
||||
self.examples = examples
|
||||
self.examples_per_page = examples_per_page
|
||||
|
||||
if isinstance(submit_btn, Button):
|
||||
self.submit_btn_parms = submit_btn.recover_kwargs(submit_btn.get_config())
|
||||
elif isinstance(submit_btn, str):
|
||||
self.submit_btn_parms = {
|
||||
"value": submit_btn,
|
||||
"variant": "primary",
|
||||
}
|
||||
else:
|
||||
raise ValueError(
|
||||
f"The submit_btn parameter must be a gr.Button or string, not {type(submit_btn)}"
|
||||
)
|
||||
|
||||
if isinstance(stop_btn, Button):
|
||||
self.stop_btn_parms = stop_btn.recover_kwargs(stop_btn.get_config())
|
||||
elif isinstance(stop_btn, str):
|
||||
self.stop_btn_parms = {
|
||||
"value": stop_btn,
|
||||
"variant": "stop",
|
||||
"visible": False,
|
||||
}
|
||||
else:
|
||||
raise ValueError(
|
||||
f"The stop_btn parameter must be a gr.Button or string, not {type(stop_btn)}"
|
||||
)
|
||||
|
||||
if isinstance(clear_btn, Button):
|
||||
self.clear_btn_params = clear_btn.recover_kwargs(clear_btn.get_config())
|
||||
elif isinstance(clear_btn, str):
|
||||
self.clear_btn_params = {
|
||||
"value": clear_btn,
|
||||
"variant": "secondary",
|
||||
}
|
||||
else:
|
||||
raise ValueError(
|
||||
f"The clear_btn parameter must be a gr.Button or string, not {type(clear_btn)}"
|
||||
)
|
||||
|
||||
self.simple_server = None
|
||||
|
||||
# For allow_flagging: (1) first check for parameter,
|
||||
@ -405,13 +449,13 @@ class Interface(Blocks):
|
||||
with self:
|
||||
self.render_title_description()
|
||||
|
||||
submit_btn, clear_btn, stop_btn, flag_btns, duplicate_btn = (
|
||||
_submit_btn, _clear_btn, _stop_btn, flag_btns, duplicate_btn = (
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
)
|
||||
) # type: ignore
|
||||
input_component_column = None
|
||||
|
||||
with Row(equal_height=False):
|
||||
@ -421,37 +465,37 @@ class Interface(Blocks):
|
||||
InterfaceTypes.UNIFIED,
|
||||
]:
|
||||
(
|
||||
submit_btn,
|
||||
clear_btn,
|
||||
stop_btn,
|
||||
_submit_btn,
|
||||
_clear_btn,
|
||||
_stop_btn,
|
||||
flag_btns,
|
||||
input_component_column,
|
||||
) = self.render_input_column()
|
||||
) = self.render_input_column() # type: ignore
|
||||
if self.interface_type in [
|
||||
InterfaceTypes.STANDARD,
|
||||
InterfaceTypes.OUTPUT_ONLY,
|
||||
]:
|
||||
(
|
||||
submit_btn_out,
|
||||
clear_btn_2_out,
|
||||
_submit_btn_out,
|
||||
_clear_btn_2_out,
|
||||
duplicate_btn,
|
||||
stop_btn_2_out,
|
||||
_stop_btn_2_out,
|
||||
flag_btns_out,
|
||||
) = self.render_output_column(submit_btn)
|
||||
submit_btn = submit_btn or submit_btn_out
|
||||
clear_btn = clear_btn or clear_btn_2_out
|
||||
stop_btn = stop_btn or stop_btn_2_out
|
||||
) = self.render_output_column(_submit_btn)
|
||||
_submit_btn = _submit_btn or _submit_btn_out
|
||||
_clear_btn = _clear_btn or _clear_btn_2_out
|
||||
_stop_btn = _stop_btn or _stop_btn_2_out
|
||||
flag_btns = flag_btns or flag_btns_out
|
||||
|
||||
if clear_btn is None:
|
||||
if _clear_btn is None:
|
||||
raise RenderError("Clear button not rendered")
|
||||
|
||||
self.attach_submit_events(submit_btn, stop_btn)
|
||||
self.attach_clear_events(clear_btn, input_component_column)
|
||||
self.attach_submit_events(_submit_btn, _stop_btn)
|
||||
self.attach_clear_events(_clear_btn, input_component_column)
|
||||
if duplicate_btn is not None:
|
||||
duplicate_btn.activate()
|
||||
|
||||
self.attach_flagging_events(flag_btns, clear_btn)
|
||||
self.attach_flagging_events(flag_btns, _clear_btn)
|
||||
self.render_examples()
|
||||
self.render_article()
|
||||
|
||||
@ -477,7 +521,7 @@ class Interface(Blocks):
|
||||
list[Button] | None,
|
||||
Column,
|
||||
]:
|
||||
submit_btn, clear_btn, stop_btn, flag_btns = None, None, None, None
|
||||
_submit_btn, _clear_btn, _stop_btn, flag_btns = None, None, None, None
|
||||
|
||||
with Column(variant="panel"):
|
||||
input_component_column = Column()
|
||||
@ -493,9 +537,9 @@ class Interface(Blocks):
|
||||
InterfaceTypes.STANDARD,
|
||||
InterfaceTypes.INPUT_ONLY,
|
||||
]:
|
||||
clear_btn = ClearButton()
|
||||
_clear_btn = ClearButton(**self.clear_btn_params) # type: ignore
|
||||
if not self.live:
|
||||
submit_btn = Button("Submit", variant="primary")
|
||||
_submit_btn = Button(**self.submit_btn_parms) # type: ignore
|
||||
# Stopping jobs only works if the queue is enabled
|
||||
# We don't know if the queue is enabled when the interface
|
||||
# is created. We use whether a generator function is provided
|
||||
@ -504,30 +548,30 @@ class Interface(Blocks):
|
||||
if inspect.isgeneratorfunction(
|
||||
self.fn
|
||||
) or inspect.isasyncgenfunction(self.fn):
|
||||
stop_btn = Button("Stop", variant="stop", visible=False)
|
||||
_stop_btn = Button(**self.stop_btn_parms)
|
||||
elif self.interface_type == InterfaceTypes.UNIFIED:
|
||||
clear_btn = ClearButton()
|
||||
submit_btn = Button("Submit", variant="primary")
|
||||
_clear_btn = ClearButton(**self.clear_btn_params) # type: ignore
|
||||
_submit_btn = Button(**self.submit_btn_parms) # type: ignore
|
||||
if (
|
||||
inspect.isgeneratorfunction(self.fn)
|
||||
or inspect.isasyncgenfunction(self.fn)
|
||||
) and not self.live:
|
||||
stop_btn = Button("Stop", variant="stop")
|
||||
_stop_btn = Button(**self.stop_btn_parms)
|
||||
if self.allow_flagging == "manual":
|
||||
flag_btns = self.render_flag_btns()
|
||||
elif self.allow_flagging == "auto":
|
||||
flag_btns = [submit_btn]
|
||||
flag_btns = [_submit_btn]
|
||||
return (
|
||||
submit_btn,
|
||||
clear_btn,
|
||||
stop_btn,
|
||||
_submit_btn,
|
||||
_clear_btn,
|
||||
_stop_btn,
|
||||
flag_btns,
|
||||
input_component_column,
|
||||
)
|
||||
|
||||
def render_output_column(
|
||||
self,
|
||||
submit_btn_in: Button | None,
|
||||
_submit_btn_in: Button | None,
|
||||
) -> tuple[
|
||||
Button | None,
|
||||
ClearButton | None,
|
||||
@ -535,8 +579,8 @@ class Interface(Blocks):
|
||||
Button | None,
|
||||
list | None,
|
||||
]:
|
||||
submit_btn = submit_btn_in
|
||||
clear_btn, duplicate_btn, flag_btns, stop_btn = (
|
||||
_submit_btn = _submit_btn_in
|
||||
_clear_btn, duplicate_btn, flag_btns, _stop_btn = (
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
@ -549,8 +593,8 @@ class Interface(Blocks):
|
||||
component.render()
|
||||
with Row():
|
||||
if self.interface_type == InterfaceTypes.OUTPUT_ONLY:
|
||||
clear_btn = ClearButton()
|
||||
submit_btn = Button("Generate", variant="primary")
|
||||
_clear_btn = ClearButton(**self.clear_btn_params) # type: ignore
|
||||
_submit_btn = Button("Generate", variant="primary")
|
||||
if (
|
||||
inspect.isgeneratorfunction(self.fn)
|
||||
or inspect.isasyncgenfunction(self.fn)
|
||||
@ -560,22 +604,22 @@ class Interface(Blocks):
|
||||
# is created. We use whether a generator function is provided
|
||||
# as a proxy of whether the queue will be enabled.
|
||||
# Using a generator function without the queue will raise an error.
|
||||
stop_btn = Button("Stop", variant="stop", visible=False)
|
||||
_stop_btn = Button(**self.stop_btn_parms)
|
||||
if self.allow_flagging == "manual":
|
||||
flag_btns = self.render_flag_btns()
|
||||
elif self.allow_flagging == "auto":
|
||||
if submit_btn is None:
|
||||
if _submit_btn is None:
|
||||
raise RenderError("Submit button not rendered")
|
||||
flag_btns = [submit_btn]
|
||||
flag_btns = [_submit_btn]
|
||||
|
||||
if self.allow_duplication:
|
||||
duplicate_btn = DuplicateButton(scale=1, size="lg", _activate=False)
|
||||
|
||||
return (
|
||||
submit_btn,
|
||||
clear_btn,
|
||||
_submit_btn,
|
||||
_clear_btn,
|
||||
duplicate_btn,
|
||||
stop_btn,
|
||||
_stop_btn,
|
||||
flag_btns,
|
||||
)
|
||||
|
||||
@ -583,15 +627,17 @@ class Interface(Blocks):
|
||||
if self.article:
|
||||
Markdown(self.article)
|
||||
|
||||
def attach_submit_events(self, submit_btn: Button | None, stop_btn: Button | None):
|
||||
def attach_submit_events(
|
||||
self, _submit_btn: Button | None, _stop_btn: Button | None
|
||||
):
|
||||
if self.live:
|
||||
if self.interface_type == InterfaceTypes.OUTPUT_ONLY:
|
||||
if submit_btn is None:
|
||||
if _submit_btn is None:
|
||||
raise RenderError("Submit button not rendered")
|
||||
super().load(self.fn, None, self.output_components)
|
||||
# For output-only interfaces, the user probably still want a "generate"
|
||||
# button even if the Interface is live
|
||||
submit_btn.click(
|
||||
_submit_btn.click(
|
||||
self.fn,
|
||||
None,
|
||||
self.output_components,
|
||||
@ -618,19 +664,19 @@ class Interface(Blocks):
|
||||
postprocess=not (self.api_mode),
|
||||
)
|
||||
else:
|
||||
if submit_btn is None:
|
||||
if _submit_btn is None:
|
||||
raise RenderError("Submit button not rendered")
|
||||
fn = self.fn
|
||||
extra_output = []
|
||||
|
||||
triggers = [submit_btn.click] + [
|
||||
triggers = [_submit_btn.click] + [
|
||||
component.submit # type: ignore
|
||||
for component in self.input_components
|
||||
if component.has_event(Events.submit)
|
||||
]
|
||||
|
||||
if stop_btn:
|
||||
extra_output = [submit_btn, stop_btn]
|
||||
if _stop_btn:
|
||||
extra_output = [_submit_btn, _stop_btn]
|
||||
|
||||
def cleanup():
|
||||
return [Button(visible=True), Button(visible=False)]
|
||||
@ -642,7 +688,7 @@ class Interface(Blocks):
|
||||
Button(visible=True),
|
||||
),
|
||||
inputs=None,
|
||||
outputs=[submit_btn, stop_btn],
|
||||
outputs=[_submit_btn, _stop_btn],
|
||||
queue=False,
|
||||
show_api=False,
|
||||
).then(
|
||||
@ -666,10 +712,10 @@ class Interface(Blocks):
|
||||
show_api=False,
|
||||
)
|
||||
|
||||
stop_btn.click(
|
||||
_stop_btn.click(
|
||||
cleanup,
|
||||
inputs=None,
|
||||
outputs=[submit_btn, stop_btn],
|
||||
outputs=[_submit_btn, _stop_btn],
|
||||
cancels=predict_event,
|
||||
queue=False,
|
||||
show_api=False,
|
||||
@ -691,11 +737,11 @@ class Interface(Blocks):
|
||||
|
||||
def attach_clear_events(
|
||||
self,
|
||||
clear_btn: ClearButton,
|
||||
_clear_btn: ClearButton,
|
||||
input_component_column: Column | None,
|
||||
):
|
||||
clear_btn.add(self.input_components + self.output_components)
|
||||
clear_btn.click(
|
||||
_clear_btn.add(self.input_components + self.output_components)
|
||||
_clear_btn.click(
|
||||
None,
|
||||
[],
|
||||
([input_component_column] if input_component_column else []), # type: ignore
|
||||
@ -715,7 +761,7 @@ class Interface(Blocks):
|
||||
)
|
||||
|
||||
def attach_flagging_events(
|
||||
self, flag_btns: list[Button] | None, clear_btn: ClearButton
|
||||
self, flag_btns: list[Button] | None, _clear_btn: ClearButton
|
||||
):
|
||||
if not (
|
||||
flag_btns
|
||||
@ -765,7 +811,7 @@ class Interface(Blocks):
|
||||
queue=False,
|
||||
show_api=False,
|
||||
)
|
||||
clear_btn.click(
|
||||
_clear_btn.click(
|
||||
flag_method.reset, None, flag_btn, queue=False, show_api=False
|
||||
)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user