Creating an AI Chatbot with Hugging Face Transformers and Gradio

With the AI wind blowing, I wanted to get my hands dirty by creating a chatbot with Hugging Face Transformers and Gradio, exploring the powerful tools available in the world of Machine Learning, Generative AI, LLMs, fine-tuned LLMs, and RLHF.
Creating an AI Chatbot
Share
In this article

With the AI wind blowing, I wanted to get my hands dirty by creating a chatbot with Hugging Face Transformers and Gradio, exploring the powerful tools available in the world of Machine Learning, Generative AI, LLMs, fine-tuned LLMs, and RLHF. But what do they all really mean? To find out, I dove right in, starting by understanding the basics and building something tangible — a chatbot! And not just any chatbot, but one powered by Hugging Face’s Transformers.

What is Hugging Face?

Hugging Face is a company that has quickly become a cornerstone of the AI and machine learning community. They provide a powerful open-source platform for natural language processing (NLP) and a wide array of models that you can use out of the box.

Transformers

Transformers is a Python library that makes downloading and training state-of-the-art ML models easy. Although it was initially made for developing language models, its functionality has expanded to include models for computer vision, audio processing, and beyond.

“Chatbots are not just a tool for conversation — they are the first step toward creating AI that understands and interacts with us on a deeply human level.”

The Evolution of Chatbots

We’ve all seen the classic chatbots that respond based on predefined responses tied to specific keywords in our questions. These are pretty basic and, well, not exactly thrilling.

But LLMs? They’re a whole different beast. Instead of relying on a static response list, LLMs leverage vast amounts of data and sophisticated models to generate responses on the fly, keeping track of the conversation and providing more natural, context-aware replies. It’s like having a conversation with a (somewhat) knowledgeable friend rather than just querying a database.


Setting Up the Environment: Installing Transformers with Conda

Before diving into code, we need to set up the environment. Here’s how you can get started:

  1. Install Python: If you don’t already have Python installed, grab the latest version from python.org. I’d recommend Python 3.10 or above for compatibility with the latest libraries.
  2. Start by creating a virtual environment in your project directory:

 

Plain Text

python -m venv .env

 

Activate the virtual environment. On Linux and macOS:

 

Plain Text

source .env/bin/activate

 

Activate Virtual environment on Windows

 

Plain Text

.env/Scripts/activate

 

Create a Conda Environment: First, you’ll need to install Conda. If you’re new to Conda, you can get it via the Miniconda installer.

Once Conda is installed, create a yml file (hf-env.yml) using the below configuration.

 

Plain Text

