Thank you for using the Microsoft Q&A forum.
It seems that the SpeechConfig object does not directly expose the constructed endpoint URL attribute. Instead, it constructs the endpoint internally based on the provided subscription key and region.
To print the constructed endpoint URL, you can create the endpoint URL manually based on the provided region. Here's how you can modify the code to print the constructed endpoint URL based on the provided subscription key and region:
import azure.cognitiveservices.speech as speechsdk
def main():
subscription_key = "YOUR_SPEECH_KEY"
region = "YOUR_SPEECH_REGION"
# Construct endpoint URL based on the provided region
endpoint_url = "https://" + region + ".api.cognitive.microsoft.com/sts/v1.0/issuetoken"
# Print the constructed endpoint URL
print("Constructed Endpoint URL:", endpoint_url)
# Rest of your code.
speech_config = speechsdk.SpeechConfig(subscription=subscription_key, endpoint=endpoint_url)
audio_config = speechsdk.audio.AudioOutputConfig(use_default_speaker=True)
speech_config.speech_synthesis_voice_name = 'en-US-JennyMultilingualNeural'
speech_synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config, audio_config=audio_config)
TEXT = input("Enter the text to be speech-synthesized: ")
speech_synthesis_result = speech_synthesizer.speak_text_async(TEXT).get()
if speech_synthesis_result.reason == speechsdk.ResultReason.SynthesizingAudioCompleted:
print("Speech synthesized for text [{}]".format(TEXT))
elif speech_synthesis_result.reason == speechsdk.ResultReason.Canceled:
cancellation_details = speech_synthesis_result.cancellation_details
print("Speech synthesis canceled: {}".format(cancellation_details.reason))
if cancellation_details.reason == speechsdk.CancellationReason.Error:
if cancellation_details.error_details:
print("Error details: {}".format(cancellation_details.error_details))
print("Did you set the speech resource key and region values?")
if __name__ == "__main__":
main()
Output:
I also tried setting the subscription key and region. Below is the repro I tried at my end, and it was working as expected.
import os
import azure.cognitiveservices.speech as speechsdk
def main():
# ValueError: cannot construct SpeechConfig with both region and endpoint or host information
# ValueError: either subscription key or authorization token must be given along with a region
# https://learn.microsoft.com/en-us/python/api/azure-cognitiveservices-speech/azure.cognitiveservices.speech.speech?view=azure-python
speech_config = speechsdk.SpeechConfig(subscription="YOUR_SPEECH_KEY",
region="YOUR_SPEECH_REGION")
audio_config = speechsdk.audio.AudioOutputConfig(use_default_speaker=True)
# https://learn.microsoft.com/en-us/azure/ai-services/speech-service/language-support?tabs=tts
speech_config.speech_synthesis_voice_name='en-US-JennyMultilingualNeural'
speech_synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config, audio_config=audio_config)
TEXT = input("Enter the text to be speech-synthesised: ")
speech_synthesis_result = speech_synthesizer.speak_text_async(TEXT).get()
if speech_synthesis_result.reason == speechsdk.ResultReason.SynthesizingAudioCompleted:
print("Speech synthesized for text [{}]".format(TEXT))
elif speech_synthesis_result.reason == speechsdk.ResultReason.Canceled:
cancellation_details = speech_synthesis_result.cancellation_details
print("Speech synthesis canceled: {}".format(cancellation_details.reason))
if cancellation_details.reason == speechsdk.CancellationReason.Error:
if cancellation_details.error_details:
print("Error details: {}".format(cancellation_details.error_details))
print("Did you set the speech resource key and region values?")
if __name__ == "__main__":
main()
Output:
I hope you understand. Thank you.