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 in an Android app.
For more information about the capabilities, models, and configuration options of this task, see the Overview.
Code example
The Text Summarizer Android example app demonstrates the API on a physical Android device or emulator.
You can use the app as a starting point for your own Android app, or refer to it when modifying an existing app. The Text Summarizer example code is hosted 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 Android.
Dependencies
Text Summarizer uses the com.google.mediapipe:tasks-text libraries. Add this
dependency to the build.gradle file of your Android app development project.
You can import the required dependencies with the following code:
dependencies {
implementation 'com.google.mediapipe:tasks-text:latest.release'
}
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 the model, and then store it within your project directory:
<dev-project-root>/src/main/assets
Specify the model within the options object using either a path or a file
descriptor. Use the setModelPath() function to specify the path used by the
model, or use the setModelAssetFileDescriptor() function to specify the file
descriptor of the model asset.
Create the task
You can use the createFromOptions() function to create the task. The
createFromOptions() function accepts configuration options to set the task
options. For more information on configuration options, see Configuration
options.
The following code demonstrates how to build and configure this task.
Model Path
// Configure the options using model path
TextSummarizerOptions options = TextSummarizerOptions.builder()
.setModelPath("path/to/your/model.litertlm")
.setMode(TextSummarizerOptions.Mode.TLDR)
.build();
File Descriptor
// Configure the options using file descriptor
// Note: 'parcelFileDescriptor' is the ParcelFileDescriptor of
// the model asset
TextSummarizerOptions options = TextSummarizerOptions.builder()
.setModelAssetFileDescriptor(parcelFileDescriptor)
.setMode(TextSummarizerOptions.Mode.TLDR)
.build();
Create the engine from options:
// Create the engine from options
// Note: 'context' is the Android Context
TextSummarizer summarizer = TextSummarizer.createFromOptions(context, options);
Configuration options
This task has the following configuration options for Android apps:
| Option Name | Description | Value Range | Default Value |
|---|---|---|---|
setMode |
The summarization mode of the text summarizer task. Can be a short summary paragraph or bulleted list of key points. |
TextSummarizerOptions.Mode.TLDR, TextSummarizerOptions.Mode.KEYPOINTS
|
TextSummarizerOptions.Mode.KEYPOINTS
|
setMaxNumTokens |
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 |
Model default (8k)
|
setModelPath |
The model path to a model asset file in the Android app assets folder. | File path as a string | Not set |
setModelAssetFileDescriptor |
The file descriptor integer of a model asset file. | Integer specifying the file descriptor | Not set |
Prepare data
Text Summarizer accepts text (String) data and doesn't require any preparations.
String inputText = "Some input text for the summarization task";
Run the task
To summarize text, you can use the synchronous approach which blocks the execution thread until the summary is returned, or the streaming approach which returns immediately and streams the result content through a callback interface.
Option A: Synchronous Summarization
Use the synchronous approach when you want to wait for the entire text summary to be generated and returned at once.
import android.util.Log;
import com.google.mediapipe.tasks.text.textsummarizer.TextSummarizerResult;
String textToSummarize = "Here is a long meeting transcript or block of text...";
// Run summarization (blocks current thread until done)
TextSummarizerResult result = summarizer.summarize(textToSummarize);
String summary = result.getSummary();
Log.i("TextSummarizer", "Summary: " + summary);
Option B: Streaming Summarization
The streaming approach is recommended for longer generation tasks or maintaining UI responsiveness. Here, a callback is provided to receive partial chunks of the summary text as they are computed.
import android.util.Log;
import com.google.mediapipe.tasks.text.textsummarizer.TextSummarizerStreamingResult;
String textToSummarize = "Here is a long meeting transcript or block of text...";
// Starts summarization and returns immediately. Iteratively invokes callback.
summarizer.summarizeStreaming(textToSummarize, new TextSummarizer.SummarizationResultCallback() {
@Override
public void onNext(TextSummarizerStreamingResult result) {
// Appends the next stream chunk
String chunk = result.getChunk();
// e.g., runOnUiThread(() -> resultTextView.append(chunk));
}
@Override
public void onError(Throwable throwable) {
// Handle summarization error
Log.e("TextSummarizer", "Summarization failed: " + throwable.getMessage());
}
@Override
public void onDone() {
// Fired when summarization completes
Log.i("TextSummarizer", "Summarization finished!");
}
});
Handle and display results
On task execution:
- For synchronous summarization, the task returns a
TextSummarizerResultcontaining the final summary text. - For streaming summarization, the task outputs
TextSummarizerStreamingResultchunks repeatedly to the callback interface.
Clean up
To free up memory and C++ resources, you must explicitly close the task when done using it.
@Override
protected void onDestroy() {
super.onDestroy();
if (summarizer != null) {
summarizer.close(); // Cleans up underlying handle
}
}