- [Instructor] Next up let's take a look
at the run translator function.
Now, this function is only going to get called,
if we run this .py file as a script
which is, of course, what I demonstrated to you
in a preceding video.
So, let me just scroll down to the bottom here for a moment
and you'll see that we have this "if" statement
that calls run translator only if we run the script as a,
or run the file as a script
which if you recall from earlier lessons,
means that the global variable called name for this module
is going to be set to the value,
double underscore main double underscore, as a string.
So if this condition is true,
then we know that we're running the file as a script
and in that case run translator will get called
and because this is the very last thing in the file
all of these other functions that I've defined
up above that "if" statement in the file
will be defined before we get a chance to call them.
So, let me scroll way back up here and let's take a look
at this run translator function.
Now, this function is broken down into the ten steps
that I just summarized for you
before we started looking at the source code.
So, as you can see for each of those steps,
we have a comment specifying with what the step is.
So, the reason that we broke it down this way
with all the functions as well
is because there's a bunch of things we do repetitively
both for going from English to Spanish
and then for going from Spanish to English.
So, we've define the function called record audio
that for step one you give it the name of the audio file.
In our case english.wav which will be stored
in the same folder with the script
and it's going to record five seconds of audio,
as you'll see in the subsequent video
and place the bytes of that audio
in binary format into the english.wav file.
Then in step two, we have a function
that we define called speech to text.
We give it two arguments.
The first is going to be the file name
that we would like to send
to the Watson Speech to Text service
and the second is a predefined model
that the IBM folks have provided for us
called en-US_BroadbandModel.
Now, the speech to text service
has a whole bunch of predefined models
for the different languages that they support
for converting speech into text.
They have both what they called broadband models
and narrowband models
and that has to do with the overall audio quality.
So, if you have audio that's greater than 16 kilohertz
in audio quality then, IBM basically recommends working
with the broadband models.
And in their documentation for the speech to text service
they list out all the supported models
that are available to you.
As you'll see when we record the audio
in our record audio function,
we're actually going to record very high quality audio
at 44.1 kilohertz.
So, that's why we chose to use the en-US_BroadbandModel.
Now, because we're speaking English in the United States
we use the U.S. version and as you might expect,
there's also an English for U.K. as well.
And those models have been trained
to understand English different ways
based on how it's used in each of those countries.
So, once we have the text of the speech
that will be return to us and will stored
in the variable English which we will then display to you.
Next up in step three,
we're going to call our translate function
and this translate function is going to take as arguments,
the text that we want to pass-through
to the IBM Watson translator service
and separately, we need to give it a model once again.
And the reason we're providing a model argument
to our translate function
is so that we can choose between the languages
that we wanna translate.
So, in this case we're using a predefined model
for Watson called en-es which translates
from English to Spanish.
And as you might expect, there's also an es-en
which translates from Spanish to English.
And there are many other models
for the many different languages
that IBM Watson's translation service supports.
So, this function is going to give us back
the Spanish representation of the text
which we will then display for you.
Next up in step four, we're going to take that Spanish text
and turn it back into speech and for that purpose
we've defined a text to speech function,
we give it in argument
which represents the text we want to speak.
We also give it the so called voice to use.
And as you'll see when you study
the text to speech documentation for IBM Watson,
there are a variety of voices that they provide.
One of those voices is for Spanish in U.S. Spanish,
so es-US is Spanish in the United States
and it's a woman's voice which they called the Sofia voice
and that's the one that you heard in my demo.
And then separately, we also give it the file name
in which we would like to store the spoken texts.
So remember, what's going to come back
from the Watson web service.
In this case is actual audio
which we need to stick into a file on hour
and we'll play back that file in step five
with our play audio function.
Now step six through 10 do the same thing again,
but going from Spanish spoken or Spanish speech excuse me,
back to English speech.
So, we'll record the audio into the spanishresponse.wav file
in the current folder.
We will then pass that off to the IBM Watson service
to transcribe it into Spanish text
and for this purpose, we'll use the es-ES
which is the Spain version of Spanish broadband model.
We will speak the Spanish response.
Then, we're going to translate that text that we get back
into Spanish using the es-en model
and we'll display the English version
of that text once it comes back.
And then we'll create the English audio from that text.
So again, we'll call our text to speech function
giving at the English text.
We will specify the voice that we wish to use
which is going to be U.S. English for Alison's voice.
And finally, we have the file
that we would like to store the spoken text in.
And once we get that back,
we will then be able to use our play audio function
to actually play the local audio file stored
in englishresponse.wav.