Building an Interface for ChatterBot with Gradio

Advancements in AI are accelerating at a pace now being described by some as a Red Queen's Race. The phrase here originates from Lewis Carroll's Through the Looking-Glass where Red Queen tells Alice "it takes all the running you can do, to keep in the same place." Advances in AI currently seem to arrive daily; the next most powerful model, the MCP protocol for connecting LLMs to external services, agentic protocols, and the list keeps growing. All attempts to get ahead in the race just seem to be met by the next major change emerging.
In cases like this, sometimes having the right toolset can help tremendously. Gradio provides tooling that makes it quick and easy to create demos of machine learning models, and share them with others. At the rate that AI is advancing the ability to share quick spikes and iterations of a project can be a pivotal improvement to the work of development. In this tutorial, we'll explore using Gradio and an instance of ChatterBot to build a sharable chat app.
Setup
Start by installing the required packages.
pip install --upgrade gradio chatterbot[dev]
The following example code demonstrates how to initialize an instance of ChatterBot and set up a Gradio chat interface for it. While this example is intended to be simple, it is definitely worth checking out the Gradio ChatInterface
documentation which covers a wide range of other components you can add to your chat interface (such as feedback for returned responses).
# gradio-example.py
import gradio as gr
from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer
chatbot = ChatBot(
"Chatbot",
storage_adapter="chatterbot.storage.SQLStorageAdapter",
logic_adapters=[
"chatterbot.logic.BestMatch",
"chatterbot.logic.TimeLogicAdapter"
],
database_uri="sqlite:///database.sqlite3",
read_only=True
)
def train_chatbot():
"""
Train the chatbot with a small amount of simple data
"""
# Skip training if the chatbot has already has data
if chatbot.storage.count() > 0:
return
trainer = ListTrainer(chatbot)
trainer.train([
"Hello, how are you?",
"I am good, thank you!",
"You're welcome!",
])
trainer.train([
"Who are you?",
"I am a chatbot built using ChatterBot and Gradio.",
"It's nice to meet you!",
"Nice to meet you too!",
])
trainer.train([
"Tell me a joke.",
"Why did the chicken cross the road? To get to the other side!",
"So funny!",
"I know, right?",
])
def response_function(message, history):
return chatbot.get_response(message).text
train_chatbot()
interface = gr.ChatInterface(
fn=response_function,
type="messages",
title="ChatterBot with Gradio",
description="Enter your message and get a response from the chatbot.",
)
interface.launch(server_port=9000)
Run the server
The following command can be used to run the server for your Gradio chat interface.
python gradio-example.py
You should see output similar to the following:
* Running on local URL: http://127.0.0.1:5000
To create a public link, set `share=True` in `launch()`.
Navigate to http://localhost:5000
in your browser. You should have a chat interface available:
Sharing
Gradio supports a method allowing you to create a public link to the app you’re created. The code itself stays running on your machine, but a unique public url will be generated by the Gradio Share Servers to forward traffic.
To set this up you can make the following 1-line change to your code:
# diff: add share=True to the launch parameters
-interface.launch(server_port=9000)
+interface.launch(server_port=9000, share=True)
Optionally, if you have concerns about this link being publicly accessible, you can also include the following additions so that authentication is required before users can interact with your shared app:
def example_auth(username, password):
"""
NOTE: When implementing authentication beyond the purposes of
this example, you can have this function check against an auth
provider or other secure keystore.
"""
return username == 'admin' and password == 'password'
interface.launch(server_port=9000, share=True, auth=example_auth)
Next, use ctrl c
to stop your app and then rerun it with the python
command from earlier. This time we should see the following output:
* Running on local URL: http://127.0.0.1:9000
* Running on public URL: https://00609f9247dec67ca3.gradio.live
This share link expires in 72 hours. For free permanent hosting and GPU upgrades,
run `gradio deploy` from the terminal in the working directory to deploy
to Hugging Face Spaces (https://huggingface.co/spaces)
Now, we can navigate to that public URL in out browser. If you chose to include the authentication changes noted previously you’ll be required to login.
Your shared link should then be accessible:
Conclusion
This wraps up our simple example showing how to share a instance of ChatterBot using Gradio. Be sure to look over the other examples Gradio has on their site since they support a lot more functionality than just chat interfaces. For example, in a recent blog post they announced that Gradio servers like the one we launched here can also be used as MCP servers (basically adding mcp_server=True
to the launch
parameters, see their docs for the full implementation and usage details).
"blog post topic"
tag when doing so.