图灵机器人

注册并访问

官网

 创建机器人

 

机器人设置

人物设置

 

对接

官方文档

 

import requests
from aip import AipSpeech
import os
from aip import AipNlp

""" 你的 APPID AK SK """
APP_ID = '15842727'
API_KEY = 'gBsfoHWw4pOh9n3sNhwoB853'
SECRET_KEY = '4e0WXxlTo5lMgFu45lLnO490SnnpLQLN'

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

res = nlp_client.simnet('你叫什么名字','你的名字叫什么')
print('两个字段的相识度',res.get('score'))

# 读取文件
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()


# 识别本地文件
ret = client.asr(get_file_content('xiaohua.wma'), 'pcm', 16000, {
    'dev_pid': 1536,
})

text = ret.get("result")[0]

#智能问答
def to_tuling(text,uid):
    data = {
        "perception": {
            "inputText": {
                "text": "北京"
            },
        },
        "userInfo": {
            "apiKey": "934b6f5ee4c44370bd2daf71ed7b0b77",
            "userId": "11"
        }
    }
    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.58:
        a = '我叫人工智障'
        return a

    if nlp_client.simnet(text,"你今年几岁了").get('score') >= 0.75:
        a = '永远18岁'
        return a
    a = to_tuling(text,'open123')
    return a
a = my_nlp(text)
result = client.synthesis(a, 'zh', 1, {
    'vol': 5,
    'spd': 4,
    'pit': 7,
    'per': 4,
})

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

os.system('audio.MP3')
讲笑话

 

posted @ 2019-03-25 22:06  答&案  阅读(403)  评论(0编辑  收藏  举报