From the course: AI Text Summarization with Hugging Face

Generating summaries with Hugging Face Transformers - Hugging Face Tutorial

From the course: AI Text Summarization with Hugging Face

Generating summaries with Hugging Face Transformers

In this movie, we'll see how we can use the T5 transformer model from the Hugging Face library for text summarization using Zero-Shot Learning. Zero-shot learning allows us to leverage state-of-the-art NLP models with absolutely no training. These are pre-trained models that we can use right out of the box. With zero-shot learning, a model can learn to recognize things that it hasn't explicitly seen before in training. Now, usually zero-shot learning applies to problems such as classification, but here with summarization, I use it in the context that will ask the model to summarize text of a kind that the model has never seen before. Now the article that we'll ask the model to summarize is the article at this example text index, Index 33 from the training data, and it's an article about a cloned Labrador retriever. If you directly use a pre-trained model with zero-shot learning, really with Hugging Face, it's just 2 or 3 lines of code to actually use that model. Now the model that we've chosen is the T5 Small model. The T5 model is a text-to-text model that works on a range of NLP tasks, including summarization. In order to work with this T5 Small pre-trained model, we'll be using the pipeline object from the Hugging Face library. A Hugging Face pipeline abstracts away all of the complex code associated with NLP models. It offers a simple API dedicated to several NLP tasks, including summarization. You can see the input for the pipeline object on line 1, and on line 3 I've specified the prefix to pass to our model while performing summarization. The prefix is summarize followed by a colon. The reason this prefix is needed is because the T5 model can perform several NLP tasks, including summarization. By parsing in summarizers as prefix, you're indicating to the T5 model that the input text should be summarized and don't perform any other kind of NLP tasks such as translation on it. Instantiating a pipeline is straightforward. Simply invoke the pipeline method, specify the kind of task that you want to perform as the first input argument, and then specify the model that you want to use. In fact, the model input argument is optional. If you don't specify a model name, it will pick a default model to perform summarization. And that's it. You have a summarizer object, which is essentially a reference to your pipeline and you can generate a summary by invoking the summarizer object on your example text. Make sure you pass in the prefix so that the model performs the right task. When you invoke the pipeline on your input text that's on line 7 when I invoked summarizer, there are three different things that happen behind the scenes. Your input text is preprocessed into a format the model can understand, the preprocessed inputs are passed to the model, the predictions of the model are obtained and they are post-processed so that they come back to you in a format that you can understand. Now with this out of the way, let's take a look at the summary text produced by the T5 model. You can see from the output here that the model is downloaded onto the device that Colab is running on and then the summarization is performed. But even though a summary is generated, there's a little warning: Token indices sequence length is longer than the specified maximum length for this model (887 > 512). The warning is concerning, but the summary text gets right to the point. You can see that the main point of the article about a lab being cloned is captured in the summary text. Now, the reason you see this warning is because every model in the Hugging Face Transformers library has a maximum length for the sequences that it can handle. If you see this warning, it means that we are trying to encode a sequence longer than 512, which is the maximum for the T5 Small model. Now it's possible that the model will not work correctly if you have it handle sequences longer than what it was designed for, which is why you need to tweak how you actually instantiate the model pipeline. Now there are several techniques you can use to tweak the input sentences so that they fit the maximum sequence length supported by the model. Here I've specified truncation = True when I instantiate the pipeline. This will ensure that the pipeline will truncate the input sequences that it feeds into the model so that it fits the maximum length that the model can handle. Now, after instantiating the pipeline with this new truncation set to True, I invoke the summarizer on the input text, and now let's take a look at the summary text. It's quite gratifying to see that the warning has disappeared. We are using the pipeline in the right way and the summary is also quite good. Now there were some extra information in the previous summary that's not present here in the output, but the summary still captures the essence of the original article. Let's compare this summary to the actual highlights that were present as a part of our dataset and see whether the summary is better. The actual summary has a lot more information than the generated summary, but still I feel the generated summary was quite good.

Contents