1. 程式人生 > >118 falsk智能機器人 語音合成 語音識別

118 falsk智能機器人 語音合成 語音識別

openapi return 博客 val nbsp ict ins use won

主要內容: 博客drogonfire博客

語音合成: ai.baidu.com.. 查看文檔的具體內容.

語音識別: ai.baidu.com.. 查看文檔的具體內容.

智能機器人: www.tuling123.com

from aip import AipSpeech, AipNlp
APP_ID = ‘15217769‘
API_KEY = ‘j6C0iHttxaLcPIVqlynHyuP9‘
SECRET_KEY = ‘Symuy2zsS3LB2N4Lfr5ic7rTKCb6M26W ‘

client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
nlp_client = AipNlp(APP_ID, API_KEY, SECRET_KEY)
result  = client.synthesis(‘美好的一天‘, ‘zh‘, 1, {
    ‘vol‘: 5,
})

# 識別正確返回語音二進制 錯誤則返回dict 參照下面錯誤碼
if not isinstance(result, dict):
    with open(‘auido.mp3‘, ‘wb‘) as f:
        f.write(result)
import os
# 讀取文件
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(‘1.m4a‘), ‘pcm‘, 16000, {
    ‘dev_pid‘: 1536,
})
print(res)
Q = res.get("result")[0]

print(Q)
if nlp_client.simnet(Q, "講個故事吧").get("score") >= 0.7:
    # 語音合成
    A = "當然好了."
    result = client.synthesis(A, ‘zh‘, 1, {
        "per": 4,
        "pit": 8,
        "spd": 4,
        ‘vol‘: 5,
    })
    if not isinstance(result, dict):
        with open(‘auido.mp3‘, ‘wb‘) as f:
            f.write(result)
    # 讀取聲音,
    os.system(‘auido.mp3‘)
else:
    import tuling
    A = tuling.rbots("講個故事吧", "haha")
    result =client.synthesis(A, ‘zh‘, 1, {
        "per": 4,
        "pit": 8,
        "spd": 4,
        ‘vol‘: 5,
    })
    if not isinstance(result, dict):
        with open(‘auido.mp3‘, ‘wb‘) as f:
            f.write(result)

    os.system(‘auido.mp3‘)

 函數:

import requests
url = "http://openapi.tuling123.com/openapi/api/v2"
data = {
	"reqType":0,
    "perception": {
        "inputText": {
            "text": "鄭州"
        },
    },
    "userInfo": {
        "apiKey": "dd4648b191304eecaabd82df63e7e354",
        "userId": "wonderful"
    }
}

def rbots(text, uid):
    data["perception"]["inputText"]["text"] = text
    data["userInfo"]["userId"]= uid
    res = requests.post(url, json=data)
    res_json = res.json()
    return res_json.get("results")[0].get("values").get("text")

  

 

118 falsk智能機器人 語音合成 語音識別