You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Overhaul availability testing and add expected input languages for detector
Add an `expectedInputLanguages` option to language detector creation API. This allows the browser to download relevant material if necessary, or fail-fast if a language the web developer needs to support is not available.
Then, remove the `capabilities()` methods and the accompanying `AI*Capabilities` classes.
* For translator, the only useful capabilities API was `(await ai.translator.capabilities()).languagePairAvailable()`. We simplify this to `await ai.translator.availability()`. This design also avoids the complexity where we have to retrieve all the availability information for every combination of options during the call to `capabilities()`, for later sync access. Now we can just retrieve the relevant information during the call to `availability()`.
Also, by unifying on using the same options for `create()` and `availability()`, we fix#24.
* For language detector, the capabilities supplied both `(await ai.languageDetector.capabilities()).available` and `(await ai.languageDetector.capabilities()).languageAvailable()`. We simplify this into `await ai.languageDetector.availability()`, which can either take no arguments (emulating `available`) or take the same `{ expectedInputLanguages }` argument as `create()` (emulating `languageAvailable()`).
See also webmachinelearning/writing-assistance-apis#22 and webmachinelearning/prompt-api#69.
Copy file name to clipboardExpand all lines: README.md
+41-61Lines changed: 41 additions & 61 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -77,59 +77,53 @@ Here `results` will be an array of `{ detectedLanguage, confidence }` objects, w
77
77
78
78
The language being unknown is represented by `detectedLanguage` being null. The array will always contain at least 1 entry, although it could be for the unknown (`null`) language.
79
79
80
-
### Capabilities, and a more realistic combined example
80
+
### Language detection with expected input languages
81
81
82
-
Both APIs provide a promise-returning `capabilities()` methods which let you know, before calling `create()`, what is possible with the implementation. The capabilities object that the promise fulfills with has an `available` property which is one of `"no"`, `"after-download"`, or `"readily"`:
82
+
If there are certain languages you need to be able to detect for your use case, you can include them in the `expectedInputLanguages` option when creating a language detector:
83
83
84
-
*`"no"` means that the implementation does not support translation or language detection.
85
-
*`"after-download"` means that the implementation supports translation or language detection, but it will have to download something (e.g. a machine learning model) before it can do anything.
86
-
*`"readily"` means that the implementation supports translation or language detection, and at least the base model is available without any downloads.
This will allow the implementation to download additional resources like language detection models if necessary, and will ensure that the promise is rejected with a `"NotSupportedError"``DOMException` if the browser is unable to detect the given input languages.
87
89
88
-
Each of these capabilities objects has further methods which give the state of specific translation or language detection capabilities:
90
+
### Checking before creation, and a more realistic combined example
89
91
90
-
*`languagePairAvailable(sourceLanguageTag, targetLanguageTag)`, for the `ai.translation.capabilities()` object
91
-
*`languageAvailable(languageTag)`, for the `ai.languageDetection.capabilities()` object
92
+
Both APIs provide the ability to know, before calling `create()`, what is possible with the implementation. This is done via `availability()` methods, which takes the same options as `create()`. They return a promise, which fulfills with one of the following values:
92
93
93
-
Both of these methods return `"no"`, `"after-download"`, or `"readily"`, which have the same meanings as above, except specialized to the specific arguments in question.
94
+
*`"no"` means that the implementation does not support translation or language detection of the given language(s).
95
+
*`"after-download"` means that the implementation supports translation or language detection of the given language(s), but it will have to download something (e.g., a machine learning model) as part of creating the associated object.
96
+
*`"readily"` means that the implementation supports translation or language detection of the given language(s), without performing any downloads.
94
97
95
98
Here is an example that adds capability checking to log more information and fall back to cloud services, as part of a language detection plus translation task:
@@ -313,17 +301,9 @@ We're not clear on what the right model is here, and are discussing it in [issue
313
301
314
302
### Downloading
315
303
316
-
The current design envisions that the following operations will _not_ cause downloads of language packs or other material like a language detection model:
317
-
318
-
*`ai.translator.capabilities()` and the properties/methods of the returned object
319
-
*`ai.languageDetector.capabilities()` and the properties/methods of the returned object
320
-
321
-
The following _can_ cause downloads. In all cases, whether or not a call will initiate a download can be detected beforehand by checking the corresponding capabilities object.
322
-
323
-
*`ai.translator.create()`
324
-
*`ai.languageDetector.create()`
304
+
The current design envisions that `availability()` methods will _not_ cause downloads of language packs or other material like a language detection model. Whereas, the `create()` methods _can_ cause downloads. In all cases, whether or not creation will initiate a download can be detected beforehand by the corresponding `availability()` method.
325
305
326
-
After a developer has a `AITranslator` or `AILanguageDetector` object created by these methods, further calls are not expected to cause any downloads. (Although they might require internet access, if the implementation is not entirely on-device.)
306
+
After a developer has a `AITranslator` or `AILanguageDetector` object, further calls are not expected to cause any downloads. (Although they might require internet access, if the implementation is not entirely on-device.)
327
307
328
308
This design means that the implementation must have all information about the capabilities of its translation and language detection models available beforehand, i.e. "shipped with the browser". (Either as part of the browser binary, or through some out-of-band update mechanism that eagerly pushes updates.)
329
309
@@ -339,7 +319,7 @@ Some sort of mitigation may be necessary here. We believe this is adjacent to ot
339
319
* Partitioning download status by top-level site, introducing a fake download (which takes time but does not actually download anything) for the second-onward site to download a language pack.
340
320
* Only exposing a fixed set of languages to this API, e.g. based on the user's locale or the document's main language.
341
321
342
-
As a first step, we require that detecting the availability of translation/detection be done via individual calls to `translationCapabilities.languagePairAvailable()` and `detectionCapabilities.languageAvailable()`. This allows browsers to implement possible mitigation techniques, such as detecting excessive calls to these methods and starting to return `"no"`.
322
+
As a first step, we require that detecting the availability of translation/detection be done via individual calls to `ai.translator.availability()` and `ai.languageDetector.availability()`. This allows browsers to implement possible mitigation techniques, such as detecting excessive calls to these methods and starting to return `"no"`.
343
323
344
324
Another way in which this API might enhance the web's fingerprinting surface is if translation and language detection models are updated separately from browser versions. In that case, differing results from different versions of the model provide additional fingerprinting bits beyond those already provided by the browser's major version number. Mandating that older browser versions not receive updates or be able to download models from too far into the future might be a possible remediation for this.
345
325
@@ -373,13 +353,13 @@ Should we simplify these down with convenience APIs that do both steps at once?
373
353
374
354
We're open to this idea, but we think the existing complexity is necessary to support the design wherein translation and language detection models might not be already downloaded. By separating the two stages, we allow web developers to perform the initial creation-and-possibly-downloading steps early in their page's lifecycle, in preparation for later, hopefully-quick calls to APIs like `translate()`.
375
355
376
-
Another possible simplification is to make the `capabilities()` APIs synchronous instead of asynchronous. This would be implementable by having the browser proactively load the capabilities information into the main thread's process, upon creation of the global object. We think this is not worthwhile, as it imposes a non-negligible cost on all global object creation, even when the APIs are not used.
356
+
Another possible simplification is to make the `availability()` APIs synchronous instead of asynchronous. This would be implementable by having the browser proactively load the capabilities information into the main thread's process, upon creation of the global object. We think this is not worthwhile, as it imposes a non-negligible cost on all global object creation, even when the APIs are not used.
377
357
378
358
### Allowing unknown source languages for translation
379
359
380
360
An earlier revision of this API including support for combining the language detection and translation steps into a single translation call, which did a best-guess on the source language. The idea was that this would possibly be more efficient than requiring the web developer to do two separate calls, and it could possibly even be done using a single model.
381
361
382
-
We abandoned this design when it became clear that existing browsers have very decoupled implementations of translation vs. language detection, using separate models for each. This includes supporting different languages for language detection vs. for translation. So even if the translation model supported an unknown-source-language mode, it might not support the same inputs as the language detection model, which would create a confusing developer experience and be hard to signal in the capabilities API.
362
+
We abandoned this design when it became clear that existing browsers have very decoupled implementations of translation vs. language detection, using separate models for each. This includes supporting different languages for language detection vs. for translation. So even if the translation model supported an unknown-source-language mode, it might not support the same inputs as the language detection model, which would create a confusing developer experience and be hard to signal in the API.
0 commit comments