This commit is contained in:
恍兮惚兮 2024-05-17 00:08:24 +08:00
parent bd04bd443b
commit 6f0132a166
16 changed files with 1251 additions and 145 deletions

View File

@ -1,4 +1,4 @@
from myutils.config import globalconfig, static_data
from myutils.config import getlangsrc
import requests
from urllib.parse import quote
import re
@ -11,7 +11,7 @@ class youdao(cishubase):
def srclang(self):
try:
l = static_data["language_list_translator_inner"][globalconfig["srclang3"]]
l = getlangsrc()
return l
except:

View File

@ -1,5 +1,5 @@
import os
from myutils.config import globalconfig, _TRL, static_data
from myutils.config import globalconfig, _TRL, static_data,getlanguse
from gui.usefulwidget import getsimplecombobox, getcolorbutton
@ -46,11 +46,7 @@ def setTablanglz(self):
"",
callback=lambda: os.startfile(
os.path.abspath(
"./files/lang/{}.json".format(
static_data["language_list_translator_inner"][
globalconfig["languageuse"]
]
)
"./files/lang/{}.json".format(getlanguse())
)
),
icon="fa.gear",

View File

@ -44,14 +44,14 @@ class AnkiWindow(QWidget):
def langdu(self):
if gobject.baseobject.reader:
self.audiopath.setText(
gobject.baseobject.reader.syncttstofile(self.currentword)
gobject.baseobject.reader.ttscallback(
self.currentword, self.audiopath.setText
)
def langdu2(self):
if gobject.baseobject.reader:
self.audiopath_sentence.setText(
gobject.baseobject.reader.syncttstofile(self.example.toPlainText())
gobject.baseobject.reader.ttscallback(
self.example.toPlainText(), self.audiopath_sentence.setText
)
@threader
@ -205,27 +205,41 @@ class AnkiWindow(QWidget):
def loadfakefields(self):
if len(self.editpath.text()):
with open(self.editpath.text(), "rb") as image_file:
encoded_string = base64.b64encode(image_file.read()).decode("utf-8")
encoded_string = '<img src="data:image/png;base64,{}">'.format(
encoded_string
)
try:
with open(self.editpath.text(), "rb") as image_file:
encoded_string = base64.b64encode(image_file.read()).decode("utf-8")
encoded_string = '<img src="data:image/png;base64,{}">'.format(
encoded_string
)
except:
encoded_string = ""
else:
encoded_string = ""
if len(self.audiopath.text()):
with open(self.audiopath.text(), "rb") as image_file:
encoded_string2 = base64.b64encode(image_file.read()).decode("utf-8")
encoded_string2 = """<button onclick='document.getElementById("audio1111").play()'>play audio<audio controls id="audio1111" style="display: none"><source src="data:audio/mpeg;base64,{}"></audio></button>""".format(
encoded_string2
)
try:
with open(self.audiopath.text(), "rb") as image_file:
encoded_string2 = base64.b64encode(image_file.read()).decode(
"utf-8"
)
encoded_string2 = """<button onclick='document.getElementById("audio1111").play()'>play audio<audio controls id="audio1111" style="display: none"><source src="data:audio/mpeg;base64,{}"></audio></button>""".format(
encoded_string2
)
except:
encoded_string2 = ""
else:
encoded_string2 = ""
if len(self.audiopath_sentence.text()):
with open(self.audiopath_sentence.text(), "rb") as image_file:
encoded_string3 = base64.b64encode(image_file.read()).decode("utf-8")
encoded_string3 = """<button onclick='document.getElementById("audio2222").play()'>play audio_sentence<audio controls id="audio2222" style="display: none"><source src="data:audio/mpeg;base64,{}"></audio></button>""".format(
encoded_string3
)
try:
with open(self.audiopath_sentence.text(), "rb") as image_file:
encoded_string3 = base64.b64encode(image_file.read()).decode(
"utf-8"
)
encoded_string3 = """<button onclick='document.getElementById("audio2222").play()'>play audio_sentence<audio controls id="audio2222" style="display: none"><source src="data:audio/mpeg;base64,{}"></audio></button>""".format(
encoded_string3
)
except:
encoded_string3 = ""
else:
encoded_string3 = ""
fields = {

View File

@ -1,5 +1,5 @@
from myutils.proxy import getproxy
from myutils.config import globalconfig, _TR, static_data
from myutils.config import getlangtgt, _TR, static_data, getlangsrc
from myutils.wrapper import stripwrapper
import requests
@ -34,7 +34,7 @@ class commonbase:
@property
def srclang(self):
try:
l = static_data["language_list_translator_inner"][globalconfig["srclang3"]]
l = getlangsrc()
return self.langmap_[l]
except:
return ""
@ -42,7 +42,7 @@ class commonbase:
@property
def tgtlang(self):
try:
l = static_data["language_list_translator_inner"][globalconfig["tgtlang3"]]
l = getlangtgt()
return self.langmap_[l]
except:
return ""

View File

@ -187,14 +187,22 @@ if len(globalconfig["toolbutton"]["rank"]) != len(
)
def getlanguse():
global language, languageshow
return static_data["language_list_translator_inner"][language]
def getlangsrc():
return static_data["language_list_translator_inner"][globalconfig["srclang3"]]
def getlangtgt():
return static_data["language_list_translator_inner"][globalconfig["tgtlang3"]]
def setlanguage():
global language, languageshow
language = globalconfig["languageuse"]
try:
with open(
"./files/lang/{}.json".format(
static_data["language_list_translator_inner"][language]
),
"./files/lang/{}.json".format(getlanguse()),
"r",
encoding="utf8",
) as ff:
@ -255,8 +263,6 @@ def saveallconfig():
"./userconfig/savehook_new_1.39.4.json", [savehook_new_list, savehook_new_data]
)
safesave(
"./files/lang/{}.json".format(
static_data["language_list_translator_inner"][language]
),
"./files/lang/{}.json".format(getlanguse()),
languageshow,
)

View File

@ -7,7 +7,7 @@ from myutils.config import (
postprocessconfig,
globalconfig,
savehook_new_data,
static_data,
getlangsrc,
)
@ -179,7 +179,7 @@ def _4_f(line):
def _6_fEX(line):
srclang = static_data["language_list_translator_inner"][globalconfig["srclang3"]]
srclang = getlangsrc()
if srclang in ["zh", "ja"]:
white = ""
else:

View File

@ -15,6 +15,7 @@ from traceback import print_exc
from myutils.config import (
globalconfig,
static_data,
getlanguse,
savehook_new_list,
savehook_new_data,
getdefaultsavehook,
@ -537,10 +538,6 @@ def parsemayberegexreplace(dict, res):
return res
def getlanguse():
return static_data["language_list_translator_inner"][globalconfig["languageuse"]]
def checkpostlangmatch(name):
for item in static_data["transoptimi"]:
if name == item["name"]:

View File

@ -1,5 +1,5 @@
import os
from myutils.config import globalconfig, _TR, static_data
from myutils.config import globalconfig, _TR, getlangsrc
from ocrengines.baseocrclass import baseocr
from ctypes import (
CDLL,
@ -131,9 +131,7 @@ class OCR(baseocr):
return
self._ocr.trydestroy()
path = "./files/ocr/{}".format(
static_data["language_list_translator_inner"][globalconfig["srclang3"]]
)
path = "./files/ocr/{}".format(getlangsrc())
if not (
os.path.exists(path + "/det.onnx")
and os.path.exists(path + "/rec.onnx")

View File

@ -1,7 +1,7 @@
from traceback import print_exc
from queue import Queue
from myutils.config import globalconfig, translatorsetting, static_data
from myutils.config import globalconfig, translatorsetting, getlangtgt
from threading import Thread
import time, types
import zhconv, gobject
@ -172,7 +172,7 @@ class basetrans(commonbase):
@property
def needzhconv(self):
# The API does not support direct translation to Traditional Chinese, only Simplified Chinese can be translated first and then converted to Traditional Chinese
l = static_data["language_list_translator_inner"][globalconfig["tgtlang3"]]
l = getlangtgt()
return l == "cht" and "cht" not in self.langmap()
@property

View File

@ -1,6 +1,7 @@
from myutils.config import globalconfig
import threading, os
import threading, os, functools
from myutils.wrapper import threader
from traceback import print_exc
class TTSbase:
def init(self):
@ -19,15 +20,16 @@ class TTSbase:
# 一些可能需要的属性
@property
def config(self):
return self.privateconfig['args']
return self.privateconfig["args"]
@property
def privateconfig(self):
return globalconfig["reader"][self.typename]
@property
def publicconfig(self):
return globalconfig["ttscommon"]
########################
def __init__(self, typename, showlistsignal, mp3playsignal) -> None:
@ -58,15 +60,14 @@ class TTSbase:
threading.Thread(target=_).start()
def read(self, content, force=False):
def _(content, force):
fname = self.syncttstofile(content)
def _(force, fname):
volume = self.publicconfig["volume"]
if fname:
self.mp3playsignal.emit(fname, volume, force)
self.mp3playsignal.emit(fname, volume, force)
threading.Thread(target=_, args=(content, force)).start()
self.ttscallback(content, functools.partial(_, force))
def syncttstofile(self, content):
@threader
def ttscallback(self, content, callback):
if self.loadok == False:
return
if len(content) == 0:
@ -77,5 +78,10 @@ class TTSbase:
rate = self.publicconfig["rate"]
voice = self.privateconfig["voice"]
voice_index = self.voicelist.index(voice)
fname = self.speak(content, rate, voice, voice_index)
return os.path.abspath(fname)
try:
fname = self.speak(content, rate, voice, voice_index)
if fname:
callback(os.path.abspath(fname))
except:
print_exc()
return

File diff suppressed because it is too large Load Diff

View File

@ -24,36 +24,31 @@ class TTS(TTSbase):
def speak(self, content, rate, voice, voiceidx):
try:
headers = {
"authority": "translate.volcengine.com",
"accept": "application/json, text/plain, */*",
"accept-language": "zh-CN,zh;q=0.9",
"origin": "chrome-extension://klgfhbdadaspgppeadghjjemk",
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "none",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36",
}
headers = {
"authority": "translate.volcengine.com",
"accept": "application/json, text/plain, */*",
"accept-language": "zh-CN,zh;q=0.9",
"origin": "chrome-extension://klgfhbdadaspgppeadghjjemk",
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "none",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36",
}
json_data = {
"text": content,
"speaker": voice,
} #
response = requests.post(
"https://translate.volcengine.com/crx/tts/v1/",
headers=headers,
json=json_data,
proxies={"http": None, "https": None},
)
fname = str(time.time())
b64 = base64.b64decode(response.json()["audio"]["data"])
os.makedirs("./cache/tts/", exist_ok=True)
with open("./cache/tts/" + fname + ".mp3", "wb") as ff:
ff.write(b64)
json_data = {
"text": content,
"speaker": voice,
} #
response = requests.post(
"https://translate.volcengine.com/crx/tts/v1/",
headers=headers,
json=json_data,
proxies={"http": None, "https": None},
)
fname = str(time.time())
b64 = base64.b64decode(response.json()["audio"]["data"])
os.makedirs("./cache/tts/", exist_ok=True)
with open("./cache/tts/" + fname + ".mp3", "wb") as ff:
ff.write(b64)
return "./cache/tts/" + fname + ".mp3"
except:
print_exc()
return None
return "./cache/tts/" + fname + ".mp3"

View File

@ -126,16 +126,16 @@ class TTS(TTSbase):
def speak(self, content, rate, voice, voice_idx):
self.checkpath()
# def _():
if True:
try:
content.encode("shift-jis")
except:
return
code1 = content.encode("shift-jis")
# print(code1)
windows.WriteFile(self.hPipe, code1)
try:
content.encode("shift-jis")
except:
return
code1 = content.encode("shift-jis")
# print(code1)
windows.WriteFile(self.hPipe, code1)
fname = windows.ReadFile(self.hPipe, 1024).decode("utf8")
if os.path.exists(fname):
return fname
fname = windows.ReadFile(self.hPipe, 1024).decode("utf8")
if os.path.exists(fname):
return fname

View File

@ -115,17 +115,16 @@ class TTS(TTSbase):
def speak(self, content, rate, voice, voice_idx):
self.checkpath()
# def _():
if True:
try:
content.encode("shift-jis")
except:
return
code1 = content.encode("shift-jis")
# print(code1)
windows.WriteFile(self.hPipe, code1)
try:
content.encode("shift-jis")
except:
return
code1 = content.encode("shift-jis")
# print(code1)
windows.WriteFile(self.hPipe, code1)
fname = windows.ReadFile(self.hPipe, 1024).decode("utf8")
if os.path.exists(fname):
return fname
fname = windows.ReadFile(self.hPipe, 1024).decode("utf8")
if os.path.exists(fname):
return fname

View File

@ -74,36 +74,33 @@ class TTS(TTSbase):
def speak(self, content, rate, voice, voiceidx):
# def _():
if True:
headers = {
"Content-Type": "application/x-www-form-urlencoded",
}
headers = {
"Content-Type": "application/x-www-form-urlencoded",
}
params = {"speaker": voiceidx, "text": content}
params = {"speaker": voiceidx, "text": content}
response = requests.post(
f"http://localhost:{self.config['Port']}/audio_query",
params=params,
headers=headers,
proxies={"http": None, "https": None},
)
print(response.json())
fname = str(time.time())
headers = {
"Content-Type": "application/json",
}
params = {
"speaker": voiceidx,
}
response = requests.post(
f"http://localhost:{self.config['Port']}/synthesis",
params=params,
headers=headers,
data=json.dumps(response.json()),
)
os.makedirs("./cache/tts/", exist_ok=True)
with open("./cache/tts/" + fname + ".wav", "wb") as ff:
ff.write(response.content)
return "./cache/tts/" + fname + ".wav"
response = requests.post(
f"http://localhost:{self.config['Port']}/audio_query",
params=params,
headers=headers,
proxies={"http": None, "https": None},
)
print(response.json())
fname = str(time.time())
headers = {
"Content-Type": "application/json",
}
params = {
"speaker": voiceidx,
}
response = requests.post(
f"http://localhost:{self.config['Port']}/synthesis",
params=params,
headers=headers,
data=json.dumps(response.json()),
)
os.makedirs("./cache/tts/", exist_ok=True)
with open("./cache/tts/" + fname + ".wav", "wb") as ff:
ff.write(response.content)
return "./cache/tts/" + fname + ".wav"

View File

@ -725,6 +725,11 @@
"name": "GPT-SOVITS preset"
}
}
},
"gtts": {
"use": false,
"name": "谷歌",
"voice": ""
}
},
"hirasetting": {