Inference Providers documentation

Token Classification

Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

Token Classification

Token classification is a task in which a label is assigned to some tokens in a text. Some popular token classification subtasks are Named Entity Recognition (NER) and Part-of-Speech (PoS) tagging.

For more details about the token-classification task, check out its dedicated page! You will find examples and related materials.

Recommended models

Explore all available models and find the one that suits you best here.

Using the API

import os
from huggingface_hub import InferenceClient

client = InferenceClient(
    provider="hf-inference",
    api_key=os.environ["HF_TOKEN"],
)

result = client.token_classification(
    "My name is Sarah Jessica Parker but you can call me Jessica",
    model="dslim/bert-base-NER",
)

API specification

Request

Headers
authorizationstringAuthentication header in the form 'Bearer: hf_****' when hf_**** is a personal user access token with “Inference Providers” permission. You can generate one from your settings page.
Payload
inputs*stringThe input text data
parametersobject
        ignore_labelsstring[]A list of labels to ignore
        strideintegerThe number of overlapping tokens between chunks when splitting the input text.
        aggregation_strategystringOne of the following:
                 (#1)’none’Do not aggregate tokens
                 (#2)’simple’Group consecutive tokens with the same label in a single entity.
                 (#3)’first’Similar to “simple”, also preserves word integrity (use the label predicted for the first token in a word).
                 (#4)’average’Similar to “simple”, also preserves word integrity (uses the label with the highest score, averaged across the word’s tokens).
                 (#5)’max’Similar to “simple”, also preserves word integrity (uses the label with the highest score across the word’s tokens).

Response

Body
(array)object[]Output is an array of objects.
        entity_groupstringThe predicted label for a group of one or more tokens
        entitystringThe predicted label for a single token
        scorenumberThe associated score / probability
        wordstringThe corresponding text
        startintegerThe character position in the input where this group begins.
        endintegerThe character position in the input where this group ends.
Update on GitHub