stream output

This commit is contained in:
恍兮惚兮 2024-03-03 01:27:25 +08:00
parent 6cb02b58aa
commit 230423fc83
6 changed files with 69 additions and 30 deletions

View File

@ -41,9 +41,9 @@ class MAINUI() :
def __init__(self) -> None: def __init__(self) -> None:
super().__init__() super().__init__()
self.lasttranslatorindex=0 self.lasttranslatorindex=0
self.usefultranslators=0
self.translators={} self.translators={}
self.cishus={} self.cishus={}
self.iterresstatus={}
self.reader=None self.reader=None
self.textsource_p=None self.textsource_p=None
self.currentmd5='0' self.currentmd5='0'
@ -151,7 +151,7 @@ class MAINUI() :
self.currentsignature=time.time() self.currentsignature=time.time()
if type(text)==str: if type(text)==str:
if text.startswith('<notrans>'): if text.startswith('<notrans>'):
self.translation_ui.displayres.emit('',globalconfig['rawtextcolor'],text[len('<notrans>'):],onlytrans) self.translation_ui.displayres.emit('',globalconfig['rawtextcolor'],text[len('<notrans>'):],onlytrans,[])
self.currenttext=text self.currenttext=text
self.currentread=text self.currentread=text
return return
@ -245,7 +245,7 @@ class MAINUI() :
skip=is_auto_run and (len(text_solved)<globalconfig['minlength'] or len(text_solved)>globalconfig['maxlength'] ) skip=is_auto_run and (len(text_solved)<globalconfig['minlength'] or len(text_solved)>globalconfig['maxlength'] )
self.premtalready=['premt'] self.premtalready=['premt']
self.usefultranslators=len(self.translators) self.usefultranslators=list(self.translators.keys())
if 'premt' in self.translators: if 'premt' in self.translators:
try: try:
res=self.translators['premt'].translate(text_solved) res=self.translators['premt'].translate(text_solved)
@ -255,7 +255,7 @@ class MAINUI() :
_colork=k _colork=k
else: else:
_colork='premt' _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: except:
print_exc() print_exc()
@ -270,6 +270,7 @@ class MAINUI() :
keys=list(self.translators.keys())+list(self.translators.keys()) keys=list(self.translators.keys())+list(self.translators.keys())
keys=keys[self.lasttranslatorindex:self.lasttranslatorindex+_len] keys=keys[self.lasttranslatorindex:self.lasttranslatorindex+_len]
#print(keys,usenum,self.lasttranslatorindex) #print(keys,usenum,self.lasttranslatorindex)
self.iterresstatus.clear()
for engine in keys: for engine in keys:
if engine not in self.premtalready: 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)) 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 break
def GetTranslationCallback(self,onlytrans,classname,currentsignature,optimization_params,_showrawfunction,_showrawfunction_sig,contentraw,res,embedcallback): def GetTranslationCallback(self,onlytrans,classname,currentsignature,optimization_params,_showrawfunction,_showrawfunction_sig,contentraw,res,embedcallback,is_iter_res):
self.usefultranslators-=1
if classname in self.usefultranslators:
self.usefultranslators.remove(classname)
if embedcallback is None and currentsignature!=self.currentsignature:return 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='' embedtrans=''
if embedcallback and embedtrans is not None: if embedcallback and embedtrans is not None:
embedcallback(embedtrans) 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: if type(res)==str:
@ -313,7 +316,14 @@ class MAINUI() :
_showrawfunction() _showrawfunction()
if currentsignature==self.currentsignature and globalconfig['showfanyi']: 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: try:

View File

@ -225,6 +225,16 @@ class Textbrowser( ):
self.addtag(tag) self.addtag(tag)
self.movep(0,self.savey) 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 ): def showyinyingtext(self,color ):
linei=self.yinyingposline linei=self.yinyingposline

View File

@ -28,7 +28,7 @@ from gui.rangeselect import moveresizegame ,rangeselct_function
from gui.usefulwidget import resizableframeless from gui.usefulwidget import resizableframeless
from gui.dialog_savedgame import browserdialog from gui.dialog_savedgame import browserdialog
class QUnFrameWindow(resizableframeless): class QUnFrameWindow(resizableframeless):
displayres = pyqtSignal(str,str,str ,bool) displayres = pyqtSignal(str,str,str ,bool,list)
displayraw1 = pyqtSignal(list, str,str,bool,bool) displayraw1 = pyqtSignal(list, str,str,bool,bool)
displaystatus=pyqtSignal(str,str,bool,bool) displaystatus=pyqtSignal(str,str,bool,bool)
showhideuisignal=pyqtSignal() showhideuisignal=pyqtSignal()
@ -70,8 +70,16 @@ class QUnFrameWindow(resizableframeless):
pass pass
self.move(self.pos().x()+ other[0],self.pos().y()+ other[1]) 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: 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) gobject.baseobject.transhis.getnewtranssignal.emit(name,res)
if onlyshowhist: if onlyshowhist:
return return
@ -89,7 +97,8 @@ class QUnFrameWindow(resizableframeless):
self.showline(clear,[None,_res],color ,1,False) self.showline(clear,[None,_res],color ,1,False)
#print(globalconfig['fanyi'][_type]['name']+' '+res+'\n') #print(globalconfig['fanyi'][_type]['name']+' '+res+'\n')
if len(iter_res_info) and starting:
self.saveiterclasspointer[klass]=self.translate_text.getcurrpointer()+len(res)
except: except:
print_exc() print_exc()
def showraw(self,hira,res,color ,onlyshowhist,clear): def showraw(self,hira,res,color ,onlyshowhist,clear):
@ -406,6 +415,7 @@ class QUnFrameWindow(resizableframeless):
self.isbindedwindow=False self.isbindedwindow=False
self.buttons=[] self.buttons=[]
self.showbuttons=[] self.showbuttons=[]
self.saveiterclasspointer={}
self.addbuttons() self.addbuttons()

