Fill Mask

Learn to fill mask in the text using the Hugging Face Inference API.

We mask some words in the fill mask task and ask the transformer model to fill it by suggesting suitable fill-in words. It can find many uses in NLP tasks—for example, correcting the misprinted words in a book, guessing the lost words from ancient hieroglyphs or manuscripts, some word games, or just the language model's analysis itself.

Press + to interact

Fill mask using the API

The bert-base-uncased model is recommended for the fill mask task. However, there are many models available for this task, and some common models are below:

Models for Fill Mask

Model

Description

bert-base-uncased

Pretrained on the English dataset, which is uncased. Learns in a bidirectional fashion of the sentence, creating the mask's completion.

bert-base-multilingual-cased

Pretrained on 104 languages datasets, which are case sensitive. Learns in a bidirectional fashion of the sentence, creating the mask's completion.

microsoft/deberta-v3-base

Based on bert and roberta models, and deberta-v3 is an advanced version of deberta, which improves performance. Trained on the 160 gigabytes dataset.

jackaduma/SecBERT

Based on bert and trained on the cyber security dataset. Use this model for downstream tasks, like semantic search, classification, and question-answer.

flaubert/flaubert_base_cased

Pretrained on the French dataset, which is cased. Learns in a bidirectional fashion of the sentence, creating the mask's completion.

We can call the following endpoint via the POST request method for the fill mask task by replacing the path parameter {model} with any model mentioned above:

https://api-inference.huggingface.co/models/{model}

Request parameters

The request parameters for this API call are as follows:

Parameter

Type

Category

Description

inputs

String

Required

Specifies a string or string [ ] is to be filled with a text to complete

options.use_cache

Boolean

Optional

Hugging Face Inference API has a cache mechanism implemented to speed up the requests. Use it for the deterministic models. Default value is true.

options.wait_for_model

Boolean

Optional

Hugging Face Inference API models takes time to initiate and process the requests. If the value is true, it waits for the model to get ready instead of returning an error. Default value is false.

The widget below presents the code for the fill mask task.

Press + to interact
// Endpoint URL
const endpointUrl = "https://api-inference.huggingface.co/models/bert-base-uncased";
const headerParameters = {
"Authorization": "Bearer {{ACCESS_TOKEN}}"
};
// Input text to classify
const data = JSON.stringify({
inputs: "DNA is the [MASK] of life.",
options: {
wait_for_model: true
}
});
const options = {
method: "POST",
headers: headerParameters,
body: data
};
async function fillMask() {
try {
const response = await fetch(endpointUrl, options);
printResponse(response);
} catch (error) {
printError(error);
}
}
fillMask();

Let’s have a look at the highlighted lines shown in the code widget above:

  • Line 2: We specify the bert-base-uncased model for fill mask task.

  • Line 9: We specify the inputs, which include a sentence with the [MASK].

  • Line 21–28: We create a function, fillMask, to make the API call and handle the exceptions.

  • Line 30: We call the fillMask function to invoke the endpoint.

Response fields

The API call above returns a dictionary object or a list of dictionary objects, depending on the inputs. The response contains the following fields.

Parameter

Type

Description

sequence

String

Specifies the complete phrase after filling the mask

score

Float

The likelihood of the token in the sentence. A higher value means the token has a higher probability of occurring in the sentence, and vice versa.

token

Integer

Specifies the ID of the token, and is the model-generated ID

token_str

String

Specifies the token that is filled in the place of the mask

Examples

Try out the following examples, and choose the best model that can perform the following tasks. Change the inputs at line 9 in the code widget above with the examples provided below.

Fill the mask in the French language:

Press + to interact
inputs: "L’amour est comme un arbre, il pousse de lui-même, jette <special1> ses racines dans tout notre être"

Note: Please notice the use of the <special1> tag here for the mask. When we use the [MASK] tag, it leads to an error.

Fill the mask in the protein sequence:

Press + to interact
inputs: "D L I P T S S K L V V [MASK] D T S L Q"