Skip to content

Commit 28733f4

Browse files
committed
Change progress to be between 0 and 1
See webmachinelearning/writing-assistance-apis#15.
1 parent 2dd11f5 commit 28733f4

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

README.md

+8-2
Original file line numberDiff line numberDiff line change
@@ -374,20 +374,26 @@ if (supportsOurUseCase !== "no") {
374374

375375
### Download progress
376376

377-
In cases where the model needs to be downloaded as part of creation, you can monitor the download progress (e.g. in order to show your users a progress bar) using code such as the following:
377+
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:
378378

379379
```js
380380
const session = await ai.languageModel.create({
381381
monitor(m) {
382382
m.addEventListener("downloadprogress", e => {
383-
console.log(`Downloaded ${e.loaded} of ${e.total} bytes.`);
383+
console.log(`Downloaded ${e.loaded * 100}%`);
384384
});
385385
}
386386
});
387387
```
388388

389389
If the download fails, then `downloadprogress` events will stop being emitted, and the promise returned by `create()` will be rejected with a "`NetworkError`" `DOMException`.
390390

391+
Note that in the case that multiple entities are downloaded (e.g., a base model plus a [LoRA fine-tuning](https://arxiv.org/abs/2106.09685) for the `expectedInputLanguages`) web developers do not get the ability to monitor the individual downloads. All of them are bundled into the overall `downloadprogress` events, and the `create()` promise is not fulfilled until all downloads and loads are successful.
392+
393+
The event is a [`ProgressEvent`](https://developer.mozilla.org/en-US/docs/Web/API/ProgressEvent) whose `loaded` property is between 0 and 1, and whose `total` property is always 1. (The exact number of total or downloaded bytes are not exposed; see the discussion in [webmachinelearning/writing-assistance-apis issue #15](https://github.com/webmachinelearning/writing-assistance-apis/issues/15).)
394+
395+
At least two events, with `e.loaded === 0` and `e.loaded === 1`, will always be fired. This is true even if creating the model doesn't require any downloading.
396+
391397
<details>
392398
<summary>What's up with this pattern?</summary>
393399

0 commit comments

Comments
 (0)