人工智能(百度AI+图灵)

基于百度AIP + 图灵实现语音识别

登录百度AI官网: http://ai.baidu.com

1. 创建应用, 选择要添加的功能, 如: 自然语言处理

2. 查看相应文档, 实现响应的功能 (一般选择SDK文档)

语音合成

pip install baidu-aip

# 基于 python 实现语音合成

from aip import AipSpeech

""" 你的 APPID AK SK """
APP_ID = '你的 App ID'
API_KEY = '你的 Api Key'
SECRET_KEY = '你的 Secret Key'

client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)

def radio_identify(text):
    result = client.synthesis(text, 'zh', 1, {
        'vol': 5,  # 音量,取值0-15,默认为5中音量
        'per': 0,  # 发音人选择, 0:女声,1:男声,3:情感合成-度逍遥,4:情感合成-度丫丫,默认:普通女
        'pit': 5,  # 音调,取值0-9,默认为5中语调
        'spd': 5,  # 语速,取值0-9,默认为5中语速
    })

    # 识别正确返回语音二进制 错误则返回dict 参照下面错误码
    if not isinstance(result, dict):
        with open('audio.mp3', 'wb') as f:
            f.write(result)


text = "讲个笑话"
radio_identify(text)

# 合成文本长度必须小于1024字节,如果本文长度较长,可以采用多次请求的方式。

语音识别

import os
from aip import AipSpeech
from 语音合成 import radio_identify

""" 你的 APPID AK SK """
APP_ID = '你的 App ID'
API_KEY = '你的 Api Key'
SECRET_KEY = '你的 Secret Key'

client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)

# 读取文件
def get_file_content(filePath):
    os.system(f"ffmpeg -y  -i {filePath}  -acodec pcm_s16le -f s16le -ac 1 -ar 16000 {filePath}.pcm")   # 见文件转为pcm格式
    with open(f"{filePath}.pcm", 'rb') as fp:
        return fp.read()

# 识别本地文件
res = client.asr(get_file_content('audio.mp3'), 'pcm', 16000, {
    'dev_pid': 1536,   # 语言种类 1536:普通话
})
print(res.get('result')[0])

if res.get('result')[0] == "你叫什么名字":
    radio_identify('我叫小帅哥')
    
# format (String)  语音文件的格式,pcm 或者 wav 或者 amr。
注意:
    这里需要处理你的音频文件为.pcm格式
1. 通过 FFmpeg 转为.pcm格式 (不能直接修改后缀名):
百度链接:https://pan.baidu.com/s/1jonSAa_TG2XuaJEy3iTmHg
密   码:w6hk

2. 安装后添加环境变量

3. 通过以下命令实现文件转换
命令: ffmpeg -y  -i audio.mp3  -acodec pcm_s16le -f s16le -ac 1 -ar 16000 audio.pcm

自然语言处理

NLP: Natural language processing
# 基于AipNlp实现语言处理, 通过短文本相似度, 进行判断语句

from aip import AipSpeech, AipNlp
import os

""" 你的 APPID AK SK """
APP_ID = '15838232'
API_KEY = 'TUL9jIGgWbB2lmdfxsSta8Qq'
SECRET_KEY = 'KeLTN8EwopzGmV0pXHaXdfXXRM8l9TRf'

client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
nlp_client = AipNlp(APP_ID, API_KEY, SECRET_KEY)  # 实例化AipNlp

# 语音识别部分
# 读取文件
def get_file_content(filePath):
    os.system(f"ffmpeg -y  -i {filePath}  -acodec pcm_s16le -f s16le -ac 1 -ar 16000 {filePath}.pcm")
    with open(f"{filePath}.pcm", 'rb') as fp:
        return fp.read()

# 识别本地文件
res = client.asr(get_file_content('audio.mp3'), 'pcm', 16000, {
    'dev_pid': 1536,  # 语言种类 1536:普通话
})

text = res.get('result')[0]

# 智能问答
def my_nlp(text):
    if nlp_client.simnet(text, "你叫什么名字").get('score') >= 0.68: # 带参数调用短文本相似度
        A = '我叫小帅哥'      # score 为两个文本相似度得分
        return A
    elif nlp_client.simnet(text, "你今年几岁了").get('score') >= 0.58:
        A = '我今年18岁了, 哈哈'
        return A

    A = '我不知道你在说什么'
    return A

