奇迹969

 

百度ai 接口调用

1.百度智能云 

2.右上角 管理控制台

3.左上角产品服务 选择应用

4.创建应用

5.应用详情下面的查看文档

6.选择pythonSDK  查看下面快速入门文档  和  接口说明文档。

7.按步骤写.py文件

import os
from tl import go_tl
from aip import AipSpeech,AipNlp

""" 你的 APPID AK SK """
APP_ID = '16815882'
API_KEY = 'CwT84xR1L1s0ONs8dnhn2nuD'
SECRET_KEY = 'OqYUlXGjFWKPqajFNTVmXzyo6etOS9GI'

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

 

语音合成(字典中的参数 一定要按照说明书 填写  不然会报     501 错误     如果正确result为二进制码   错误为字典)

result = client.synthesis("     ", 'zh', 1, {
    'vol': 5,
    "pit": 15,
    "per": 4
})
print(result)
#识别正确返回语音二进制 错误则返回dict 参照下面错误码
if not isinstance(result, dict):
    with open('Anwser.mp3', 'wb') as f:
        f.write(result)

 

语音识别

def get_file_content(filePath):
    cmd = f"ffmpeg -y -i {filePath} -acodec pcm_s16le -f s16le -ac 1 -ar 16000 {filePath}.pcm"
    os.system(cmd)
    with open(f"{filePath}.pcm", 'rb') as fp:
        return fp.read()


#识别本地文件

res = client.asr(get_file_content('auido.mp3'), 'pcm', 16000, {
    'dev_pid': 1536,
})

Q=res.get("result")[0]

 

 

nlp 短文本相似度(在创建应用时一定要勾选  不然会报 errror_code : 6 错误与)

sim = NLP_client.simnet(Q,"你的名字是什么").get("score")
print(sim)
if sim >= 0.58:
    A = "我叫"
else:
    A = go_tl(Q)
    print(A)

 

 

引入图灵机器人    创建机器人    终端设置  api使用文档     apikey:617192b2130c48159b97b278220563cb    apii随便填写  text:问题 

import requests


def go_tl(Q):

    data = {
        "perception": {
            "inputText": {
                "text": Q
            },

        },
        "userInfo": {
            "apiKey": "617192b2130c48159b97b278220563cb",
            "userId": "123"
        }
    }
    res = requests.post("http://openapi.tuling123.com/openapi/api/v2", json=data)

    res_dict = res.json()
    print(res_dict)
    return res_dict.get("results")[0].get("values").get("text")

 

posted on 2019-07-16 11:12  奇迹969  阅读(872)  评论(0编辑  收藏  举报

导航