Skip to content

Commit

Permalink
opticode
Browse files Browse the repository at this point in the history
  • Loading branch information
Bistutu committed Mar 2, 2024
1 parent 05127a2 commit 419be45
Showing 1 changed file with 6 additions and 34 deletions.
40 changes: 6 additions & 34 deletions FluentRead.js
Original file line number Diff line number Diff line change
Expand Up @@ -972,14 +972,18 @@ function parseJwt(token) {
// endregion

// region DeepL
function deepL(origin) {
// native 判断是否为本地部署模型
function deepL(origin, native = false) {
return new Promise((resolve, reject) => {
let target_lang = langManager.getTo();
if (target_lang === 'zh-Hans') target_lang = 'zh'; // DeepL 不支持 zh-Hans

// 获取 DeepL API 密钥
let deepLAuthKey = tokenManager.getToken(transModel.deepL);

// 判断是否为本地部署模型(模型所需参数些许不同)
let text = native ? origin : [origin]

// 发起翻译请求
GM_xmlhttpRequest({
method: 'POST',
Expand All @@ -989,7 +993,7 @@ function deepL(origin) {
'Authorization': 'DeepL-Auth-Key ' + deepLAuthKey
},
data: JSON.stringify({
text: [origin], // 确保text是一个数组
text: text, // 确保text是一个数组
target_lang: target_lang,
tag_handling: 'html',
context: url.host + document.title, // 添加上下文辅助
Expand All @@ -1008,38 +1012,6 @@ function deepL(origin) {
});
}

// 本地部署 DeepL 模型(类似但不完全相同,主要体现在 text: origin、[origin])
function deepLFake(origin) {
return new Promise((resolve, reject) => {
let target_lang = langManager.getTo();
if (target_lang === 'zh-Hans') target_lang = 'zh'; // DeepL 不支持 zh-Hans

// 发起翻译请求
GM_xmlhttpRequest({
method: 'POST',
url: "http://127.0.0.1:1188/translate",
headers: {'Content-Type': 'application/json',},
data: JSON.stringify({
text: origin,
target_lang: target_lang,
tag_handling: 'html',
context: url.host + document.title, // 添加上下文辅助
preserve_formatting: true
}),
onload: resp => {
try {
let resultJson = JSON.parse(resp.responseText);
resolve(resultJson.data);
} catch (e) {
reject(resp.responseText);
}
},
onerror: error => reject(error)
});
});
}


// endregion

// region openai
Expand Down

0 comments on commit 419be45

Please sign in to comment.