From 98cbe4a44d487e5d8e6bfd7af8bad57d94c4a047 Mon Sep 17 00:00:00 2001 From: Domenic Denicola Date: Wed, 22 Jan 2025 14:37:36 +0900 Subject: [PATCH] Change progress to be between 0 and 1 See https://github.com/webmachinelearning/writing-assistance-apis/issues/15. --- README.md | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1e6be8f..7aebd4f 100644 --- a/README.md +++ b/README.md @@ -374,13 +374,13 @@ if (supportsOurUseCase !== "no") { ### Download progress -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: +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: ```js const session = await ai.languageModel.create({ monitor(m) { m.addEventListener("downloadprogress", e => { - console.log(`Downloaded ${e.loaded} of ${e.total} bytes.`); + console.log(`Downloaded ${e.loaded * 100}%`); }); } }); @@ -388,6 +388,12 @@ const session = await ai.languageModel.create({ If the download fails, then `downloadprogress` events will stop being emitted, and the promise returned by `create()` will be rejected with a "`NetworkError`" `DOMException`. +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. + +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).) + +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. +
What's up with this pattern?