• Courses
  • Tutorials
  • Interview Prep
Switch to Dark Mode

How to Use Hugging Face Pretrained Model

Last Updated : 15 Apr, 2026

Hugging Face is an open source platform that provides tools, libraries and a large community for building and sharing AI models. It makes it easy to access pre trained models and use them in real world applications.

  • Provides ready-made models for NLP, computer vision and audio tasks, saving time on training from scratch.
  • Enables performing tasks like sentiment analysis, text classification, translation and Q&A directly.
  • Allows models to be accessed via the Transformers library, Hugging Face APIs or embedded in applications seamlessly.
  • Helps in quick experimentation, fine-tuning and deployment of AI solutions in real-world projects.

Getting Started

Pretrained models are trained on large datasets and can be applied to specific tasks without training from scratch. The Hugging Face Transformers library offers models like BERT, GPT and T5.

Step 1: Install Transformers Library

Install the Transformers library to access pretrained models from Hugging Face. It includes tools for loading models, tokenizers and running different machine learning tasks.

pip install transformers

Step 2: Load Pretrained Model and Tokenizer

Load a pretrained model and its tokenizer to perform tasks like text classification. The tokenizer converts text into a format the model understands, while the model processes it for predictions.

  • AutoModelForSequenceClassification loads a model designed for classification tasks like sentiment analysis
  • AutoTokenizer converts input text into tokens required by the model
from transformers import AutoModelForSequenceClassification, AutoTokenizer

model_name = "bert-base-uncased"
model = AutoModelForSequenceClassification.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)

Output:

Screenshot-2026-03-23-180834
Load Pretrained Model and Tokenizer

Step 3: Generate Predictions

The input text is processed using the tokenizer and passed to the model to get prediction results. The output is then used to determine the final predicted class.

  • tokenizer converts text into tensors and the model returns logits as output
  • torch.argmax() is used to select the class with the highest score
import torch
text = "Hugging Face makes NLP easier!"
inputs = tokenizer(text, return_tensors="pt")
with torch.no_grad():
    outputs = model(**inputs)

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

Output:

Predicted class: 1

Step 4: Sentiment Analysis using Pipeline

Sentiment analysis can be performed easily using the Hugging Face pipeline, which provides a simple way to use pretrained models for specific tasks without manual setup.

  • pipeline() simplifies the process by combining model loading and inference in one step
  • The sentiment-analysis pipeline returns labels like POSITIVE or NEGATIVE with confidence scores
  • Multiple texts can be passed at once for batch predictions
from transformers import pipeline

model_name = "distilbert-base-uncased-finetuned-sst-2-english"
classifier = pipeline("sentiment-analysis", model=model_name)
texts = [
    "I love making models!",
    "The weather today is terrible."
]

results = classifier(texts)

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

Output:

Screenshot-2026-03-23-181603
Output

As we can see our sentiment analysis model is working fine.

Suggested Quiz

10 Questions

What is the primary purpose of Hugging Face's Transformers library?

  • A

    To provide tools for training language models

  • B

    To offer pre-trained models for diverse NLP tasks

  • C

    To host and deploy machine learning applications

  • D

    To inspect and analyze neural network structures

Which of the following tasks can be performed using Hugging Face's pre-trained models?

  • A

    Text classification

  • B

    Named entity recognition (NER)

  • C

    Question answering

  • D

    All of the above

What is the first step in using a pre-trained model from Hugging Face?

  • A

    Fine-tune the model

  • B

    Install the transformers library

  • C

    Deploy the model to production

  • D

    Collect a large dataset

Which Python package is required to use Hugging Face's pre-trained models?

  • A

    torch

  • B

    tensorflow

  • C

    transformers

  • D

    huggingface_hub

How do you load a pre-trained BERT model for sequence classification using Hugging Face?

  • A

    AutoModelForSequenceClassification.from_pretrained('bert-base-uncased')

  • B

    BertModel.from_pretrained('bert-base-uncased')

  • C

    load_model('bert-base-uncased')

  • D

    transformers.load('bert-base-uncased')

What is the purpose of the tokenizer in Hugging Face's pipeline?

  • A

    To convert raw text into model-readable tokens

  • B

    To generate predictions from processed inputs

  • C

    To transform tokens into contextual embeddings

  • D

    To decode model outputs into readable text

Which of the following is a pre-trained model available in Hugging Face's Model Hub?

  • A

    GPT

  • B

    RoBERTa

  • C

    BERT

  • D

    All of the above

What does "fine-tuning" a pre-trained model involve?

  • A

    Visualizing the model's architecture

  • B

    Training the model from scratch

  • C

    Adjusting the model on a specific task with a smaller dataset

  • D

    Deploying the model to production

How can you perform inference using a pre-trained model in Hugging Face?

  • A

    By feeding input data into the model and obtaining predictions

  • B

    By evaluating the model's performance

  • C

    By training the model on new data

  • D

    By deploying the model to production

What is the advantage of using pre-trained models from Hugging Face?

  • A

    They require fewer resources during training

  • B

    They are designed only for classification tasks

  • C

    They remove the need for preparing input data

  • D

    They start with knowledge learned from large datasets

success
Quiz Completed Successfully

Your Score :0/10

Accuracy :0%

Login to View Explanation

1/10