Revisiting AI/ML with HuggingFace's Transformers Module

Revisiting AI/ML with HuggingFace's Transformers Module

Artificial Intelligence is the latest and greatest trend in tech. It stands to take away jobs, promote misinformation and is already invading the world of art, media and content creation. That being said, it is also pitched as the next step for humanity, the next phase in evolution as it were. Suffice to say, there's a lot going on in the realm of Artificial Intelligence and Machine Learning. A lot of really cool tech with just as much dramatic hype and buzzwords to go along with it. So what does this mean? Where are we with AI, where are we going and most importantly, how does this affect me as a developer?

The Big Picture

We have seen an ever increasing array of smart technology over the years. It seems like everything we interact with these days is "smart": our TVs, refrigerators, cars, phones, you name it. But while these things are indeed intelligent in their own way, the real catalyst for pushing true artificial intelligence into the public eye in recent years has been OpenAI with the release of its Large-Language-Model (LLM) ChatGPT.

While the original GPT (Generative Pre-Trained Transformer) architecture was introduced by OpenAI in a research paper in 2018 - the birth of the infamous ChatGPT was the first time this sort of technology was made available and accessible to the public at large. For the first time ever people were able to interact with artificial intelligence outside of a research capacity, in a way that was intuitive and accessible. All this is very cool, but can also be overwhelming. As a software developer, you feel a certain responsibility to explore this new world of AI and to stay up-to-date on the changes that our technological landscape is facing, and that is where HuggingFace comes in.

HuggingFace | Transformers

HuggingFace is an AI/ML company that was founded in 2016. It provides an incredible set of tools for developers to build and maintain machine learning models and datasets. It is designed to be open source, so you can sign up for a free account and have full access to all the resources that people in the community have published. Not only does HuggingFace provide a platform for their AI/ML community, but they have also built a few tools and libraries of their own that allow users to integrate with their resources seamlessly, the biggest one being their Transformers library. This library allows you to implement AI into your application with just a few lines of code.

# Using a pre-trained model with Transformers
from transformers import pipeline

ARTICLE = "Your awesome article. Probably much longer than this...."

summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
print(summarizer(ARTICLE, truncation=True))

# -- Output --
[{'summary_text': 'awesome summary . . .'}]

It really is that simple. In addition to using models out of the box, you can also fine tune existing models yourself. This allows for some incredible versatility in application as well as gives you the opportunity to dive a little deeper into how these things actually work.

In addition to its simplicity, one of the most appealing things about the Transformers library is that, once you have downloaded a base model from HuggingFace, you can save and manage it locally. This way your application will not have to be reliant on an external API and you have complete control of further fine-tuning, and how your data (and your customer's data) is used in your application. This is a HUGE deal given how unique these AI systems are relative to other software and the safety/privacy concerns they raise.

# Save and load models/tokenizers locally with Transformers
from transformers import AutoTokenizer, AutoModel

model = AutoModel.from_pretrained("facebook/bart-large-cnn")
tokenizer = AutoTokenizer.from_pretrained("facebook/bart-large-cnn")
# Save
model.save_pretrained('/path/to/model_directory')
tokenizer.save_pretrained('/path/to/model_directory')
# Load 
AutoModel.from_pretrained('/path/to/model_directory')
AutoTokenizer.from_pretrained('/path/to/model_directory')

In addition to this, the HuggingFace community also provides incredible documentation on their website. In fact, many of the models include links to associated research papers as well as the datasets and processes that were used to train the model. So while you won't be building your AI/ML system from the ground up, you will still be able to see exactly what was done to build it and can take necessary steps to measure potential bias, perform fine-tuning and ensure quality results in your own implementation.

Practically Speaking

There is no doubt that artificial intelligence is here to stay. Will it take our jobs and destroy civilization as we know it? Who can say. But looking to the future, it certainly seems like every developer should have some AI/ML capabilities in their tool belt. The technology is out there. Practically speaking, at this point it is less about being able to build state of the art AI systems from the ground up, big tech has taken care of that for us. Rather, us mere mortal devs should focus on being able to incorporate AI into our tech stack and understanding the basics of what makes these systems tick. Being able to make use of resources like HuggingFace to explore AI and its basic concepts such as training, tokenization, pre-processing and data bias will set you up for success in our technological future.

# TODO: pip install transformers