In this video, we will discuss
Application Program Interfaces that
use some kind of artificial intelligence.
We will transcribe an audio file using
the Watson Text to Speech API.
We will then translate the text to
a new language using the Watson Language Translator API.
In the API call,
you will send a copy of the audio file to the API.
This is sometimes called a POST request.
Then the API will send
the text transcription of what the individual is saying.
Under the hood, the API is making a GET request.
We then send the text we would like to translate
into a second language to a second API.
The API will translate the text
and send the translation back to you.
In this case, we translate English to Spanish.
We then provide an overview of API keys and endpoints,
Watson Speech to Text, and Watson Translate.
First, we will review API keys and endpoints.
They will give you access to the API.
An API key as a way to access the API.
It's a unique set of characters that the API
uses to identify you and authorize you.
Usually, your first call to the API includes the API key.
This will allow you access to the API.
In many APIs, you may get charged for each call.
So like your password,
you should keep your API key a secret.
An endpoint is simply the location of the service.
It's used to find the API on
the Internet just like a web address.
Now, we will transcribe an audio file
using the Watson Text to Speech API.
Before you start the lab,
you should sign up for an API key.
We will download an audio file into your directory.
First, we import SpeechToTextV1 from IBM Watson.
The service endpoint is based on
the location of the service instance.
We store the information in the variable url_s2t.
To find out which URL to use,
view the service credentials.
You will do the same for your API key.
You create a speech-to-text adapter object.
The parameters are the endpoint and API key.
You will use this object to communicate
with the Watson Speech to Text service.
We have the path of the wav file
we would like to convert to text.
We create the file object wav
with the wav file using open.
We set the mode to rb,
which means to read the file in binary format.
The file object allows us
access to the wav file that contains the audio.
We use the method recognize from
the speech to text adapter object.
This basically sends the audio file
to Watson Speech to Text service.
The parameter audio is the file object.
The content type is the audio file format.
The service sends a response
stored in the object response.
The attribute result contains a python dictionary.
The key results value has
a list that contains a dictionary.
We are interested in the key transcript.
We can assign it to
the variable recognized_text as follows.
Recognized_text now contains a string
with a transcribed text.
Now let's see how to translate
the text using the Watson Language Translator.
First, we import LanguageTranslatorV3 from ibm_watson.
We assign the service endpoint to the variable url_l2.
You can obtain the service in the lab instructions.
You require an API key,
see the lab instructions on how to obtain the API key.
This API request requires
the date of the version, see the documentation.
We create a language translator object,
LanguageTranslator.
We can get a list of the languages that
the service can identify as follows.
The method returns the language code.
For example, English has a symbol en to Spanish,
which has the symbol es.
In the last section, we assigned
the transcribed texts to the variable to recognized_text.
We can use the method translate.
This will translate the text.
The result is a detailed response object.
The parameter text is the text.
model_id is the type of model we would like to use.
In this case, we set it to en-es for English to Spanish.
We use the method get result to get
the translated text and
assign it to the variable translation.
The result is a dictionary that includes
the translation word count and character count.
We can obtain the translation and assign it to
the variable spanish_translation as follows.
Using the variable spanish_translation,
we can translate the text back to English as follows.
The result is a dictionary.
We can obtain the string with the text as follows.
We can then translate the text to French as follows.
Thanks for watching this video.
(Music)