Building a Simple Chatbot with DialoGPT and Gradio

Explore the creation of a chatbot that combines the power of DialoGPT model and Gradio interface to chat and compute.

sabrilben
7 min read1 day ago
Photo by Jason Leung on Unsplash

Introduction

As a graduate of Automation Engineering, I’ve always been fascinated by AI and eager to explore its potential. However, as a beginner, I often found myself wondering what project to start with.

If you share this curiosity, this project is perfect for you. In this article, we’ll walk through the process, challenges, and learnings of creating a chatbot that can engage in simple conversations and perform basic arithmetic calculations.

Before we dive into the building process, let’s get acquainted with the two powerful tools we’ll be using: DialoGPT and Gradio.

What is DialoGPT? 🤖

DialoGPT is a state-of-the-art conversational AI model developed by Microsoft. It is based on the GPT-2 architecture and fine-tuned specifically for dialogue generation. DialoGPT can generate human-like responses, making it ideal for building chatbots and other conversational applications. Its ability to understand and generate contextually relevant responses makes it a powerful tool for AI enthusiasts.

What is Gradio? 📻

No, it’s not a radio made by Google! Gradio is an open-source Python library that allows us to quickly create user-friendly web interfaces for machine learning models. With Gradio, we can easily build interactive demos and applications without needing extensive web development skills. It provides a simple and intuitive way to showcase your AI projects and make them accessible to a broader audience.

Example of Chatbot Interaction: Text Generation

Why Use DialoGPT and Gradio for Your Projects? 🤔

Combining DialoGPT and Gradio offers several benefits for AI enthusiasts:

  • Ease of Use: Both DialoGPT and Gradio are designed to be user-friendly, making it easy for beginners to get started.
  • Powerful Capabilities: DialoGPT’s advanced conversational abilities and Gradio’s interactive interfaces provide a robust foundation for building engaging AI applications.
  • Quick Prototyping: With Gradio, you can quickly create and share prototypes of your AI projects, allowing for rapid iteration and feedback.

I’ll provide the link to download the full project at the end of this article. Essentially, you will only need the simplechatbot.py and requirements.txt files to start the project.

Now, let’s walk through the code! 🧑‍💻

Setting Up the Environment 🌳

First things first, let’s create a virtual environment for our project. A virtual environment helps you maintain a clean, organized, and consistent development setup, making it easier to manage and share your projects.

If you’re using VS Code, you can create a virtual environment by running the following command in the terminal:

virtualenv venv

Next, activate the virtual environment with this command:

venv\Scripts\activate

Now, let’s install the necessary libraries: transformers, gradio, and torch. Make sure the simplechatbot.py and requirements.txt files are in the same folder, then run the following command in the terminal:

pip install -r requirements.txt

Import Libraries & Load the Model 📚

We will use the transformers library to load the DialoGPT-medium model and tokenizer. The model is responsible for generating responses, while the tokenizer processes the input text.

import gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch

# Load the Hugging Face model and tokenizer
model_name = "microsoft/DialoGPT-medium"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)

Building the Chatbot Function and Conversation History 💬

The heart of our chatbot is the chatbot_response() function. This function handles user inputs, generates responses using the DialoGPT model, and manages the conversation history. Additionally, we'll add code to perform simple arithmetic calculations, which will be explained in the next section.

To ensure the chatbot maintains context, we’ll implement a conversation history feature. The chatbot will keep track of the last few interactions and reset the history after a certain number of interactions to prevent repetitive responses.

conversation_history = []
conversation = None
interaction_count = 0

def chatbot_response(prompt):
global conversation_history
global conversation
global interaction_count

# Reset conversation history after every 5 interactions
if interaction_count >= 5:
conversation_history = []
conversation = None
interaction_count = 0
# Encode the new user input, add the eos_token and return a tensor in Pytorch
new_user_input_ids = tokenizer.encode(prompt + tokenizer.eos_token, return_tensors='pt')

# Append the new user input tokens to the chat history
if conversation is not None:
bot_input_ids = torch.cat([conversation, new_user_input_ids], dim=-1)
else:
bot_input_ids = new_user_input_ids

# Generate a response while limiting the total chat history to 1000 tokens
conversation = model.generate(bot_input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id)

