Skip to content

Commit 81edd13

Browse files
committed
Merge branch 'master' of https://github.com/zhayujie/chatgpt-on-wechat into master-dev
2 parents 7a94745 + 06b02f5 commit 81edd13

File tree

3 files changed

+98
-16
lines changed

3 files changed

+98
-16
lines changed

voice/baidu/README.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
## 说明
2+
百度语音识别与合成参数说明
3+
百度语音依赖,经常会出现问题,可能就是缺少依赖:
4+
pip install baidu-aip
5+
pip install pydub
6+
pip install pysilk
7+
还有ffmpeg,不同系统安装方式不同
8+
9+
系统中收到的语音文件为mp3格式(wx)或者sil格式(wxy),如果要识别需要转换为pcm格式,转换后的文件为16k采样率,单声道,16bit的pcm文件
10+
发送时又需要(wx)转换为mp3格式,转换后的文件为16k采样率,单声道,16bit的pcm文件,(wxy)转换为sil格式,还要计算声音长度,发送时需要带上声音长度
11+
这些事情都在audio_convert.py中封装了,直接调用即可
12+
13+
14+
参数说明
15+
识别参数
16+
https://ai.baidu.com/ai-doc/SPEECH/Vk38lxily
17+
合成参数
18+
https://ai.baidu.com/ai-doc/SPEECH/Gk38y8lzk
19+
20+
## 使用说明
21+
分两个地方配置
22+
23+
1、对于def voiceToText(self, filename)函数中调用的百度语音识别API,中接口调用asr(参数)这个配置见CHATGPT-ON-WECHAT工程目录下的`config.json`文件和config.py文件。
24+
参数 可需 描述
25+
app_id 必填 应用的APPID
26+
api_key 必填 应用的APIKey
27+
secret_key 必填 应用的SecretKey
28+
dev_pid 必填 语言选择,填写语言对应的dev_pid值
29+
30+
2、对于def textToVoice(self, text)函数中调用的百度语音合成API,中接口调用synthesis(参数)在本目录下的`config.json`文件中进行配置。
31+
参数 可需 描述
32+
tex 必填 合成的文本,使用UTF-8编码,请注意文本长度必须小于1024字节
33+
lan 必填 固定值zh。语言选择,目前只有中英文混合模式,填写固定值zh
34+
spd 选填 语速,取值0-15,默认为5中语速
35+
pit 选填 音调,取值0-15,默认为5中语调
36+
vol 选填 音量,取值0-15,默认为5中音量(取值为0时为音量最小值,并非为无声)
37+
per(基础音库) 选填 度小宇=1,度小美=0,度逍遥(基础)=3,度丫丫=4
38+
per(精品音库) 选填 度逍遥(精品)=5003,度小鹿=5118,度博文=106,度小童=110,度小萌=111,度米朵=103,度小娇=5
39+
aue 选填 3为mp3格式(默认); 4为pcm-16k;5为pcm-8k;6为wav(内容同pcm-16k); 注意aue=4或者6是语音识别要求的格式,但是音频内容不是语音识别要求的自然人发音,所以识别效果会受影响。
40+
41+
关于per参数的说明,注意您购买的哪个音库,就填写哪个音库的参数,否则会报错。如果您购买的是基础音库,那么per参数只能填写0到4,如果您购买的是精品音库,那么per参数只能填写5003,5118,106,110,111,103,5其他的都会报错。
42+
### 配置文件
43+
44+
将文件夹中`config.json.template`复制为`config.json`
45+
46+
``` json
47+
{
48+
"lang": "zh",
49+
"ctp": 1,
50+
"spd": 5,
51+
"pit": 5,
52+
"vol": 5,
53+
"per": 0
54+
}
55+
```

voice/baidu/baidu_voice.py

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
"""
33
baidu voice service
44
"""
5+
import json
6+
import os
57
import time
68
from aip import AipSpeech
79
from bridge.reply import Reply, ReplyType
@@ -21,29 +23,47 @@
2123
- 1837:四川话
2224
要使用本模块, 首先到 yuyin.baidu.com 注册一个开发者账号,
2325
之后创建一个新应用, 然后在应用管理的"查看key"中获得 API Key 和 Secret Key
24-
填入 config.json 中.
25-
baidu_app_id: ''
26-
baidu_api_key: ''
27-
baidu_secret_key: ''
28-
baidu_dev_pid: '1536'
29-
"""
26+
然后在 config.json 中填入这两个值, 以及 app_id, dev_pid
27+
"""
3028

3129

3230
class BaiduVoice(Voice):
33-
APP_ID = conf().get('baidu_app_id')
34-
API_KEY = conf().get('baidu_api_key')
35-
SECRET_KEY = conf().get('baidu_secret_key')
36-
DEV_ID = conf().get('baidu_dev_pid')
37-
client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
3831

3932
def __init__(self):
40-
pass
33+
try:
34+
curdir = os.path.dirname(__file__)
35+
config_path = os.path.join(curdir, "config.json")
36+
bconf = None
37+
if not os.path.exists(config_path): #如果没有配置文件,创建本地配置文件
38+
bconf = { "lang": "zh", "ctp": 1, "spd": 5,
39+
"pit": 5, "vol": 5, "per": 0}
40+
with open(config_path, "w") as fw:
41+
json.dump(bconf, fw, indent=4)
42+
else:
43+
with open(config_path, "r") as fr:
44+
bconf = json.load(fr)
45+
46+
self.app_id = conf().get('baidu_app_id')
47+
self.api_key = conf().get('baidu_api_key')
48+
self.secret_key = conf().get('baidu_secret_key')
49+
self.dev_id = conf().get('baidu_dev_pid')
50+
self.lang = bconf["lang"]
51+
self.ctp = bconf["ctp"]
52+
self.spd = bconf["spd"]
53+
self.pit = bconf["pit"]
54+
self.vol = bconf["vol"]
55+
self.per = bconf["per"]
56+
57+
self.client = AipSpeech(self.app_id, self.api_key, self.secret_key)
58+
except Exception as e:
59+
logger.warn("BaiduVoice init failed: %s, ignore " % e)
4160

61+
4262
def voiceToText(self, voice_file):
4363
# 识别本地文件
4464
logger.debug('[Baidu] voice file name={}'.format(voice_file))
4565
pcm = get_pcm_from_wav(voice_file)
46-
res = self.client.asr(pcm, "pcm", 16000, {"dev_pid": self.DEV_ID})
66+
res = self.client.asr(pcm, "pcm", 16000, {"dev_pid": self.dev_id})
4767
if res["err_no"] == 0:
4868
logger.info("百度语音识别到了:{}".format(res["result"]))
4969
text = "".join(res["result"])
@@ -57,9 +77,8 @@ def voiceToText(self, voice_file):
5777
return reply
5878

5979
def textToVoice(self, text):
60-
result = self.client.synthesis(text, 'zh', 1, {
61-
'spd': 5, 'pit': 5, 'vol': 5, 'per': 111
62-
})
80+
result = self.client.synthesis(text, self.lang, self.ctp, {
81+
'spd': self.spd, 'pit': self.pit, 'vol': self.vol, 'per': self.per})
6382
if not isinstance(result, dict):
6483
fileName = TmpDir().path() + '语音回复_' + str(int(time.time())) + '.mp3'
6584
with open(fileName, 'wb') as f:

voice/baidu/config.json.template

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"lang": "zh",
3+
"ctp": 1,
4+
"spd": 5,
5+
"pit": 5,
6+
"vol": 5,
7+
"per": 0
8+
}

0 commit comments

Comments
 (0)