From a83c9ff74532e3c1fdab22b906368f879add85d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=81=8D=E5=85=AE=E6=83=9A=E5=85=AE?= <101191390+HIllya51@users.noreply.github.com> Date: Wed, 24 Jul 2024 17:43:48 +0800 Subject: [PATCH] dev --- ...ev_chatgpt.js => commonhookfetchstream.js} | 6 +- .../LunaTranslator/translator/dev_chatgpt.py | 10 ++- .../LunaTranslator/translator/dev_moonshot.py | 10 ++- .../LunaTranslator/translator/dev_qwen.py | 74 +++++++++++++++++++ .../files/defaultconfig/config.json | 7 ++ .../defaultconfig/translatorsetting.json | 22 ++++++ 6 files changed, 120 insertions(+), 9 deletions(-) rename LunaTranslator/LunaTranslator/translator/{dev_chatgpt.js => commonhookfetchstream.js} (82%) create mode 100644 LunaTranslator/LunaTranslator/translator/dev_qwen.py diff --git a/LunaTranslator/LunaTranslator/translator/dev_chatgpt.js b/LunaTranslator/LunaTranslator/translator/commonhookfetchstream.js similarity index 82% rename from LunaTranslator/LunaTranslator/translator/dev_chatgpt.js rename to LunaTranslator/LunaTranslator/translator/commonhookfetchstream.js index 79e8b067..10f38a63 100644 --- a/LunaTranslator/LunaTranslator/translator/dev_chatgpt.js +++ b/LunaTranslator/LunaTranslator/translator/commonhookfetchstream.js @@ -4,7 +4,7 @@ var hasdone = true var thistext = '' window.fetch = function (input, init) { const fetchPromise = originalFetch.apply(this, arguments); - if (!input.includes("conversation")) return fetchPromise; + if (!%s) return fetchPromise; hasdone = false; thistext = '' fetchPromise.then(response => { @@ -26,9 +26,7 @@ window.fetch = function (input, init) { line = line.trim() if (line.length == 0) continue; try { - const chunk = JSON.parse(line.substring(6)); - console.log("Chunk received:", chunk.message.content.parts[0]); - thistext = chunk.message.content.parts[0] + %s } catch { } diff --git a/LunaTranslator/LunaTranslator/translator/dev_chatgpt.py b/LunaTranslator/LunaTranslator/translator/dev_chatgpt.py index 13cc5bc8..3f137869 100644 --- a/LunaTranslator/LunaTranslator/translator/dev_chatgpt.py +++ b/LunaTranslator/LunaTranslator/translator/dev_chatgpt.py @@ -28,11 +28,15 @@ class TS(basetransdev): if self.Runtime_evaluate("window.injectedjs")["result"]["type"] != "undefined": return with open( - os.path.join(os.path.dirname(__file__), "dev_chatgpt.js"), + os.path.join(os.path.dirname(__file__), "commonhookfetchstream.js"), "r", encoding="utf8", ) as ff: - js = ff.read() + js = ff.read() % ( + 'input.includes("conversation")', + r"""const chunk = JSON.parse(line.substring(6)); + thistext = chunk.message.content.parts[0]""", + ) self.Runtime_evaluate(js) self.Runtime_evaluate("window.injectedjs=true") @@ -49,7 +53,7 @@ class TS(basetransdev): ) content = prompt + content self.Runtime_evaluate( - 'textarea=document.querySelector("#prompt-textarea");textarea.value="";event = new Event("input", {{bubbles: true, cancelable: true }});textarea.dispatchEvent(event);textarea=document.querySelector("textarea");textarea.value=`{}`;event = new Event("input", {{bubbles: true, cancelable: true }});textarea.dispatchEvent(event);'.format( + 'textarea=document.querySelector("#prompt-textarea");textarea.value="";event = new Event("input", {{bubbles: true, cancelable: true }});textarea.dispatchEvent(event);textarea.value=`{}`;event = new Event("input", {{bubbles: true, cancelable: true }});textarea.dispatchEvent(event);'.format( content ) ) diff --git a/LunaTranslator/LunaTranslator/translator/dev_moonshot.py b/LunaTranslator/LunaTranslator/translator/dev_moonshot.py index 85b1e3db..e65fdfed 100644 --- a/LunaTranslator/LunaTranslator/translator/dev_moonshot.py +++ b/LunaTranslator/LunaTranslator/translator/dev_moonshot.py @@ -28,11 +28,17 @@ class TS(basetransdev): if self.Runtime_evaluate("window.injectedjs")["result"]["type"] != "undefined": return with open( - os.path.join(os.path.dirname(__file__), "dev_moonshot.js"), + os.path.join(os.path.dirname(__file__), "commonhookfetchstream.js"), "r", encoding="utf8", ) as ff: - js = ff.read() + js = ff.read() % ( + 'input.endsWith("completion/stream")', + """const chunk = JSON.parse(line.substring(6)); + if(chunk.event!='cmpl')continue; + if(chunk.text) + thistext += chunk.text""", + ) self.Runtime_evaluate(js) self.Runtime_evaluate("window.injectedjs=true") diff --git a/LunaTranslator/LunaTranslator/translator/dev_qwen.py b/LunaTranslator/LunaTranslator/translator/dev_qwen.py new file mode 100644 index 00000000..51a209ad --- /dev/null +++ b/LunaTranslator/LunaTranslator/translator/dev_qwen.py @@ -0,0 +1,74 @@ +from translator.basetranslator_dev import basetransdev +import time, os + + +class TS(basetransdev): + target_url = "https://tongyi.aliyun.com/qianwen" + + 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__), "commonhookfetchstream.js"), + "r", + encoding="utf8", + ) as ff: + js = ff.read() % ( + 'input.endsWith("dialog/conversation")', + r"""const chunk = JSON.parse(line.substring(6)); + thistext = chunk.contents[0].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.getElementsByTagName("textarea")[0].click()') + self.send_keys(content) + self.Runtime_evaluate( + r"""document.querySelector("#tongyiPageLayout > div.sc-fQpRED.jsoEZg > div > div.sc-hNGPaV.erDcgy.pageContentWrap--AovzQ5wq > div > div.inputField--PE5FhWzd > div > div.chatInput--eJzBH8LP > div.operateBtn--zFx6rSR0").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 b70b9630..262fd65d 100644 --- a/LunaTranslator/files/defaultconfig/config.json +++ b/LunaTranslator/files/defaultconfig/config.json @@ -1582,6 +1582,13 @@ "name": "moonshot", "is_gpt_like": true }, + "dev_qwen": { + "use": false, + "color": "blue", + "type": "dev", + "name": "qianwen", + "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 4f51b269..ba5fc3ec 100644 --- a/LunaTranslator/files/defaultconfig/translatorsetting.json +++ b/LunaTranslator/files/defaultconfig/translatorsetting.json @@ -776,6 +776,28 @@ } } }, + "dev_qwen": { + "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",