Named Entity Recognition with Hugging Face (original) (raw)

Last Updated : 14 Apr, 2026

Named Entity Recognition (NER) is an NLP task that identifies and classifies entities in text, such as people, organizations and locations. It helps systems automatically understand important information within sentences, even when language and context vary.

For example, in the sentence: Barack Obama was born in Hawaii and served as President of the United States.

NER identifies:

Implementation of Named Entity Recognition

Run the following command in your command prompt to install required libraries

pip install transformers torch

1. Without Pipeline

Let's see the implementation of Named Entity Recognition using a BERT model without using the Hugging face transformer's pipeline API

Step 1: Import Required Classes

from transformers import BertTokenizer, BertForTokenClassification import torch

`

Step 2: Load Model and Tokenizer

model_name = "dslim/bert-base-NER"

tokenizer = BertTokenizer.from_pretrained(model_name) model = BertForTokenClassification.from_pretrained(model_name)

`

**Output:

hugging_face

Loading pretrained model

Step 3: Tokenize Input

This converts the sentence into tensors that the model can process, preparing it for entity prediction.

Python `

text = "Sundar Pichai is the CEO of Google and lives in California."

inputs = tokenizer( text, return_tensors="pt", truncation=True, padding=True )

`

Step 4: Run Model Inference

We use torch.no_grad() to disable gradient calculations because we are only performing inference, not training. This makes prediction faster and more memory efficient.

Python `

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

`

Step 5: Extract Predictions

logits = outputs.logits predictions = torch.argmax(logits, dim=2)

`

Step 6: Map Token IDs to Labels

tokens = tokenizer.convert_ids_to_tokens(inputs["input_ids"][0]) labels = [model.config.id2label[p.item()] for p in predictions[0]]

for token, label in zip(tokens, labels): print(token, "→", label)

`

**Output:

withoutpipeline

Output

2. With Pipeline

**Step 1: Import Required Libraries

Import the pipeline from Transformers, as it provides a high level interface that automatically manages tokenization, model loading, inference and output formatting in a single streamlined workflow.

Python `

from transformers import pipeline

`

**Step 2: Initialize NER Model

ner = pipeline( "ner", model="dslim/bert-base-NER", aggregation_strategy="simple"

)

`

**Output:

hugging_face_pretrained_model

Pretrained model

**Step 3: Run NER

The pipeline processes the sentence, identifies named entities and returns them with their categories like Person, Organization, Location, etc. The loop simply prints each detected entity along with its label. The output shows the entities detected by the NER model:

text = "GeeksforGeeks was founded in India and is widely used by programming students."

entities = ner(text)

for entity in entities: print(f"{entity['word']} → {entity['entity_group']}")

`

**Output:

Output_huggingface

Output

We can see our model is working fine.

You can download the full code from here