Skip to content

Commit

Permalink
Load the image-to-text model when opening the pdf viewer in Firefox (…
Browse files Browse the repository at this point in the history
…bug 1908938)
  • Loading branch information
calixteman committed Jul 19, 2024
1 parent 5b0e15a commit c9fb206
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 24 deletions.
5 changes: 3 additions & 2 deletions src/display/editor/stamp.js
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,8 @@ class StampEditor extends AnnotationEditor {
return;
}
this.#hasMLBeenQueried = true;
if (!this._uiManager.isMLEnabledFor("altText") || this.hasAltText()) {
const isMLEnabled = await this._uiManager.isMLEnabledFor("altText");
if (!isMLEnabled || this.hasAltText()) {
return;
}
const offscreen = new OffscreenCanvas(width, height);
Expand All @@ -447,7 +448,7 @@ class StampEditor extends AnnotationEditor {
height
);
const response = await this._uiManager.mlGuess({
service: "image-to-text",
service: "moz-image-to-text",
request: {
data: ctx.getImageData(0, 0, width, height).data,
width,
Expand Down
4 changes: 2 additions & 2 deletions src/display/editor/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -855,8 +855,8 @@ class AnnotationEditorUIManager {
return this.#mlManager?.guess(data) || null;
}

isMLEnabledFor(name) {
return !!this.#mlManager?.isEnabledFor(name);
async isMLEnabledFor(name) {
return !!(await this.#mlManager?.isEnabledFor(name));
}

get useNewAltTextFlow() {
Expand Down
28 changes: 14 additions & 14 deletions web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ const PDFViewerApplication = {
isViewerEmbedded: window.parent !== window,
url: "",
baseUrl: "",
mlManager: null,
_downloadUrl: "",
_eventBusAbortController: null,
_windowAbortController: null,
Expand Down Expand Up @@ -205,6 +206,11 @@ const PDFViewerApplication = {
if (mode) {
document.documentElement.classList.add(mode);
}
} else {
// We want to load the image-to-text AI engine as soon as possible.
this.mlManager = new MLManager({
enableAltText: AppOptions.get("enableAltText"),
});
}

// Ensure that the `L10n`-instance has been initialized before creating
Expand Down Expand Up @@ -370,11 +376,14 @@ const PDFViewerApplication = {

let eventBus;
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
eventBus = AppOptions.eventBus = new FirefoxEventBus(
AppOptions.get("allowedGlobalEvents"),
externalServices,
AppOptions.get("isInAutomation")
);
eventBus =
AppOptions.eventBus =
this.mlManager.eventBus =
new FirefoxEventBus(
AppOptions.get("allowedGlobalEvents"),
externalServices,
AppOptions.get("isInAutomation")
);
} else {
eventBus = new EventBus();
}
Expand Down Expand Up @@ -731,15 +740,6 @@ const PDFViewerApplication = {
return shadow(this, "externalServices", new ExternalServices());
},

get mlManager() {
const enableAltText = AppOptions.get("enableAltText");
return shadow(
this,
"mlManager",
enableAltText === true ? new MLManager({ enableAltText }) : null
);
},

get initialized() {
return this._initializedCapability.settled;
},
Expand Down
44 changes: 39 additions & 5 deletions web/firefoxcom.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,19 +308,53 @@ class FirefoxScripting {
}

class MLManager {
#enabled = new Map();
#enabled = null;

constructor({ enableAltText }) {
this.#enabled.set("altText", enableAltText);
eventBus = null;

constructor(options) {
this.enable({ ...options, listenToProgress: false });
}

isEnabledFor(name) {
return this.#enabled.get(name);
async isEnabledFor(name) {
return !!(await this.#enabled.get(name));
}

guess(data) {
return FirefoxCom.requestAsync("mlGuess", data);
}

enable({ enableAltText, listenToProgress }) {
if (enableAltText) {
this.#loadAltTextEngine(listenToProgress);
}
}

async #loadAltTextEngine(listenToProgress) {
if (this.#enabled.has("altText")) {
// We already have a promise for the "altText" service.
return;
}
if (listenToProgress) {
const callback = ({ detail }) => {
this.eventBus.dispatch("loadaiengineprogress", {
source: this,
detail,
});
if (detail.finished) {
window.removeEventListener("loadAIEngineProgress", callback);
}
};
window.addEventListener("loadAIEngineProgress", callback);
}
(this.#enabled ||= new Map()).set(
"altText",
FirefoxCom.requestAsync("loadAIEngine", {
service: "moz-image-to-text",
listenToProgress,
})
);
}
}

class ExternalServices extends BaseExternalServices {
Expand Down
2 changes: 1 addition & 1 deletion web/genericcom.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class ExternalServices extends BaseExternalServices {
}

class MLManager {
isEnabledFor(_name) {
async isEnabledFor(_name) {
return false;
}

Expand Down

0 comments on commit c9fb206

Please sign in to comment.