Implement the new alt text flow (bug 1909604)

For the Firefox pdf viewer, we want to use AI to guess an alt-text when adding an image to a pdf.
For now the telemtry stuff is not implemented and will come soon.
In order to test it locally:
 - set enableAltText, enableFakeMLManager and enableUpdatedAddImage to true.
or in Firefox:
 - set browser.ml.enable, pdfjs.enableAltText and pdfjs.enableUpdatedAddImage to true.
This commit is contained in:
Calixte Denizet 2024-07-24 09:34:32 +02:00
parent 8378c40f4c
commit ed22d934e5
22 changed files with 1366 additions and 91 deletions

View file

@ -310,6 +310,8 @@ class FirefoxScripting {
class MLManager {
#enabled = null;
#ready = null;
eventBus = null;
constructor(options) {
@ -320,6 +322,10 @@ class MLManager {
return !!(await this.#enabled?.get(name));
}
isReady(name) {
return this.#ready?.has(name) ?? false;
}
deleteModel(service) {
return FirefoxCom.requestAsync("mlDelete", service);
}
@ -338,14 +344,33 @@ class MLManager {
this.altTextLearnMoreUrl = altTextLearnMoreUrl;
}
async toggleService(name, enabled) {
if (name !== "altText") {
return;
}
if (enabled) {
await this.#loadAltTextEngine(false);
} else {
this.#enabled?.delete(name);
this.#ready?.delete(name);
}
}
async #loadAltTextEngine(listenToProgress) {
if (this.#enabled?.has("altText")) {
// We already have a promise for the "altText" service.
return;
}
this.#ready ||= new Set();
const promise = FirefoxCom.requestAsync("loadAIEngine", {
service: "moz-image-to-text",
listenToProgress,
}).then(ok => {
if (ok) {
this.#ready.add("altText");
}
return ok;
});
(this.#enabled ||= new Map()).set("altText", promise);
if (listenToProgress) {