diff --git a/LunaTranslator/LunaTranslator/gui/setting_translate.py b/LunaTranslator/LunaTranslator/gui/setting_translate.py index 94518ee3..a9840d2d 100644 --- a/LunaTranslator/LunaTranslator/gui/setting_translate.py +++ b/LunaTranslator/LunaTranslator/gui/setting_translate.py @@ -380,7 +380,9 @@ def initsome2(self, l, label=None, btnplus=None): type="grid", title="大模型", grid=is_gpt_likes, - internallayoutname="damoxinggridinternal" + btnplus, + internallayoutname=( + ("damoxinggridinternal" + btnplus) if btnplus else None + ), parent=self, ), 0, @@ -546,20 +548,19 @@ def setTabTwo_lazy(self, basel): D_getspinbox(0, 65535, globalconfig, "debugport"), "", ], + [(functools.partial(createstatuslabel, self), 0)], ), ), 0, "group", ) ], - [(functools.partial(createstatuslabel, self), 16)], - [], ] lixians, pre, mianfei, develop, shoufei = splittranslatortypes() offlinegrid = initsome2(self, lixians, btnplus="offline") onlinegrid = initsome11(self, mianfei) - developgrid += initsome11(self, develop) + developgrid += initsome2(self, develop) online_reg_grid += initsome2(self, shoufei, btnplus="api") pretransgrid += initsome11(self, pre) vw, vl = getvboxwidget() diff --git a/LunaTranslator/LunaTranslator/translator/basetranslator_dev.py b/LunaTranslator/LunaTranslator/translator/basetranslator_dev.py index 2f7cbf0e..dcbc0025 100644 --- a/LunaTranslator/LunaTranslator/translator/basetranslator_dev.py +++ b/LunaTranslator/LunaTranslator/translator/basetranslator_dev.py @@ -25,7 +25,6 @@ class basetransdev(basetrans): state = self.Runtime_evaluate(expression) try: value = state["result"]["value"] - print(value) if multi: if not (value in badresult): return value diff --git a/LunaTranslator/LunaTranslator/translator/dev_moonshot.js b/LunaTranslator/LunaTranslator/translator/dev_moonshot.js new file mode 100644 index 00000000..17af4443 --- /dev/null +++ b/LunaTranslator/LunaTranslator/translator/dev_moonshot.js @@ -0,0 +1,46 @@ + +const originalFetch = window.fetch; +var hasdone = true +var thistext = '' +window.fetch = function (input, init) { + const fetchPromise = originalFetch.apply(this, arguments); + if (!input.endsWith("completion/stream")) return fetchPromise; + hasdone = false; + thistext = '' + fetchPromise.then(response => { + const clone = response.clone() + const contentType = clone.headers.get('content-type'); + if (contentType && contentType.includes('text/event-stream')) { + const reader = clone.body.getReader(); + + reader.read().then(function processStream({ done, value }) { + + if (done) { + hasdone = true; + return; + } + let lines = new TextDecoder('utf-8').decode(value); + 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 { + const chunk = JSON.parse(line.substring(6)); + if(chunk.event!='cmpl')continue; + console.log(chunk) + if(chunk.text) + thistext += chunk.text + } catch { } + } + return reader.read().then(processStream); + }).catch(error => { + console.error('Error reading stream:', error); + }); + } + }).catch(error => { + console.error('Fetch error:', error); + }); + + return fetchPromise; +}; diff --git a/LunaTranslator/LunaTranslator/translator/dev_moonshot.py b/LunaTranslator/LunaTranslator/translator/dev_moonshot.py new file mode 100644 index 00000000..85b1e3db --- /dev/null +++ b/LunaTranslator/LunaTranslator/translator/dev_moonshot.py @@ -0,0 +1,71 @@ +from translator.basetranslator_dev import basetransdev +import time, os + + +class TS(basetransdev): + target_url = "https://kimi.moonshot.cn/" + + 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__), "dev_moonshot.js"), + "r", + encoding="utf8", + ) as ff: + js = ff.read() + 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("#root > div.MuiBox-root.css-qmryj6 > div > div.layoutContent___NvxZ_ > div.MuiBox-root.css-ar9pyi > div > div > div > div.MuiBox-root.css-8atqhb > div.chatInput___bMC0h.matchHomePageLayout___WewPT > div > div > div.editor___KShcc.editor___DSPKC.matchHomePageLayout___XTlpC > div")? document.querySelector("#root > div.MuiBox-root.css-qmryj6 > div > div.layoutContent___NvxZ_ > div.MuiBox-root.css-ar9pyi > div > div > div > div.MuiBox-root.css-8atqhb > div.chatInput___bMC0h.matchHomePageLayout___WewPT > div > div > div.editor___KShcc.editor___DSPKC.matchHomePageLayout___XTlpC > div").click() : document.querySelector("#root > div.MuiBox-root.css-qmryj6 > div > div.layoutContent___NvxZ_ > div.MuiBox-root.css-ar9pyi > div > div > div > div > div.chatPageLayoutRightBoxLeft___taL5l > div.content___inD6V > div > div.chatBottom___jS9Jd.MuiBox-root.css-jdjpte > div.chatInput___bMC0h.matchHomePageLayout___WewPT > div > div > div.editor___KShcc.editor___DSPKC.matchHomePageLayout___XTlpC > div.editorContentEditable___FZJd9").click()' + ) + self.send_keys(content) + self.wait_for_result('document.querySelector("#send-button").disabled', True) + self.Runtime_evaluate("""document.querySelector("#send-button").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 24780cdc..b70b9630 100644 --- a/LunaTranslator/files/defaultconfig/config.json +++ b/LunaTranslator/files/defaultconfig/config.json @@ -1575,6 +1575,13 @@ "name": "chatgpt", "is_gpt_like": true }, + "dev_moonshot": { + "use": false, + "color": "blue", + "type": "dev", + "name": "moonshot", + "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 ddaf27d6..4f51b269 100644 --- a/LunaTranslator/files/defaultconfig/translatorsetting.json +++ b/LunaTranslator/files/defaultconfig/translatorsetting.json @@ -754,6 +754,28 @@ } } }, + "dev_moonshot": { + "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",