Skip to content

Instantly share code, notes, and snippets.

@cristofima
Created July 3, 2023 02:17
Show Gist options
  • Select an option

  • Save cristofima/6a6ab55b9146ee3d176581524337b26a to your computer and use it in GitHub Desktop.

Select an option

Save cristofima/6a6ab55b9146ee3d176581524337b26a to your computer and use it in GitHub Desktop.
Text to Speech, Speech to Text, and Speech Translation using Azure Cognetive Services
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!pip install azure-cognitiveservices-speech"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"id": "ISfrwmurxjpj"
},
"outputs": [],
"source": [
"import azure.cognitiveservices.speech as speechsdk"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"id": "FICAbruKyNsa"
},
"outputs": [],
"source": [
"SPEECH_KEY = \"<YOUR_API_KEY>\"\n",
"SPEECH_REGION = \"<YOUR_REGION>\""
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"id": "iFpblA213Dpj"
},
"outputs": [],
"source": [
"def text_to_speech(text: str, audio_config):\n",
" speech_config = speechsdk.SpeechConfig(subscription=SPEECH_KEY, region=SPEECH_REGION)\n",
"\n",
" # The language of the voice that speaks\n",
" speech_config.speech_synthesis_voice_name = 'en-US-GuyNeural'\n",
"\n",
" speech_synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config, audio_config=audio_config)\n",
"\n",
" speech_synthesis_result = speech_synthesizer.speak_text_async(text).get()\n",
"\n",
" if speech_synthesis_result.reason == speechsdk.ResultReason.SynthesizingAudioCompleted:\n",
" print(\"Speech synthesized for text [{}]\".format(text))\n",
" elif speech_synthesis_result.reason == speechsdk.ResultReason.Canceled:\n",
" cancellation_details = speech_synthesis_result.cancellation_details\n",
" print(\"Speech synthesis canceled: {}\".format(cancellation_details.reason))\n",
" if cancellation_details.reason == speechsdk.CancellationReason.Error:\n",
" if cancellation_details.error_details:\n",
" print(\"Error details: {}\".format(cancellation_details.error_details))\n",
" print(\"Did you set the speech resource key and region values?\")"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"id": "E5o2JknezCVB"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Speech synthesized for text [New York is a great city to visit]\n"
]
}
],
"source": [
"text = \"New York is a great city to visit\"\n",
"audio_config = speechsdk.audio.AudioOutputConfig(filename=\"file1.mp3\")\n",
"text_to_speech(text, audio_config)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"id": "U9a9cHXs4nAq"
},
"outputs": [],
"source": [
"def speech_to_text():\n",
" speech_config = speechsdk.SpeechConfig(subscription=SPEECH_KEY, region=SPEECH_REGION)\n",
" speech_config.speech_recognition_language = \"en-US\"\n",
"\n",
" audio_config = speechsdk.audio.AudioConfig(use_default_microphone=True)\n",
" speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_config)\n",
"\n",
" print(\"Speak into your microphone.\")\n",
" speech_recognition_result = speech_recognizer.recognize_once_async().get()\n",
"\n",
" if speech_recognition_result.reason == speechsdk.ResultReason.RecognizedSpeech:\n",
" print(\"Recognized: {}\".format(speech_recognition_result.text))\n",
" elif speech_recognition_result.reason == speechsdk.ResultReason.NoMatch:\n",
" print(\"No speech could be recognized: {}\".format(speech_recognition_result.no_match_details))\n",
" elif speech_recognition_result.reason == speechsdk.ResultReason.Canceled:\n",
" cancellation_details = speech_recognition_result.cancellation_details\n",
" print(\"Speech Recognition canceled: {}\".format(cancellation_details.reason))\n",
" if cancellation_details.reason == speechsdk.CancellationReason.Error:\n",
" print(\"Error details: {}\".format(cancellation_details.error_details))\n",
" print(\"Did you set the speech resource key and region values?\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"speech_to_text()"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"def translate_speech_to_text(target_language: str, audio_config):\n",
" speech_translation_config = speechsdk.translation.SpeechTranslationConfig(subscription=SPEECH_KEY, region=SPEECH_REGION)\n",
" speech_translation_config.speech_recognition_language = \"en-US\"\n",
"\n",
" speech_translation_config.add_target_language(target_language)\n",
"\n",
" translation_recognizer = speechsdk.translation.TranslationRecognizer(translation_config=speech_translation_config, audio_config=audio_config)\n",
"\n",
" translation_recognition_result = translation_recognizer.recognize_once()\n",
"\n",
" if translation_recognition_result.reason == speechsdk.ResultReason.TranslatedSpeech:\n",
" print(\"Recognized: {}\".format(translation_recognition_result.text))\n",
" print(\"\"\"Translated into '{}': {}\"\"\".format(\n",
" target_language, \n",
" translation_recognition_result.translations[target_language]))\n",
" elif translation_recognition_result.reason == speechsdk.ResultReason.NoMatch:\n",
" print(\"No speech could be recognized: {}\".format(translation_recognition_result.no_match_details))\n",
" elif translation_recognition_result.reason == speechsdk.ResultReason.Canceled:\n",
" cancellation_details = translation_recognition_result.cancellation_details\n",
" print(\"Speech Recognition canceled: {}\".format(cancellation_details.reason))\n",
" if cancellation_details.reason == speechsdk.CancellationReason.Error:\n",
" print(\"Error details: {}\".format(cancellation_details.error_details))\n",
" print(\"Did you set the speech resource key and region values?\")"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Recognized: New York is a great city to visit.\n",
"Translated into 'pt': Nova York é uma ótima cidade para visitar.\n"
]
}
],
"source": [
"audio_config = speechsdk.audio.AudioConfig(filename=\"file1.mp3\")\n",
"translate_speech_to_text(\"pt\", audio_config)"
]
}
],
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.13"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment