From 7c26cece3e32744b535be6c9c493a1398083acdc Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Tue, 28 May 2024 19:46:04 +0200 Subject: [PATCH] feat: add experimentalAltTextGeneration flag --- components/publish/PublishAttachment.vue | 18 +++++++++++++++--- composables/settings/definition.ts | 2 ++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/components/publish/PublishAttachment.vue b/components/publish/PublishAttachment.vue index cba57c33..5118626d 100644 --- a/components/publish/PublishAttachment.vue +++ b/components/publish/PublishAttachment.vue @@ -23,6 +23,8 @@ const description = ref(props.attachment.description ?? '') const generationInProgress = ref(false) +const userSettings = useUserSettings() + async function generateAltText() { // eslint-disable-next-line no-console console.log(JSON.parse(JSON.stringify(props))) @@ -35,7 +37,18 @@ async function generateAltText() { if (generationInProgress.value) return - // TODO @Shinigami92 2024-05-27: Show confirm dialog warning message that a model with ~250MiB will be downloaded + const experimentalAltTextGeneration = getPreferences(userSettings.value, 'experimentalAltTextGeneration') + + if (!experimentalAltTextGeneration) { + // TODO @Shinigami92 2024-05-28: Use a fancy dialog instead of the browser's alert + // eslint-disable-next-line no-alert + const allow = confirm('This will download a model with ~250MiB. Do you want to continue? This is an experimental feature and might fail in several scenarios.') + + if (!allow) + return + + togglePreferences('experimentalAltTextGeneration') + } generationInProgress.value = true @@ -44,11 +57,10 @@ async function generateAltText() { const pipe = await pipeline('image-to-text', 'Xenova/vit-gpt2-image-captioning') - // const imageElement = document.querySelector('.dialog-main img.status-attachment-image')! const imageElement = new Image() + // See https://www.hacksoft.io/blog/handle-images-cors-error-in-chrome for why using `?request-with-cors` imageElement.crossOrigin = 'Anonymous' imageElement.src = `${url}?request-with-cors` - await imageElement.decode() const dataUrl = new Promise((resolve) => { imageElement.onload = () => { diff --git a/composables/settings/definition.ts b/composables/settings/definition.ts index c54c9736..d6aeef6c 100644 --- a/composables/settings/definition.ts +++ b/composables/settings/definition.ts @@ -32,6 +32,7 @@ export interface PreferencesSettings { experimentalGitHubCards: boolean experimentalUserPicker: boolean experimentalEmbeddedMedia: boolean + experimentalAltTextGeneration: boolean } export interface UserSettings { @@ -88,6 +89,7 @@ export const DEFAULT__PREFERENCES_SETTINGS: PreferencesSettings = { experimentalGitHubCards: true, experimentalUserPicker: true, experimentalEmbeddedMedia: false, + experimentalAltTextGeneration: false, } export function getDefaultUserSettings(locales: string[]): UserSettings {