Skip to content

Commit 7a78e20

Browse files
committed
Remove the "AI" namespace
Follows webmachinelearning/writing-assistance-apis#45.
1 parent b924f58 commit 7a78e20

File tree

3 files changed

+130
-193
lines changed

3 files changed

+130
-193
lines changed

README.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ See [below](#language-tag-handling) for more on the details of how language tags
4747
Here is the basic usage of the translator API, with no error handling:
4848

4949
```js
50-
const translator = await ai.translator.create({
50+
const translator = await Translator.create({
5151
sourceLanguage: "en",
5252
targetLanguage: "ja"
5353
});
@@ -65,7 +65,7 @@ Note that the `create()` method call here might cause the download of a translat
6565
A similar simplified example of the language detector API:
6666

6767
```js
68-
const detector = await ai.languageDetector.create();
68+
const detector = await LanguageDetector.create();
6969

7070
const results = await detector.detect(someUserText);
7171
for (const result of results) {
@@ -84,7 +84,7 @@ For more details on the ways low-confidence results are excluded, see [the speci
8484
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:
8585

8686
```js
87-
const detector = await ai.languageDetector.create({ expectedInputLanguages: ["en", "ja"] });
87+
const detector = await LanguageDetector.create({ expectedInputLanguages: ["en", "ja"] });
8888
```
8989

9090
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.
@@ -102,7 +102,7 @@ Here is an example that adds capability checking to log more information and fal
102102

103103
```js
104104
async function translateUnknownCustomerInput(textToTranslate, targetLanguage) {
105-
const detectorAvailability = await ai.languageDetector.availability();
105+
const detectorAvailability = await LanguageDetector.availability();
106106

107107
// If there is no language detector, then assume the source language is the
108108
// same as the document language.
@@ -114,7 +114,7 @@ async function translateUnknownCustomerInput(textToTranslate, targetLanguage) {
114114
console.log("Language detection is available, but something will have to be downloaded. Hold tight!");
115115
}
116116

117-
const detector = await ai.languageDetector.create();
117+
const detector = await LanguageDetector.create();
118118
const [bestResult] = await detector.detect(textToTranslate);
119119

120120
if (bestResult.detectedLanguage ==== "und" || bestResult.confidence < 0.4) {
@@ -126,7 +126,7 @@ async function translateUnknownCustomerInput(textToTranslate, targetLanguage) {
126126
}
127127

128128
// Now we've figured out the source language. Let's translate it!
129-
const translatorAvailability = await ai.translator.availability({ sourceLanguage, targetLanguage });
129+
const translatorAvailability = await Translator.availability({ sourceLanguage, targetLanguage });
130130
if (translatorAvailability === "unavailable") {
131131
console.warn("Translation is not available. Falling back to cloud API.");
132132
return await useSomeCloudAPIToTranslate(textToTranslate, { sourceLanguage, targetLanguage });
@@ -136,7 +136,7 @@ async function translateUnknownCustomerInput(textToTranslate, targetLanguage) {
136136
console.log("Translation is available, but something will have to be downloaded. Hold tight!");
137137
}
138138

139-
const translator = await ai.translator.create({ sourceLanguage, targetLanguage });
139+
const translator = await Translator.create({ sourceLanguage, targetLanguage });
140140
return await translator.translate(textToTranslate);
141141
}
142142
```
@@ -146,7 +146,7 @@ async function translateUnknownCustomerInput(textToTranslate, targetLanguage) {
146146
For cases where using the API is only possible after a download, you can monitor the download progress (e.g. in order to show your users a progress bar) using code such as the following:
147147

148148
```js
149-
const translator = await ai.translator.create({
149+
const translator = await Translator.create({
150150
sourceLanguage,
151151
targetLanguage,
152152
monitor(m) {
@@ -189,7 +189,7 @@ The "usage" concept is specific to the implementation, and could be something li
189189
This allows detecting failures due to overlarge inputs and giving clear feedback to the user, with code such as the following:
190190

191191
```js
192-
const detector = await ai.languageDetector.create();
192+
const detector = await LanguageDetector.create();
193193

194194
try {
195195
console.log(await detector.detect(potentiallyLargeInput));
@@ -206,7 +206,7 @@ try {
206206
In some cases, instead of providing errors after the fact, the developer needs to be able to communicate to the user how close they are to the limit. For this, they can use the `inputQuota` property and the `measureInputUsage()` method on the translator or language detector objects:
207207

208208
```js
209-
const translator = await ai.translator.create({
209+
const translator = await Translator.create({
210210
sourceLanguage: "en",
211211
targetLanguage: "jp"
212212
});
@@ -247,7 +247,7 @@ The API comes equipped with a couple of `signal` options that accept `AbortSigna
247247
const controller = new AbortController();
248248
stopButton.onclick = () => controller.abort();
249249

250-
const languageDetector = await ai.languageDetector.create({ signal: controller.signal });
250+
const languageDetector = await LanguageDetector.create({ signal: controller.signal });
251251
await languageDetector.detect(document.body.textContent, { signal: controller.signal });
252252
```
253253

@@ -281,7 +281,7 @@ A future option might be to instead have the API return back the splitting of th
281281

282282
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.
283283

284-
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.)
284+
After a developer has a `Translator` or `LanguageDetector` object, further calls are not expected to cause any downloads. (Although they might require internet access, if the implementation is not entirely on-device.)
285285

286286
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.)
287287

@@ -297,7 +297,7 @@ Some sort of mitigation may be necessary here. We believe this is adjacent to ot
297297
* 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.
298298
* Only exposing a fixed set of languages to this API, e.g. based on the user's locale or the document's main language.
299299

300-
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 `"unavailable"`.
300+
As a first step, we require that detecting the availability of translation/detection be done via individual calls to `Translator.availability()` and `LanguageDetector.availability()`. This allows browsers to implement possible mitigation techniques, such as detecting excessive calls to these methods and starting to return `"unavailable"`.
301301

302302
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.
303303

@@ -320,10 +320,10 @@ That said, we are aware of [research](https://arxiv.org/abs/2005.08595) on trans
320320
The current design requires multiple async steps to do useful things:
321321

322322
```js
323-
const translator = await ai.translator.create(options);
323+
const translator = await Translator.create(options);
324324
const text = await translator.translate(sourceText);
325325

326-
const detector = await ai.languageDetector.create();
326+
const detector = await LanguageDetector.create();
327327
const results = await detector.detect(sourceText);
328328
```
329329

0 commit comments

Comments
 (0)