How to Use Hugging Face Pretrained Model
Last Updated :
24 Sep, 2024
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!
Similar Reads
How to Use Hugging Face Model for Question Answering
In the rapidly advancing field of natural language processing (NLP), question answering systems are becoming increasingly sophisticated and accessible. Hugging Face, a leader in the AI community, provides an array of pre-trained models through its Transformers library, making it easier for developer
5 min read
How to use Hugging Face with LangChain ?
Hugging Face is an open-source platform that provides tools, datasets, and pre-trained models to build Generative AI applications. We can access a wide variety of open-source models using its API. With the Hugging Face API, we can build applications based on image-to-text, text generation, text-to-i
3 min read
How to Use Hugging Face API
Hugging Face is one of the best platforms for machine learning, and artificial intelligence (AI) models. Using the Hugging Face API, we can easily interact with various pre-trained models for tasks like text generation, translation, sentiment analysis, etc. In this article, we are going to discuss h
8 min read
How to upload and share model to huggingface?
Hugging Face has emerged as a leading platform for sharing and collaborating on machine learning models, particularly those related to natural language processing (NLP). With its user-friendly interface and robust ecosystem, it allows researchers and developers to easily upload, share, and deploy th
5 min read
How to Save a Model When Using MXNet in R
MXNet is a versatile and efficient deep learning framework that supports multiple programming languages, including R. When training machine learning or deep learning models, saving your model for later use is an essential step. This article will guide you through the process of saving a model in MXN
3 min read
How to Download a Model from Hugging Face
Hugging Face has emerged as a go-to platform for machine learning enthusiasts and professionals alike, especially in the field of Natural Language Processing (NLP). The platform offers an impressive repository of pre-trained models for various tasks, such as text generation, translation, question an
5 min read
Top 5 Use Cases for Hugging Face Models in 2024
In 2024, the landscape of natural language processing (NLP) is experiencing rapid advancements, with Hugging Face at the helm of these innovations. Hugging Faceâs models, including cutting-edge architectures like GPT-4, BERT, and T5, are driving significant changes across various industries. Top 5 U
5 min read
Top 10 Spaces on Hugging Face
The Hugging Face has emerged as a leading platform in the AI and machine learning community offering a rich repository of the models, datasets and tools. One of its standout features is the "Spaces" section where developers and researchers can share and deploy machine learning applications.Top 10 Sp
5 min read
How to Fine-Tune an LLM from Hugging Face
Large Language Models (LLMs) have transformed different tasks in natural language processing (NLP) such as translation, summarization, and text generation. Hugging Face's Transformers library offers a wide range of pre-trained models that can be customized for specific purposes through fine-tuning.
5 min read
How To Use Docker for Machine Learning?
Docker is a containerization platform that allows you to package your machine learning code and dependencies into an image that can be run on any machine. Docker separates your application from the underlying infrastructure. The image you create is portable, meaning it can run on any machine that ha
5 min read