Provides an interface for performing text summarization.
It supports both standard and streaming inferences, as well as methods for preparing and cleaning up model resources.
Typical usage:
SummarizationRequest request = SummarizationRequest.builder(text).build();
ListenableFuture future = summarizer.runInference(request);
Futures.addCallback(future, new FutureCallback<>() {
@Override
public void onSuccess(SummarizationResult result) {
Log.d(TAG, result.getSummary());
}
@Override
public void onFailure(Throwable t) {
Log.e(TAG, "Failed to summarize", t);
}
}, executor);
Public Method Summary
ListenableFuture<Integer> |
checkFeatureStatus()
Checks the current availability status of the summarization feature.
|
void |
close()
Releases resources associated with the summarization engine.
|
ListenableFuture<Void> |
downloadFeature(DownloadCallback callback)
Downloads the required model assets for the summarization feature if they are
not already available.
|
ListenableFuture<String> |
getBaseModelName()
Returns the name of the base model used by this summarizer instance.
|
ListenableFuture<Void> |
prepareInferenceEngine()
Prepares the inference engine for use by loading necessary models and
initializing runtime components.
|
ListenableFuture<SummarizationResult> |
runInference(SummarizationRequest
request)
Performs asynchronous summarization on the provided input request.
|
ListenableFuture<SummarizationResult> |
runInference(SummarizationRequest
request, StreamingCallback streamingCallback)
Performs streaming summarization inference on the provided input request.
|
Inherited Method Summary
Public Methods
public ListenableFuture<Integer> checkFeatureStatus ()
Checks the current availability status of the summarization feature.
Returns
- a
ListenableFuture
resolving to aFeatureStatus
indicating feature readiness.
public void close ()
Releases resources associated with the summarization engine.
This should be called when the summarizer is no longer needed. Can be safely called multiple times.
public ListenableFuture<Void> downloadFeature (DownloadCallback callback)
Downloads the required model assets for the summarization feature if they are not already available.
Use this method to proactively download models before inference. The provided
DownloadCallback
is invoked to report progress and completion status.
Parameters
callback | a non-null DownloadCallback
for receiving download updates. |
---|
Returns
- a
ListenableFuture
that completes when the download finishes or fails. It completes immediately if already downloaded.
public ListenableFuture<String> getBaseModelName ()
Returns the name of the base model used by this summarizer instance.
The model name may be used for logging, debugging, or feature gating purposes.
Returns
- a
ListenableFuture
resolving to the base model name.
public ListenableFuture<Void> prepareInferenceEngine ()
Prepares the inference engine for use by loading necessary models and initializing runtime components.
If the models haven't been downloaded yet, it will trigger the download and wait for it to complete first.
While calling this method is optional, we recommend invoking it well before the first inference call to reduce the latency of the initial inference.
Returns
- a cancellable
ListenableFuture
that completes when the engine is ready.
public ListenableFuture<SummarizationResult> runInference (SummarizationRequest request)
Performs asynchronous summarization on the provided input request.
This is the standard, non-streaming version of inference. The full summary is returned once the model completes processing.
This method is non-blocking. To handle the result, callers should attach a listener
to the returned ListenableFuture
using
Futures.addCallback(ListenableFuture, FutureCallback super V>
, Executor)
or similar methods. The inference runs on background threads and
may complete at a later time depending on model availability and input size.
The returned ListenableFuture
is cancellable. If the inference is no longer needed (e.g., the user navigates away or
input changes), calling future.cancel(true)
will attempt to interrupt the
process and free up resources.
Note that inference requests may fail under certain conditions such as:
- Invalid or malformed input in the
SummarizationRequest
- Exceeded usage quota or failed safety checks
GenAiException
handling should be implemented when attaching the callback to the future.
Parameters
request | a non-null
SummarizationRequest containing input text. |
---|
Returns
- a cancellable
ListenableFuture
that resolves to aSummarizationResult
.
public ListenableFuture<SummarizationResult> runInference (SummarizationRequest request, StreamingCallback streamingCallback)
Performs streaming summarization inference on the provided input request.
Partial results are delivered incrementally through the provided StreamingCallback
.
This is useful to build more responsive UI. Streaming can be interrupted by a
GenAiException
.
In that case, consider removing any already streamed partial output from the UI.
The returned ListenableFuture
is cancellable. If the inference is no longer needed (e.g., the user navigates away or
input changes), calling future.cancel(true)
will attempt to interrupt the
process and free up resources.
Note that inference requests may fail under certain conditions such as:
- Invalid or malformed input in the
SummarizationRequest
- Exceeded usage quota or failed safety checks
GenAiException
handling should be implemented when attaching the callback to the future.
Parameters
request | a non-null
SummarizationRequest containing the input text. |
---|---|
streamingCallback | a non-null StreamingCallback
for receiving streamed results. |
Returns
- a cancellable
ListenableFuture
resolving to the finalSummarizationResult
.