7.4 KiB
Translate speech - Raspberry Pi
In this part of the lesson, you will write code to translate text using the translator service.
Convert text to speech using the translator service
The speech service REST API doesn't support direct translations. Instead, you can use the Translator service to translate the text generated by the speech-to-text service, as well as the text for the spoken response. This service provides a REST API that you can use to perform translations.
Task - Use the translator resource to translate text
-
Your smart timer will have two languages set: the language of the server used to train LUIS (this same language is used to create the messages spoken to the user) and the language spoken by the user. Update the
language
variable to reflect the language spoken by the user, and add a new variable calledserver_language
for the language used to train LUIS:language = '<user language>' server_language = '<server language>'
Replace
<user language>
with the locale name for the language you will be speaking, such asfr-FR
for French orzn-HK
for Cantonese.Replace
<server language>
with the locale name for the language used to train LUIS.You can find a list of supported languages and their locale names in the Language and voice support documentation on Microsoft docs.
💁 If you don't speak multiple languages, you can use a service like Bing Translate or Google Translate to translate from your preferred language to another language of your choice. These services can also play audio of the translated text.
For example, if you train LUIS in English but want to use French as the user language, you can translate sentences like "set a 2 minute and 27 second timer" from English to French using Bing Translate, then use the Listen translation button to speak the translation into your microphone.
-
Add the translator API key below the
speech_api_key
:translator_api_key = '<key>'
Replace
<key>
with the API key for your translator service resource. -
Above the
say
function, define atranslate_text
function that will translate text from the server language to the user language:def translate_text(text, from_language, to_language):
The source and target languages are passed to this function. Your app needs to convert from the user language to the server language when recognizing speech, and from the server language to the user language when providing spoken feedback.
-
Inside this function, define the URL and headers for the REST API call:
url = f'https://api.cognitive.microsofttranslator.com/translate?api-version=3.0' headers = { 'Ocp-Apim-Subscription-Key': translator_api_key, 'Ocp-Apim-Subscription-Region': location, 'Content-type': 'application/json' }
The URL for this API is not location-specific; instead, the location is passed as a header. The API key is used directly, so unlike the speech service, there is no need to obtain an access token from the token issuer API.
-
Below this, define the parameters and body for the call:
params = { 'from': from_language, 'to': to_language } body = [{ 'text' : text }]
The
params
define the parameters to pass to the API call, specifying the source and target languages. This call will translate text from thefrom
language into theto
language.The
body
contains the text to translate. This is an array, as multiple blocks of text can be translated in a single call. -
Make the REST API call and retrieve the response:
response = requests.post(url, headers=headers, params=params, json=body)
The response is a JSON array containing one item with the translations. This item includes an array of translations for all the text blocks passed in the body.
[ { "translations": [ { "text": "Set a 2 minute 27 second timer.", "to": "en" } ] } ]
-
Return the
text
property from the first translation in the first item of the array:return response.json()[0]['translations'][0]['text']
-
Update the
while True
loop to translate the text from theconvert_speech_to_text
function from the user language to the server language:if len(text) > 0: print('Original:', text) text = translate_text(text, language, server_language) print('Translated:', text) message = Message(json.dumps({ 'speech': text })) device_client.send_message(message)
This code also prints both the original and translated versions of the text to the console.
-
Update the
say
function to translate the text to be spoken from the server language to the user language:def say(text): print('Original:', text) text = translate_text(text, server_language, language) print('Translated:', text) speech = get_speech(text) play_speech(speech)
This code also prints both the original and translated versions of the text to the console.
-
Run your code. Ensure your function app is running, and request a timer in the user language, either by speaking that language yourself or by using a translation app.
pi@raspberrypi:~/smart-timer $ python3 app.py Connecting Connected Using voice fr-FR-DeniseNeural Original: Définir une minuterie de 2 minutes et 27 secondes. Translated: Set a timer of 2 minutes and 27 seconds. Original: 2 minute 27 second timer started. Translated: 2 minute 27 seconde minute a commencé. Original: Times up on your 2 minute 27 second timer. Translated: Chronométrant votre minuterie de 2 minutes 27 secondes.
💁 Due to differences in phrasing across languages, you may receive translations that differ slightly from the examples you provided to LUIS. If this happens, add more examples to LUIS, retrain the model, and re-publish it.
💁 You can find this code in the code/pi folder.
😀 Your multilingual timer program was a success!
Disclaimer:
This document has been translated using the AI translation service Co-op Translator. While we aim for accuracy, please note that automated translations may include errors or inaccuracies. The original document in its native language should be regarded as the authoritative source. For critical information, professional human translation is advised. We are not responsible for any misunderstandings or misinterpretations resulting from the use of this translation.