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

问题描述 投票:0回答:0

所以,我一直在尝试处理一个包含数千个文本文件的文件夹,以使用 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 个文件,然后转换错误。对获得更稳定的过程有什么帮助吗?

谢谢。

python azure rest text-to-speech azure-cognitive-services
© www.soinside.com 2019 - 2024. All rights reserved.