Translate speech - Wio Terminal
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, and the text of the spoken response. This service has a REST API you can use to translate the text, but to make it easier to use this will be wrapped in another HTTP trigger in your functions app.
Task - create a serverless function to translate text
-
Open your
smart-timer-trigger
project in VS Code, and open the terminal ensuring the virtual environment is activated. If not, kill and re-create the terminal. -
Open the
local.settings.json
file and add settings for the translator API key and location:"TRANSLATOR_KEY": "<key>",
"TRANSLATOR_LOCATION": "<location>"Replace
<key>
with the API key for your translator service resource. Replace<location>
with the location you used when you created the translator service resource. -
Add a new HTTP trigger to this app called
translate-text
using the following command from inside the VS Code terminal in the root folder of the functions app project:func new --name translate-text --template "HTTP trigger"
This will create an HTTP trigger called
translate-text
. -
Replace the contents of the
__init__.py
file in thetranslate-text
folder with the following:import logging
import os
import requests
import azure.functions as func
location = os.environ['TRANSLATOR_LOCATION']
translator_key = os.environ['TRANSLATOR_KEY']
def main(req: func.HttpRequest) -> func.HttpResponse:
req_body = req.get_json()
from_language = req_body['from_language']
to_language = req_body['to_language']
text = req_body['text']
logging.info(f'Translating {text} from {from_language} to {to_language}')
url = f'https://api.cognitive.microsofttranslator.com/translate?api-version=3.0'
headers = {
'Ocp-Apim-Subscription-Key': translator_key,
'Ocp-Apim-Subscription-Region': location,
'Content-type': 'application/json'
}
params = {
'from': from_language,
'to': to_language
}
body = [{
'text' : text
}]
response = requests.post(url, headers=headers, params=params, json=body)
return func.HttpResponse(response.json()[0]['translations'][0]['text'])This code extracts the text and the languages from the HTTP request. It then makes a request to the translator REST API, passing the languages as parameters for the URL and the text to translate as the body. Finally, the translation is returned.
-
Run your function app locally. You can then call this using a tool like curl in the same way that you tested your
text-to-timer
HTTP trigger. Make sure to pass the text to translate and the languages as a JSON body:{
"text": "Définir une minuterie de 30 secondes",
"from_language": "fr-FR",
"to_language": "en-US"
}This example translates Définir une minuterie de 30 secondes from French to US English. It will return Set a 30-second timer.
💁 You can find this code in the code/functions folder.
Task - use the translator function to translate text
-
Open the
smart-timer
project in VS Code if it is not already open. -
Your smart timer will have 2 languages set - the language of the server that was used to train LUIS (the same language is also used to build the messages to speak to the user), and the language spoken by the user. Update the
LANGUAGE
constant in theconfig.h
header file to be the language that will be spoken by the user, and add a new constant calledSERVER_LANGUAGE
for the language used to train LUIS:const char *LANGUAGE = "<user language>";
const char *SERVER_LANGUAGE = "<server language>";Replace
<user language>
with the locale name for language you will be speaking in, for examplefr-FR
for French, orzn-HK
for Cantonese.Replace
<server language>
with the locale name for language used to train LUIS.You can find a list of the 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 a language of your choice. These services can then 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 into French using Bing Translate, then use the Listen translation button to speak the translation into your microphone.
-
Add the translator API key and location below the
SPEECH_LOCATION
:const char *TRANSLATOR_API_KEY = "<KEY>";
const char *TRANSLATOR_LOCATION = "<LOCATION>";Replace
<KEY>
with the API key for your translator service resource. Replace<LOCATION>
with the location you used when you created the translator service resource. -
Add the translator trigger URL below the
VOICE_URL
:const char *TRANSLATE_FUNCTION_URL = "<URL>";
Replace
<URL>
with the URL for thetranslate-text
HTTP trigger on your function app. This will be the same as the value forTEXT_TO_TIMER_FUNCTION_URL
, except with a function name oftranslate-text
instead oftext-to-timer
. -
Add a new file to the
src
folder calledtext_translator.h
. -
This new
text_translator.h
header file will contain a class to translate text. Add the following to this file to declare this class:#pragma once
#include <Arduino.h>
#include <ArduinoJson.h>
#include <HTTPClient.h>
#include <WiFiClient.h>
#include "config.h"
class TextTranslator
{
public:
private:
WiFiClient _client;
};
TextTranslator textTranslator;This declares the
TextTranslator
class, along with an instance of this class. The class has a single field for the WiFi client. -
To the
public
section of this class, add a method to translate text:String translateText(String text, String from_language, String to_language)
{
}