Open In App

How to Use Hugging Face Pretrained Model

Last Updated : 24 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Hugging Face has become a prominent player in the field of Natural Language Processing (NLP), providing a wide range of pretrained models that can be seamlessly incorporated into different applications. If you need to do tasks like text classification, sentiment analysis, machine translation, or any other NLP task, Hugging Face's pretrained models make it easier for you. This way, you can concentrate on your project's objectives instead of having to create models from the beginning.

This article will discuss the fundamentals of utilizing pretrained models from Hugging Face, including loading, performing inference, and a hands-on example with code.

What is Hugging Face?

Hugging Face is a company and an open-source community that has revolutionized NLP. It provides tools to download and use pretrained models like GPT, BERT, RoBERTa, and more, making it easier for developers to work with advanced models without starting from scratch.

The Hugging Face library includes models for:

  • Text classification
  • Named entity recognition (NER)
  • Question answering
  • Text generation
  • Machine translation

The real power of Hugging Face lies in its Transformers library, which provides seamless integration with pretrained models.

Installing Hugging Face Transformers

Before using Hugging Face models, ensure you have the transformers library installed. Run the following command to install it:

pip install transformers

You might also want to install PyTorch or TensorFlow, depending on the backend you wish to use.

# For PyTorch
pip install torch

# For TensorFlow
pip install tensorflow

Getting started with Hugging Face's Pretrained Models

Pretrained models are models that have undergone training on large datasets and can be adjusted for specific purposes or applied for inference without further training. The library transformers by Hugging Face contains many models in different categories like text classification, token classification, translation, summarization, and others.

Utilizing pre-trained models such as BERT, GPT, and T5 enables the execution of intricate tasks with minimal configuration by tapping into the knowledge stored within these models. Hugging Face offers models trained in various languages and for different tasks, making it a popular choice among NLP practitioners.

Loading Pretrained Models

After being set up, a pretrained model can be easily loaded with just a few lines of code. Let's consider loading the well-known BERT model for a task involving classifying sequences.

Python
from transformers import AutoModelForSequenceClassification, AutoTokenizer

# Load pretrained model and tokenizer
model_name = "bert-base-uncased"
model = AutoModelForSequenceClassification.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)

In the code above, we utilize the AutoModelForSequenceClassification to load BERT, a model specifically created for tasks involving sequence classification such as sentiment analysis. The AutoTokenizer is included as well, necessary for preprocessing the input text so the model can interpret it correctly.

Performing Inference

Once the model and tokenizer have been loaded, the subsequent action involves conducting inference by feeding input into the model to generate predictions. This includes breaking down the input text into tokens and utilizing the model to get the output.

Here is a demonstration of executing inference using the BERT model that has been loaded:

Python
from transformers import AutoModelForSequenceClassification, AutoTokenizer
import torch

# Load pretrained model and tokenizer
model_name = "bert-base-uncased"
model = AutoModelForSequenceClassification.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)

# Sample text for classification
text = "Hugging Face makes NLP easier!"

# Tokenize the input text
inputs = tokenizer(text, return_tensors="pt")

# Perform inference
with torch.no_grad():
    outputs = model(**inputs)

# Get the predicted class
predictions = torch.argmax(outputs.logits, dim=-1)
print(f"Predicted class: {predictions.item()}")

Output:

Predicted class: 1

In this instance, the tokenizer is used to tokenize the input text, and the model then processes the tokenized input to generate logits, which indicate the raw predictions. The torch.argmax() function is utilized to obtain the class with the highest probability from the logits.

Sentiment Analysis with HuggingFace

Let's illustrate with an example using the pretrained distilbert-base-uncased-finetuned-sst-2-english model from Hugging Face, specifically designed for sentiment analysis.

Python
from transformers import pipeline

model_name = "distilbert-base-uncased-finetuned-sst-2-english"
classifier = pipeline("sentiment-analysis", model=model_name)

# Perform sentiment analysis on sample texts
texts = [
    "I love using Hugging Face models!",
    "The weather today is terrible."
]

# Get predictions
results = classifier(texts)

# Output the results
for text, result in zip(texts, results):
    print(f"Text: {text}")
    print(f"Label: {result['label']}, Score: {result['score']:.4f}\n")

Output:

Text: I love using Hugging Face models!
Label: POSITIVE, Score: 0.9993

Text: The weather today is terrible.
Label: NEGATIVE, Score: 0.9991

This instance utilizes Hugging Face's pipeline() function, which simplifies the model loading and inference process, making it easier to use. In this study, we conduct sentiment analysis on two example texts, with the pipeline giving us the anticipated sentiment label and level of confidence.

Conclusion

Hugging Face offers a valuable tool for utilizing cutting-edge NLP models with its extensive library of pre-trained models. By quickly loading models, running inference, and writing straightforward code, you can easily incorporate advanced NLP features into your applications in no time. Whether you are tackling a basic task or developing a intricate program, Hugging Face's transformers library can expedite your progress.

By adhering to this manual, you now possess a firm grasp on how to utilize Hugging Face pre-trained models in your projects. Check out the Hugging Face Model Hub for additional pretrained models, and begin creating now!


Next Article

Similar Reads