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
- dslim/bert-base-NER: A robust performance model to identify people, locations, organizations and names of miscellaneous entities.
- FacebookAI/xlm-roberta-large-finetuned-conll03-english: A strong model to identify people, locations, organizations and names in multiple languages.
- blaze999/Medical-NER: A token classification model specialized on medical entity recognition.
- flair/ner-english: Flair models are typically the state of the art in named entity recognition tasks.
Explore all available models and find the one that suits you best here.
Using the API
import requests
API_URL = "https://api-inference.huggingface.co/models/dslim/bert-base-NER"
headers = {"Authorization": "Bearer hf_***"}
def query(payload):
response = requests.post(API_URL, headers=headers, json=payload)
return response.json()
output = query({
"inputs": "My name is Sarah Jessica Parker but you can call me Jessica",
})
To use the Python client, see huggingface_hub
’s package reference.
API specification
Request
Payload | ||
---|---|---|
inputs* | string | The input text data |
parameters | object | Additional inference parameters for Token Classification |
ignore_labels | string[] | A list of labels to ignore |
stride | integer | The number of overlapping tokens between chunks when splitting the input text. |
aggregation_strategy | string | One 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). |
Some options can be configured by passing headers to the Inference API. Here are the available headers:
Headers | ||
---|---|---|
authorization | string | Authentication header in the form 'Bearer: hf_****' when hf_**** is a personal user access token with Inference API permission. You can generate one from your settings page. |
x-use-cache | boolean, default to true | There is a cache layer on the inference API to speed up requests we have already seen. Most models can use those results as they are deterministic (meaning the outputs will be the same anyway). However, if you use a nondeterministic model, you can set this parameter to prevent the caching mechanism from being used, resulting in a real new query. Read more about caching here. |
x-wait-for-model | boolean, default to false | If the model is not ready, wait for it instead of receiving 503. It limits the number of requests required to get your inference done. It is advised to only set this flag to true after receiving a 503 error, as it will limit hanging in your application to known places. Read more about model availability here. |
For more information about Inference API headers, check out the parameters guide.
Response
Output type depends on the stream
input parameter.
If stream
is false
(default), the response will be a JSON object with the following fields:
Body | ||
---|---|---|
(array) | object[] | Output is an array of objects. |
entity_group | string | The predicted label for a group of one or more tokens |
entity | string | The predicted label for a single token |
score | number | The associated score / probability |
word | string | The corresponding text |
start | integer | The character position in the input where this group begins. |
end | integer | The character position in the input where this group ends. |
If stream
is true
, generated tokens are returned as a stream, using Server-Sent Events (SSE).
For more information about streaming, check out this guide.