name: hf-env
channels:  - conda-forge  - defaults
dependencies:  - anyio=3.7.1=pyhd8ed1ab_0
 - appnope=0.1.3=pyhd8ed1ab_0  - argon2-cffi=21.3.0=pyhd8ed1ab_0
 - argon2-cffi-bindings=21.2.0=py310h90acd4f_3  - arrow=1.2.3=pyhd8ed1ab_0
 - asttokens=2.2.1=pyhd8ed1ab_0  - async-lru=2.0.3=pyhd8ed1ab_0
 - attrs=23.1.0=pyh71513ae_1  - babel=2.12.1=pyhd8ed1ab_1
 - backcall=0.2.0=pyh9f0ad1d_0  - backports=1.0=pyhd8ed1ab_3
 - backports.functools_lru_cache=1.6.5=pyhd8ed1ab_0  - beautifulsoup4=4.12.2=pyha770c72_0
 - bleach=6.0.0=pyhd8ed1ab_0  - brotli-python=1.0.9=py310h7a76584_9
 - bzip2=1.0.8=h1de35cc_0  - ca-certificates=2023.7.22=h8857fd0_0
 - cached-property=1.5.2=hd8ed1ab_1  - cached_property=1.5.2=pyha770c72_1  - cffi=1.15.1=py310ha78151a_3
 - charset-normalizer=3.2.0=pyhd8ed1ab_0  - comm=0.1.3=pyhd8ed1ab_0
 - debugpy=1.6.7=py310h7a76584_0  - decorator=5.1.1=pyhd8ed1ab_0
 - defusedxml=0.7.1=pyhd8ed1ab_0  - entrypoints=0.4=pyhd8ed1ab_0
 - exceptiongroup=1.1.2=pyhd8ed1ab_0  - executing=1.2.0=pyhd8ed1ab_0
 - flit-core=3.9.0=pyhd8ed1ab_0  - fqdn=1.5.1=pyhd8ed1ab_0
 - idna=3.4=pyhd8ed1ab_0  - importlib-metadata=6.8.0=pyha770c72_0
 - importlib_metadata=6.8.0=hd8ed1ab_0
 - importlib_resources=6.0.0=pyhd8ed1ab_1
 - ipykernel=6.25.0=pyh5fb750a_0  - ipython=8.14.0=pyhd1c38e8_0
 - ipywidgets=8.0.7=pyhd8ed1ab_0  - isoduration=20.11.0=pyhd8ed1ab_0
 - jedi=0.18.2=pyhd8ed1ab_0  - jinja2=3.1.2=pyhd8ed1ab_1
 - json5=0.9.14=pyhd8ed1ab_0  - jsonpointer=2.0=py_0
 - jsonschema=4.18.4=pyhd8ed1ab_0  - jsonschema-specifications=2023.7.1=pyhd8ed1ab_0
 - jsonschema-with-format-nongpl=4.18.4=pyhd8ed1ab_0  - jupyter-lsp=2.2.0=pyhd8ed1ab_0
 - jupyter_client=8.3.0=pyhd8ed1ab_0
 - jupyter_core=5.3.1=py310h2ec42d9_0
 - jupyter_events=0.6.3=pyhd8ed1ab_1
 - jupyter_server=2.7.0=pyhd8ed1ab_0
 - jupyter_server_terminals=0.4.4=pyhd8ed1ab_1  - jupyterlab=4.0.3=pyhd8ed1ab_0
 - jupyterlab_pygments=0.2.2=pyhd8ed1ab_0
 - jupyterlab_server=2.24.0=pyhd8ed1ab_0
 - jupyterlab_widgets=3.0.8=pyhd8ed1ab_0
 - libcxx=16.0.6=hd57cbcb_0  - libffi=3.4.4=hecd8cb5_0
 - libsodium=1.0.18=hbcb3906_1  - markupsafe=2.1.3=py310h6729b98_0
 - matplotlib-inline=0.1.6=pyhd8ed1ab_0  - mistune=3.0.0=pyhd8ed1ab_0
 - nbclient=0.8.0=pyhd8ed1ab_0  - nbconvert-core=7.7.3=pyhd8ed1ab_0
 - nbformat=5.9.1=pyhd8ed1ab_0  - ncurses=6.4=hcec6c5f_0
 - nest-asyncio=1.5.6=pyhd8ed1ab_0  - notebook-shim=0.2.3=pyhd8ed1ab_0
 - openssl=3.1.1=h8a1eda9_1  - overrides=7.3.1=pyhd8ed1ab_0
 - packaging=23.1=pyhd8ed1ab_0  - pandocfilters=1.5.0=pyhd8ed1ab_0
 - parso=0.8.3=pyhd8ed1ab_0  - pexpect=4.8.0=pyh1a96a4e_2
 - pickleshare=0.7.5=py_1003  - pip=23.2.1=py310hecd8cb5_0
 - pkgutil-resolve-name=1.3.10=pyhd8ed1ab_0  - platformdirs=3.9.1=pyhd8ed1ab_0
 - prometheus_client=0.17.1=pyhd8ed1ab_0
 - prompt-toolkit=3.0.39=pyha770c72_0  - prompt_toolkit=3.0.39=hd8ed1ab_0  - psutil=5.9.5=py310h90acd4f_0
 - ptyprocess=0.7.0=pyhd3deb0d_0  - pure_eval=0.2.2=pyhd8ed1ab_0  - pycparser=2.21=pyhd8ed1ab_0
 - pygments=2.15.1=pyhd8ed1ab_0  - pyobjc-core=9.2=py310hef2d279_0
 - pyobjc-framework-cocoa=9.2=py310hef2d279_0  - pysocks=1.7.1=pyha2e5f31_6
 - python=3.10.12=h5ee71fb_0  - python-dateutil=2.8.2=pyhd8ed1ab_0
 - python-fastjsonschema=2.18.0=pyhd8ed1ab_0  - python-json-logger=2.0.7=pyhd8ed1ab_0
 - python_abi=3.10=2_cp310
 - pytz=2023.3=pyhd8ed1ab_0  - pyzmq=25.1.0=py310h998be00_0
 - readline=8.2=hca72f7f_0  - referencing=0.30.0=pyhd8ed1ab_0
 - requests=2.31.0=pyhd8ed1ab_0  - rfc3339-validator=0.1.4=pyhd8ed1ab_0
 - rfc3986-validator=0.1.1=pyh9f0ad1d_0  - rpds-py=0.9.2=py310h3461e44_0
 - send2trash=1.8.2=pyhd1c38e8_0  - setuptools=68.0.0=py310hecd8cb5_0
 - six=1.16.0=pyh6c4a22f_0  - sniffio=1.3.0=pyhd8ed1ab_0
 - soupsieve=2.3.2.post1=pyhd8ed1ab_0  - sqlite=3.41.2=h6c40b1e_0
 - stack_data=0.6.2=pyhd8ed1ab_0
 - terminado=0.17.1=pyhd1c38e8_0  - tinycss2=1.2.1=pyhd8ed1ab_0
 - tk=8.6.12=h5d9f67b_0  - tomli=2.0.1=pyhd8ed1ab_0
 - tornado=6.3.2=py310h6729b98_0  - traitlets=5.9.0=pyhd8ed1ab_0
 - typing-extensions=4.7.1=hd8ed1ab_0  - typing_extensions=4.7.1=pyha770c72_0  - typing_utils=0.1.0=pyhd8ed1ab_0  - tzdata=2023c=h04d1e81_0
 - uri-template=1.3.0=pyhd8ed1ab_0  - urllib3=2.0.4=pyhd8ed1ab_0
 - wcwidth=0.2.6=pyhd8ed1ab_0  - webcolors=1.13=pyhd8ed1ab_0
 - webencodings=0.5.1=py_1  - websocket-client=1.6.1=pyhd8ed1ab_0
 - wheel=0.38.4=py310hecd8cb5_0  - widgetsnbextension=4.0.8=pyhd8ed1ab_0
 - xz=5.4.2=h6c40b1e_0  - yaml=0.2.5=h0d85af4_2
 - zeromq=4.3.4=he49afe7_1  - zipp=3.16.2=pyhd8ed1ab_0
 - zlib=1.2.13=h4dc903c_0  - pip:      - accelerate==0.21.0      - certifi==2023.7.22      - filelock==3.12.2      - fsspec==2023.6.0      - gradio==3.39.0      - huggingface-hub==0.16.4      - mpmath==1.3.0      - networkx==3.1      - numpy==1.25.1      - pyyaml==6.0.1      - regex==2023.6.3      - safetensors==0.3.1      - sympy==1.12      - tokenizers==0.13.3      - torch==2.0.1      - tqdm==4.65.0      - transformers==4.31.0

 

