AI-901 ยท Text and speech solutions
Speech app with Azure Speech (Python)
Reading Python that configures a speech resource, recognizes speech from a microphone or file, synthesizes spoken audio with a chosen voice and handles results and cancellation.
Key points
- pip install azure-cognitiveservices-speech; import azure.cognitiveservices.speech as speechsdk.
- SpeechConfig: key and endpoint.
- Recognition: AudioConfig(use_default_microphone=True) or AudioConfig(filename=...), SpeechRecognizer, recognize_once_async().
- Synthesis: speech_synthesis_voice_name, SpeechSynthesizer, speak_text_async(text).
- Check result.reason (RecognizedSpeech, NoMatch, Canceled); on Canceled read cancellation_details.
Practice questions
Which PyPI package provides the Speech SDK for Python?
- azure-ai-textanalytics
- azure-cognitiveservices-speech
- azure-ai-projects
- azure-identity
Show answer
Answer: B. azure-cognitiveservices-speech
azure-cognitiveservices-speech is the Speech SDK package, imported as azure.cognitiveservices.speech. azure-ai-textanalytics is the Language client.
Which pieces belong in a minimal text-to-speech script with the Speech SDK?
- speech_config.speech_synthesis_voice_name set to a neural voice
- AudioOutputConfig pointing to the default speaker or a file
- A check for ResultReason.SynthesizingAudioCompleted
- A SpeechRecognizer to produce the audio
- add_target_language to pick the spoken language
Show answer
Answer: A. speech_config.speech_synthesis_voice_name set to a neural voice, B. AudioOutputConfig pointing to the default speaker or a file, C. A check for ResultReason.SynthesizingAudioCompleted
You choose a voice on SpeechConfig, send the audio to a speaker or file with an output config, and check the result reason. SpeechRecognizer is for the opposite direction, and a translation target is only for translation.
recognize_once_async keeps listening and returns text for as long as the speaker talks, however long the audio is.
Show answer
Answer: False
False. It transcribes a single utterance of up to about 30 seconds or until silence; longer audio needs continuous recognition.
Practice 8 questions on this topic in Bitzu
Short lessons, instant explanations and spaced review of what you get wrong. Free on iOS and Android.
