diff --git a/LunaTranslator/LunaTranslator/translator/commonhookxhrstream.js b/LunaTranslator/LunaTranslator/translator/commonhookxhrstream.js new file mode 100644 index 00000000..af4820d0 --- /dev/null +++ b/LunaTranslator/LunaTranslator/translator/commonhookxhrstream.js @@ -0,0 +1,36 @@ +const originXHR = XMLHttpRequest + +window.XMLHttpRequest = function () { + var newxhr = new originXHR() + newxhr.open_ori = newxhr.open + + newxhr.open = function () { + var input=arguments[1] + if (%s) { + + newxhr.onprogress_ori = newxhr.onprogress + newxhr.offset=0 + newxhr.onprogress = function (event) { + var current=newxhr.responseText + var lines=current.substring(newxhr.offset) + lines = lines.split('\n') + for (let i = 0; i < lines.length; i++) { + let line = lines[i] + line = line.trim() + if (line.length == 0) continue; + try { + %s + } catch { + + } + } + newxhr.offset=current.length; + if(newxhr.onprogress_ori) + return newxhr.onprogress_ori.apply(this, arguments); + }; + } + return newxhr.open_ori.apply(this, arguments); + }; + + return newxhr +} \ No newline at end of file diff --git a/LunaTranslator/LunaTranslator/translator/dev_deepseek.py b/LunaTranslator/LunaTranslator/translator/dev_deepseek.py new file mode 100644 index 00000000..4a37f703 --- /dev/null +++ b/LunaTranslator/LunaTranslator/translator/dev_deepseek.py @@ -0,0 +1,73 @@ +from translator.basetranslator_dev import basetransdev +import time, os + + +class TS(basetransdev): + target_url = "https://chat.deepseek.com/" + + def langmap(self): + return { + "zh": "Simplified Chinese", + "ja": "Japanese", + "en": "English", + "ru": "Russian", + "es": "Spanish", + "ko": "Korean", + "fr": "French", + "cht": "Traditional Chinese", + "vi": "Vietnamese", + "tr": "Turkish", + "pl": "Polish", + "uk": "Ukrainian", + "it": "Italian", + "ar": "Arabic", + "th": "Thai", + } + + def injectjs(self): + if self.Runtime_evaluate("window.injectedjs")["result"]["type"] != "undefined": + return + with open( + os.path.join(os.path.dirname(__file__), "commonhookxhrstream.js"), + "r", + encoding="utf8", + ) as ff: + js = ff.read() % ( + "input.endsWith('v0/chat/completions')", + r"""const chunk = JSON.parse(line.substring(6)); + if(!!(chunk.choices[0].delta.content)) + thistext += chunk.choices[0].delta.content""", + ) + self.Runtime_evaluate(js) + self.Runtime_evaluate("window.injectedjs=true") + + def translate(self, content): + self.injectjs() + + self.Runtime_evaluate("hasdone=false") + self.Runtime_evaluate('thistext=""') + if self.config["use_custom_prompt"]: + prompt = self.config["custom_prompt"] + else: + prompt = "You are a translator. Please help me translate the following {} text into {}, and you should only tell me the translation.\n".format( + self.srclang, self.tgtlang + ) + content = prompt + content + self.Runtime_evaluate('document.querySelector("#chat-input").click()') + self.send_keys(content) + self.Runtime_evaluate("document.getElementsByClassName('_89d4d19')[1].click()") + if self.config["usingstream"]: + curr = "" + while not self.Runtime_evaluate("hasdone")["result"]["value"]: + time.sleep(0.1) + thistext = self.Runtime_evaluate("thistext")["result"]["value"] + if thistext.startswith(curr): + yield thistext[len(curr) :] + else: + yield thistext + curr = thistext + else: + while not self.Runtime_evaluate("hasdone")["result"]["value"]: + time.sleep(0.1) + continue + yield self.Runtime_evaluate("thistext")["result"]["value"] diff --git a/LunaTranslator/files/defaultconfig/config.json b/LunaTranslator/files/defaultconfig/config.json index e7144eeb..00230ac4 100644 --- a/LunaTranslator/files/defaultconfig/config.json +++ b/LunaTranslator/files/defaultconfig/config.json @@ -1596,6 +1596,13 @@ "name": "chatglm", "is_gpt_like": true }, + "dev_deepseek": { + "use": false, + "color": "blue", + "type": "dev", + "name": "deepseek", + "is_gpt_like": true + }, "deepl_dev": { "use": false, "color": "blue", diff --git a/LunaTranslator/files/defaultconfig/translatorsetting.json b/LunaTranslator/files/defaultconfig/translatorsetting.json index d036acf6..a98f67b7 100644 --- a/LunaTranslator/files/defaultconfig/translatorsetting.json +++ b/LunaTranslator/files/defaultconfig/translatorsetting.json @@ -820,6 +820,28 @@ } } }, + "dev_deepseek": { + "args": { + "usingstream": true, + "use_custom_prompt": false, + "custom_prompt": "" + }, + "argstype": { + "usingstream": { + "name": "流式输出", + "type": "switch" + }, + "custom_prompt": { + "name": "自定义promt", + "type": "multiline", + "refswitch": "use_custom_prompt" + }, + "use_custom_prompt": { + "type": "switch_ref", + "name": "使用自定义promt" + } + } + }, "gemini": { "args": { "注册网址": "https://ai.google.dev/tutorials/python_quickstart",