If you are getting started, we recommend using the
gr.ChatInterface to create chatbots—it’s a high-level abstraction that makes it possible to create beautiful chatbot applications fast, often with a single line of code. Read more about it here.Prerequisites
We’ll be using thegradio.Blocks class to build our Chatbot demo. You can read the Guide to Blocks first if you are not already familiar with it. Also please make sure you are using the latest version of Gradio:
A simple chatbot demo
Let’s start with creating a simple chatbot. As you may have noticed, our bot simply randomly responds “How are you?”, “Today is a great day”, or “I’m very hungry” to any input. Here’s the code to create this with Gradio:- A
Chatbot, whose value stores the entire history of the conversation, as a list of response pairs between the user and bot - A
Textboxwhere the user can type their message, and then hit enter/submit to trigger the chatbot response - A
ClearButtonbutton to clear the Textbox and entire Chatbot history
respond(), which takes in the entire history of the chatbot, appends a random message, waits 1 second, and then returns the updated chat history. The respond() function also clears the textbox when it returns.
Of course, in practice, you would replace respond() with your own more complex function, which might call a pretrained model or an API, to generate a response.
Add streaming to your chatbot
There are several ways we can improve the user experience of the chatbot above. First, we can stream responses so the user doesn’t have to wait as long for a message to be generated. Second, we can have the user message appear immediately in the chat history, while the chatbot’s response is being generated. Here’s the code to achieve that:.then():
-
The first method
user()updates the chatbot with the user message and clears the input field. Because we want this to happen instantly, we setqueue=False, which would skip any queue had it been enabled. The chatbot’s history is appended with{"role": "user", "content": user_message}. -
The second method,
bot()updates the chatbot history with the bot’s response. Finally, we construct the message character by character andyieldthe intermediate outputs as they are being constructed. Gradio automatically turns any function with theyieldkeyword into a streaming output interface.
bot() with your own more complex function, which might call a pretrained model or an API, to generate a response.
Adding markdown, images, audio, or videos
Thegr.Chatbot component supports a subset of markdown including bold, italics, and code. For example, we could write a function that responds to a user’s message, with a bold That’s cool!, like this:
MultimodalTextbox component to easily upload all types of media files to your chatbot. You can customize the MultimodalTextbox further by passing in the sources parameter, which is a list of sources to enable.
To pass in a media file, we must pass in the file as a dictionary with a path key pointing to a local file. The alt_text is optional, so you can also just pass in a tuple with a single element {"path": "filepath"}, like this:
Further examples
Here are some links to Chatbots that are running on Spaces so that you can get an idea of what else is possible:- project-baize/Baize-7B: A stylized chatbot that allows you to stop generation as well as regenerate responses
- MAGAer13/mPLUG-Owl: A multimodal chatbot that allows you to upvote and downvote responses