Azure 文本转语音 (TTS) 失败,日语字符出现 HTTP 400 错误

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

我是一名学生,使用 Python 访问 TTS Azure 认知服务的 REST API。我正在测试使用 Azure 在线音频内容创建工具创建的文件,其中包含以下内容(我很高兴尝试文本转语音的日语翻译):

<speak xmlns="http://www.w3.org/2001/10/synthesis" xmlns:mstts="http://www.w3.org/2001/mstts" xmlns:emo="http://www.w3.org/2009/10/emotionml" version="1.0" xml:lang="en-US"><voice xml:lang="ja-JP" xml:gender="Female" name="ja-JP-NanamiNeural">テキスト読み上げを試すのが楽しみです</voice></speak>

该文件在工具中运行时可以工作,但当我使用 REST API 时会失败并出现 HTTP 400 错误。但是,当我使用以下文本代替日语汉字时,它也适用于 REST API:Kisuto yomiage o tamesu no ga tanoshimidesu

这是我使用的代码片段:

def send_text2(token,endpoint,uuid,ssml):
    headers = {
        'Authorization': 'Bearer '  + token,
        'Content-Type': 'application/ssml+xml',
        'X-Microsoft-OutputFormat': 'audio-48khz-96kbitrate-mono-mp3',
        'User-Agent': 'Application for Final',
        'X-ClientTraceId': uuid

    }
    vocalize_request = requests.post(endpoint, headers=headers, data=ssml)
    print(vocalize_request.status_code)
    # Retrieve mp3 data and write to file
    with open(f"vocalized_file-{uuid}.mp3", "wb") as binary_file:
    #Write bytes to file
        binary_file.write(vocalize_request.content)

我已经在原始英语以及德语和意大利语翻译上成功尝试了此代码,因此我对使用的端点和发送的令牌充满信心。我应该注意到俄语翻译也会失败并出现 HTTP 400 错误。

我尝试设置所有 xml:lang= “ja-JP”,确保格式为 uff-8,并删除 mstts 和 emo 命名空间,但所有这些都不起作用。我在谷歌上搜索并找到了另外两个可能的解决方案,这两个解决方案也不起作用:第一个,明确不设置 Content-Length 标头,第二个,将翻译后的文本包装在 中(第二个确实不应该有任何内容)效果)。

你知道这里可能发生什么吗?我不认为这是 TTS 中的错误,因为发生类似情况的唯一帐户似乎已经解决(尽管,正如我提到的,不设置内容长度似乎不起作用)。

谢谢。

azure text-to-speech python-unicode
1个回答
0
投票

我猜这个问题包括 xml 中的多字节字符。您可以使用 xml.etree.ElementTree 转义字符串。

  1. ssml 用于日语保存到 xml 文件。 (UTF-8 带 BOM 格式)
  2. 从 xmlfile 加载 ssml,如下所示:
import xml.etree.ElementTree as ET

tree = ET.parse('test.xml')
ssml = ET.tostring(tree.getroot(), encoding='utf8')
  1. 使用ssml执行post请求
send_text2(token,endpoint,uuid,ssml):
© www.soinside.com 2019 - 2024. All rights reserved.