mirror of
https://github.com/zen-browser/pdf.js.git
synced 2025-07-08 01:10:08 +02:00
Merge pull request #18461 from calixteman/bug1908938
Load the image-to-text model when opening the pdf viewer in Firefox (bug 1908938)
This commit is contained in:
commit
ed83d7c5e1
5 changed files with 62 additions and 24 deletions
|
@ -430,7 +430,8 @@ class StampEditor extends AnnotationEditor {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.#hasMLBeenQueried = true;
|
this.#hasMLBeenQueried = true;
|
||||||
if (!this._uiManager.isMLEnabledFor("altText") || this.hasAltText()) {
|
const isMLEnabled = await this._uiManager.isMLEnabledFor("altText");
|
||||||
|
if (!isMLEnabled || this.hasAltText()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const offscreen = new OffscreenCanvas(width, height);
|
const offscreen = new OffscreenCanvas(width, height);
|
||||||
|
@ -447,7 +448,7 @@ class StampEditor extends AnnotationEditor {
|
||||||
height
|
height
|
||||||
);
|
);
|
||||||
const response = await this._uiManager.mlGuess({
|
const response = await this._uiManager.mlGuess({
|
||||||
service: "image-to-text",
|
service: "moz-image-to-text",
|
||||||
request: {
|
request: {
|
||||||
data: ctx.getImageData(0, 0, width, height).data,
|
data: ctx.getImageData(0, 0, width, height).data,
|
||||||
width,
|
width,
|
||||||
|
|
|
@ -855,8 +855,8 @@ class AnnotationEditorUIManager {
|
||||||
return this.#mlManager?.guess(data) || null;
|
return this.#mlManager?.guess(data) || null;
|
||||||
}
|
}
|
||||||
|
|
||||||
isMLEnabledFor(name) {
|
async isMLEnabledFor(name) {
|
||||||
return !!this.#mlManager?.isEnabledFor(name);
|
return !!(await this.#mlManager?.isEnabledFor(name));
|
||||||
}
|
}
|
||||||
|
|
||||||
get useNewAltTextFlow() {
|
get useNewAltTextFlow() {
|
||||||
|
|
28
web/app.js
28
web/app.js
|
@ -155,6 +155,7 @@ const PDFViewerApplication = {
|
||||||
isViewerEmbedded: window.parent !== window,
|
isViewerEmbedded: window.parent !== window,
|
||||||
url: "",
|
url: "",
|
||||||
baseUrl: "",
|
baseUrl: "",
|
||||||
|
mlManager: null,
|
||||||
_downloadUrl: "",
|
_downloadUrl: "",
|
||||||
_eventBusAbortController: null,
|
_eventBusAbortController: null,
|
||||||
_windowAbortController: null,
|
_windowAbortController: null,
|
||||||
|
@ -205,6 +206,11 @@ const PDFViewerApplication = {
|
||||||
if (mode) {
|
if (mode) {
|
||||||
document.documentElement.classList.add(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
|
// Ensure that the `L10n`-instance has been initialized before creating
|
||||||
|
@ -370,11 +376,14 @@ const PDFViewerApplication = {
|
||||||
|
|
||||||
let eventBus;
|
let eventBus;
|
||||||
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
|
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
|
||||||
eventBus = AppOptions.eventBus = new FirefoxEventBus(
|
eventBus =
|
||||||
AppOptions.get("allowedGlobalEvents"),
|
AppOptions.eventBus =
|
||||||
externalServices,
|
this.mlManager.eventBus =
|
||||||
AppOptions.get("isInAutomation")
|
new FirefoxEventBus(
|
||||||
);
|
AppOptions.get("allowedGlobalEvents"),
|
||||||
|
externalServices,
|
||||||
|
AppOptions.get("isInAutomation")
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
eventBus = new EventBus();
|
eventBus = new EventBus();
|
||||||
}
|
}
|
||||||
|
@ -731,15 +740,6 @@ const PDFViewerApplication = {
|
||||||
return shadow(this, "externalServices", new ExternalServices());
|
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() {
|
get initialized() {
|
||||||
return this._initializedCapability.settled;
|
return this._initializedCapability.settled;
|
||||||
},
|
},
|
||||||
|
|
|
@ -308,19 +308,56 @@ class FirefoxScripting {
|
||||||
}
|
}
|
||||||
|
|
||||||
class MLManager {
|
class MLManager {
|
||||||
#enabled = new Map();
|
#enabled = null;
|
||||||
|
|
||||||
constructor({ enableAltText }) {
|
eventBus = null;
|
||||||
this.#enabled.set("altText", enableAltText);
|
|
||||||
|
constructor(options) {
|
||||||
|
this.enable({ ...options, listenToProgress: false });
|
||||||
}
|
}
|
||||||
|
|
||||||
isEnabledFor(name) {
|
async isEnabledFor(name) {
|
||||||
return this.#enabled.get(name);
|
return !!(await this.#enabled?.get(name));
|
||||||
}
|
}
|
||||||
|
|
||||||
guess(data) {
|
guess(data) {
|
||||||
return FirefoxCom.requestAsync("mlGuess", 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;
|
||||||
|
}
|
||||||
|
const promise = FirefoxCom.requestAsync("loadAIEngine", {
|
||||||
|
service: "moz-image-to-text",
|
||||||
|
listenToProgress,
|
||||||
|
});
|
||||||
|
(this.#enabled ||= new Map()).set("altText", promise);
|
||||||
|
if (listenToProgress) {
|
||||||
|
const callback = ({ detail }) => {
|
||||||
|
this.eventBus.dispatch("loadaiengineprogress", {
|
||||||
|
source: this,
|
||||||
|
detail,
|
||||||
|
});
|
||||||
|
if (detail.finished) {
|
||||||
|
window.removeEventListener("loadAIEngineProgress", callback);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
window.addEventListener("loadAIEngineProgress", callback);
|
||||||
|
promise.then(ok => {
|
||||||
|
if (!ok) {
|
||||||
|
window.removeEventListener("loadAIEngineProgress", callback);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class ExternalServices extends BaseExternalServices {
|
class ExternalServices extends BaseExternalServices {
|
||||||
|
|
|
@ -48,7 +48,7 @@ class ExternalServices extends BaseExternalServices {
|
||||||
}
|
}
|
||||||
|
|
||||||
class MLManager {
|
class MLManager {
|
||||||
isEnabledFor(_name) {
|
async isEnabledFor(_name) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue