From 1b1af6706409631e6bec0444b354cd5b878357d9 Mon Sep 17 00:00:00 2001 From: Lim Chee Aun Date: Sun, 19 May 2024 16:27:59 +0800 Subject: [PATCH] Experiment non-English description generation --- src/components/compose.jsx | 61 +++++++++++++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/src/components/compose.jsx b/src/components/compose.jsx index 12045bd8..954d800e 100644 --- a/src/components/compose.jsx +++ b/src/components/compose.jsx @@ -28,6 +28,7 @@ import { api } from '../utils/api'; import db from '../utils/db'; import emojifyText from '../utils/emojify-text'; import localeMatch from '../utils/locale-match'; +import localeCode2Text from '../utils/localeCode2Text'; import openCompose from '../utils/open-compose'; import pmem from '../utils/pmem'; import shortenNumber from '../utils/shorten-number'; @@ -2023,8 +2024,66 @@ function MediaAttachment({ }} > - Generate description… + {lang && lang !== 'en' ? ( + + Generate description… +
+ (English) +
+ ) : ( + Generate description… + )} + {!!lang && lang !== 'en' && ( + { + setUIState('loading'); + toastRef.current = showToast({ + text: 'Generating description. Please wait...', + duration: -1, + }); + // POST with multipart + (async function () { + try { + const body = new FormData(); + body.append('image', file); + const params = `?lang=${lang}`; + const response = await fetch( + IMG_ALT_API_URL + params, + { + method: 'POST', + body, + }, + ).then((r) => r.json()); + if (response.error) { + throw new Error(response.error); + } + setDescription(response.description); + } catch (e) { + console.error(e); + showToast( + `Failed to generate description${ + e?.message ? `: ${e.message}` : '' + }`, + ); + } finally { + setUIState('default'); + toastRef.current?.hideToast?.(); + } + })(); + }} + > + + + Generate description… +
({localeCode2Text(lang)}){' '} + + — experimental + +
+
+ )} )}