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 iOS apps.
For more information about the capabilities, models, and configuration options of this task, see the Overview.
Code example
The Text Summarizer iOS example app demonstrates the API on a physical iOS device or simulator.
You can use the app as a starting point for your own iOS app, or refer to it when modifying an existing app. You can refer to the Text Summarizer example code on GitHub.
Setup
This section describes key steps for setting up your development environment and code projects to use Text Summarizer on iOS. For general information on setting up your development environment for using MediaPipe tasks, including platform version requirements, see the Setup guide for iOS.
Dependencies
Text Summarizer uses the MediaPipeTasksText library, which must be installed
using CocoaPods. The library is compatible with Swift apps and does not require
any additional language-specific setup.
For instructions to install CocoaPods on macOS, refer to the CocoaPods
installation guide.
For instructions on how to create a Podfile with the necessary pods for your
app, refer to Using CocoaPods.
Add the MediaPipeTasksText pod in the Podfile using the following code:
target 'MyTextSummarizerApp' do
use_frameworks!
pod 'MediaPipeTasksText'
end
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 LiteRT model, and add it to your project directory using Xcode. For instructions on how to add files to your Xcode project, refer to Managing files and folders in your Xcode project.
Use the BaseOptions.modelAssetPath property to specify the path to the model
in your app bundle.
Create the task
You can create the Text Summarizer task by calling one of its initializers. The
TextSummarizer(options:) initializer accepts values for the configuration
options.
The following code demonstrates how to build and configure this task.
import MediaPipeTasksText
guard let modelPath = Bundle.main.path(forResource: "summarizer",
ofType: "litertlm") else { return }
let options = TextSummarizerOptions()
options.baseOptions.modelAssetPath = modelPath
options.mode = .tldr
let textSummarizer = try TextSummarizer(options: options)
Configuration options
This task has the following configuration options for iOS apps:
| 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. |
.tldr, .keyPoints
|
.keyPoints
|
maxTokens |
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 |
0 (model capacity 8k)
|
Run the task
To run the summarization inference on the input text, you can use the
summarize(text:) method of TextSummarizer for blocking inference, or the
summarizeStreaming(text:completion:) method for asynchronous callbacks.
Synchronous
let result = try textSummarizer.summarize(text: text)
Streaming (Asynchronous callbacks)
try textSummarizer.summarizeStreaming(text: text) { streamResult, error in
if let error = error {
print("Error: \(error)")
return
}
guard let streamResult = streamResult else { return }
print(streamResult.chunk)
if streamResult.done {
print("Completed summarizing!")
}
}
Handle and display results
Upon running inference synchronously, the Text Summarizer returns an instance
containing the complete string using the summary property.
let finalOutput = result.summary
When running in stream mode, the callback continually outputs a
TextSummarizerStreamResult, containing .chunk (the next text partial), and a
.done boolean determining stream completion status.
Clean Up
Don't forget to explicitly close the summarizer engine instances when finished.
try textSummarizer.close()