Building a Simple Chatbot with Transformers and Gradio

Surabhi Anuradha
5 min readOct 30, 2023

--

Using Python and Gradio User Interface.

image source(images.in)

In this blog post, we will delve into the exciting world of natural language processing and artificial intelligence. We’ll explore the process of building a chatbot using the powerful Transformers library by Hugging Face and Gradio, a user interface toolkit. Our chatbot will be capable of engaging in dynamic and meaningful conversations, using the Facebook BlenderBot model, which is a powerful conversational AI model, to create a chatbot that can hold conversations with users. Let’s dive into the code and understand how to create your own chatbot for a wide range of applications.

Prerequisites

Before we start, ensure that you have the required libraries installed. You can use the following commands to install them:

!pip install transformers
!pip install gradio

This first line of code is a system command that uses the pip package manager to install the "transformers" Python package. The "transformers" library is developed by Hugging Face and is widely used for natural language processing tasks. It provides pre-trained models, such as BERT, GPT-3, and more, and tools for working with them. By running this command, you ensure that the "transformers" library is installed in your Python environment, making it available for use in your code. The second line of code install the “gradio” Python package. Gradio is a library that simplifies the process of creating interactive web interfaces for machine learning models. It allows you to create user-friendly interfaces for your AI models, making them accessible through web browsers. By running this command, you install the “gradio” package, enabling you to use Gradio to build interactive interfaces for your machine learning models.

Understanding the Code

Let’s break down the code into sections to understand what each part does:

1. Importing Libraries and Setting up the Chatbot

from transformers import pipeline
from transformers import Conversation

import gradio as gr

chatbot = pipeline(model="facebook/blenderbot-400M-distill")

In this section, we import the necessary libraries and set up the chatbot. We use the pipeline function from the Transformers library to load the pre-trained "facebook/blenderbot-400M-distill" model, which is a smaller version of the original BlenderBot model but still capable of engaging in meaningful conversations. The second line of code imports the Conversation class from the Transformers library, which is a powerful library for working with various natural language processing models and tasks. The Conversation class is specifically designed to help users manage and organize conversations in a structured format. It allows developers to keep track of the history of a conversation, including user inputs and model responses, making it easier to interact with and manage chatbots or dialogue-based AI models. This class is valuable when building chatbots, question-answering systems, and any application involving back-and-forth communication.

2. Initiating a Conversation with the Chatbot

conversation = Conversation("Hi, I'm Surabhi, how are you?")
conversation = chatbot(conversation)
print(conversation)

Here, we initiate a conversation with the chatbot. The conversation starts with a simple greeting from the user, and we use the chatbot to generate a response. The generated response is then printed to the console.

Chat Assistant response for the conversation initiated.

3. Expanding the Conversation

conversation.add_user_input("Yes, Of course. I like to make handcraft items from waste material")
conversation = chatbot(conversation)
print(conversation)

We expand the conversation by adding another user input and getting a response from the chatbot. This demonstrates how you can maintain a continuous conversation with the chatbot by updating the conversation history.

Chat Assistant response after expanding the conversion

4. Creating a Gradio Interface

import gradio as gr

message_list = []
response_list = []

def mini_chatbot(message, history):
conversation = Conversation(text=message,
past_user_inputs=message_list,
generated_responses=response_list)
conversation = chatbot(conversation)

return conversation.generated_responses[-1]

demo_chatbot = gr.ChatInterface(mini_chatbot,
title="My Chatbot",
description="Enter text to start chatting.")

In this section, we define a function mini_chatbot that takes a user message and a history of previous messages. This function simulates a conversation with the chatbot by maintaining a list of user inputs and generated responses. The chatbot is called by passing the conversation object as an argument. The purpose is to have the chatbot generate a response based on the input message and the conversation history stored in the message_list and response_list. The conversation object is updated to incorporate the chatbot's response. This allows the conversation history to reflect both the user inputs and chatbot responses in a structured manner. The last generated response is returned.

We then create a Gradio chat interface named demo_chatbot. This interface takes user input and displays the chatbot's responses. The mini_chatbot function is used as the underlying chatbot logic. You can think of Gradio as a tool for creating user interfaces for machine learning models, making it easy to interact with AI models.

5. Launching the Chat Interface

demo_chatbot.launch()

Finally, we launch the Gradio interface, allowing users to interact with the chatbot in a user-friendly way.

Running the Chatbot

You can run the code, and it will start a Gradio interface where you can chat with the chatbot. The chatbot uses the Facebook BlenderBot model to generate responses based on the input you provide.

Conversating with the Chatbot

conversational chatbot (Images : left to right and top to bottom)

Conclusion

In this blog post, we’ve demonstrated how to build a chatbot using Hugging Face’s Transformers library and Gradio. We used a pre-trained model to create a simple chatbot that can engage in conversations with users. You can expand on this foundation to create more sophisticated chatbots by fine-tuning models or customizing their behavior. The combination of Transformers and Gradio makes it easy to develop interactive AI applications.

References

https://huggingface.co/facebook/blenderbot-400M-distill?text=Hey+my+name+is+Julien%21+How+are+you%3F

--

--

Surabhi Anuradha

Enthusiatic Learner & Trainer, GenAI, Keshav Memorial Institute of Technology, Hyderabad