AT&Tスピーチを使用してPythonを使ってテキストAPI
-
21-12-2019 - |
質問
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'}
.
しかし、私はファイルの送信方法がわからない。誰かが助けになることができますか?
解決
これは私が働いていたもので、リクエスト図書館を使って、他の2つのリソースを使って
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/attspehapi
http://changingjasper.blogspot。COM / 2014/06 /メイクジャスパー-AT-apt-api.html
使用法は、このファイルをAttEngine
として保存したと仮定して、以下のようなものです。from ATTEngine import ATTSpeech
a = ATTSpeech()
a.text_from_file('/Users/issackelly/Desktop/here.wav')
. 所属していません StackOverflow