import json
import base64
import os
import requests
RATE = "16000"
FORMAT = "wav"
CUID="wate_play"
DEV_PID="1536"
def get_token():
server = "https://openapi.baidu.com/oauth/2.0/token?"
grant_type = "client_credentials"
#API Key
client_id = "vyYLov63W6x33nIPvwVdLLsX"
#Secret Key
client_secret = "EzgQoFF9xp62SeGCXCBaD8FjWNxvl9kZ"
# Spell it url
url ="%sgrant_type=%s&client_id=%s&client_secret=%s"%(server,grant_type,client_id,client_secret)
# obtain token
res = requests.post(url)
token = json.loads(res.text)["access_token"]
return token
def get_word(token):
wavPath = './static/yuyin/record.wav'
with open(wavPath, "rb") as f:
speech = base64.b64encode(f.read()).decode('utf8')
size = os.path.getsize(wavPath)
headers = { 'Content-Type' : 'application/json'}
url = "https://vop.baidu.com/server_api"
data={
"format":FORMAT,
"rate":RATE,
"dev_pid":DEV_PID,
"speech":speech,
"cuid":CUID,
"len":size,
"channel":1,
"token":token,
}
try:
req = requests.post(url,json.dumps(data),headers)
result = json.loads(req.text)
ret=result["result"][0]
except :
ret = None
return ret[:-1]
wavPath by wav File path
Secret key and Api key Parameters obtained for Baidu interface