diff --git a/LunaTranslator/LunaTranslator/LunaTranslator.py b/LunaTranslator/LunaTranslator/LunaTranslator.py index 59709328..2431859c 100644 --- a/LunaTranslator/LunaTranslator/LunaTranslator.py +++ b/LunaTranslator/LunaTranslator/LunaTranslator.py @@ -41,9 +41,9 @@ class MAINUI() : def __init__(self) -> None: super().__init__() self.lasttranslatorindex=0 - self.usefultranslators=0 self.translators={} self.cishus={} + self.iterresstatus={} self.reader=None self.textsource_p=None self.currentmd5='0' @@ -151,7 +151,7 @@ class MAINUI() : self.currentsignature=time.time() if type(text)==str: if text.startswith(''): - self.translation_ui.displayres.emit('',globalconfig['rawtextcolor'],text[len(''):],onlytrans) + self.translation_ui.displayres.emit('',globalconfig['rawtextcolor'],text[len(''):],onlytrans,[]) self.currenttext=text self.currentread=text return @@ -245,7 +245,7 @@ class MAINUI() : skip=is_auto_run and (len(text_solved)globalconfig['maxlength'] ) self.premtalready=['premt'] - self.usefultranslators=len(self.translators) + self.usefultranslators=list(self.translators.keys()) if 'premt' in self.translators: try: res=self.translators['premt'].translate(text_solved) @@ -255,7 +255,7 @@ class MAINUI() : _colork=k else: _colork='premt' - self.GetTranslationCallback(onlytrans,_colork,self.currentsignature,optimization_params,_showrawfunction,_showrawfunction_sig,text,res[k],embedcallback) + self.GetTranslationCallback(onlytrans,_colork,self.currentsignature,optimization_params,_showrawfunction,_showrawfunction_sig,text,res[k],embedcallback,False) except: print_exc() @@ -270,6 +270,7 @@ class MAINUI() : keys=list(self.translators.keys())+list(self.translators.keys()) keys=keys[self.lasttranslatorindex:self.lasttranslatorindex+_len] #print(keys,usenum,self.lasttranslatorindex) + self.iterresstatus.clear() for engine in keys: if engine not in self.premtalready: self.translators[engine].gettask((partial(self.GetTranslationCallback,onlytrans,engine,self.currentsignature, optimization_params,_showrawfunction,_showrawfunction_sig,text),text,text_solved,skip,embedcallback,is_auto_run,hira)) @@ -279,19 +280,21 @@ class MAINUI() : break - def GetTranslationCallback(self,onlytrans,classname,currentsignature,optimization_params,_showrawfunction,_showrawfunction_sig,contentraw,res,embedcallback): - self.usefultranslators-=1 + def GetTranslationCallback(self,onlytrans,classname,currentsignature,optimization_params,_showrawfunction,_showrawfunction_sig,contentraw,res,embedcallback,is_iter_res): + + if classname in self.usefultranslators: + self.usefultranslators.remove(classname) if embedcallback is None and currentsignature!=self.currentsignature:return - embedtrans=self.GetTranslationCallback_(onlytrans,classname,currentsignature,optimization_params,_showrawfunction,_showrawfunction_sig,contentraw,res) + embedtrans=self.GetTranslationCallback_(onlytrans,classname,currentsignature,optimization_params,_showrawfunction,_showrawfunction_sig,contentraw,res,is_iter_res) - if embedtrans is None and self.usefultranslators==0: + if embedtrans is None and len(self.usefultranslators)==0: embedtrans='' if embedcallback and embedtrans is not None: embedcallback(embedtrans) - def GetTranslationCallback_(self,onlytrans,classname,currentsignature,optimization_params,_showrawfunction,_showrawfunction_sig,contentraw,res): + def GetTranslationCallback_(self,onlytrans,classname,currentsignature,optimization_params,_showrawfunction,_showrawfunction_sig,contentraw,res,is_iter_res): if type(res)==str: @@ -313,7 +316,14 @@ class MAINUI() : _showrawfunction() if currentsignature==self.currentsignature and globalconfig['showfanyi']: - self.translation_ui.displayres.emit(globalconfig['fanyi'][classname]['name'],globalconfig['fanyi'][classname]['color'],res,onlytrans) + if is_iter_res: + if classname not in self.iterresstatus: + self.iterresstatus[classname]=1 + self.translation_ui.displayres.emit(globalconfig['fanyi'][classname]['name'],globalconfig['fanyi'][classname]['color'],res,onlytrans,[True,classname]) + else: + self.translation_ui.displayres.emit(globalconfig['fanyi'][classname]['name'],globalconfig['fanyi'][classname]['color'],res,onlytrans,[False,classname]) + else: + self.translation_ui.displayres.emit(globalconfig['fanyi'][classname]['name'],globalconfig['fanyi'][classname]['color'],res,onlytrans,[]) try: diff --git a/LunaTranslator/LunaTranslator/gui/textbrowser.py b/LunaTranslator/LunaTranslator/gui/textbrowser.py index d8f281f8..d3d5a4b0 100644 --- a/LunaTranslator/LunaTranslator/gui/textbrowser.py +++ b/LunaTranslator/LunaTranslator/gui/textbrowser.py @@ -225,6 +225,16 @@ class Textbrowser( ): self.addtag(tag) self.movep(0,self.savey) + def getcurrpointer(self): + return self.textcursor.position() + def insertatpointer(self,pointer,text): + if self.needdouble: + self.textcursorback.setPosition(pointer) + self.textbrowserback.setTextCursor(self.textcursorback) + self.textbrowserback.insertPlainText(text) + self.textcursor.setPosition(pointer) + self.textbrowser.setTextCursor(self.textcursor) + self.textbrowser.insertPlainText(text) def showyinyingtext(self,color ): linei=self.yinyingposline diff --git a/LunaTranslator/LunaTranslator/gui/translatorUI.py b/LunaTranslator/LunaTranslator/gui/translatorUI.py index e3f0691a..dcc6fc51 100644 --- a/LunaTranslator/LunaTranslator/gui/translatorUI.py +++ b/LunaTranslator/LunaTranslator/gui/translatorUI.py @@ -28,7 +28,7 @@ from gui.rangeselect import moveresizegame ,rangeselct_function from gui.usefulwidget import resizableframeless from gui.dialog_savedgame import browserdialog class QUnFrameWindow(resizableframeless): - displayres = pyqtSignal(str,str,str ,bool) + displayres = pyqtSignal(str,str,str ,bool,list) displayraw1 = pyqtSignal(list, str,str,bool,bool) displaystatus=pyqtSignal(str,str,bool,bool) showhideuisignal=pyqtSignal() @@ -70,8 +70,16 @@ class QUnFrameWindow(resizableframeless): pass self.move(self.pos().x()+ other[0],self.pos().y()+ other[1]) - def showres(self,name,color,res,onlyshowhist): + def showres(self,name,color,res,onlyshowhist,iter_res_info): try: + if len(iter_res_info): + starting,klass=iter_res_info + print(starting,klass,res) + if not starting: + print(self.saveiterclasspointer[klass]) + self.translate_text.insertatpointer(self.saveiterclasspointer[klass],res) + self.saveiterclasspointer[klass]=self.translate_text.getcurrpointer() + return gobject.baseobject.transhis.getnewtranssignal.emit(name,res) if onlyshowhist: return @@ -89,7 +97,8 @@ class QUnFrameWindow(resizableframeless): self.showline(clear,[None,_res],color ,1,False) #print(globalconfig['fanyi'][_type]['name']+' '+res+'\n') - + if len(iter_res_info) and starting: + self.saveiterclasspointer[klass]=self.translate_text.getcurrpointer()+len(res) except: print_exc() def showraw(self,hira,res,color ,onlyshowhist,clear): @@ -406,6 +415,7 @@ class QUnFrameWindow(resizableframeless): self.isbindedwindow=False self.buttons=[] self.showbuttons=[] + self.saveiterclasspointer={} self.addbuttons() diff --git a/LunaTranslator/LunaTranslator/translator/basetranslator.py b/LunaTranslator/LunaTranslator/translator/basetranslator.py index 2ed77ab8..fc5f0637 100644 --- a/LunaTranslator/LunaTranslator/translator/basetranslator.py +++ b/LunaTranslator/LunaTranslator/translator/basetranslator.py @@ -3,7 +3,7 @@ from queue import Queue from myutils.config import globalconfig,translatorsetting,static_data from threading import Thread,Lock -import os,time ,codecs +import os,time ,types import zhconv,gobject import sqlite3 from myutils.commonbase import commonbase @@ -122,7 +122,7 @@ class basetrans(commonbase): @property def is_gpt_like(self): try: - return translatorsetting[self.typename]['is_gpt_like'] + return globalconfig['fanyi'][self.typename]['is_gpt_like'] except: return False @property @@ -183,13 +183,11 @@ class basetrans(commonbase): res=self.dispatch_translate(contentsolved,hira) else: res=self.intervaledtranslate(contentsolved,hira) - + return res + def cachesetatend(self,contentsolved,res): if globalconfig['uselongtermcache']: self.longtermcacheset(contentsolved,res) self.shorttermcacheset(contentsolved,res) - - return res - def maybecachetranslate(self,contentraw,contentsolved,hira,is_auto_run): if self.transtype=='pre': res=self.translate(contentraw) @@ -261,11 +259,18 @@ class basetrans(commonbase): self._private_init() return self.maybecachetranslate(contentraw,contentsolved,hira,is_auto_run) res=timeoutfunction(reinitandtrans,checktutukufunction=checktutukufunction ) - if self.needzhconv: - res=zhconv.convert(res, 'zh-tw' ) - - callback(res,embedcallback) - + collectiterres=[] + def __callback(_,is_iter_res): + if self.needzhconv: + _=zhconv.convert(_, 'zh-tw' ) + callback(_,embedcallback,is_iter_res) + collectiterres.append(_) + if isinstance(res,types.GeneratorType): + for _res in res: + __callback(_res,True) + else: + __callback(res,False) + self.cachesetatend(contentsolved,''.join(collectiterres)) except Exception as e: if self.using and globalconfig['showtranexception']: if isinstance(e,ArgsEmptyExc): @@ -279,6 +284,6 @@ class basetrans(commonbase): self.needreinit=True msg=''+msg - callback(msg,embedcallback) + callback(msg,embedcallback,False) \ No newline at end of file diff --git a/LunaTranslator/files/defaultconfig/config.json b/LunaTranslator/files/defaultconfig/config.json index d1b4d1f1..b584d858 100644 --- a/LunaTranslator/files/defaultconfig/config.json +++ b/LunaTranslator/files/defaultconfig/config.json @@ -1126,13 +1126,15 @@ "type": "api", "use": false, "color": "blue", - "name": "ChatGPT" + "name": "ChatGPT", + "is_gpt_like":true }, "chatgpt-3rd-party": { "type": "api", "use": false, "color": "blue", - "name": "ChatGPT(第三方接口)" + "name": "ChatGPT(第三方接口)", + "is_gpt_like":true }, "hanshant": { "type": "offline", diff --git a/LunaTranslator/files/defaultconfig/translatorsetting.json b/LunaTranslator/files/defaultconfig/translatorsetting.json index 73377705..e8b0b050 100644 --- a/LunaTranslator/files/defaultconfig/translatorsetting.json +++ b/LunaTranslator/files/defaultconfig/translatorsetting.json @@ -166,7 +166,6 @@ } }, "chatgpt": { - "is_gpt_like":true, "args": { "注册网址": "https://platform.openai.com/account/api-keys", "注册网址2": "https://learn.microsoft.com/zh-cn/azure/cognitive-services/openai/quickstart", @@ -212,7 +211,6 @@ } }, "chatgpt-3rd-party": { - "is_gpt_like":true, "args": { "注册地址": "https://platform.openai.com/account/api-keys", "项目地址": "https://github.com/songquanpeng/one-api", @@ -482,9 +480,13 @@ "max_new_token": 128, "repetition_penalty": 1, "frequency_penalty": 0, - "fix_degeneration": true + "fix_degeneration": true, + "流式输出":false }, "argstype":{ + "流式输出":{ + "type": "switch" + }, "Sakura部署教程":{ "type":"label", "islink": true