Với API Hiệu đính bằng AI tạo sinh của Bộ công cụ học máy, bạn có thể giúp người dùng kiểm tra ngữ pháp và chính tả trong các đoạn văn bản ngắn.
Các chức năng chính
- Hiệu đính văn bản được nhập qua bàn phím hoặc giọng nói
- Các yêu cầu sẽ trả về ít nhất một đề xuất. Nếu nhiều đề xuất được trả về, kết quả sẽ được sắp xếp theo độ tin cậy giảm dần.
Kết quả mẫu
Input |
Loại đầu vào |
Đầu ra |
this is a short msg |
Bàn phím |
Đây là một tin nhắn ngắn |
Dự án gần như hoàn tất nhưng cần được xem xét |
Bàn phím |
Dự án gần hoàn tất nhưng cần được xem xét |
Vui lòng gặp tôi ở chỗ con gấu, |
Voice |
Vui lòng gặp tôi tại quầy bar. |
Bắt đầu
Để bắt đầu sử dụng GenAI Proofreading API, hãy thêm phần phụ thuộc này vào tệp bản dựng của dự án.
implementation("com.google.mlkit:genai-proofreading:1.0.0-beta1")
Sau đó, hãy định cấu hình và lấy một ứng dụng Proofreader
có chế độ cài đặt ngôn ngữ và loại đầu vào cụ thể. Kiểm tra và đảm bảo các tính năng cần thiết của mô hình trên thiết bị đều có sẵn (kích hoạt quá trình tải xuống nếu cần). Gửi văn bản bạn muốn phân tích trong ProofreadingRequest
, thực thi suy luận hiệu đính và cuối cùng xử lý các đề xuất được trả về để sửa văn bản.
Kotlin
val textToProofread = "The praject is compleet but needs too be reviewd"
// Define task with required input and output format
val options = ProofreaderOptions.builder(context)
// InputType can be KEYBOARD or VOICE. VOICE indicates that
// the user generated text based on audio input.
.setInputType(ProofreaderOptions.InputType.KEYBOARD)
// Refer to ProofreaderOptions.Language for available
// languages
.setLanguage(ProofreaderOptions.Language.ENGLISH)
.build()
val proofreader = Proofreading.getClient(options)
suspend fun prepareAndStartProofread() {
// Check feature availability, status will be one of the
// following: UNAVAILABLE, DOWNLOADABLE, DOWNLOADING, AVAILABLE
val featureStatus = proofreader.checkFeatureStatus().await()
if (featureStatus == FeatureStatus.DOWNLOADABLE) {
// Download feature if necessary.
// If downloadFeature is not called, the first inference
// request will also trigger the feature to be downloaded
// if it's not already downloaded.
proofreader.downloadFeature(object : DownloadCallback {
override fun onDownloadStarted(bytesToDownload: Long) { }
override fun onDownloadFailed(e: GenAiException) { }
override fun onDownloadProgress(
totalBytesDownloaded: Long
) {}
override fun onDownloadCompleted() {
startProofreadingRequest(textToProofread, proofreader)
}
})
} else if (featureStatus == FeatureStatus.DOWNLOADING) {
// Inference request will automatically run once feature is
// downloaded.
// If Gemini Nano is already downloaded on the device, the
// feature-specific LoRA adapter model will be downloaded
// very quickly. However, if Gemini Nano is not already
// downloaded, the download process may take longer.
startProofreadingRequest(textToProofread, proofreader)
} else if (featureStatus == FeatureStatus.AVAILABLE) {
startProofreadingRequest(textToProofread, proofreader)
}
}
suspend fun startProofreadingRequest(
text: String, proofreader: Proofreader
) {
// Create task request
val proofreadingRequest =
ProofreadingRequest.builder(text).build()
// Start proofreading request with non-streaming response
// More than 1 result may be returned. If multiple suggestions
// are returned, results will be sorted by descending confidence.
val proofreadingResults =
proofreader.runInference(proofreadingRequest).await().results
// You can also start a streaming request
// proofreader.runInference(proofreadingRequest) { newText ->
// // show new text in UI
// }
}
// Be sure to release the resource when no longer needed
// For example, on viewModel.onCleared() or activity.onDestroy()
proofreader.close()
Java
String textToProofread = "The praject is compleet but needs too be reviewd";
// Define task with required input and output format
ProofreaderOptions proofreaderOptions =
ProofreaderOptions
.builder(context)
// InputType can be KEYBOARD or VOICE. VOICE indicates that the
// user generated text based on audio input.
.setInputType(ProofreaderOptions.InputType.KEYBOARD)
// Refer to ProofreaderOptions.Language for available languages
.setLanguage(ProofreaderOptions.Language.ENGLISH)
.build();
Proofreader proofreader = Proofreading.getClient(proofreaderOptions);
void prepareAndStartProofread(Context context) throws ExecutionException,
InterruptedException {
// Check feature availability, status will be one of the following:
// UNAVAILABLE, DOWNLOADABLE, DOWNLOADING, AVAILABLE
try {
int featureStatus = proofreader.checkFeatureStatus().get();
if (featureStatus == FeatureStatus.DOWNLOADABLE) {
// Download feature if necessary.
// If downloadFeature is not called, the first inference request
// will also trigger the feature to be downloaded if it's not
// already downloaded.
proofreader.downloadFeature(new DownloadCallback() {
@Override
public void onDownloadCompleted() {
startProofreadingRequest(textToProofread, proofreader);
}
@Override
public void onDownloadFailed(GenAiException e) {
}
@Override
public void onDownloadProgress(long totalBytesDownloaded) {
}
@Override
public void onDownloadStarted(long bytesDownloaded) {
}
});
} else if (featureStatus == FeatureStatus.DOWNLOADING) {
// Inference request will automatically run once feature is
// downloaded.
// If Gemini Nano is already downloaded on the device, the
// feature-specific LoRA adapter model will be downloaded
// very quickly. However, if Gemini Nano is not already
// downloaded, the download process may take longer.
startProofreadingRequest(textToProofread, proofreader);
} else if (featureStatus == FeatureStatus.AVAILABLE) {
startProofreadingRequest(textToProofread, proofreader);
}
} catch (ExecutionException | InterruptedException e) {
e.printStackTrace();
}
}
void startProofreadingRequest(String text, Proofreader proofreader) {
// Create task request
ProofreadingRequest proofreadingRequest = ProofreadingRequest
.builder(text).build();
try {
// Start proofreading request with non-streaming response
// More than 1 result may be returned. If multiple suggestions are
// returned, results will be sorted by descending confidence.
proofreader.runInference(proofreadingRequest).get().getResults();
// You can also start a streaming request
// proofreader.runInference(proofreadingRequest, newText -> {
// // Show new text in UI
// });
} catch (ExecutionException | InterruptedException e) {
e.printStackTrace();
}
}
// Be sure to release the resource when no longer needed
// For example, on viewModel.onCleared() or activity.onDestroy()
proofreader.close();
Cách mô hình xử lý nhiều loại dữ liệu đầu vào
Khi có thêm thông tin về cách người dùng nhập văn bản (bàn phím hoặc giọng nói), mô hình có thể dự đoán chính xác hơn loại lỗi có thể xuất hiện. Văn bản nhập bằng bàn phím dễ bị sai chính tả hơn do các phím gần nhau, trong khi văn bản nhập bằng giọng nói dễ bị sai chính tả hơn đối với những từ có cách phát âm giống nhau.
Các tính năng được hỗ trợ và hạn chế
Tính năng hiệu đính hỗ trợ các ngôn ngữ sau: tiếng Anh, tiếng Nhật, tiếng Pháp, tiếng Đức, tiếng Ý, tiếng Tây Ban Nha và tiếng Hàn. Các ngôn ngữ này được xác định trong ProofreaderOptions.Language
. Đầu vào phải ít hơn 256 mã thông báo.
Khả năng sử dụng cấu hình tính năng cụ thể (do ProofreaderOptions
chỉ định) có thể thay đổi tuỳ theo cấu hình của thiết bị cụ thể và các mô hình đã được tải xuống thiết bị.
Cách đáng tin cậy nhất để nhà phát triển đảm bảo rằng tính năng API dự kiến được hỗ trợ trên một thiết bị có ProofreaderOptions
được yêu cầu là gọi phương thức checkFeatureStatus()
. Phương thức này cung cấp trạng thái xác định về khả năng sử dụng tính năng trên thiết bị trong thời gian chạy.
Các vấn đề thường gặp về chế độ thiết lập
Các API GenAI của Bộ công cụ học máy dựa vào ứng dụng Android AICore để truy cập vào Gemini Nano. Khi thiết bị vừa được thiết lập (bao gồm cả thiết lập lại) hoặc ứng dụng AICore vừa được thiết lập lại (ví dụ: xoá dữ liệu, gỡ cài đặt rồi cài đặt lại), ứng dụng AICore có thể không có đủ thời gian để hoàn tất quá trình khởi động (bao gồm cả việc tải các cấu hình mới nhất xuống từ máy chủ). Do đó, các API AI tạo sinh của Bộ công cụ học máy có thể không hoạt động như mong đợi. Sau đây là các thông báo lỗi thiết lập thường gặp mà bạn có thể gặp phải và cách xử lý các lỗi đó:
Ví dụ về thông báo lỗi | Cách xử lý |
AICore không hoạt động được do lỗi loại 4-CONNECTION_ERROR và mã lỗi 601-BINDING_FAILURE: Không liên kết được dịch vụ AICore. | Điều này có thể xảy ra khi bạn cài đặt ứng dụng bằng các API GenAI của Bộ công cụ học máy ngay sau khi thiết lập thiết bị hoặc khi AICore bị gỡ cài đặt sau khi ứng dụng của bạn được cài đặt. Việc cập nhật ứng dụng AICore rồi cài đặt lại ứng dụng của bạn sẽ khắc phục được vấn đề này. |
AICore không hoạt động được do lỗi loại 3 – PREPARATION_ERROR và mã lỗi 606 – FEATURE_NOT_FOUND: Không có tính năng .... |
Điều này có thể xảy ra khi AICore chưa tải xong các cấu hình mới nhất. Khi thiết bị kết nối với Internet, quá trình cập nhật thường mất từ vài phút đến vài giờ. Việc khởi động lại thiết bị có thể giúp quá trình cập nhật diễn ra nhanh hơn.
Xin lưu ý rằng nếu trình tải khởi động của thiết bị bị mở khoá, bạn cũng sẽ thấy lỗi này. API này không hỗ trợ các thiết bị có trình tải khởi động đã mở khoá. |
AICore gặp lỗi loại 1 – DOWNLOAD_ERROR và mã lỗi 0 – UNKNOWN: Tính năng ... gặp lỗi với trạng thái lỗi 0 và lỗi esz: UNAVAILABLE: Unable to resolve host ... | Giữ kết nối mạng, chờ vài phút rồi thử lại. |