This repository has been archived by the owner on Sep 14, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspeech.py
56 lines (43 loc) · 1.64 KB
/
speech.py
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
import json
import uuid
from urllib.parse import urlencode
import requests
from bs4 import BeautifulSoup
class RequestError(Exception):
pass
class UnknownValueError(Exception):
pass
def recognize_bing(audio, key, language="en-US"):
session = requests.session()
credential_url = "https://api.cognitive.microsoft.com/sts/v1.0/issueToken"
credential_request = session.post(credential_url, data=b"", headers={
"Content-type": "application/x-www-form-urlencoded",
"Content-Length": "0",
"Ocp-Apim-Subscription-Key": key,
})
access_token = BeautifulSoup(credential_request.content, 'html.parser')
url = "https://speech.platform.bing.com/speech/recognition/interactive/cognitiveservices/v1?{}".format(urlencode({
"language": language,
"locale": language,
"requestid": uuid.uuid4(),
}))
try:
response = session.post(url, data=stream_audio_file(audio), headers={
"Authorization": "Bearer {}".format(access_token),
"Content-type": "audio/ogg; codec=\"audio/pcm\"; samplerate=16000",
"Transfer-Encoding": "chunked",
})
except:
raise RequestError()
result = json.loads(str(BeautifulSoup(response.content, 'html.parser')))
if "RecognitionStatus" not in result or result["RecognitionStatus"] != "Success" or "DisplayText" not in result:
raise UnknownValueError()
return result["DisplayText"]
def stream_audio_file(speech_file):
# Chunk audio file
with open(speech_file, 'rb') as f:
while 1:
data = f.read(1024)
if not data:
break
yield data