# 语音合成
A = my_nlp(text)
result = client.synthesis(A, 'zh', 1, {
    'vol': 5,
    'per': 0,
    'pit': 5,
    'spd': 3,
})

# 识别正确返回语音二进制 错误则返回dict 参照下面错误码
if not isinstance(result, dict):
    with open('audio.mp3', 'wb') as f:
        f.write(result)

# 自动读取文件
os.system("audio.mp3")

图灵的简单应用

图灵官网: http://www.tuling123.com

1. 创建一个聊天机器人

2. 查看api使用文档, 进入接入教程

3. 通过requests, 发送post请求接口

4. 参数详解
1) 接口地址: http://openapi.tuling123.com/openapi/api/v2
2) 请求方式: HTTP请求, POST请求方式
3) 请求参数格式为json
示例:
{
	"reqType":0,
    "perception": {
        "inputText": {            # 文本
            "text": "附近的酒店"
        },
        "inputImage": {           # 图片地址
            "url": "imageUrl"
        },
        "selfInfo": {             # 地理位置信息
            "location": {
                "city": "北京",
                "province": "北京",
                "street": "信息路"
            }
        }
    },
    "userInfo": {
        "apiKey": "",
        "userId": ""
    }
}

# 注意:输入参数必须包含inputText或inputImage或inputMedia!
userInfo:
	apiKey	String	必填	32位	机器人标识
	userId	String	必填	长度小于等于32位	用户唯一标识
import requests

data = {
    "reqType": 0,
    "perception": {
        "inputText": {
            "text": "北京"
        },
    },
    "userInfo": {
        "apiKey": "a91a1022ec7f41ba9f220daf64411686",
        "userId": "2"
    }
}

res = requests.post('http://openapi.tuling123.com/openapi/api/v2', json=data)
text = res.json().get('results')[0].get('values').get('text')
print(text)

图灵 + 百度AIP 实现语音技术

from aip import AipSpeech, AipNlp
import os
import requests

""" 你的 APPID AK SK """
APP_ID = '15838232'
API_KEY = 'TUL9jIGgWbB2lmdfxsSta8Qq'
SECRET_KEY = 'KeLTN8EwopzGmV0pXHaXdfXXRM8l9TRf'

client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
nlp_client = AipNlp(APP_ID, API_KEY, SECRET_KEY)

# 语音识别部分
# 读取文件
def get_file_content(filePath):
    os.system(f"ffmpeg -y  -i {filePath}  -acodec pcm_s16le -f s16le -ac 1 -ar 16000 {filePath}.pcm")
    with open(f"{filePath}.pcm", 'rb') as fp:
        return fp.read()

# 识别本地文件
res = client.asr(get_file_content('audio.mp3'), 'pcm', 16000, {
    'dev_pid': 1536,  # 语言种类 1536:普通话
})

text = res.get('result')[0]

def to_tuling(text, uid):
    data = {
        "reqType": 0,
        "perception": {
            "inputText": {
                "text": "北京"
            },
        },
        "userInfo": {
            "apiKey": "a91a1022ec7f41ba9f220daf64411686",
            "userId": "2"
        }
    }

    data["perception"]["inputText"]["text"] = text
    data["userInfo"]["userId"] = uid

    res = requests.post('http://openapi.tuling123.com/openapi/api/v2', json=data)
    text = res.json().get('results')[0].get('values').get('text')
    print(text)
    return text


# 智能问答
def my_nlp(text):
    if nlp_client.simnet(text, "你叫什么名字").get('score') >= 0.68:  # 带参数调用短文本相似度
        A = '我叫小帅哥'  # score 为两个文本相似度得分
        return A
    elif nlp_client.simnet(text, "你今年几岁了").get('score') >= 0.58:
        A = '我今年18岁了, 哈哈'
        return A

    A = to_tuling(text, '999')
    return A

# 语音合成
A = my_nlp(text)
result = client.synthesis(A, 'zh', 1, {
    'vol': 5,
    'per': 0,
    'pit': 5,
    'spd': 3,
})

# 识别正确返回语音二进制 错误则返回dict 参照下面错误码
if not isinstance(result, dict):
    with open('audio.mp3', 'wb') as f:
        f.write(result)

# 自动读取文件
os.system("audio.mp3")

posted @ 2019-07-08 15:29  言值  阅读(1752)  评论(0编辑  收藏  举报