基于讯飞星火的语音AI

没有介绍,直接上代码

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值