mirror of
https://github.com/zhayujie/chatgpt-on-wechat.git
synced 2026-03-19 21:38:18 +08:00
formatting code
This commit is contained in:
@@ -29,7 +29,7 @@ dev_pid 必填 语言选择,填写语言对应的dev_pid值
|
||||
|
||||
2、对于def textToVoice(self, text)函数中调用的百度语音合成API,中接口调用synthesis(参数)在本目录下的`config.json`文件中进行配置。
|
||||
参数 可需 描述
|
||||
tex 必填 合成的文本,使用UTF-8编码,请注意文本长度必须小于1024字节
|
||||
tex 必填 合成的文本,使用UTF-8编码,请注意文本长度必须小于1024字节
|
||||
lan 必填 固定值zh。语言选择,目前只有中英文混合模式,填写固定值zh
|
||||
spd 选填 语速,取值0-15,默认为5中语速
|
||||
pit 选填 音调,取值0-15,默认为5中语调
|
||||
@@ -40,14 +40,14 @@ aue 选填 3为mp3格式(默认); 4为pcm-16k;5为pcm-8k;6为wav
|
||||
|
||||
关于per参数的说明,注意您购买的哪个音库,就填写哪个音库的参数,否则会报错。如果您购买的是基础音库,那么per参数只能填写0到4,如果您购买的是精品音库,那么per参数只能填写5003,5118,106,110,111,103,5其他的都会报错。
|
||||
### 配置文件
|
||||
|
||||
|
||||
将文件夹中`config.json.template`复制为`config.json`。
|
||||
|
||||
``` json
|
||||
{
|
||||
"lang": "zh",
|
||||
"lang": "zh",
|
||||
"ctp": 1,
|
||||
"spd": 5,
|
||||
"spd": 5,
|
||||
"pit": 5,
|
||||
"vol": 5,
|
||||
"per": 0
|
||||
|
||||
@@ -1,17 +1,19 @@
|
||||
|
||||
"""
|
||||
baidu voice service
|
||||
"""
|
||||
import json
|
||||
import os
|
||||
import time
|
||||
|
||||
from aip import AipSpeech
|
||||
|
||||
from bridge.reply import Reply, ReplyType
|
||||
from common.log import logger
|
||||
from common.tmp_dir import TmpDir
|
||||
from voice.voice import Voice
|
||||
from voice.audio_convert import get_pcm_from_wav
|
||||
from config import conf
|
||||
from voice.audio_convert import get_pcm_from_wav
|
||||
from voice.voice import Voice
|
||||
|
||||
"""
|
||||
百度的语音识别API.
|
||||
dev_pid:
|
||||
@@ -28,40 +30,37 @@ from config import conf
|
||||
|
||||
|
||||
class BaiduVoice(Voice):
|
||||
|
||||
def __init__(self):
|
||||
try:
|
||||
curdir = os.path.dirname(__file__)
|
||||
config_path = os.path.join(curdir, "config.json")
|
||||
bconf = None
|
||||
if not os.path.exists(config_path): #如果没有配置文件,创建本地配置文件
|
||||
bconf = { "lang": "zh", "ctp": 1, "spd": 5,
|
||||
"pit": 5, "vol": 5, "per": 0}
|
||||
if not os.path.exists(config_path): # 如果没有配置文件,创建本地配置文件
|
||||
bconf = {"lang": "zh", "ctp": 1, "spd": 5, "pit": 5, "vol": 5, "per": 0}
|
||||
with open(config_path, "w") as fw:
|
||||
json.dump(bconf, fw, indent=4)
|
||||
else:
|
||||
with open(config_path, "r") as fr:
|
||||
bconf = json.load(fr)
|
||||
|
||||
self.app_id = conf().get('baidu_app_id')
|
||||
self.api_key = conf().get('baidu_api_key')
|
||||
self.secret_key = conf().get('baidu_secret_key')
|
||||
self.dev_id = conf().get('baidu_dev_pid')
|
||||
|
||||
self.app_id = conf().get("baidu_app_id")
|
||||
self.api_key = conf().get("baidu_api_key")
|
||||
self.secret_key = conf().get("baidu_secret_key")
|
||||
self.dev_id = conf().get("baidu_dev_pid")
|
||||
self.lang = bconf["lang"]
|
||||
self.ctp = bconf["ctp"]
|
||||
self.spd = bconf["spd"]
|
||||
self.pit = bconf["pit"]
|
||||
self.vol = bconf["vol"]
|
||||
self.per = bconf["per"]
|
||||
|
||||
|
||||
self.client = AipSpeech(self.app_id, self.api_key, self.secret_key)
|
||||
except Exception as e:
|
||||
logger.warn("BaiduVoice init failed: %s, ignore " % e)
|
||||
|
||||
|
||||
def voiceToText(self, voice_file):
|
||||
# 识别本地文件
|
||||
logger.debug('[Baidu] voice file name={}'.format(voice_file))
|
||||
logger.debug("[Baidu] voice file name={}".format(voice_file))
|
||||
pcm = get_pcm_from_wav(voice_file)
|
||||
res = self.client.asr(pcm, "pcm", 16000, {"dev_pid": self.dev_id})
|
||||
if res["err_no"] == 0:
|
||||
@@ -72,21 +71,25 @@ class BaiduVoice(Voice):
|
||||
logger.info("百度语音识别出错了: {}".format(res["err_msg"]))
|
||||
if res["err_msg"] == "request pv too much":
|
||||
logger.info(" 出现这个原因很可能是你的百度语音服务调用量超出限制,或未开通付费")
|
||||
reply = Reply(ReplyType.ERROR,
|
||||
"百度语音识别出错了;{0}".format(res["err_msg"]))
|
||||
reply = Reply(ReplyType.ERROR, "百度语音识别出错了;{0}".format(res["err_msg"]))
|
||||
return reply
|
||||
|
||||
def textToVoice(self, text):
|
||||
result = self.client.synthesis(text, self.lang, self.ctp, {
|
||||
'spd': self.spd, 'pit': self.pit, 'vol': self.vol, 'per': self.per})
|
||||
result = self.client.synthesis(
|
||||
text,
|
||||
self.lang,
|
||||
self.ctp,
|
||||
{"spd": self.spd, "pit": self.pit, "vol": self.vol, "per": self.per},
|
||||
)
|
||||
if not isinstance(result, dict):
|
||||
fileName = TmpDir().path() + 'reply-' + str(int(time.time())) + '.mp3'
|
||||
with open(fileName, 'wb') as f:
|
||||
fileName = TmpDir().path() + "reply-" + str(int(time.time())) + ".mp3"
|
||||
with open(fileName, "wb") as f:
|
||||
f.write(result)
|
||||
logger.info(
|
||||
'[Baidu] textToVoice text={} voice file name={}'.format(text, fileName))
|
||||
"[Baidu] textToVoice text={} voice file name={}".format(text, fileName)
|
||||
)
|
||||
reply = Reply(ReplyType.VOICE, fileName)
|
||||
else:
|
||||
logger.error('[Baidu] textToVoice error={}'.format(result))
|
||||
logger.error("[Baidu] textToVoice error={}".format(result))
|
||||
reply = Reply(ReplyType.ERROR, "抱歉,语音合成失败")
|
||||
return reply
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
{
|
||||
"lang": "zh",
|
||||
"ctp": 1,
|
||||
"spd": 5,
|
||||
"pit": 5,
|
||||
"vol": 5,
|
||||
"per": 0
|
||||
}
|
||||
{
|
||||
"lang": "zh",
|
||||
"ctp": 1,
|
||||
"spd": 5,
|
||||
"pit": 5,
|
||||
"vol": 5,
|
||||
"per": 0
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user