Text Summarization
Learn to summarize text using the Hugging Face Inference API.
We'll cover the following
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.
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 |
| Based on |
| Based on |
| Based on |
| Based on |
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 |
| String | Required | Specifies a string or string [ ] to be summarized |
| Integer | Optional | Specifies the minimum length of the output summary tokens |
| Integer | Optional | Specifies the maximum length of the output summary tokens |
| Integer | Optional | Specifies how many tokens will be considered during the summary creation from the input text |
| 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 |
| Float | Optional | Specifies a sampling technique, and the value ranges from 1.0 to 100.0. Setting |
| 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 |
| Boolean | Optional | Hugging Face Inference API models takes time to initiate and process the requests. If the value is |
The following code summarizes the provided text.
// Endpoint URLconst endpointUrl = "https://api-inference.huggingface.co/models/facebook/bart-large-cnn";const headerParameters = {"Authorization": "Bearer {{ACCESS_TOKEN}}"};// Input text to classifyconst 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 |
| String | Specifies summarized text of the |
Examples
Let's run the same example by providing the some optional parameters
, and observe the effect of these parameters on the output summary.
// Endpoint URLconst endpointUrl = "https://api-inference.huggingface.co/models/facebook/bart-large-cnn";const headerParameters = {"Authorization": "Bearer {{ACCESS_TOKEN}}"};// Input text to classifyconst 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.