gpt4 book ai didi

python - Azure 认知服务文本转语音 REST API 中的 requests.exceptions.ConnectTimeout 错误

转载 作者:行者123 更新时间:2023-12-03 06:18:21 24 4
gpt4 key购买 nike

因此,我一直在尝试使用 Azure 认知服务文本转语音 REST API 处理一个包含数千个文本文件的文件夹,将每个文件转换为语音。它工作得很好,直到它不起作用。多次成功转换后出现错误。我希望有一个稳定的连接,这样我就可以可靠地让脚本运行,而不必在每次出现错误时手动重新启动。

TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='eastus.api.cognitive.microsoft.com', port=443): Max retries exceeded with url: /sts/v1.0/issueToken (Caused by ConnectTimeoutError(<urllib3.connection.HTTPSConnection object at 0x000001F63AF32650>, 'Connection to eastus.api.cognitive.microsoft.com timed out. (connect timeout=None)'))

raise ConnectTimeout(e, request=request)
requests.exceptions.ConnectTimeout: HTTPSConnectionPool(host='eastus.api.cognitive.microsoft.com', port=443): Max retries exceeded with url: /sts/v1.0/issueToken (Caused by ConnectTimeoutError(<urllib3.connection.HTTPSConnection object at 0x000001F63AF32650>, 'Connection to eastus.api.cognitive.microsoft.com timed out. (connect timeout=None)'))

这是我当前的脚本:

import os
import requests
import time
import chardet

subscription_key = 'here my subscription key'
region = 'eastus'
voice_name = 'es-MX-DaliaNeural'
output_format = 'audio-24khz-96kbitrate-mono-mp3'

tts_url = f'https://{region}.tts.speech.microsoft.com/cognitiveservices/v1'
headers = {
'Authorization': '',
'Content-Type': 'application/ssml+xml',
'X-Microsoft-OutputFormat': output_format,
'User-Agent': 'YOUR_RESOURCE_NAME'
}

# looping through all text files in the input folder
input_folder = 'C:/path/to/text/files'
output_folder = 'C:/path/to/folder'
for filename in os.listdir(input_folder):
# Check if the file is a text file
if filename.endswith('.txt'):
# Read the contents of the file and detect the encoding
with open(os.path.join(input_folder, filename), 'rb') as f:
rawdata = f.read()
encoding = chardet.detect(rawdata)['encoding']
text = rawdata.decode(encoding)

# creating the SSML body for the TTS request
ssml = f'<speak version="1.0" xmlns="http://www.w3.org/2001/10/synthesis" xmlns:mstts="https://www.w3.org/2001/mstts" xml:lang="es-MX"><voice name="{voice_name}">{text}</voice></speak>'

# getting the access token for the TTS service
token_url = f'https://{region}.api.cognitive.microsoft.com/sts/v1.0/issueToken'
token_headers = {'Ocp-Apim-Subscription-Key': subscription_key}
response = requests.post(token_url, headers=token_headers)
access_token = response.text

headers['Authorization'] = f'Bearer {access_token}'

response = requests. Post(tts_url, headers=headers, data=ssml.encode('utf-8'))

if response.status_code == 200:
# save the audio content to a file
audio_filename = os.path.splitext(filename)[0] + '.mp3'
with open(os.path.join(output_folder, audio_filename), 'wb') as f:
f.write(response.content)
print(f'Successfully converted "{filename}" to speech')
else:
print(f'Error converting "{filename}" to speech: {response.content}')

time. Sleep(30)

我在每次转换之间留了 30 秒,但它不起作用。它转换 20-30 个文件,然后转换错误。有什么帮助可以获得更稳定的过程吗?

谢谢。

最佳答案

使用下面的代码片段,我尝试一次转换 10 个文件、20 个文件,然后是 100 个文件。到目前为止,此代码片段在转换过程中没有任何中断:

 import  os
import requests
import time
import chardet
import retrying

subscription_key = '<subscription_key>'
region = 'eastus'
voice_name = 'en-IN-NeerjaNeural'
output_format = 'audio-24khz-96kbitrate-mono-mp3'

tts_url = f'https://eastus.tts.speech.microsoft.com/cognitiveservices/v1'

headers = {
'Authorization': f'Bearer <subscription_key>',
'Content-Type': 'application/ssml+xml',
'X-Microsoft-OutputFormat': output_format,
'User-Agent': 'snap'
}

@retrying.retry(wait_exponential_multiplier=1000, wait_exponential_max=10000, stop_max_attempt_number=5)

def post_request(url, headers, data):
response = requests.post(url, headers=headers, data=data, timeout=30)
response.raise_for_status()
return response

input_folder = 'C:/Users/kamali/Documents/python,tts/input'
output_folder = 'C:/Users/kamali/Documents/python,tts/output'

for filename in os.listdir(input_folder):
if filename.endswith('.txt'):

with open(os.path.join(input_folder, filename), 'rb') as f:
rawdata = f.read()
encoding = chardet.detect(rawdata)['encoding']
text = rawdata.decode(encoding)


ssml = f'<speak version="1.0" xmlns="http://www.w3.org/2001/10/synthesis" xmlns:mstts="https://www.w3.org/2001/mstts" xml:lang="es-MX"><voice name="{voice_name}">{text}</voice></speak>'
token_url = f'https://eastus.api.cognitive.microsoft.com/sts/v1.0/issueToken'

token_headers = {'Ocp-Apim-Subscription-Key': subscription_key}
response = post_request(token_url, headers=token_headers, data=None)
access_token = response.text

headers['Authorization'] = f'Bearer {access_token}'
response = post_request(tts_url, headers=headers, data=ssml.encode('utf-8'))
with open(os.path.join(output_folder, f'{filename}.mp3'), 'wb') as f:
f.write(response.content)

time.sleep(1)

引用:MS Doc of Text-To-Speech conversion using Python.

结果:

enter image description here

对于您遇到的错误,Python 示例 Site 中提到了它服务器应该处于启动和运行状态,并具有良好的配置,例如用于连接的代码片段、时间限制、最大重试次数等。

关于python - Azure 认知服务文本转语音 REST API 中的 requests.exceptions.ConnectTimeout 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76060592/

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