In your terminal (or anaconda command prompt), you can create a new conda environment based on the above hf-env.yml file using the following commands

 

Plain Text

cd <directory with hf-env.yml>conda env create --file hf-env.yml

 

This may take a couple of minutes to install, but once it’s complete, you should be ready to go!

You’ll see something like this once the environment is successfully created:

Creation of new environment using CondaCreation of new environment using Conda

Once the environment is created, activate it with:

 

Plain Text

conda activate hf-env

Now let’s build the Chatbot  with Transformers

With our environment set up, let’s build a simple chatbot using Hugging Face’s Transformers. Here’s a basic example:

 

Plain Text

from transformers import Conversation, pipeline
# Initialize the chatbot pipelinechatbot = pipeline(model="facebook/blenderbot-400M-distill",
                  task="conversational")
# Initiate the conversationconversation = Conversation("Hi I'm Ram, how are you?")
response = chatbot(conversation)# Print the resultprint(conversation)
# Add user input to the conversationconversation.add_user_input("Where do you work?")
response = chatbot(conversation)
print(conversation)

 

This script initializes a conversational agent using the facebook/blenderbot-400M-distill model. It’s a lightweight version of Facebook’s BlenderBot, designed for conversational AI. The code creates a conversation object and then continues the dialogue based on user input.

