Text Summarization

Learn to summarize text using the Hugging Face Inference API.

Imagine being able to summarize a book to get to the crux of it or being able to skim through one-pager summaries of patents or legal contracts. Similarly, it would be useful to be able to pick out the useful information from the heaps of newsletters we’ve subscribed to.

Text summarization is another excellent technique by NLP. It enables us to perform these complex tasks without needing any human experts. While the need for summarization was always there, it wasn’t easy to perform with statistical NLP. Therefore, it had limited accuracy and usage.

Press + to interact

Summarization

The facebook/bart-large-cnn model is recommended for the summarization task. This model is fine-tuned to the CNN Daily Mail dataset. However, there are many models available for this task, and some common models are below:

Models for Summarization

Model

Description

facebook/bart-large-cnn

Based on bart and fine-tuned on the CNN Daily Mail dataset for the English language. Has the ability to summarize, translate, classify, and answer questions.

sshleifer/distilbart-xsum-12-3

Based on ditilbart and fine-tuned on the Extreme Summarization dataset. Built explicitly for text summarization tasks.

pszemraj/long-t5-tglobal-base-16384-book-summary

Based on google/long-t5-tglobal-base and fine-tuned on the kmfoda/booksum dataset. Used for text summarization.

google/pegasus-xsum

Based on pegasus and is fine-tuned on the C4 and Huge News datasets. Built explicitly for text summarization tasks.

We can call the following endpoint via the POST request method for the summarization 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 [ ] to be summarized

parameters.min_length

Integer

Optional

Specifies the minimum length of the output summary tokens

parameters.max_length

Integer

Optional

Specifies the maximum length of the output summary tokens

parameters.top_k

Integer

Optional

Specifies how many tokens will be considered during the summary creation from the input text

parameters.top_p

Float

Optional

Specifies the probability of the token to be added in summary from the most probable to less probable until the sum of the probabilities is higher than top_p

parameters.temperature

Float

Optional

Specifies a sampling technique, and the value ranges from 1.0 to 100.0. Setting temperature to 0 takes tokens with the highest probability. If we set temperature to 1, it does regular sampling. At 100.0 temperature, it selects the tokens with uniform probability.

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 following code summarizes the provided text.

Press + to interact
// Endpoint URL
const endpointUrl = "https://api-inference.huggingface.co/models/facebook/bart-large-cnn";
const headerParameters = {
"Authorization": "Bearer {{ACCESS_TOKEN}}"
};
// Input text to classify
const data = JSON.stringify({
inputs: "Filby became pensive. “Clearly,” the Time Traveller proceeded, “any \
real body must have extension in four directions: it must have Length, \
Breadth, Thickness, and—Duration. But through a natural infirmity of the \
flesh, which I'll explain to you in a moment, we incline to overlook this \
fact. There are really four dimensions, three which we call the three \
planes of Space, and a fourth, Time. There is, however, a tendency to \
draw an unreal distinction between the former three dimensions and the \
latter, because it happens that our consciousness moves intermittently in \
one direction along the latter from the beginning to the end of our lives.”",
options: {
wait_for_model: true
}
});
const options = {
method: "POST",
headers: headerParameters,
body: data
};
async function summarizeText() {
try {
const response = await fetch(endpointUrl, options);
printResponse(response);
} catch (error) {
printError(error);
}
}
summarizeText();

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

  • Line 2: We specify the endpoint URL with the facebook/bart-large-cnn model for the text summarization.

  • Lines 9–17: We provide input text for the text summarization.

  • Lines 29–36: We create a function, summarizeText, to call the API and handle exceptions.

  • Line 38: We call the summarizeText 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 field.

Parameter

Type

Description

summarization_text

String

Specifies summarized text of the inputs

Examples

Let's run the same example by providing the some optional parameters, and observe the effect of these parameters on the output summary.

Press + to interact
// Endpoint URL
const endpointUrl = "https://api-inference.huggingface.co/models/facebook/bart-large-cnn";
const headerParameters = {
"Authorization": "Bearer {{ACCESS_TOKEN}}"
};
// Input text to classify
const data = JSON.stringify({
inputs: "Filby became pensive. “Clearly,” the Time Traveller proceeded, “any \
real body must have extension in four directions: it must have Length, \
Breadth, Thickness, and—Duration. But through a natural infirmity of the \
flesh, which I'll explain to you in a moment, we incline to overlook this \
fact. There are really four dimensions, three which we call the three \
planes of Space, and a fourth, Time. There is, however, a tendency to \
draw an unreal distinction between the former three dimensions and the \
latter, because it happens that our consciousness moves intermittently in \
one direction along the latter from the beginning to the end of our lives.”",
parameters: {
min_length: 20,
max_length: 30,
temperature: 0
},
options: {
wait_for_model: true
}
});
const options = {
method: "POST",
headers: headerParameters,
body: data
};
async function summarizeText() {
try {
const response = await fetch(endpointUrl, options);
printResponse(response);
} catch (error) {
printError(error);
}
}
summarizeText();

We have set min_length to 20, max_length to 30, and temperature to 0. Now, try the code above by adding new optional parameters and setting different values.