View File

@ -3,7 +3,7 @@ from queue import Queue
from myutils.config import globalconfig,translatorsetting,static_data from myutils.config import globalconfig,translatorsetting,static_data
from threading import Thread,Lock from threading import Thread,Lock
import os,time ,codecs import os,time ,types
import zhconv,gobject import zhconv,gobject
import sqlite3 import sqlite3
from myutils.commonbase import commonbase from myutils.commonbase import commonbase
@ -122,7 +122,7 @@ class basetrans(commonbase):
@property @property
def is_gpt_like(self): def is_gpt_like(self):
try: try:
return translatorsetting[self.typename]['is_gpt_like'] return globalconfig['fanyi'][self.typename]['is_gpt_like']
except: except:
return False return False
@property @property
@ -183,13 +183,11 @@ class basetrans(commonbase):
res=self.dispatch_translate(contentsolved,hira) res=self.dispatch_translate(contentsolved,hira)
else: else:
res=self.intervaledtranslate(contentsolved,hira) res=self.intervaledtranslate(contentsolved,hira)
return res
def cachesetatend(self,contentsolved,res):
if globalconfig['uselongtermcache']: if globalconfig['uselongtermcache']:
self.longtermcacheset(contentsolved,res) self.longtermcacheset(contentsolved,res)
self.shorttermcacheset(contentsolved,res) self.shorttermcacheset(contentsolved,res)
return res
def maybecachetranslate(self,contentraw,contentsolved,hira,is_auto_run): def maybecachetranslate(self,contentraw,contentsolved,hira,is_auto_run):
if self.transtype=='pre': if self.transtype=='pre':
res=self.translate(contentraw) res=self.translate(contentraw)
@ -261,11 +259,18 @@ class basetrans(commonbase):
self._private_init() self._private_init()
return self.maybecachetranslate(contentraw,contentsolved,hira,is_auto_run) return self.maybecachetranslate(contentraw,contentsolved,hira,is_auto_run)
res=timeoutfunction(reinitandtrans,checktutukufunction=checktutukufunction ) res=timeoutfunction(reinitandtrans,checktutukufunction=checktutukufunction )
if self.needzhconv: collectiterres=[]
res=zhconv.convert(res, 'zh-tw' ) def __callback(_,is_iter_res):
if self.needzhconv:
callback(res,embedcallback) _=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: except Exception as e:
if self.using and globalconfig['showtranexception']: if self.using and globalconfig['showtranexception']:
if isinstance(e,ArgsEmptyExc): if isinstance(e,ArgsEmptyExc):
@ -279,6 +284,6 @@ class basetrans(commonbase):
self.needreinit=True self.needreinit=True
msg='<msg_translator>'+msg msg='<msg_translator>'+msg
callback(msg,embedcallback) callback(msg,embedcallback,False)

View File

@ -1126,13 +1126,15 @@
"type": "api", "type": "api",
"use": false, "use": false,
"color": "blue", "color": "blue",
"name": "ChatGPT" "name": "ChatGPT",
"is_gpt_like":true
}, },
"chatgpt-3rd-party": { "chatgpt-3rd-party": {
"type": "api", "type": "api",
"use": false, "use": false,
"color": "blue", "color": "blue",
"name": "ChatGPT(第三方接口)" "name": "ChatGPT(第三方接口)",
"is_gpt_like":true
}, },
"hanshant": { "hanshant": {
"type": "offline", "type": "offline",

View File

@ -166,7 +166,6 @@
} }
}, },
"chatgpt": { "chatgpt": {
"is_gpt_like":true,
"args": { "args": {
"注册网址": "https://platform.openai.com/account/api-keys", "注册网址": "https://platform.openai.com/account/api-keys",
"注册网址2": "https://learn.microsoft.com/zh-cn/azure/cognitive-services/openai/quickstart", "注册网址2": "https://learn.microsoft.com/zh-cn/azure/cognitive-services/openai/quickstart",
@ -212,7 +211,6 @@
} }
}, },
"chatgpt-3rd-party": { "chatgpt-3rd-party": {
"is_gpt_like":true,
"args": { "args": {
"注册地址": "https://platform.openai.com/account/api-keys", "注册地址": "https://platform.openai.com/account/api-keys",
"项目地址": "https://github.com/songquanpeng/one-api", "项目地址": "https://github.com/songquanpeng/one-api",
@ -482,9 +480,13 @@
"max_new_token": 128, "max_new_token": 128,
"repetition_penalty": 1, "repetition_penalty": 1,
"frequency_penalty": 0, "frequency_penalty": 0,
"fix_degeneration": true "fix_degeneration": true,
"流式输出":false
}, },
"argstype":{ "argstype":{
"流式输出":{
"type": "switch"
},
"Sakura部署教程":{ "Sakura部署教程":{
"type":"label", "type":"label",
"islink": true "islink": true