mirror of
https://github.com/cheeaun/phanpy.git
synced 2025-01-23 09:06:23 +01:00
Add multiple translation instances as fallbacks with retries
This commit is contained in:
parent
34fcf5e8bd
commit
da03de4115
1 changed files with 39 additions and 17 deletions
|
@ -1,5 +1,6 @@
|
||||||
import './translation-block.css';
|
import './translation-block.css';
|
||||||
|
|
||||||
|
import pRetry from 'p-retry';
|
||||||
import pThrottle from 'p-throttle';
|
import pThrottle from 'p-throttle';
|
||||||
import { useEffect, useRef, useState } from 'preact/hooks';
|
import { useEffect, useRef, useState } from 'preact/hooks';
|
||||||
|
|
||||||
|
@ -15,12 +16,21 @@ const throttle = pThrottle({
|
||||||
interval: 2000,
|
interval: 2000,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Using other API instances instead of lingva.ml because of this bug (slashes don't work):
|
||||||
|
// https://github.com/thedaviddelta/lingva-translate/issues/68
|
||||||
|
const LINGVA_INSTANCES = [
|
||||||
|
'lingva.garudalinux.org',
|
||||||
|
'lingva.lunar.icu',
|
||||||
|
'translate.plausibility.cloud',
|
||||||
|
];
|
||||||
|
let currentLingvaInstance = 0;
|
||||||
|
|
||||||
function lingvaTranslate(text, source, target) {
|
function lingvaTranslate(text, source, target) {
|
||||||
console.log('TRANSLATE', text, source, target);
|
console.log('TRANSLATE', text, source, target);
|
||||||
// Using another API instance instead of lingva.ml because of this bug (slashes don't work):
|
const fetchCall = () => {
|
||||||
// https://github.com/thedaviddelta/lingva-translate/issues/68
|
let instance = LINGVA_INSTANCES[currentLingvaInstance];
|
||||||
return fetch(
|
return fetch(
|
||||||
`https://lingva.garudalinux.org/api/v1/${source}/${target}/${encodeURIComponent(
|
`https://${instance}/api/v1/${source}/${target}/${encodeURIComponent(
|
||||||
text,
|
text,
|
||||||
)}`,
|
)}`,
|
||||||
)
|
)
|
||||||
|
@ -33,6 +43,18 @@ function lingvaTranslate(text, source, target) {
|
||||||
info: res.info,
|
info: res.info,
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
};
|
||||||
|
return pRetry(fetchCall, {
|
||||||
|
retries: 3,
|
||||||
|
onFailedAttempt: (e) => {
|
||||||
|
currentLingvaInstance =
|
||||||
|
(currentLingvaInstance + 1) % LINGVA_INSTANCES.length;
|
||||||
|
console.log(
|
||||||
|
'Retrying translation with another instance',
|
||||||
|
currentLingvaInstance,
|
||||||
|
);
|
||||||
|
},
|
||||||
|
});
|
||||||
// return masto.v1.statuses.translate(id, {
|
// return masto.v1.statuses.translate(id, {
|
||||||
// lang: DEFAULT_LANG,
|
// lang: DEFAULT_LANG,
|
||||||
// });
|
// });
|
||||||
|
@ -66,7 +88,7 @@ function TranslationBlock({
|
||||||
const translate = async () => {
|
const translate = async () => {
|
||||||
setUIState('loading');
|
setUIState('loading');
|
||||||
try {
|
try {
|
||||||
const { content, detectedSourceLanguage, provider, ...props } =
|
const { content, detectedSourceLanguage, provider, error, ...props } =
|
||||||
await onTranslate(text, apiSourceLang.current, targetLang);
|
await onTranslate(text, apiSourceLang.current, targetLang);
|
||||||
if (content) {
|
if (content) {
|
||||||
if (detectedSourceLanguage) {
|
if (detectedSourceLanguage) {
|
||||||
|
|
Loading…
Reference in a new issue