gpt4 book ai didi

python - 将 AT&T 语音转文本 API 与 Python 结合使用

转载 作者:行者123 更新时间:2023-11-28 17:41:56 26 4
gpt4 key购买 nike

我正在尝试使用 AT&T 语音转文本 API。至此,我可以获得访问 token

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/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 语音转文本 API 与 Python 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23039101/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com