How to Use Hugging Face Pretrained Model (original) (raw)

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.

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.

Python `

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.

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.

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.

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.