Introducing Gradio Clients
WatchIntroducing Gradio Clients
WatchNew to Gradio? Start here: Getting Started
See the Release History
To install Gradio from main, run the following command:
pip install https://gradio-builds.s3.amazonaws.com/d8c939444451e8ce80a6256daba3ab7fbc1dcac3/gradio-4.44.1-py3-none-any.whl
*Note: Setting share=True
in
launch()
will not work.
gradio.ChatInterface(fn, Β·Β·Β·)
Basic Example: A chatbot that echoes back the usersβs message
import gradio as gr
def echo(message, history):
return message
demo = gr.ChatInterface(fn=echo, examples=["hello", "hola", "merhaba"], title="Echo Bot")
demo.launch()
Custom Chatbot: A gr.ChatInterface
with a custom gr.Chatbot
that includes a placeholder as well as upvote/downvote buttons. The upvote/downvote buttons are automatically added when a .like()
event is attached to a gr.Chatbot
. In order to attach event listeners to your custom chatbot, wrap the gr.Chatbot
as well as the gr.ChatInterface
inside of a gr.Blocks
like this:
import gradio as gr
def yes(message, history):
return "yes"
def vote(data: gr.LikeData):
if data.liked:
print("You upvoted this response: " + data.value["value"])
else:
print("You downvoted this response: " + data.value["value"])
with gr.Blocks() as demo:
chatbot = gr.Chatbot(placeholder="<strong>Your Personal Yes-Man</strong><br>Ask Me Anything")
chatbot.like(vote, None, None)
gr.ChatInterface(fn=yes, chatbot=chatbot)
demo.launch()
fn: Callable
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.
multimodal: bool
= False
if True, the chat interface will use a gr.MultimodalTextbox component for the input, which allows for the uploading of multimedia files. If False, the chat interface will use a gr.Textbox component for the input.
type: Literal['messages', 'tuples']
= "tuples"
chatbot: Chatbot | None
= None
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: Textbox | MultimodalTextbox | None
= None
an instance of the gr.Textbox or gr.MultimodalTextbox component to use for the chat interface, if you would like to customize the textbox properties. If not provided, a default gr.Textbox or gr.MultimodalTextbox component will be created.
additional_inputs: str | Component | list[str | Component] | None
= None
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: str | None
= None
Deprecated. Will be removed in a future version of Gradio. Use the `additional_inputs_accordion` parameter instead.
additional_inputs_accordion: str | Accordion | None
= None
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: list[str] | list[dict[str, str | list]] | list[list] | None
= None
sample inputs for the function; if provided, appear below the chatbot and can be clicked to populate the chatbot input. Should be a list of strings if `multimodal` is False, and a list of dictionaries (with keys `text` and `files`) if `multimodal` is True.
cache_examples: bool | Literal['lazy'] | None
= None
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.
examples_per_page: int
= 10
if examples are provided, how many to display per page.
title: str | None
= None
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: str | None
= None
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 | str | None
= None
a Theme object or a string representing a theme. If a string, will look for a built-in theme with that name (e.g. "soft" or "default"), or will attempt to load a theme from the Hugging Face Hub (e.g. "gradio/monochrome"). If None, will use the Default theme.
css: str | None
= None
custom css as a string or path to a css file. This css will be included in the demo webpage.
js: str | None
= None
custom js as a string or path to a js file. The custom js should be in the form of a single js function. This function will automatically be executed when the page loads. For more flexibility, use the head parameter to insert js inside <script> tags.
head: str | None
= None
custom html to insert into the head of the demo webpage. This can be used to add custom meta tags, multiple scripts, stylesheets, etc. to the page.
analytics_enabled: bool | None
= None
whether to allow basic telemetry. If None, will use GRADIO_ANALYTICS_ENABLED environment variable if defined, or default to True.
submit_btn: str | None | Button
= "Submit"
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: str | None | Button
= "Stop"
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.
retry_btn: str | None | Button
= "π Retry"
text to display on the retry button. If None, no button will be displayed. If a Button object, that button will be used.
undo_btn: str | None | Button
= "β©οΈ Undo"
text to display on the delete last button. If None, no button will be displayed. If a Button object, that button will be used.
clear_btn: str | None | Button
= "ποΈ Clear"
text to display on the clear button. If None, no button will be displayed. If a Button object, that button will be used.
autofocus: bool
= True
if True, autofocuses to the textbox when the page loads.
concurrency_limit: int | None | Literal['default']
= "default"
if set, this is the maximum number of chatbot submissions that can be running simultaneously. Can be set to None to mean no limit (any number of chatbot submissions can be running simultaneously). Set to "default" to use the default concurrency limit (defined by the `default_concurrency_limit` parameter in `.queue()`, which is 1 by default).
fill_height: bool
= True
if True, the chat interface will expand to the height of window.
delete_cache: tuple[int, int] | None
= None
a tuple corresponding [frequency, age] both expressed in number of seconds. Every `frequency` seconds, the temporary files created by this Blocks instance will be deleted if more than `age` seconds have passed since the file was created. For example, setting this to (86400, 86400) will delete temporary files every day. The cache will be deleted entirely when the server restarts. If None, no cache deletion will occur.
show_progress: Literal['full', 'minimal', 'hidden']
= "minimal"
how to show the progress animation while event is running: "full" shows a spinner which covers the output component area as well as a runtime display in the upper right corner, "minimal" only shows the runtime display, "hidden" shows no progress animation at all
fill_width: bool
= False
Whether to horizontally expand to fill container fully. If False, centers and constrains app to a maximum width.
import gradio as gr
def echo(message, history):
return message["text"]
demo = gr.ChatInterface(
fn=echo,
examples=[{"text": "hello"}, {"text": "hola"}, {"text": "merhaba"}],
title="Echo Bot",
multimodal=True,
)
demo.launch()