-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.py
More file actions
58 lines (46 loc) · 1.51 KB
/
example_test.py
File metadata and controls
58 lines (46 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import base64
import urllib
import requests
import json
API_KEY = "MrDbZCeiFV9ljw58kHDwH8iA"
SECRET_KEY = "TxsMHM3fa1k7RZ4GnScQfn6lGQose0HE"
def main():
url = "https://vop.baidu.com/server_api"
payload = json.dumps({
"format": "wav",
"rate": 16000,
"channel": 1,
"cuid": "1R21mHHqmfLRolkqMQhdQjM9IdLuN0h1",
"token": get_access_token(),
"dev_pid": 1537,
"speech": get_file_content_as_base64("Chinese_test.wav"),
"len": 27773
})
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
def get_file_content_as_base64(path, urlencoded=False):
"""
获取文件base64编码
:param path: 文件路径
:param urlencoded: 是否对结果进行urlencoded
:return: base64编码信息
"""
with open(path, "rb") as f:
content = base64.b64encode(f.read()).decode("utf8")
if urlencoded:
content = urllib.parse.quote_plus(content)
return content
def get_access_token():
"""
使用 AK,SK 生成鉴权签名(Access Token)
:return: access_token,或是None(如果错误)
"""
url = "https://aip.baidubce.com/oauth/2.0/token"
params = {"grant_type": "client_credentials", "client_id": API_KEY, "client_secret": SECRET_KEY}
return str(requests.post(url, params=params).json().get("access_token"))
if __name__ == '__main__':
main()