gpt4 book ai didi

azure - 身份验证错误 Azure 语音转文本

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

我已经检查了SO中的其他问答,但他们的回答没有帮助。我正在运行语音转文本的示例代码。我尝试过 REST、Python 和 JavaSript,但在所有这些方法中我都收到 401 错误。我什至记录了错误,但没有发现任何有用的东西。我正在使用免费版本并在非公开的网络内运行。我在公共(public)网络上尝试过一次,得到了同样的错误。但下面的代码是在私有(private)中运行的。我没有找到任何文档说明我需要在网络中设置入站调用设置。

import sys
sys.path
import azure.cognitiveservices.speech as speechsdk
print("hello from Azure")
import os
from azure.cognitiveservices.speech.audio import AudioOutputConfig


def recognize_from_microphone():
print(os.environ.get('SPEECH_KEY'))
print(os.environ.get('SPEECH_REGION'))
print(os.environ.get('COGNITIVE_SERVICE_KEY'))
speech_config = speechsdk.SpeechConfig(subscription=os.environ.get('SPEECH_KEY'), region=os.environ.get('SPEECH_REGION'))
speech_config.speech_recognition_language="en-US"
speech_config.set_property(speechsdk.PropertyId.Speech_LogFilename, "LOGS.txt")

audio_config = speechsdk.audio.AudioConfig(use_default_microphone=True)
speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_config)

print("Speak into your microphone.")
speech_recognition_result = speech_recognizer.recognize_once()

# speech_recognition_result = speech_recognizer.recognize_once_async().get()

if speech_recognition_result.reason == speechsdk.ResultReason.RecognizedSpeech:
print("Recognized: {}".format(speech_recognition_result.text))
elif speech_recognition_result.reason == speechsdk.ResultReason.NoMatch:
print("No speech could be recognized: {}".format(speech_recognition_result.no_match_details))
elif speech_recognition_result.reason == speechsdk.ResultReason.Canceled:
cancellation_details = speech_recognition_result.cancellation_details
print("Speech Recognition canceled: {}".format(cancellation_details.reason))
if cancellation_details.reason == speechsdk.CancellationReason.Error:
print("Error details: {}".format(cancellation_details.error_details))
print("Did you set the speech resource key and region values?")

recognize_from_microphone()

错误:

错误详细信息:WebSocket 升级失败:身份验证错误 (401)。请检查订阅信息和地区名称。 session ID:

最佳答案

来自给定MS Doc的源代码.

代码:

import sys
import azure.cognitiveservices.speech as speechsdk
import os
from azure.cognitiveservices.speech.audio import AudioOutputConfig

def recognize_from_microphone():
subscription_key = "<your_key>"
region = "<your_region>"

speech_config = speechsdk.SpeechConfig(subscription=subscription_key, region=region)
speech_config.speech_recognition_language = "en-US"
speech_config.set_property(speechsdk.PropertyId.Speech_LogFilename, "LOGS.txt")

audio_config = speechsdk.audio.AudioConfig(use_default_microphone=True)
speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_config)

print("Speak into your microphone.")
speech_recognition_result = speech_recognizer.recognize_once()

if speech_recognition_result.reason == speechsdk.ResultReason.RecognizedSpeech:
print("Recognized: {}".format(speech_recognition_result.text))
elif speech_recognition_result.reason == speechsdk.ResultReason.NoMatch:
print("No speech could be recognized: {}".format(speech_recognition_result.no_match_details))
elif speech_recognition_result.reason == speechsdk.ResultReason.Canceled:
cancellation_details = speech_recognition_result.cancellation_details
print("Speech Recognition canceled: {}".format(cancellation_details.reason))
if cancellation_details.reason == speechsdk.CancellationReason.Error:
print("Error details: {}".format(cancellation_details.error_details))
print("Did you set the speech resource key and region values?")

recognize_from_microphone()

输出:它会提示对着麦克风说话。如下所示,

enter image description here

我得到了我所说内容的文本输出,

enter image description here

我得到了如下的LOG.txt文件,

enter image description here

关于azure - 身份验证错误 Azure 语音转文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76404329/

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