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 can be installed
using Swift Package
Manager (SPM). The library
is compatible with Swift apps and does not require any additional
language-specific setup.
To add the MediaPipeTasksText library to your Xcode project:
- With your project open in Xcode, select File > Add Package Dependencies....
- In the search field in the upper right, enter the repository URL:
https://github.com/google-ai-edge/mediapipe - Select the
mediapipepackage and choose your dependency rule (for example, Up to Next Major Version or themasterbranch). - Click Add Package, then select
MediaPipeTasksTextand add it to your application target.
If you use a Package.swift file, add the MediaPipe package and
MediaPipeTasksText target dependency using the following code:
dependencies: [
.package(
url: "https://github.com/google-ai-edge/mediapipe",
branch: "master"
),
],
targets: [
.target(
name: "MyTextSummarizerApp",
dependencies: [
.product(name: "MediaPipeTasksText", package: "mediapipe"),
]
),
]
For more information on configuring dependencies, refer to the Setup guide for iOS.
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()