from aip import AipSpeech
from aip import AipNlp
from uuid import uuid4
import setting
import os
APP_ID = "11562884"
API_KEY = "9iOLKP9VCo4nsEf3N8dcOUmT"
SECRET_KEY = "aW0kwOHFbHrQely6bcmGTzU49t2jOYdL"
baidu_client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
baidu_nlp = AipNlp(APP_ID, API_KEY, SECRET_KEY)
#.1 文本转语音.
def text2audio(text):
file_name = f"{uuid4()}.mp3"
print("filename:%s"%file_name)
file_path = os.path.join(setting.AUDIO_FILE_PATH, file_name)
print("file_path:%s" %(file_path))
res = baidu_client.synthesis(text, "zh", 1, {
"vol": 5,
"pit": 7,
"spd": 4,
"per": 4
})
if type(res) == dict:
return res
with open(file_path, "wb") as f:
f.write(res)
return file_name
#
# print(text2audio("强哥威武"))
#.2 语音转文本.
def audio2text(file_name):
file_pcm_path =os.path.join(setting.AUDIO_FILE_PATH,file_name)
cmd_str =f"ffmpeg -y -i {file_pcm_path} -acodec pcm_s16le -f s16le -ac 1 -ar 16000 {file_pcm_path}.pcm"
os.system(cmd_str)
with open(f"{file_pcm_path}","rb") as f :
audio_context =f.read()
res =baidu_client.asr(audio_context,"pcm",16000,{"dev_pid": 1537})
if res.get('err_no'):
return res
return res.get("result")[0]
#
print(audio2text("qiang.wma"))
#.3 语音识别
def my_nlp(text):
if baidu_nlp.simnet("你今年几岁了 ",text).get("score")>=0.72:
print(baidu_nlp.simnet("你今年几岁了 ",text).get("score"))
return "我今年73岁了,不然84岁也行"
if baidu_nlp.simnet("你叫什么名字",text).get("score")>=0.72:
return "我的名字叫做小嘿嘿"
if baidu_nlp.simnet("你在哪里学习",text).get("score")>=0.72:
return "我在学习python"
print(my_nlp('你多大了')) #结果:我今年73岁了,不然84岁也行
print(my_nlp("你的名字是")) #结果:我的名字叫做小嘿嘿