A Love Letter to HuggingFace

 

Sentiment Analysis

If I told you that I'd written a program that can take a sentence and detect whether the sentiment expressed was positive or negative, that would be pretty impressive, right?

Being able to understand the nuances of human language, to sense the underlying intention and emotion... It feels almost the realm of science fiction. That just isn't what computers are good at. Such a system, if it existed, would have valuable real world applications - for example, being able to automatically categorise product reviews, distinguishing between "This product is amazing!" and "Stupid thing doesn't even work" at scale. Or analysing trends with social media comments.

Here, I'll write that program for you now:

 

from transformers import pipeline

ai = pipeline("sentiment-analysis")
result = ai("I love you, HuggingFace")

 

Three lines of code! Such is the power of the HuggingFace transformers library. The output, by the way, determines that there is a 99.9% chance that "I love you, HuggingFace" is a positive sentiment.


Image Description

What about an advanced multimodal vision-language task? Looking at an image and describing what's in it in natural language feels like a very human task. Computers work with numbers. How on earth can it look at an image of a kitten surfing and figure out what's going on in the image at a high, semantic level?

 

from transformers import pipeline
from
PIL import Image

image =  Image.open("my_image.jpg")

ai = pipeline("visual-question-answering")
question = "What animal is this?"

result = ai (image, question, top_k=1)


There you go. When asked "What animal is this?", the AI thinks there's a 91.1% chance that the image above is of a cat. Let's ask a couple more questions.


question = "Is it a kitten or a fully grown cat?"

 

Answer: 97.9% chance it's a kitten.


question = "What is the kitten doing?"

 

Answer: 77.9% chance it is surfing.

So even though it can't have been trained on many images of kittens surfing, it could figure out that both those things were in the image. Amazing. Ten years ago that wasn't possible, yet alone something you could do in just a handful of lines of code.


What is HuggingFace?

HuggingFace is a platform for machine learning, offering a wealth of open-source models (i.e. pre-trained neural networks that are freely available) ready for a variety of tasks, as well as datasets for training your own, plus documentation, tutorials and 'spaces' where people can show off their latest creations and others can try them out. Whenever a company releases an open-source model, you'll usually find it on HuggingFace and can use it right away.


Explanation

The code above is so simple it barely needs an explanation. It's written in Python, which is largely popular because it makes it easy to use libraries of code written by other people. Machine learning uses a lot of clever maths and data structures and graphs, and rather than writing all that from scratch, it's much easier to just import libraries that do all the heavy lifting for you.

HuggingFace's pipeline class adds an extra level of abstraction on top of that, and simplifies the task of using a pre-trained model down to the absolute minimum: To perform a task, you simply tell it what type of task you want to perform, and then give it the input you want it to perform the task on. The rest is taken care of for you.

What's happening behind the scenes is that it picks the most popular model in the task category and uses that as the default. You can also specify a specific model instead, and take control of any stage in the process you want.

It also makes fine-tuning a model for a specific use case much easier than it would otherwise be. Fine-tuning involves freezing the weights in some layers of the pre-trained model to retain the features it has learned, while training later layers on a specific new dataset to customise it further. On different types of kittens, perhaps, or different categories of surfing positions, to further hone its intuitions when analysing surfing kittens.

Fine-tuning is what I'm going to be studying next, so will have more to say about that soon.

Thanks, HuggingFace!



Comments