Gradio logo

New to Gradio? Start here: Getting Started

See the Release History

ChatInterface

gradio.ChatInterface(fn, ยทยทยท)

Description

ChatInterface is Gradio's high-level abstraction for creating chatbot UIs, and allows you to create a web-based demo around a chatbot model in a few lines of code. Only one parameter is required: fn, which takes a function that governs the response of the chatbot based on the user input and chat history. Additional parameters can be used to control the appearance and behavior of the demo.

Example Usage

import gradio as gr

def echo(message, history):
    return message

demo = gr.ChatInterface(fn=echo, examples=["hello", "hola", "merhaba"], title="Echo Bot")
demo.launch()

Initialization

Parameter Description
fn

Callable

required

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

Chatbot | None

default: 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 | None

default: None

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.

examples

list[str] | None

default: None

sample inputs for the function; if provided, appear below the chatbot and can be clicked to populate the chatbot input.

cache_examples

bool | None

default: 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.

title

str | None

default: 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

default: 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

default: None

Theme to use, loaded from gradio.themes.

css

str | None

default: None

custom css or path to custom css file to use with interface.

analytics_enabled

bool | None

default: 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

default: "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

default: "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

default: "๐Ÿ”„ 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

default: "โ†ฉ๏ธ 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

default: "๐Ÿ—‘๏ธ Clear"

Text to display on the clear button. If None, no button will be displayed. If a Button object, that button will be used.

Demos

import random
import gradio as gr

def random_response(message, history):
    return random.choice(["Yes", "No"])

demo = gr.ChatInterface(random_response)

if __name__ == "__main__":
    demo.launch()