# Decode the response
response = tokenizer.decode(conversation[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)

# Update conversation history
conversation_history.append(f"Q: {prompt}")
conversation_history.append(f"A: {response}")

# Increment interaction count
interaction_count += 1

history = "\n".join(conversation_history[-6:]) # Show last 3 interactions (Q&A pairs)

return history

Handling Arithmetic Calculations 🧮

Since the DialoGPT model doesn’t have the ability to handle arithmetic calculations, we’ll add code to enable the chatbot to perform these operations. If the input specifically starts with ‘calculate’, the chatbot processes the arithmetic operation and returns the result. This feature is implemented within the chatbot_response() function.

def is_float(value):
try:
float(value)
return True
except ValueError:
return False

def chatbot_response(prompt):

lines = prompt.splitlines()

if len(lines) > 1:
response = "Please only input 1 line per prompt."
elif len(lines) == 1 and prompt.lower().startswith('calculate '): # "calculate" in prompt.lower():
calculation = prompt.lower().lstrip("calculate ")
component = calculation.split()
if len(component) == 3:
num1, operator, num2 = component
if is_float(num1) == True and is_float(num2) == True and operator in ['+', '-', '*', '/', '**', '//', '%']:
num1 = float(num1)
num2 = float(num2)

if operator == '+':
result = num1 + num2
response = f"The result is {result}"
elif operator == '-':
result = num1 - num2
response = f"The result is {result}"
elif operator == '*':
result = num1 * num2
response = f"The result is {result}"
elif operator == '**':
result = num1 ** num2
response = f"The result is {result}"
elif operator == '%':
result = num1 % num2
response = f"The result is {result}"
elif operator == '/':
if num2 != 0:
result = num1 / num2
response = f"The result is {result}"
else:
response = "Error: Division by zero"
elif operator == '//':
if num2 != 0:
result = num1 // num2
response = f"The result is {result}"
else:
response = "Error: Division by zero"
else:
response = "Invalid operator and/or calculation format. Please use 'calculate <num1> <operator> <num2>"
else:
response = "Invalid operator and/or calculation format. Please use 'calculate <num1> <operator> <num2>"
elif len(lines) == 1 and prompt.lower() == "hello":
response = "Hi there! How can I help you today?"
elif len(lines) == 1 and prompt.lower() == "bye":
response = "Goodbye! Have a great day!"

return history

Creating the Gradio Interface 🏠

To make the chatbot accessible and easy to use, we’ll create a Gradio interface. This interface allows users to interact with the chatbot by typing their questions or statements and receiving responses in real-time.

For this chatbot, I’ve chosen the allenai/gradio-theme as the theme interface. If you prefer a different theme, you can explore various options in the Theme Gallery.

I also recommend reading How To Gradio: Understanding the basics of gradio.Interface by Huggy Monkey to better understand how to use and modify your Gradio interface.

desc = "Your friendly chatbot powered by DialoGPT and Gradio. Let's chat! (pleasee.. 🥲)."

article = "<h3>How to Use:</h3> " \
"<ol><li>Open the chatbot.</li> " \
"<li>Type anything in the <strong>Question</strong> box (e.g., 'hello', 'What is AI?', 'calculate 9 * 7', 'bye', etc.).</li>" \
"<li>For calculations, use the following operators:" \
"<ul><li>+ for addition</li>" \
"<li>- for subtraction</li>" \
"<li>* for multiplication</li>" \
"<li>/ for division</li>" \
"<li>** for exponentiation</li>" \
"<li>// for floor division</li>" \
"<li>% for modulus</li></ul></li>" \
"<li>Press 'Enter' or click the 'Submit' button.</li></ol>" \
"<h5>The chatbot will respond to your queries.</h5>"

demo = gr.Interface(fn=chatbot_response,
inputs=gr.Textbox(label="User", placeholder="Say something pleaseee..."),
outputs=gr.Textbox(label="Conversation"),
title="ChatMate!",
description=desc,
article=article,
theme='allenai/gradio-theme').launch()

This code sets up a user-friendly interface for interacting with the chatbot. Users can type their questions or statements in the input box and receive responses in real-time.

Testing the Chatbot 🔍

To test the chatbot, run the full simplechatbot.py code in VS Code. You should see a message in the terminal that says “Running on local URL: http://127.X.X.X:XXXX”. You can ctrl + click this URL link to open the Gradio interface in your web browser.

Once the interface is open, you can type anything in the User’s Input textbox, and the chatbot will generate a response. For arithmetic functions, make sure to follow the exact syntax: “calculate <num1> <operator> <num2>".

For example, you can try:

  • calculate 9 + 7
  • calculate 15 / 3
  • calculate 2 ** 3

The chatbot will process these inputs and return the results.

Example of Chatbot Interaction: Basic Calculation

Conclusion

Building this chatbot was an exciting and educational experience. It highlights the power of the DialoGPT model and the simplicity of the Gradio interface. This project is perfect for beginners who want to dive into AI development.

You can find the complete project files on my GitHub repository. If you prefer to try it out in a Python Notebook, there’s also a link to my Google Colab in the .py file. Happy coding! 😊

--

--

No responses yet