Skip to content

Commit 9378997

Browse files
patch translator
1 parent fdf5cd0 commit 9378997

File tree

4 files changed

+27
-27
lines changed

4 files changed

+27
-27
lines changed

extras/translator/README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ The AnythingLLM Auto-translator is a way for us to translate our locales but at
44

55
## Getting started
66

7-
- Install Docker [w/Docker Model Runner](https://docs.docker.com/ai/model-runner/)
8-
- `docker pull model mintplexlabs/translategemma4b:Q4_K_M`
9-
- Ensure TCP connections in for Docker Model Runner extension is live.
7+
- Install Ollama [w/Docker Model Runner](https://ollama.com)
8+
- `ollama pull translategemma:4b`
109
- `cd extras/translator && cp .env.example .env`
1110

1211
## Run the script

extras/translator/index.mjs

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import fs from 'fs';
22
import {resources} from '../../frontend/src/locales/resources.js';
33
import "../../server/node_modules/dotenv/lib/main.js";
4-
import DMRModule from '../../server/utils/AiProviders/dockerModelRunner/index.js';
5-
const { DockerModelRunnerLLM, getDockerModels } = DMRModule;
64

75
function getNestedValue(obj, path) {
86
const keys = path.split('.');
@@ -26,16 +24,8 @@ function setNestedValue(obj, path, value) {
2624
}
2725

2826
class Translator {
29-
static modelTag = 'mintplexlabs/translategemma4b:Q4_K_M'
30-
constructor(provider = "dmr") {
31-
switch (provider) {
32-
case "dmr":
33-
this.provider = new DockerModelRunnerLLM(null, Translator.modelTag);
34-
break;
35-
default:
36-
throw new Error(`Unsupported provider: ${provider}`);
37-
}
38-
27+
static modelTag = 'translategemma:4b'
28+
constructor() {
3929
this.localeObj = new Intl.DisplayNames(Object.keys(resources), { type: 'language' });
4030
}
4131

@@ -66,12 +56,6 @@ Produce only the ${targetLanguage} translation, without any additional explanati
6656
${text}`
6757
}
6858

69-
async verifyReady() {
70-
const models = await getDockerModels(process.env.DOCKER_MODEL_RUNNER_BASE_PATH);
71-
if(!models.find(m => m.id.toLowerCase() === Translator.modelTag.toLowerCase())) throw new Error(`Model ${Translator.modelTag} not found. Pull with docker model pull ${Translator.modelTag}`);
72-
return true;
73-
}
74-
7559
/**
7660
* Clean the output text from the model
7761
* Output text: `在助手回复中呈现 HTML 响应。这可以显著提高回复的质量,但也可能带来潜在的安全风险。<|im_end|>`
@@ -84,10 +68,24 @@ ${text}`
8468
}
8569

8670
async translate(text, sourceLangCode, targetLangCode) {
87-
await this.verifyReady();
8871
const prompt = this.buildPrompt(text, sourceLangCode, targetLangCode);
89-
const response = await this.provider.getChatCompletion([{ role: 'user', content: prompt }], { temperature: 0.1 });
90-
return this.cleanOutputText(response.textResponse);
72+
return fetch(`http://localhost:11434/api/chat`, {
73+
method: 'POST',
74+
headers: { 'Content-Type': 'application/json' },
75+
body: JSON.stringify({
76+
model: Translator.modelTag,
77+
messages: [{ role: 'user', content: prompt }],
78+
temperature: 0.1,
79+
stream: false,
80+
}),
81+
})
82+
.then((res) => {
83+
if(!res.ok) throw new Error(`Failed to translate: ${res.statusText}`);
84+
return res.json();
85+
})
86+
.then((data) => {
87+
return this.cleanOutputText(data.message.content);
88+
});
9189
}
9290

9391
writeTranslations(langCode, translations) {
@@ -109,7 +107,7 @@ export default TRANSLATIONS;`
109107

110108

111109
// Deep traverse the english translations and get all the path to any all keys
112-
const translator = new Translator("dmr");
110+
const translator = new Translator();
113111
const englishTranslations = resources.en.common;
114112
const allKeys = [];
115113
function traverseTranslations(translations, parentKey = '') {
@@ -168,6 +166,7 @@ async function translateSingleLanguage(langCode) {
168166
totalTranslations++;
169167
}
170168

169+
if(totalTranslations === 0) return console.log('No translations performed!');
171170
console.log(`--------------------------------`);
172171
console.log(`Translated ${totalTranslations} translations for ${langCode}`);
173172
translator.writeTranslations(langCode, resources[langCode].common);

frontend/src/locales/ar/common.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ const TRANSLATIONS = {
6767
yes: "نعم",
6868
no: "لا",
6969
search: "بحث",
70-
username_requirements: null,
70+
username_requirements:
71+
"يجب أن يتكون اسم المستخدم من 2 إلى 32 حرفًا، ويبدأ بحرف صغير، ويحتوي فقط على حروف وأرقام وعلامات التسطير والنقاط.",
7172
},
7273
settings: {
7374
title: "إعدادات المثيل",

frontend/src/locales/da/common.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ const TRANSLATIONS = {
6969
yes: "Ja",
7070
no: "Nej",
7171
search: "Søg",
72-
username_requirements: null,
72+
username_requirements:
73+
"Brugernavnet skal bestå af 2-32 tegn, starte med et lille bogstav, og kun indeholde små bogstaver, tal, understregninger, bindestreger og punktummer.",
7374
},
7475
settings: {
7576
title: "Instansindstillinger",

0 commit comments

Comments
 (0)