When you run this script, the conversation gets printed in your terminal, showing how the chatbot interacts with the user. Here’s an example of what it looks like:

Chatbot Execution without gradio integrationChatbot Execution without gradio integration

In this example, the chatbot responds to the user’s initial greeting and continues the conversation when asked about work. The conversation history is maintained and displayed in a clear, structured format, showing how both the user and the bot contribute to the dialogue. This makes it easy to follow the flow of the conversation and understand how the chatbot is processing and responding to inputs.

But conversing with the chatbot like this in the terminal isn’t really fun, nor is it very practical. Typing commands and waiting for responses in a text-only interface can feel a bit outdated. What if we had a real UI where we could type whatever we want, just like chatting with a friend?

Making It Fun with Gradio

This is where Gradio comes into play!

As I was building the chatbot, I stumbled upon Gradio — a tool that lets you create simple web interfaces for your machine-learning models with just a few lines of code. This got me thinking, why not spin up a UI for my chatbot? It turns out, it’s incredibly easy!

The combination of Hugging Face Transformers and Gradio simplifies the process of creating a chatbot.

Here’s how you can do it:

  1. Create a file named gr_chat.py

 

Plain Text

import gradio as grfrom transformers import Conversation, pipeline

message_list = []
response_list = []
# Initialize the chatbot pipelinechatbot = pipeline(model="facebook/blenderbot-400M-distill",
                  task="conversational")

def chat_with_bot(message, history):
   conversation = Conversation(        text=message, past_user_inputs=message_list, generated_responses=response_list)    conversation = chatbot(conversation)
   return conversation.generated_responses[-1]


iface = gr.ChatInterface(
   chat_with_bot, title="My Conversational AI", description="A chatbot powered by Hugging Face Transformers!")
iface.launch()

 

This code sets up a simple conversational chatbot using Hugging Face’s Transformers library and deploys it in a web interface using Gradio. The user types a message in the Gradio UI, which is then processed by the chat_with_bot function. The chatbot model responds, and the response is displayed back in the Gradio interface, creating a seamless conversational experience.

This approach allows you to have a much more interactive and user-friendly experience compared to chatting with the bot through a terminal. Gradio takes care of the UI, letting you focus on building and refining your chatbot’s conversational abilities.

2. Run the below command to execute your Python code.

 

Plain Text

python gr_chat.py

Transformers and gradio python code snippetTransformers and gradio python code snippet

 

It will take some time to execute the command and once this code is run, you’ll have a web-based chatbot that’s easy to use. You can type in your messages, and the chatbot will respond in a conversational manner.

This simple UI makes the whole experience more engaging compared to interacting with the chatbot in a terminal.

Gradio Chatbot UIGradio Chatbot UI

Conclusion

We went from getting our feet wet with AI concepts to building a conversational chatbot with Hugging Face and taking it up a notch by adding a user-friendly interface with Gradio.

The best part? You can do all of this with minimal setup, thanks to the power of modern AI tools and libraries. So go ahead, get your hands dirty, and start building something awesome!

Share