The MediaPipe Text Summarizer task lets you identify the most important information in a text and generate a shorter version while maintaining the original context meaning. These instructions show you how to use the Text Summarizer with Python.
For more information about the capabilities, models, and configuration options of this task, see the Overview.
Code example
The example code for Text Summarizer provides a complete implementation of this task in Python for your reference. This code helps you test this task and get started on building your own text summarizer. You can view the source code for this example on GitHub.
Setup
This section describes key steps for setting up your development environment and code projects specifically to use Text Summarizer. For general information on setting up your development environment for using MediaPipe tasks, including platform version requirements, see the Setup guide for Python.
Packages
Text Summarizer uses the mediapipe pip package. You can install the dependency
with the following command:
$ python -m pip install mediapipe
Imports
Import the following classes to access the Text Summarizer task functions:
import mediapipe as mp
from mediapipe.tasks import python
from mediapipe.tasks.python import text
Model
The MediaPipe Text Summarizer task requires a trained model that is compatible with this task. For more information on available trained models for Text Summarizer, see the task overview Models section.
Select and download a model, and then store it in a local directory.
model_path = '/absolute/path/to/summarizer.litertlm'
Specify the path of the model using the model_asset_path parameter, as
follows:
base_options = python.BaseOptions(model_asset_path=model_path)
Create the task
The MediaPipe Text Summarizer task uses the create_from_options function to set up the
task. The create_from_options function accepts values for configuration
options to set the summarizer options. You can also initialize the task using the
create_from_model_path factory function. The create_from_model_path function
accepts a relative or absolute path to the trained model file.
For more information on configuration options, see Configuration options.
The following code demonstrates how to build and configure this task.
import mediapipe as mp
BaseOptions = mp.tasks.BaseOptions
TextSummarizer = mp.tasks.text.TextSummarizer
TextSummarizerOptions = mp.tasks.text.TextSummarizerOptions
TextSummarizerMode = mp.tasks.text.TextSummarizerMode
# Build the configuration options
options = TextSummarizerOptions(
base_options=BaseOptions(model_asset_path=model_path),
mode=TextSummarizerMode.TLDR
)
text_summarizer = TextSummarizer.create_from_options(options)
Configuration options
This task has the following configuration options for Python applications:
| Option Name | Description | Value Range | Default Value |
|---|---|---|---|
mode |
The summarization mode of the text summarizer task. Can be a short summary paragraph or bulleted list of key points. |
TextSummarizerMode.TLDR, TextSummarizerMode.KEYPOINTS
|
TextSummarizerMode.KEYPOINTS
|
max_num_tokens |
The maximum number of tokens for summarization tasks. If set, the summarization will be truncated if the input and output exceed this value. If not set, then the default limit is decided by the model capacity. | Integer |
None (model capacity 8k)
|
Prepare data
Text Summarizer accepts text (str) data and doesn't require any preparations.
input_text = "The extremely long input text to be summarized goes here..."
Run the task
The Text Summarizer uses the summarize function for synchronous execution,
and summarize_async for asynchronous streaming execution to generate summaries
token by token.
Synchronous Execution
The following code demonstrates how to execute the processing synchronously.
# Perform text summarization on the provided input text.
summarization_result = text_summarizer.summarize(input_text)
Asynchronous Streaming Execution
To execute asynchronous streaming where parts of the text are generated chunk
by chunk, use summarize_async. The streaming engine leverages the callback to
relay incremental updates, mapping the stream .summary and .done fields
into the output result.
You must provide a callback function to handle these incoming results.
from typing import Optional
def result_callback(result: Optional[mp.tasks.text.TextSummarizerResult], error: Optional[str]):
if error:
print(f"Error: {error}")
return
if result and result.summary:
# Result contains the partial chunk string
print(result.summary, end="")
if result and result.done:
print("\nFinished stream!")
# Perform streaming text summarization on the provided input text.
text_summarizer.summarize_async(input_text, result_callback)
Handle and display results
The Text Summarizer outputs a TextSummarizerResult that contains the completed
or partial summary and a boolean flag indicating completion status.
For streaming tasks, the Python TextSummarizerResult maps the package updates
with the newly appended token chunk populated into the summary property, and
the done property indicating completion.
The following shows an example of the output data from this task:
# TextSummarizerResult:
# summary: "This is a short summary of the text."
# done: True
# Print the output summary text
print("Summary:", summarization_result.summary)