简单的人工智能对话

下面是一个简单的人工智能对话的程序,做起来很简单基于俩个工具就可以完成了.

百度AI开放平台:  ai.baidu.com    里面有很多实现人工智能的模型可以免费使用

图灵机器人: www.turingapi.com  定制一个属于自己的专属智能机器人

 

在进行语音识别的时候,要求传入音频的格式是pcm,我们电脑的录音机的格式不是人家要求的需要对格式进行转码,在这之前需要下载安装插件  http://ffmpeg.org/download.html

这样就可以玩了.

首先我们需要去录音机录制一段语音.稍后要进行使用.

下面是具体代码,语音合成和识别的代码其实特别简单,按照百度文档里的内容复制粘贴就行

 

from aip import AipSpeech
from to_tolin import to_tolin
import os
"""读取音频文件,语音转化成字符串传给图灵"""

""" 你的 APPID AK SK """
APP_ID = '15838233'
API_KEY = 'Xy8d3wRbsjfmpGsPryvNexcL'
SECRET_KEY = 'PmpwVdVj0mHU1BTeAhyXZNlBi8CrPOP2'

client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)

# 读取文件
def get_file_content(filePath):
    new = filePath.split(".")[0]
    os.system(f"ffmpeg -y  -i {filePath}  -acodec pcm_s16le -f s16le -ac 1 -ar 16000 {new}.pcm")
    with open(f"{new}.pcm", 'rb') as fp:
        return fp.read()

# 识别本地文件
ret = client.asr(get_file_content('auido.mp3'), 'pcm', 16000, {
    'dev_pid': 1536,
})
ret_new = ret.get("result")[0]
to_tolin(ret_new)
语音识别的代码
import requests
from output import VoiceOutput
"""借用第三方,进行语音智能聊天,图灵返回的字符串传回给output.py,把语言合成并播放出来"""
def to_tolin(text):
    data = {
        "perception": {
            "inputText": {
                "text": f"{text}"
            },

        },
        "userInfo": {
            "apiKey": "5598e8fcceda4ea4a005892bcb053ee8",
            "userId": "1"
        }
    }
    res = requests.post("http://openapi.tuling123.com/openapi/api/v2",json=data)
    new_res = res.json().get("results")[0].get("values").get("text")
    print(new_res)
    VoiceOutput(new_res)
语音识别后传给图灵机器人进行对话
from aip import AipSpeech
import os
"""
把图灵机器人回复的话通过语音合成,输出
"""

def VoiceOutput(text):
    """ 你的 APPID AK SK """
    APP_ID = '15838233'
    API_KEY = 'Xy8d3wRbsjfmpGsPryvNexcL'
    SECRET_KEY = 'PmpwVdVj0mHU1BTeAhyXZNlBi8CrPOP2'

    client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)

    result = client.synthesis(f'{text}', 'zh', 1, {
        'vol': 5,
        'per': 4,
        'pit': 7,
        'spd': 4
    })
    print(text)
    # 识别正确返回语音二进制 错误则返回dict 参照下面错误码
    if not isinstance(result, dict):
        with open('auido.mp3', 'wb') as f:
            f.write(result)
    os.system('auido.mp3')
图灵机器人返回的文本进行语音合成并播放

由于我懒得去一边一边去录,所以我做的是机器人自问自答的   哈哈哈哈

 

posted @ 2019-03-26 18:02  哇!好难  阅读(2050)  评论(0编辑  收藏  举报