我正在尝试使用AT&T语音到Text 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'}
.

但我不确定如何发送文件。任何人都可以帮忙吗?

有帮助吗?

解决方案

这就是我刚刚工作的,使用请求库以及几个其他资源,我将链接在下面

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/brsbrss/attspeechapi

http://changingjasper.blogspot。COM / 2014/06 /制作 - jasper-usion-at-speep-api.html

用法是如下所示,假设您将此文件保存为验证

from ATTEngine import ATTSpeech
a = ATTSpeech()
a.text_from_file('/Users/issackelly/Desktop/here.wav')
.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top