Skip to content

Commit

Permalink
Fix bug in translate.py (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
jiangyuxiaoxiao authored Oct 16, 2023
1 parent 9e8c4a1 commit 5ffa506
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 32 deletions.
67 changes: 37 additions & 30 deletions tools/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,33 +22,40 @@ def translate(Sentence: str, to_Language: str = "jp", from_Language: str = ""):
if appid == "" or key == "":
return "请在开发者config.yml中配置app_key与secret_key"
url = "https://fanyi-api.baidu.com/api/trans/vip/translate"
# 签名计算 参考文档 https://api.fanyi.baidu.com/product/113
salt = str(random.randint(1, 100000))
signString = appid + Sentence + salt + key
hs = hashlib.md5()
hs.update(signString.encode("utf-8"))
signString = hs.hexdigest()
if from_Language == "":
from_Language = "auto"
headers = {"Content-Type": "application/x-www-form-urlencoded"}
payload = {
"q": Sentence,
"from": from_Language,
"to": to_Language,
"appid": appid,
"salt": salt,
"sign": signString,
}
# 发送请求
try:
response = requests.post(url=url, data=payload, headers=headers, timeout=3)
response = response.json()
if "trans_result" in response.keys():
result = response["trans_result"][0]
if "dst" in result.keys():
dst = result["dst"]
return dst
except Exception:
return Sentence
# 不成功就返回原句子
return Sentence
texts = Sentence.split("\n")
outTexts = []
for t in texts:
if t != "":
# 签名计算 参考文档 https://api.fanyi.baidu.com/product/113
salt = str(random.randint(1, 100000))
signString = appid + Sentence + salt + key
hs = hashlib.md5()
hs.update(signString.encode("utf-8"))
signString = hs.hexdigest()
if from_Language == "":
from_Language = "auto"
headers = {"Content-Type": "application/x-www-form-urlencoded"}
payload = {
"q": Sentence,
"from": from_Language,
"to": to_Language,
"appid": appid,
"salt": salt,
"sign": signString,
}
# 发送请求
try:
response = requests.post(
url=url, data=payload, headers=headers, timeout=3
)
response = response.json()
if "trans_result" in response.keys():
result = response["trans_result"][0]
if "dst" in result.keys():
dst = result["dst"]
outTexts.append(dst)
except Exception:
return Sentence
else:
outTexts.append(t)
return "\n".join(outTexts)
4 changes: 2 additions & 2 deletions webui.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import numpy as np
from config import config
from tools.translate import translate
from oldVersion import V111
from oldVersion.V111.models import SynthesizerTrn as V111SynthesizerTrn

net_g = None

Expand Down Expand Up @@ -65,7 +65,7 @@ def tts_fn(
logging.basicConfig(level=logging.DEBUG)
hps = utils.get_hparams_from_file(config.webui_config.config_path)
version = hps.version if hasattr(hps, "version") else "1.1.1-dev"
SynthesizerTrnMap = {"1.1.1": V111.SynthesizerTrn}
SynthesizerTrnMap = {"1.1.1": V111SynthesizerTrn}
if version != "1.1.1-dev":
net_g = SynthesizerTrnMap[version](
len(symbols),
Expand Down

0 comments on commit 5ffa506

Please sign in to comment.