How To Load A Pre-trained Model From Hugging Face?

Introduction

Today, I'll be discussing an overview of hugging face models, including the steps required to load a pre-trained model from hugging face.

What is Hugging Face?

Hugging Face is an open-source platform provider that develops tools for building applications using Machine Learning. This hugging face enables users to build, run and deploy their Machine Learning models. It has a library named Transformers that contains a wide range of pre-trained models on NLP tasks. You can choose any model based on your requirements. For more information, read the article- Getting Started With Hugging Face

Hugging Face Models

Tasks: Categorizing the Models into different categories. Like Multimodal, Computer Vision, Natural Language Processing, Audio, Tabular, and Reinforcement Learning. They all are further divided into subcategories.

Hugging_Face_Models

Models: You can load these pre-built models from Hugging Face for fine-tuning or for any other use by following the steps described below.

hugging_face_models

Loading a Model from Hugging Face

To load a Pre-trained model from Hugging Face, you'll need to follow these steps.

Step 1. Install Libraries and Classes

If you have 't installed the library Transformer, you can do this by using the below code. For loading any model from Hugging Face, you need to install the library as this library provides a wide range of pre-trained models.

# Install library
!pip install transformers
# Import Library and classes
from transformers import AutoTokenizer, AutoModelForSequenceClassification

Use of ! (Exclamation Mark)- If you want to install a Python package on your computer, you usually do it by opening the command prompt and typing a command like

pip install package_name

But in Google Colab, you can do the same thing by using the exclamation mark (!); here, you're telling Colab to pass that command to the underlying system (just like you would in the command prompt or terminal).

Step 2. Choose the Model

Choosing the model totally depends on the task you are working on, as Hugging Face's Transformers library offers a number of pre-trained models, and each model is designed for a specific task.

Step 3. Load the Model

After selecting the model, you need to load the model with all its necessary files. Let's understand this better by taking an example of the Natural Language Processing category model for Text Classification. In the below image, the name of the model is enclosed inside the red rectangle box, and you can also use the model by giving the desired input and clicking the compute button; the text will be classified. For more detail about the model.

hugging_face_text_classification

Then, click on the Files and versions, and all the files will be displayed. Then, download all the files except .gitattributes and README.md, as shown in the below image. After downloading all the files, save these files in the same folder.

Hugging_face_download_files

In the code editor (google Colab), you need to write the following code for loading the model.

#path where all the files are saved in a folder
model_name = "nlptown/bert-base-multilingual-uncased-sentiment"
model = AutoModelForSequenceClassification.from_pretrained(model_name)

Step 4. Load the Tokenizer

Similarly, Tokenization will also be loaded in the same manner, but you don't need to download all the files again for tokenization as all the files were already downloaded. So you write the coding part only.

tokenizer = AutoTokenizer.from_pretrained(model_name)

Step 5. Use the Model

In the below code, you'll see the proper way to load a model and use it.

from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
sentiment_model = "D:\PYTHON\TOXIC_Words\sentiment_model" #here specify your path
model_1 = AutoModelForSequenceClassification.from_pretrained(sentiment_model)
tokenizer_1 = AutoTokenizer.from_pretrained(sentiment_model)
def sentiment_text(input_text):
    inputs = tokenizer_1(
        input_text,
        truncation=True,
        padding=True,
        return_tensors="pt"
    )
    model_1.eval()
    with torch.no_grad():
        outputs = model_1(**inputs)
    logits = outputs.logits
    predicted_class_sentiment = torch.argmax(logits, dim=1).item()
    # Map the sentiment label to a rating
    sentiment_mapping = {
            0: "very negative ",
            1: "negative",
            2: "neutral",
            3: "positive",
            4: "very positive"
        }
    predicted_label_sentiment = sentiment_mapping[predicted_class_sentiment]
    return predicted_label_sentiment
# Call the function with your input text
input_text = "you are very bad."
predicted_sentiment = sentiment_text(input_text)
print("Predicted Sentiment:", predicted_sentiment)

output_for_load_model

Conclusion

This article provides you with a step-by-step process for loading a model from a Hugging Face. Thanks for reading.

FAQ's

Q. How do I choose the right pre-trained model for my NLP task?

A. Hugging Face offers a wide selection of pre-trained models, each designed for different NLP tasks. Choose a model that aligns with your specific use case. For instance, BERT is suitable for a variety of tasks, while models like GPT-2 are ideal for language generation.

Q. Can I fine-tune a pre-trained model on my specific dataset?

A. Yes, you can fine-tune pre-trained models using transfer learning to adapt them to your specific task and dataset. Hugging Face's library also offers pre-built scripts to streamline the fine-tuning process.

Q. Can I use a pre-trained model for languages other than English?

A. Yes, Hugging Face provides many multilingual models that support various languages. For example, "bert-base-multilingual-uncased" is capable of handling text in multiple languages as shown in the above code example


Similar Articles