By: [email protected]

Interested in learning about ML/AI? Start here!

Published 12/17/2023, 7:36:55 PM


HuggingFace - Introduction, Transformers and Pipelines Oh My!

Introduction

Over the last few years, ML, AI, NLP and so many other abbreviations have come so far and dare I say, become part of our daily lives.

We see them in the news, in our social media feeds, and even in our favorite TV shows.

I have experience with PyTorch and some basic ML models, but I wanted to learn more about "New AI" and how I could realistically use it in my projects.

I have to be honest - it still felt overwhelming to start jumping back into this. So many new libraries, companies, models and DATA. It was a lot to take in.

Enter HuggingFace

HuggingFace logo

HuggingFace is like a superhero for ML/AI enthusiasts. I like to think of them as like GitHub for ML models.

What does that mean? Well, they have a ton of models and fun functionality that are open source and available for you to use!

That said, it's still overwhelming! I mean just visit their Documentation Hub - models and datasets and pipelines, oh my!

Each category has a ton of options and it's hard to know where to start - each one could warrant its own blog post!

So where do we start?

Transformers

Well, Transformers is debtably HuggingFace's bread and butter. It was released in 2017 and quickly became a hit in the community! I highly recommend clicking that link and reading the summary - it's a great overview of what Transformers is and how it works.

Transformers has evolved to encompass a vast array of ML applications. What has humble beginnings has now become a versatile tool for rapidly implementing powerful solutions. In the words of the creators:

Transformers is more than a toolkit to use pretrained models: it's a community of projects built around it and the HuggingFace Hub. We want Transformers to enable developers, researchers, students, professors, engineers, and anyone else to build their dream projects.

To see some more examples of what you can do with Transformers, check out their summary page. NOTE you may notice the word pipeline in there - we'll get to that in a bit!

So, let's start there!

Installation

You can view the installation instructions here - I'll summarize them here.

To get started, you'll need to install the transformers library - preferably in a virtual environment.

You'll also need to install some deep learning library - either PyTorch or TensorFlow. This will be determined by your hardware and models you're going to use, I recommend just going with PyTorch and calling it a day.

Once Installed you can test your installation by running:

python -c "from transformers import pipeline; print(pipeline('sentiment-analysis')('we love you'))"

It should give you a response like this:

[{'label': 'POSITIVE', 'score': 0.9998704791069031}]

Congrats! You've become an AI/ML expert!

AI Expert Medal

Not really, but either way what can we do with this?

Great, Now What?

Now what can we actually do with this!

Off the bat they have a ton of demos and examples that you can use to get started. I'm going to walk through a few basic ones here.

Pipelines

Before we get started, let's talk about pipelines. Pipelines`` are a great way to get started withTransformers`.

They are a high-level API that allows you to easily use pretrained models for a variety of tasks.

That's a lot of words, but what does it mean? Well, it means that you can use a pretrained model to do a variety of tasks without having to do a lot of work!

You'll see in practice what I mean shortly, but for now it's just important to know pipelines are a tool that you can use to get started with Transformers really quickly.

You can (and should) read more about pipelines here.

Sentiment Analysis

So one of the classic examples of ML/AI is sentiment analysis. This is where you take a piece of text and determine if it's positive or negative.

More specifically this falls under Natural Language Processing (NLP) and is a great way to get started with Transformers and HuggingFace!

This one is pretty straight forward - in face we already showed it off earlier when we tested our installation!

>>> from transformers import pipeline

# Allocate a pipeline for sentiment-analysis
>>> classifier = pipeline('sentiment-analysis')
>>> classifier('We are very happy to introduce pipeline to the transformers repository.')
[{'label': 'POSITIVE', 'score': 0.9996980428695679}]

You can also use this to classify multiple pieces of text at once:

>>> classifier(["We are very happy to introduce pipeline to the transformers repository.", "We hope you don't hate it."])
[{'label': 'POSITIVE', 'score': 0.9996980428695679}, {'label': 'NEGATIVE', 'score': 0.9996438026428223}]

You can even use your own model! I'll save that for potentially another post though, as it's a bit more involved and for sentiment analysis you can usually get away with using a pretrained model - but that's the exact kind of thing you can play around with here and see what works for you!

Sentiment analysis is super powerful, you can use it for all sorts of fun tasks like:

  • Tweet sentiment analysis - I did this to create a stock ticker years ago, my first real ML project!
  • Comment sentiment analysis - to help moderate comments on your blog or social media
  • Email sentiment analysis - to help you prioritize your inbox

Object Detection

While transformers is great with NLP, it can also do a lot of other things in the "Computer Vision" space as well! One of those things is object detection.

For a quick example:

>>> import requests
>>> from PIL import Image

# Download an image with cute cats
>>> url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/coco_sample.png"
>>> image_data = requests.get(url, stream=True).raw
>>> image = Image.open(image_data)

We start by using PIL to download an image of some cats.

