Merge pull request #1771 from SillyTavern/lingva

Lingva
This commit is contained in:
Cohee 2024-01-31 22:41:42 +02:00 committed by GitHub
commit 62b6f9782d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 73 additions and 1 deletions

View File

@ -139,7 +139,7 @@ const languageCodes = {
};
const KEY_REQUIRED = ['deepl', 'libre'];
const LOCAL_URL = ['libre', 'oneringtranslator', 'deeplx'];
const LOCAL_URL = ['libre', 'oneringtranslator', 'deeplx', 'lingva'];
function showKeysButton() {
const providerRequiresKey = KEY_REQUIRED.includes(extension_settings.translate.provider);
@ -249,6 +249,27 @@ async function translateProviderGoogle(text, lang) {
throw new Error(response.statusText);
}
/**
* Translates text using an instance of the Lingva Translate
* @param {string} text Text to translate
* @param {string} lang Target language code
* @returns {Promise<string>} Translated text
*/
async function translateProviderLingva(text, lang) {
const response = await fetch('/api/translate/lingva', {
method: 'POST',
headers: getRequestHeaders(),
body: JSON.stringify({ text: text, lang: lang }),
});
if (response.ok) {
const result = await response.text();
return result;
}
throw new Error(response.statusText);
}
/**
* Translates text using the DeepL API
* @param {string} text Text to translate
@ -355,6 +376,8 @@ async function translate(text, lang) {
return await translateProviderLibre(text, lang);
case 'google':
return await chunkedTranslate(text, lang, translateProviderGoogle, 5000);
case 'lingva':
return await chunkedTranslate(text, lang, translateProviderLingva, 5000);
case 'deepl':
return await translateProviderDeepl(text, lang);
case 'deeplx':
@ -507,6 +530,7 @@ jQuery(() => {
<select id="translation_provider" name="provider" class="margin0">
<option value="libre">Libre</option>
<option value="google">Google</option>
<option value="lingva">Lingva</option>
<option value="deepl">DeepL</option>
<option value="deeplx">DeepLX</option>
<option value="bing">Bing</option>
@ -569,6 +593,7 @@ jQuery(() => {
const optionText = $('#translation_provider option:selected').text();
const exampleURLs = {
'libre': 'http://127.0.0.1:5000/translate',
'lingva': 'https://lingva.ml/api/v1',
'oneringtranslator': 'http://127.0.0.1:4990/translate',
'deeplx': 'http://127.0.0.1:1188/translate',
};

View File

@ -17,6 +17,7 @@ const SECRET_KEYS = {
DEEPL: 'deepl',
LIBRE: 'libre',
LIBRE_URL: 'libre_url',
LINGVA_URL: 'lingva_url',
OPENROUTER: 'api_key_openrouter',
SCALE: 'api_key_scale',
AI21: 'api_key_ai21',

View File

@ -102,6 +102,52 @@ router.post('/google', jsonParser, async (request, response) => {
}
});
router.post('/lingva', jsonParser, async (request, response) => {
try {
const baseUrl = readSecret(SECRET_KEYS.LINGVA_URL);
if (!baseUrl) {
console.log('Lingva URL is not configured.');
return response.sendStatus(400);
}
const text = request.body.text;
const lang = request.body.lang;
if (!text || !lang) {
return response.sendStatus(400);
}
console.log('Input text: ' + text);
const url = `${baseUrl}/auto/${lang}/${encodeURIComponent(text)}`;
https.get(url, (resp) => {
let data = '';
resp.on('data', (chunk) => {
data += chunk;
});
resp.on('end', () => {
try {
const result = JSON.parse(data);
console.log('Translated text: ' + result.translation);
return response.send(result.translation);
} catch (error) {
console.log('Translation error', error);
return response.sendStatus(500);
}
});
}).on('error', (err) => {
console.log('Translation error: ' + err.message);
return response.sendStatus(500);
});
} catch (error) {
console.log('Translation error', error);
return response.sendStatus(500);
}
});
router.post('/deepl', jsonParser, async (request, response) => {
const key = readSecret(SECRET_KEYS.DEEPL);