在 Python 中使用 AT&T Speech to Text API
Posted
技术标签:
【中文标题】在 Python 中使用 AT&T Speech to Text API【英文标题】:Using the AT&T Speech to Text API With Python 【发布时间】:2014-04-13 04:26:35 【问题描述】:我正在尝试使用 AT&T 语音转文本 API。到目前为止,我可以获得访问令牌
def get_access_token(client_id, client_secret):
headers = 'Content-Type': 'application/x-www-form-urlencoded', 'Accept': 'application/json'
data = 'client_id': client_id, 'client_secret': client_secret, 'scope': 'SPEECH',
'grant_type': 'client_credentials'
response = requests.post(oauth_url, data=data, headers=headers)
return response.text
到目前为止,这就是我发送音频文件以获取 json 响应的内容:
def get_text_from_file(file, access_token):
headers = 'Authorization': 'Bearer ' + access_token, 'Accept': 'application/json', 'Content-Type': 'audio/wav',
'X-SpeechContext': 'Generic', 'Connection': 'Keep-Alive'
但我不确定如何发送文件。有人可以帮忙吗?
【问题讨论】:
【参考方案1】:这是我刚刚开始使用的请求库,以及我将在下面链接的其他一些资源
import json
import requests
class ATTSpeech:
CLIENT_ID = "SOME"
CLIENT_SECRET = "ID"
TOKEN = None
def __init__(self, *args, **kwargs):
self.get_token()
def get_token(self):
# Get Access Token via OAuth.
# https://matrix.bf.sl.attcompute.com/apps/constellation-sandbox
response = requests.post("https://api.att.com/oauth/token",
"client_id": self.CLIENT_ID,
"client_secret": self.CLIENT_SECRET,
"grant_type": "client_credentials",
"scope": "SPEECH,STTC"
)
content = json.loads(response.content)
self.TOKEN = content["access_token"]
def text_from_file(self, path):
with open(path, 'rb') as f:
response = requests.post("https://api.att.com/speech/v3/speechToText",
headers =
"Authorization": "Bearer %s" % self.TOKEN,
"Accept": "application/json",
"Content-Type": "audio/wav",
"X-SpeechContext": "Generic",
, data=f)
content = json.loads(response.content)
return content
https://sites.google.com/site/brssbrss/attspeechapi
http://changingjasper.blogspot.com/2014/06/making-jasper-use-at-speech-api.html
用法如下所示,假设您将此文件保存为 ATTEngine
from ATTEngine import ATTSpeech
a = ATTSpeech()
a.text_from_file('/Users/issackelly/Desktop/here.wav')
【讨论】:
以上是关于在 Python 中使用 AT&T Speech to Text API的主要内容,如果未能解决你的问题,请参考以下文章