For reference, this is just your standard internet cat photo:

Cats

Once downloaded, we can use transformers to detect the objects in the image:

>>> from transformers import pipeline

# Allocate a pipeline for object detection
>>> object_detector = pipeline('object-detection')
>>> object_detector(image)
[{'score': 0.9982201457023621,
  'label': 'remote',
  'box': {'xmin': 40, 'ymin': 70, 'xmax': 175, 'ymax': 117}},
 {'score': 0.9960021376609802,
  'label': 'remote',
  'box': {'xmin': 333, 'ymin': 72, 'xmax': 368, 'ymax': 187}},
 {'score': 0.9954745173454285,
  'label': 'couch',
  'box': {'xmin': 0, 'ymin': 1, 'xmax': 639, 'ymax': 473}},
 {'score': 0.9988006353378296,
  'label': 'cat',
  'box': {'xmin': 13, 'ymin': 52, 'xmax': 314, 'ymax': 470}},
 {'score': 0.9986783862113953,
  'label': 'cat',
  'box': {'xmin': 345, 'ymin': 23, 'xmax': 640, 'ymax': 368}}]

You can see it's able to detect the cats, the couch, and the remotes in the image! While this is a simple example, you can quickly see how powerful this can be!

Text Summarization

Maybe one of the most boring tasks available here admittedly however, in my opinion, one of the most actually useful!

Text summarization is a great way to quickly get the gist of a piece of text. This is great for things like: - News articles - Blog posts - Emails - Jira tickets - Documentation - So much more!

Quick example:

>>> from transformers import pipeline

>>> summarizer = pipeline(task="summarization")
>>> summarizer(
...    "In this work, we presented the Transformer, the first sequence transduction model based entirely on attention, replacing the recurrent layers most commonly used in encoder-decoder architectures with multi-headed self-attention. For translation tasks, the Transformer can be trained significantly faster than architectures based on recurrent or convolutional layers. On both WMT 2014 English-to-German and WMT 2014 English-to-French translation tasks, we achieve a new state of the art. In the former task our best model outperforms even all previously reported ensembles."
... )
[{'summary_text': ' The Transformer is the first sequence transduction model based entirely on attention . It replaces the recurrent layers most commonly used in encoder-decoder architectures with multi-headed self-attention . For translation tasks, the Transformer can be trained significantly faster than architectures based on recurrent or convolutional layers .'}]

Pretty cool right? You can see how this could be useful for a variety of tasks!

More Examples

I'm not gonna go through all of these, but again, if you'd like a more comprehensive list I'd highly recommend reading through the summary page.

Random Tips

AutoClasses

It's hard to know what model to use for what task. It's especially hard before you really jump into learning about models.

Luckily, Transformers has a feature called AutoClasses that can help you with that! In their words:

With so many different Transformer architectures, it can be challenging to create one for your checkpoint. As a part of πŸ€— Transformers core philosophy to make the library easy, simple and flexible to use, an AutoClass automatically infers and loads the correct architecture from a given checkpoint. The from_pretrained() method lets you quickly load a pretrained model for any architecture so you don’t have to devote time and resources to train a model from scratch. Producing this type of checkpoint-agnostic code means if your code works for one checkpoint, it will work with another checkpoint - as long as it was trained for a similar task - even if the architecture is different.

The TLDR is that you can use AutoClasses to automatically load the correct model for your task. This is great because it means you don't have to worry about which model to use, you can just use the AutoClass and it will figure it out for you!

For example, for vision tasks, an image processor processes the image into the correct input format.

from transformers import AutoImageProcessor
image_processor = AutoImageProcessor.from_pretrained("google/vit-base-patch16-224")

Read more here

Offline Mode

Run HuggingFace Transformers offline or behind a firewall by locally cached files. You can do this by setting the environment variable TRANSFORMERS_OFFLINE=1.

You can also run by doing somethng like this:

from transformers import T5Model
model = T5Model.from_pretrained("./path/to/local/directory", local_files_only=True)

Read more here

Conclusion

And that's about it! I hope this was a helpful introduction to Transformers and HuggingFace - it can be really overwhelming so I hope this gives you a better idea of at least where to look.

Machine Learning and Artificial Intelligence are really powerful tools that are getting more powerful by the moment. While these examples are simple, they are a great way to get started and see what's possible.

More importantly I think they highlight how accessible things are becoming. I remember when I first started with ML/AI years ago and it was a lot more involved, math focused and very expensive - emphasis on the last one.

Now, you can get started with just a few lines of code and even though running in production is still very involved and expensive, most pretrained models work great and regular computer hardware and GPUs have vastly improved which makes things significantly cheaper.

Thanks for reading! If you liked this or would be interested in more posts about HuggingFace or ML/AI please let me know!

Resources

Some resources I found helpful while writing this:


Comments

None available :/

Must be logged in to comment


Tags

HuggingFace

ML/AI

Python