首页 > 解决方案 > 使用文本到语音 (TTS) 天蓝色认知服务时出现 CORS 错误

问题描述

我使用了来自 azure 的 TTS(文本到语音)认知服务,使用的库是 python azure-cognitiveservices-speech == 1.17.0 我最初尝试并将其部署在 Heroku 云中,它按预期工作。

但是,当我在 VM 中部署它时,我收到了 CORS 错误。我的 CORS 设置正确,我尝试允许所有也没有帮助的主机('*')(我的后端是 Django)

speech_key = settings.SPEECH_KEY
service_region = settings.SERVICE_REGION
speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)
speech_config.speech_synthesis_voice_name = "ar-SA-Naayf"
filename = './audio.wav'
filename = filename.replace('.wav', '_' + str(random.randint(10, 10000)) + '.wav')
audio_file_path = Path(str(filename)).resolve()
audio_config = AudioOutputConfig(filename=str(audio_file_path))

# Creates a speech synthesizer using the default speaker as audio output.
speech_synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config, audio_config=audio_config)

# Receives a text from console input.
text = "Hello World"
result = speech_synthesizer.speak_text_async(text).get()

在我用于 CORS 的 prod.py 中

ALLOWED_HOSTS = ['api.abc.com']

在我的 CORS 的 settings.py 中

# Cors Header
# Allowed cors headers
CORS_ORIGIN_ALLOW_ALL = True
CORS_ALLOW_HEADERS = ('Authorization', 'Content-Type', 'Cache-Control', 'X-Requested-With')

错误 :

Access to XMLHttpRequest at 'https://api.abc.com/tts/' from origin 'https://api-dev.abc.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

我应该研究或更改配置的任何设置?

标签: azurecorsazure-cognitive-servicesdjango-cors-headers

解决方案


推荐阅读