mirror of
https://github.com/HIllya51/LunaTranslator.git
synced 2024-12-29 16:44:13 +08:00
fix
This commit is contained in:
parent
b20e3517f2
commit
09f11f2a32
@ -1,5 +1,5 @@
|
|||||||
from translator.basetranslator import basetrans
|
from translator.basetranslator import basetrans
|
||||||
import json, requests
|
import json, requests, threading
|
||||||
from myutils.config import globalconfig
|
from myutils.config import globalconfig
|
||||||
import websocket, time
|
import websocket, time
|
||||||
from traceback import print_exc
|
from traceback import print_exc
|
||||||
@ -38,28 +38,27 @@ class basetransdev(basetrans):
|
|||||||
#########################################
|
#########################################
|
||||||
def _private_init(self):
|
def _private_init(self):
|
||||||
self._id = 1
|
self._id = 1
|
||||||
|
self.sendrecvlock = threading.Lock()
|
||||||
self._createtarget()
|
self._createtarget()
|
||||||
super()._private_init()
|
super()._private_init()
|
||||||
|
|
||||||
def _SendRequest(self, method, params, ws=None):
|
def _SendRequest(self, method, params, ws=None):
|
||||||
if self.using == False:
|
if self.using == False:
|
||||||
return
|
return
|
||||||
self._id += 1
|
with self.sendrecvlock:
|
||||||
|
self._id += 1
|
||||||
|
|
||||||
if ws is None:
|
if ws is None:
|
||||||
ws = self.ws
|
ws = self.ws
|
||||||
ws.send(json.dumps({"id": self._id, "method": method, "params": params}))
|
ws.send(json.dumps({"id": self._id, "method": method, "params": params}))
|
||||||
res = ws.recv()
|
res = ws.recv()
|
||||||
|
|
||||||
res = json.loads(res)
|
res = json.loads(res)
|
||||||
try:
|
try:
|
||||||
return res["result"]
|
return res["result"]
|
||||||
except:
|
except:
|
||||||
if res["error"]["code"] == -32600:
|
print(res)
|
||||||
self._id += 1
|
raise Exception()
|
||||||
self._SendRequest(method, params, ws)
|
|
||||||
else:
|
|
||||||
raise
|
|
||||||
|
|
||||||
def _createtarget(self):
|
def _createtarget(self):
|
||||||
if self.using == False:
|
if self.using == False:
|
||||||
|
@ -1,44 +1,47 @@
|
|||||||
|
(function () {
|
||||||
|
window.hasdone = false
|
||||||
|
window.thistext = ''
|
||||||
|
if (window.injectedjs) return
|
||||||
|
window.injectedjs = true
|
||||||
|
const originalFetch = window.fetch;
|
||||||
|
window.fetch = function (input, init) {
|
||||||
|
const fetchPromise = originalFetch.apply(this, arguments);
|
||||||
|
if (!%s) return fetchPromise;
|
||||||
|
window.hasdone = false;
|
||||||
|
window.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();
|
||||||
|
|
||||||
const originalFetch = window.fetch;
|
reader.read().then(function processStream({ done, value }) {
|
||||||
var hasdone = true
|
|
||||||
var thistext = ''
|
|
||||||
window.fetch = function (input, init) {
|
|
||||||
const fetchPromise = originalFetch.apply(this, arguments);
|
|
||||||
if (!%s) 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 {
|
|
||||||
%s
|
|
||||||
} catch {
|
|
||||||
|
|
||||||
|
if (done) {
|
||||||
|
hasdone = true;
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
let lines = new TextDecoder('utf-8').decode(value);
|
||||||
return reader.read().then(processStream);
|
lines = lines.split('\n')
|
||||||
}).catch(error => {
|
for (let i = 0; i < lines.length; i++) {
|
||||||
console.error('Error reading stream:', error);
|
let line = lines[i]
|
||||||
});
|
line = line.trim()
|
||||||
}
|
if (line.length == 0) continue;
|
||||||
}).catch(error => {
|
try {
|
||||||
console.error('Fetch error:', error);
|
%s
|
||||||
});
|
} catch {
|
||||||
|
|
||||||
return fetchPromise;
|
}
|
||||||
};
|
}
|
||||||
|
return reader.read().then(processStream);
|
||||||
|
}).catch(error => {
|
||||||
|
console.error('Error reading stream:', error);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}).catch(error => {
|
||||||
|
console.error('Fetch error:', error);
|
||||||
|
});
|
||||||
|
|
||||||
|
return fetchPromise;
|
||||||
|
};
|
||||||
|
})()
|
@ -1,44 +1,47 @@
|
|||||||
const originXHR = XMLHttpRequest
|
(function () {
|
||||||
var hasdone = true
|
window.hasdone = false
|
||||||
var thistext = ''
|
window.thistext = ''
|
||||||
window.XMLHttpRequest = function () {
|
if (window.injectedjs) return
|
||||||
var newxhr = new originXHR()
|
const originXHR = XMLHttpRequest
|
||||||
newxhr.open_ori = newxhr.open
|
window.XMLHttpRequest = function () {
|
||||||
|
var newxhr = new originXHR()
|
||||||
|
newxhr.open_ori = newxhr.open
|
||||||
|
|
||||||
newxhr.open = function () {
|
newxhr.open = function () {
|
||||||
var input = arguments[1]
|
var input = arguments[1]
|
||||||
if (%s) {
|
if (%s) {
|
||||||
hasdone = false;
|
window.hasdone = false;
|
||||||
thistext = ''
|
window.thistext = ''
|
||||||
newxhr.onprogress_ori = newxhr.onprogress
|
newxhr.onprogress_ori = newxhr.onprogress
|
||||||
newxhr.onload_ori = newxhr.onload_ori
|
newxhr.onload_ori = newxhr.onload_ori
|
||||||
newxhr.offset = 0
|
newxhr.offset = 0
|
||||||
newxhr.onload = function () {
|
newxhr.onload = function () {
|
||||||
hasdone = true;
|
hasdone = true;
|
||||||
if (newxhr.onload_ori)
|
if (newxhr.onload_ori)
|
||||||
return newxhr.onload_ori.apply(this, arguments);
|
return newxhr.onload_ori.apply(this, arguments);
|
||||||
};
|
};
|
||||||
newxhr.onprogress = function (event) {
|
newxhr.onprogress = function (event) {
|
||||||
var current = newxhr.responseText
|
var current = newxhr.responseText
|
||||||
var lines = current.substring(newxhr.offset)
|
var lines = current.substring(newxhr.offset)
|
||||||
lines = lines.split('\n')
|
lines = lines.split('\n')
|
||||||
for (let i = 0; i < lines.length; i++) {
|
for (let i = 0; i < lines.length; i++) {
|
||||||
let line = lines[i]
|
let line = lines[i]
|
||||||
line = line.trim()
|
line = line.trim()
|
||||||
if (line.length == 0) continue;
|
if (line.length == 0) continue;
|
||||||
try {
|
try {
|
||||||
%s
|
%s
|
||||||
} catch {
|
} catch {
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
newxhr.offset = current.length;
|
||||||
newxhr.offset = current.length;
|
if (newxhr.onprogress_ori)
|
||||||
if (newxhr.onprogress_ori)
|
return newxhr.onprogress_ori.apply(this, arguments);
|
||||||
return newxhr.onprogress_ori.apply(this, arguments);
|
};
|
||||||
};
|
}
|
||||||
}
|
return newxhr.open_ori.apply(this, arguments);
|
||||||
return newxhr.open_ori.apply(this, arguments);
|
};
|
||||||
};
|
|
||||||
|
|
||||||
return newxhr
|
return newxhr
|
||||||
}
|
}
|
||||||
|
})()
|
@ -1,76 +1,19 @@
|
|||||||
from translator.basetranslator_dev import basetransdev
|
from translator.dev_llm_common import commonllmdev
|
||||||
import time, os
|
|
||||||
|
|
||||||
|
|
||||||
class TS(basetransdev):
|
class TS(commonllmdev):
|
||||||
target_url = "https://chatglm.cn/main/alltoolsdetail"
|
target_url = "https://chatglm.cn/main/alltoolsdetail"
|
||||||
|
jsfile = "commonhookfetchstream.js"
|
||||||
|
function1 = 'input.endsWith("assistant/stream")'
|
||||||
|
function2 = """const chunk = JSON.parse(line.substring(6));
|
||||||
|
thistext = chunk.parts[0].content[0].text"""
|
||||||
|
|
||||||
def langmap(self):
|
def dotranslate(self, content):
|
||||||
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":
|
|
||||||
print(self.Runtime_evaluate("window.injectedjs")["result"]["type"])
|
|
||||||
return
|
|
||||||
with open(
|
|
||||||
os.path.join(os.path.dirname(__file__), "commonhookfetchstream.js"),
|
|
||||||
"r",
|
|
||||||
encoding="utf8",
|
|
||||||
) as ff:
|
|
||||||
js = ff.read() % (
|
|
||||||
'input.endsWith("assistant/stream")',
|
|
||||||
"""const chunk = JSON.parse(line.substring(6));
|
|
||||||
thistext = chunk.parts[0].content[0].text""",
|
|
||||||
)
|
|
||||||
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(
|
self.Runtime_evaluate(
|
||||||
'document.querySelector("#search-input-box > div.input-wrap.flex.flex-x-between.flex-y-center > div.input-box-inner > textarea").focus()'
|
'document.querySelector("#search-input-box > div.input-wrap.flex.flex-x-between.flex-y-center > div.input-box-inner > textarea").focus()'
|
||||||
)
|
)
|
||||||
self.send_keys(content)
|
self.send_keys(content)
|
||||||
|
|
||||||
self.Runtime_evaluate("""document.querySelector("#search-input-box > div.input-wrap.flex.flex-x-between.flex-y-center > div.enter.m-three-row > img").click()""")
|
self.Runtime_evaluate(
|
||||||
if self.config["usingstream"]:
|
"""document.querySelector("#search-input-box > div.input-wrap.flex.flex-x-between.flex-y-center > div.enter.m-three-row > img").click()"""
|
||||||
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"]
|
|
||||||
|
@ -1,57 +1,14 @@
|
|||||||
from translator.basetranslator_dev import basetransdev
|
from translator.dev_llm_common import commonllmdev
|
||||||
import time, os
|
|
||||||
|
|
||||||
|
|
||||||
class TS(basetransdev):
|
class TS(commonllmdev):
|
||||||
target_url = "https://chatgpt.com/"
|
target_url = "https://chatgpt.com/"
|
||||||
|
jsfile = "commonhookfetchstream.js"
|
||||||
|
function1 = 'input.includes("conversation")'
|
||||||
|
function2 = r"""const chunk = JSON.parse(line.substring(6));
|
||||||
|
thistext = chunk.message.content.parts[0]"""
|
||||||
|
|
||||||
def langmap(self):
|
def dotranslate(self, content):
|
||||||
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.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")
|
|
||||||
|
|
||||||
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(
|
self.Runtime_evaluate(
|
||||||
'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(
|
'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
|
content
|
||||||
@ -60,18 +17,3 @@ class TS(basetransdev):
|
|||||||
self.Runtime_evaluate(
|
self.Runtime_evaluate(
|
||||||
r"""document.querySelector("#__next > div.relative.z-0.flex.h-full.w-full.overflow-hidden > div > main > div.flex.h-full.flex-col.focus-visible\\:outline-0 > div.md\\:pt-0.dark\\:border-white\\/20.md\\:border-transparent.md\\:dark\\:border-transparent.w-full > div.text-base.px-3.md\\:px-4.m-auto.md\\:px-5.lg\\:px-1.xl\\:px-5 > div > form > div > div.flex.w-full.items-center > div > div > button").click()"""
|
r"""document.querySelector("#__next > div.relative.z-0.flex.h-full.w-full.overflow-hidden > div > main > div.flex.h-full.flex-col.focus-visible\\:outline-0 > div.md\\:pt-0.dark\\:border-white\\/20.md\\:border-transparent.md\\:dark\\:border-transparent.w-full > div.text-base.px-3.md\\:px-4.m-auto.md\\:px-5.lg\\:px-1.xl\\:px-5 > div > form > div > div.flex.w-full.items-center > div > div > 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"]
|
|
||||||
|
@ -1,73 +1,15 @@
|
|||||||
from translator.basetranslator_dev import basetransdev
|
from translator.dev_llm_common import commonllmdev
|
||||||
import time, os
|
|
||||||
|
|
||||||
|
|
||||||
class TS(basetransdev):
|
class TS(commonllmdev):
|
||||||
target_url = "https://chat.deepseek.com/"
|
target_url = "https://chat.deepseek.com/"
|
||||||
|
jsfile = "commonhookxhrstream.js"
|
||||||
def langmap(self):
|
function1 = "input.endsWith('v0/chat/completions')"
|
||||||
return {
|
function2 = r"""const chunk = JSON.parse(line.substring(6));
|
||||||
"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))
|
if(!!(chunk.choices[0].delta.content))
|
||||||
thistext += 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):
|
def dotranslate(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.Runtime_evaluate('document.querySelector("#chat-input").click()')
|
||||||
self.send_keys(content)
|
self.send_keys(content)
|
||||||
self.Runtime_evaluate("document.getElementsByClassName('_89d4d19')[1].click()")
|
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"]
|
|
||||||
|
46
LunaTranslator/LunaTranslator/translator/dev_llm_common.py
Normal file
46
LunaTranslator/LunaTranslator/translator/dev_llm_common.py
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
from translator.basetranslator_dev import basetransdev
|
||||||
|
import time, os
|
||||||
|
|
||||||
|
|
||||||
|
class commonllmdev(basetransdev):
|
||||||
|
|
||||||
|
def injectjs(self):
|
||||||
|
with open(
|
||||||
|
os.path.join(os.path.dirname(__file__), self.jsfile),
|
||||||
|
"r",
|
||||||
|
encoding="utf8",
|
||||||
|
) as ff:
|
||||||
|
js = ff.read() % (
|
||||||
|
self.function1,
|
||||||
|
self.function2,
|
||||||
|
)
|
||||||
|
self.Runtime_evaluate(js)
|
||||||
|
|
||||||
|
def dotranslate(self, content): ...
|
||||||
|
|
||||||
|
def translate(self, content):
|
||||||
|
self.injectjs()
|
||||||
|
|
||||||
|
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.dotranslate(content)
|
||||||
|
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"]
|
@ -1,77 +1,19 @@
|
|||||||
from translator.basetranslator_dev import basetransdev
|
from translator.dev_llm_common import commonllmdev
|
||||||
import time, os
|
|
||||||
|
|
||||||
|
|
||||||
class TS(basetransdev):
|
class TS(commonllmdev):
|
||||||
target_url = "https://kimi.moonshot.cn/"
|
target_url = "https://kimi.moonshot.cn/"
|
||||||
|
jsfile = "commonhookfetchstream.js"
|
||||||
def langmap(self):
|
function1 = 'input.endsWith("completion/stream")'
|
||||||
return {
|
function2 = """const chunk = JSON.parse(line.substring(6));
|
||||||
"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("completion/stream")',
|
|
||||||
"""const chunk = JSON.parse(line.substring(6));
|
|
||||||
if(chunk.event!='cmpl')continue;
|
if(chunk.event!='cmpl')continue;
|
||||||
if(chunk.text)
|
if(chunk.text)
|
||||||
thistext += chunk.text""",
|
thistext += chunk.text"""
|
||||||
)
|
|
||||||
self.Runtime_evaluate(js)
|
|
||||||
self.Runtime_evaluate("window.injectedjs=true")
|
|
||||||
|
|
||||||
def translate(self, content):
|
def dotranslate(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(
|
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()'
|
'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.send_keys(content)
|
||||||
self.wait_for_result('document.querySelector("#send-button").disabled', True)
|
self.wait_for_result('document.querySelector("#send-button").disabled', True)
|
||||||
self.Runtime_evaluate("""document.querySelector("#send-button").click()""")
|
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"]
|
|
||||||
|
@ -1,74 +1,16 @@
|
|||||||
from translator.basetranslator_dev import basetransdev
|
from translator.dev_llm_common import commonllmdev
|
||||||
import time, os
|
|
||||||
|
|
||||||
|
|
||||||
class TS(basetransdev):
|
class TS(commonllmdev):
|
||||||
target_url = "https://tongyi.aliyun.com/qianwen"
|
target_url = "https://tongyi.aliyun.com/qianwen"
|
||||||
|
jsfile = "commonhookfetchstream.js"
|
||||||
|
function1 = 'input.endsWith("dialog/conversation")'
|
||||||
|
function2 = r"""const chunk = JSON.parse(line.substring(6));
|
||||||
|
thistext = chunk.contents[0].content"""
|
||||||
|
|
||||||
def langmap(self):
|
def dotranslate(self, content):
|
||||||
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].focus()')
|
self.Runtime_evaluate('document.getElementsByTagName("textarea")[0].focus()')
|
||||||
self.send_keys(content)
|
self.send_keys(content)
|
||||||
self.Runtime_evaluate(
|
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()"""
|
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"]
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user