首页 > 编程知识 正文

百度人工智能api,百度语音AI

时间:2023-05-05 08:57:32 阅读:266416 作者:4272

1.登陆百度AI的官网

1

2
1.注册:没有账号注册
2.创建应用

1
3.创建应用

1
4.查看应用的ID

1

5.Python代码
1
2
3
4
5
6
7
from aip import AipSpeech

APP_ID = “appid "
API_KEY=”**********"
SECRET_KEY="**************"

client = AipSpeech(APP_ID,API_KEY,SECRET_KEY)
6.语音识别
1
2
3
4
5
6

语音识别

def speech_sb(file_path_name):
os.system(f’ffmpeg -y -i {file_path_name}.m4a -acodec pcm_s16le -f s16le -ac 1 -ar 16000 {file_path_name}.pcm’)

with open(f'{file_path_name}.pcm', 'rb') as fp: return fp.read() 调用语音识别

ret = client.asr(speech_sb(‘123’), ‘pcm’, 16000, {
‘dev_pid’: 1536,
})
7.语音合成
1
2
3
4
5
6
7
8
9
#语音合成
def speech_hc(title,text):
# title是生成文件的标题,text是哟啊合成的文本
result = client.synthesis(text)
if not isinstance(result, dict):
with open(f’{title}.mp3’, ‘wb’) as f:
f.write(result)
return
else:
return result
8.自然语言处理加图灵机器人

1
2
3
4
5
6
7
8
9
10
11
12

自然语音的处理

def my_npl(text,id):
#传入要对比的文本,id主要用于图灵机器人,
print(nlp_client.simnet(text,“你叫什么名字”).get(‘score’))
if nlp_client.simnet(text,“你叫什么名字”).get(‘score’)>=0.7:
ret_name = “我是Sopython,Sopython就是我”
return ret_name
else:
ret_tj = tuling(text,id)
if isinstance(ret, dict):
ret_tj = ret_tj.get(“results”)[0].get(“values”).get(‘text’)
print(ret_tj)
return ret_tj

#{‘corpus_no’: ‘6672231296183866724’, ‘err_msg’: ‘success.’, ‘err_no’: 0, ‘result’: [‘叫什么名字’], ‘sn’: ‘90391695291553499907’}
ret = my_npl(ret.get(‘result’),132) # 调用完语音识别之后,获取到识别的结果传入自然语言处理,id=132

如果自定义的自然语言处理成功的话,那么就返回自定的结果,否则的话调用图灵机器人来回答问题 最后返回处理的结果,

9.图灵机器人

1
2
1.注册图灵机器人
2.创建机器人

3.Python代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

调用图灵的机器人

def tuling(text, id):
data = {
“perception”: {
“inputText”: {
“text”: f"{text}"
}
},
“userInfo”: {
“apiKey”: “a1f6dbf66978411c9127585f7779cd04”,
“userId”: f"{id}"
}
}

res = requests.post("http://openapi.tuling123.com/openapi/api/v2", json=data)# print(res.content)res_json = res.json()return res_json<br>  #返回图灵机器人自己的答案

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。