没有介绍,直接上代码
import requests
import json
import base64
import hashlib
import time
from aip import AipSpeech
# 获取用户输入的参数
APP_ID = input('请输入您的讯飞星火应用ID:')
API_KEY = input('请输入您的讯飞星火API密钥:')
SECRET_KEY = input('请输入您的讯飞星火密钥:')
CUID = input('请输入您的讯飞账号ID:')
TOKEN = input('请输入您的讯飞API令牌:')
CNAME = input('请输入您的讯飞应用名称:')
# 初始化AipSpeech对象
client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
# 定义语音转写函数
def speech_to_text(file_path):
with open(file_path, 'rb') as f:
audio_data = f.read()
result = client.asr(audio_data, 'pcm', 16000, {
'dev_pid': 1537,
})
if result['err_no'] == 0:
return result['result'][0]
else:
return None
# 定义访问讯飞星火的程序函数
def access_xf_program(text):
url = 'http://openapi.xfyun.cn/v2/ai/synthesis'
headers = {'Content-Type': 'application/json;charset=utf-8'}
data = {
'text': text,
'percept': 'online',
'spd': 5,
'pit': 5,
'vol': 5,
'perc': 5,
'spe': 1200,
'pits': 1,
'vols': 1,
'percs': 1,
'spes': 1200,
'channel': 1,
'cuid': CUID,
'token': TOKEN,
'lry': 3,
'rate': 0,
'type': 1,
'cname': CNAME,
'sd': 0,
'bgm': {
'type': 0,
'name': '中性',
'uri': '',
'loop': 0,
'onTime': 0,
'offset': 0,
'volume': 0,
'duration': 0,
'uriList': [],
},
'emotion': {
'type': 0,
'name': '中立',
'uri': '',
'loop': 0,
'onTime': 0,
'offset': 0,
'volume': 0,
'duration': 0,
'uriList': [],
},
'field': {
'type': 0,
'name': '中性',
'uri': '',
'loop': 0,
'onTime': 0,
'offset': 0,
'volume': 0,
'duration': 0,
'uriList': [],
},
}
response = requests.post(url, headers=headers, data=json.dumps(data))
if response.status_code == 200:
result = json.loads(response.content.decode('utf-8'))
if result['err_no'] == 0:
return result['result'][0]
else:
return None
else:
print('访问讯飞星火程序失败,请重试。')
return None
# 定义主函数
def main():
print('欢迎使用讯飞语音转写AI助手!')
while True:
print('请选择操作:')
print('1. 语音转写')
print('2. 语音识别')
print('3. 访问讯飞星火程序')
print('4. 退出')
choice = input('请输入数字选择:')
if choice == '1':
file_path = input('请输入音频文件路径:')
text = speech_to_text(file_path)
if text is not None:
print('识别结果:', text)
program_text = access_xf_program(text)
if program_text is not None:
print('讯飞星火程序返回结果:', program_text)
else:
print('访问讯飞星火程序失败,请重试。')
else:
print('语音转写失败,请重试。')
elif choice == '2':
file_path = input('请输入音频文件路径:')
text = recognize_speech(client, file_path)
if text is not None:
print('识别结果:', text)
else:
print('语音识别失败,请重试。')
elif choice == '3':
print('感谢使用讯飞语音转写AI助手,再见!')
break
elif choice == '4':
print('感谢使用讯飞语音转写AI助手,再见!')
break
else:
print('无效的选择,请重新输入。')
if __name__ == '__main__':
main()
下载链接:
下载:https://wwzc.lanzouj.com/ia31k19o1qyb 密码:d4im