Skip to content

Instantly share code, notes, and snippets.

@writetojoyson
Created January 18, 2021 17:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save writetojoyson/0ddeca8178639df8c8b59ae61eccbd32 to your computer and use it in GitHub Desktop.
Save writetojoyson/0ddeca8178639df8c8b59ae61eccbd32 to your computer and use it in GitHub Desktop.
Created on Skills Network Labs
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<center>\n",
" <img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/Logos/organization_logo/organization_logo.png\" width=\"300\" alt=\"cognitiveclass.ai logo\" />\n",
"</center>\n",
"\n",
"# Watson Speech to Text Translator\n",
"\n",
"Estimated time needed: **25** minutes\n",
"\n",
"## Objectives\n",
"\n",
"After completing this lab you will be able to:\n",
"\n",
"- Create Speech to Text Translator\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Introduction\n",
"\n",
"<p>In this notebook, you will learn to convert an audio file of an English speaker to text using a Speech to Text API. Then you will translate the English version to a Spanish version using a Language Translator API. <b>Note:</b> You must obtain the API keys and enpoints to complete the lab.</p>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-block alert-info\" style=\"margin-top: 20px\">\n",
"<h2>Table of Contents</h2>\n",
"<ul>\n",
" <li><a href=\"#ref0\">Speech To Text</a></li>\n",
" <li><a href=\"#ref1\">Language Translator</a></li>\n",
" <li><a href=\"#ref2\">Exercise</a></li>\n",
"</ul>\n",
"</div>\n"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Requirement already satisfied: PyJWT==1.7.1 in /home/jupyterlab/conda/envs/python/lib/python3.6/site-packages (1.7.1)\n",
"Requirement already satisfied: ibm_watson in /home/jupyterlab/conda/envs/python/lib/python3.6/site-packages (5.1.0)\n",
"Requirement already satisfied: wget in /home/jupyterlab/conda/envs/python/lib/python3.6/site-packages (3.2)\n",
"Requirement already satisfied: websocket-client==0.48.0 in /home/jupyterlab/conda/envs/python/lib/python3.6/site-packages (from ibm_watson) (0.48.0)\n",
"Requirement already satisfied: python-dateutil>=2.5.3 in /home/jupyterlab/conda/envs/python/lib/python3.6/site-packages (from ibm_watson) (2.8.1)\n",
"Requirement already satisfied: requests<3.0,>=2.0 in /home/jupyterlab/conda/envs/python/lib/python3.6/site-packages (from ibm_watson) (2.25.0)\n",
"Requirement already satisfied: ibm-cloud-sdk-core>=3.3.6 in /home/jupyterlab/conda/envs/python/lib/python3.6/site-packages (from ibm_watson) (3.3.6)\n",
"Requirement already satisfied: six in /home/jupyterlab/conda/envs/python/lib/python3.6/site-packages (from websocket-client==0.48.0->ibm_watson) (1.15.0)\n",
"Requirement already satisfied: chardet<4,>=3.0.2 in /home/jupyterlab/conda/envs/python/lib/python3.6/site-packages (from requests<3.0,>=2.0->ibm_watson) (3.0.4)\n",
"Requirement already satisfied: urllib3<1.27,>=1.21.1 in /home/jupyterlab/conda/envs/python/lib/python3.6/site-packages (from requests<3.0,>=2.0->ibm_watson) (1.25.11)\n",
"Requirement already satisfied: certifi>=2017.4.17 in /home/jupyterlab/conda/envs/python/lib/python3.6/site-packages (from requests<3.0,>=2.0->ibm_watson) (2020.12.5)\n",
"Requirement already satisfied: idna<3,>=2.5 in /home/jupyterlab/conda/envs/python/lib/python3.6/site-packages (from requests<3.0,>=2.0->ibm_watson) (2.10)\n"
]
}
],
"source": [
"#you will need the following library \n",
"!pip install PyJWT==1.7.1 ibm_watson wget"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2 id=\"ref0\">Speech to Text</h2>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<p>First we import <code>SpeechToTextV1</code> from <code>ibm_watson</code>.For more information on the API, please click on this <a href=\"https://cloud.ibm.com/apidocs/speech-to-text?code=python\">link</a></p>\n"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [],
"source": [
"from ibm_watson import SpeechToTextV1 \n",
"import json\n",
"from ibm_cloud_sdk_core.authenticators import IAMAuthenticator"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<p>The service endpoint is based on the location of the service instance, we store the information in the variable URL. To find out which URL to use, view the service credentials and paste the url here.</p>\n"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [],
"source": [
"url_s2t = \"https://api.eu-de.speech-to-text.watson.cloud.ibm.com/instances/b512ce33-af83-4969-af4d-d70017781d98\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<p>You require an API key, and you can obtain the key on the <a href=\"https://cloud.ibm.com/resources\">Dashboard </a>.</p>\n"
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [],
"source": [
"iam_apikey_s2t = \"Pge292-_kBbbTRl6o6mAepVHN-Hsxf1CJ3gb0QwWBEIE\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<p>You create a <a href=\"http://watson-developer-cloud.github.io/python-sdk/v0.25.0/apis/watson_developer_cloud.speech_to_text_v1.html\">Speech To Text Adapter object</a> the parameters are the endpoint and API key.</p>\n"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<ibm_watson.speech_to_text_v1_adapter.SpeechToTextV1Adapter at 0x7fcf0b73e2b0>"
]
},
"execution_count": 48,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"authenticator = IAMAuthenticator(iam_apikey_s2t)\n",
"s2t = SpeechToTextV1(authenticator=authenticator)\n",
"s2t.set_service_url(url_s2t)\n",
"s2t"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<p>Lets download the audio file that we will use to convert into text.</p>\n"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"--2021-01-18 17:10:11-- https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/labs/PolynomialRegressionandPipelines.mp3\n",
"Resolving s3-api.us-geo.objectstorage.softlayer.net (s3-api.us-geo.objectstorage.softlayer.net)... 67.228.254.196\n",
"Connecting to s3-api.us-geo.objectstorage.softlayer.net (s3-api.us-geo.objectstorage.softlayer.net)|67.228.254.196|:443... connected.\n",
"HTTP request sent, awaiting response... 200 OK\n",
"Length: 4234179 (4.0M) [audio/mpeg]\n",
"Saving to: ‘PolynomialRegressionandPipelines.mp3’\n",
"\n",
"PolynomialRegressio 100%[===================>] 4.04M 4.06MB/s in 1.0s \n",
"\n",
"2021-01-18 17:10:12 (4.06 MB/s) - ‘PolynomialRegressionandPipelines.mp3’ saved [4234179/4234179]\n",
"\n"
]
}
],
"source": [
"!wget -O PolynomialRegressionandPipelines.mp3 https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/labs/PolynomialRegressionandPipelines.mp3\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<p>We have the path of the wav file we would like to convert to text</p>\n"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [],
"source": [
"filename='PolynomialRegressionandPipelines.mp3'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<p>We create the file object <code>wav</code> with the wav file using <code>open</code> ; we set the <code>mode</code> to \"rb\" , this is similar to read mode, but it ensures the file is in binary mode.We use the method <code>recognize</code> to return the recognized text. The parameter audio is the file object <code>wav</code>, the parameter <code>content_type</code> is the format of the audio file.</p>\n"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [],
"source": [
"with open(filename, mode=\"rb\") as wav:\n",
" response = s2t.recognize(audio=wav, content_type='audio/mp3')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<p>The attribute result contains a dictionary that includes the translation:</p>\n"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'result_index': 0,\n",
" 'results': [{'final': True,\n",
" 'alternatives': [{'transcript': 'in this video we will cover polynomial regression and pipelines ',\n",
" 'confidence': 0.94}]},\n",
" {'final': True,\n",
" 'alternatives': [{'transcript': \"what do we do when a linear model is not the best fit for our data let's look into another type of regression model the polynomial regression we transform our data into a polynomial then use linear regression to fit the parameters that we will discuss pipelines pipelines are way to simplify your code \",\n",
" 'confidence': 0.9}]},\n",
" {'final': True,\n",
" 'alternatives': [{'transcript': \"polynomial regression is a special case of the general linear regression this method is beneficial for describing curvilinear relationships what is a curvilinear relationship it's what you get by squaring or setting higher order terms of the predictor variables in the model transforming the data the model can be quadratic which means the predictor variable in the model is squared we use a bracket to indicated as an exponent this is the second order polynomial regression with a figure representing the function \",\n",
" 'confidence': 0.95}]},\n",
" {'final': True,\n",
" 'alternatives': [{'transcript': 'the model can be cubic which means the predictor variable is cute this is the third order polynomial regression we see by examining the figure that the function has more variation ',\n",
" 'confidence': 0.95}]},\n",
" {'final': True,\n",
" 'alternatives': [{'transcript': \"there also exists higher order polynomial regressions when a good fit hasn't been achieved by second or third order we can see in figures how much the graphs change when we change the order of the polynomial regression the degree of the regression makes a big difference and can result in a better fit if you pick the right value in all cases the relationship between the variable in the parameter is always linear \",\n",
" 'confidence': 0.91}]},\n",
" {'final': True,\n",
" 'alternatives': [{'transcript': \"let's look at an example from our data we generate a polynomial regression model \",\n",
" 'confidence': 0.89}]},\n",
" {'final': True,\n",
" 'alternatives': [{'transcript': 'in python we do this by using the poly fit function in this example we develop a third order polynomial regression model base we can print out the model symbolic form for the model is given by the following expression ',\n",
" 'confidence': 0.92}]},\n",
" {'final': True,\n",
" 'alternatives': [{'transcript': \"negative one point five five seven X. one cute plus two hundred four point eight X. one squared plus eight thousand nine hundred sixty five X. one plus one point three seven times ten to the power of five we can also have multi dimensional polynomial linear regression the expression can get complicated here are just some of the terms for two dimensional second order polynomial none pies poly fit function cannot perform this type of regression we use the preprocessing librarian scikit learn to create a polynomial feature object the constructor takes the degree of the polynomial as a parameter then we transform the features into a polynomial feature with the fit underscore transform method let's do a more intuitive example \",\n",
" 'confidence': 0.9}]},\n",
" {'final': True,\n",
" 'alternatives': [{'transcript': 'consider the feature shown here applying the method we transform the data we now have a new set of features that are transformed version of our original features as that I mention of the data gets larger we may want to normalize multiple features as scikit learn instead we can use the preprocessing module to simplify many tasks for example we can standardize each feature simultaneously we import standard scaler we train the object fit the scale object then transform the data into a new data frame on a rate X. underscore scale there are more normalization methods available in the pre processing library as well as other transformations we can simplify our code by using a pipeline library there are many steps to getting a prediction for example normalization polynomial transform and linear regression we simplify the process using a pipeline ',\n",
" 'confidence': 0.9}]},\n",
" {'final': True,\n",
" 'alternatives': [{'transcript': 'pipeline sequentially perform a series of transformations the last step carries out a prediction first we import all the modules we need then we import the library pipeline we create a list of topples the first element in the topple contains the name of the estimator model the second element contains model constructor we input the list in the pipeline constructor we now have a pipeline object we can train the pipeline by applying the train method to the pipeline object we can also produce a prediction as well ',\n",
" 'confidence': 0.89}]},\n",
" {'final': True,\n",
" 'alternatives': [{'transcript': 'the method normalizes the data performs a polynomial transform then outputs a prediction ',\n",
" 'confidence': 0.89}]}]}"
]
},
"execution_count": 53,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"response.result"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from pandas.io.json import json_normalize\n",
"\n",
"json_normalize(response.result['results'],\"alternatives\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"response"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<p>We can obtain the recognized text and assign it to the variable <code>recognized_text</code>:</p>\n"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"data": {
"text/plain": [
"str"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"recognized_text=response.result['results'][0][\"alternatives\"][0][\"transcript\"]\n",
"type(recognized_text)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2 id=\"ref1\">Language Translator</h2>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<p>First we import <code>LanguageTranslatorV3</code> from ibm_watson. For more information on the API click <a href=\"https://cloud.ibm.com/apidocs/speech-to-text?code=python\"> here</a></p>\n"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [],
"source": [
"from ibm_watson import LanguageTranslatorV3"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<p>The service endpoint is based on the location of the service instance, we store the information in the variable URL. To find out which URL to use, view the service credentials.</p>\n"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [],
"source": [
"url_lt='https://gateway.watsonplatform.net/language-translator/api'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<p>You require an API key, and you can obtain the key on the <a href=\"https://cloud.ibm.com/resources\">Dashboard</a>.</p>\n"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [],
"source": [
"apikey_lt='Pge292-_kBbbTRl6o6mAepVHN-Hsxf1CJ3gb0QwWBEIE'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<p>API requests require a version parameter that takes a date in the format version=YYYY-MM-DD. This lab describes the current version of Language Translator, 2018-05-01</p>\n"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [],
"source": [
"version_lt='2018-05-01'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<p>we create a Language Translator object <code>language_translator</code>:</p>\n"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"data": {
"text/plain": [
"<ibm_watson.language_translator_v3.LanguageTranslatorV3 at 0x7fcf0b7390f0>"
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"authenticator = IAMAuthenticator(apikey_lt)\n",
"language_translator = LanguageTranslatorV3(version=version_lt,authenticator=authenticator)\n",
"language_translator.set_service_url(url_lt)\n",
"language_translator"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<p>We can get a Lists the languages that the service can identify.\n",
"The method Returns the language code. For example English (en) to Spanis (es) and name of each language.</p>\n"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"ERROR:root:No such child resource.\n",
"Traceback (most recent call last):\n",
" File \"/home/jupyterlab/conda/envs/python/lib/python3.6/site-packages/ibm_cloud_sdk_core/base_service.py\", line 246, in send\n",
" response.status_code, http_response=response)\n",
"ibm_cloud_sdk_core.api_exception.ApiException: Error: No such child resource., Code: 404 , X-global-transaction-id: 39e96cb3-21ce-4734-a6ea-2a299427e2ab\n"
]
},
{
"ename": "ApiException",
"evalue": "Error: No such child resource., Code: 404 , X-global-transaction-id: 39e96cb3-21ce-4734-a6ea-2a299427e2ab",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mApiException\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-43-96c9f439bbc1>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mpandas\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mio\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mjson\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mjson_normalize\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mjson_normalize\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlanguage_translator\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlist_identifiable_languages\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_result\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"languages\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32m~/conda/envs/python/lib/python3.6/site-packages/ibm_watson/language_translator_v3.py\u001b[0m in \u001b[0;36mlist_identifiable_languages\u001b[0;34m(self, **kwargs)\u001b[0m\n\u001b[1;32m 227\u001b[0m params=params)\n\u001b[1;32m 228\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 229\u001b[0;31m \u001b[0mresponse\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mrequest\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 230\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mresponse\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 231\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/conda/envs/python/lib/python3.6/site-packages/ibm_cloud_sdk_core/base_service.py\u001b[0m in \u001b[0;36msend\u001b[0;34m(self, request, **kwargs)\u001b[0m\n\u001b[1;32m 244\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 245\u001b[0m raise ApiException(\n\u001b[0;32m--> 246\u001b[0;31m response.status_code, http_response=response)\n\u001b[0m\u001b[1;32m 247\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mrequests\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mexceptions\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mSSLError\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 248\u001b[0m \u001b[0mlogging\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mexception\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mERROR_MSG_DISABLE_SSL\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mApiException\u001b[0m: Error: No such child resource., Code: 404 , X-global-transaction-id: 39e96cb3-21ce-4734-a6ea-2a299427e2ab"
]
}
],
"source": [
"from pandas.io.json import json_normalize\n",
"\n",
"json_normalize(language_translator.list_identifiable_languages().get_result(), \"languages\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<p>We can use the method <code>translate</code> this will translate the text. The parameter text is the text. Model_id is the type of model we would like to use use we use list the language . In this case, we set it to 'en-es' or English to Spanish. We get a Detailed Response object translation_response</p>\n"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"ERROR:root:Forbidden\n",
"Traceback (most recent call last):\n",
" File \"/home/jupyterlab/conda/envs/python/lib/python3.6/site-packages/ibm_cloud_sdk_core/base_service.py\", line 246, in send\n",
" response.status_code, http_response=response)\n",
"ibm_cloud_sdk_core.api_exception.ApiException: Error: Forbidden, Code: 403 , X-global-transaction-id: 1752f70a-e09c-4015-a33e-759479d48bd1\n"
]
},
{
"ename": "ApiException",
"evalue": "Error: Forbidden, Code: 403 , X-global-transaction-id: 1752f70a-e09c-4015-a33e-759479d48bd1",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mApiException\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-35-a75496d94732>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m translation_response = language_translator.translate(\\\n\u001b[0;32m----> 2\u001b[0;31m text=recognized_text, model_id='en-es')\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0mtranslation_response\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/conda/envs/python/lib/python3.6/site-packages/ibm_watson/language_translator_v3.py\u001b[0m in \u001b[0;36mtranslate\u001b[0;34m(self, text, model_id, source, target, **kwargs)\u001b[0m\n\u001b[1;32m 189\u001b[0m data=data)\n\u001b[1;32m 190\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 191\u001b[0;31m \u001b[0mresponse\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mrequest\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 192\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mresponse\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 193\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/conda/envs/python/lib/python3.6/site-packages/ibm_cloud_sdk_core/base_service.py\u001b[0m in \u001b[0;36msend\u001b[0;34m(self, request, **kwargs)\u001b[0m\n\u001b[1;32m 244\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 245\u001b[0m raise ApiException(\n\u001b[0;32m--> 246\u001b[0;31m response.status_code, http_response=response)\n\u001b[0m\u001b[1;32m 247\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mrequests\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mexceptions\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mSSLError\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 248\u001b[0m \u001b[0mlogging\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mexception\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mERROR_MSG_DISABLE_SSL\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mApiException\u001b[0m: Error: Forbidden, Code: 403 , X-global-transaction-id: 1752f70a-e09c-4015-a33e-759479d48bd1"
]
}
],
"source": [
"translation_response = language_translator.translate(\\\n",
" text=recognized_text, model_id='en-es')\n",
"translation_response"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<p>The result is a dictionary.</p>\n"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'translation_response' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-36-d6660b45e082>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtranslation\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mtranslation_response\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_result\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mtranslation\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mNameError\u001b[0m: name 'translation_response' is not defined"
]
}
],
"source": [
"translation=translation_response.get_result()\n",
"translation"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<p>We can obtain the actual translation as a string as follows:</p>\n"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'translation' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-37-d3124080e01b>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mspanish_translation\u001b[0m \u001b[0;34m=\u001b[0m\u001b[0mtranslation\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'translations'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'translation'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mspanish_translation\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mNameError\u001b[0m: name 'translation' is not defined"
]
}
],
"source": [
"spanish_translation =translation['translations'][0]['translation']\n",
"spanish_translation "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<p>We can translate back to English</p>\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"translation_new = language_translator.translate(text=spanish_translation ,model_id='es-en').get_result()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<p>We can obtain the actual translation as a string as follows:</p>\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"translation_eng=translation_new['translations'][0]['translation']\n",
"translation_eng"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<br>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2>Quiz</h2>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Translate to French.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Write your code below and press Shift+Enter to execute\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<details><summary>Click here for the solution</summary>\n",
"\n",
"```python\n",
"French_translation=language_translator.translate(\n",
" text=translation_eng , model_id='en-fr').get_result()\n",
"\n",
"French_translation['translations'][0]['translation']\n",
"\n",
"```\n",
"\n",
"</details>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>Language Translator</h3>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" <a href=\"https://cloud.ibm.com/catalog/services/watson-studio\"><img src=\"https://ibm.box.com/shared/static/irypdxea2q4th88zu1o1tsd06dya10go.png\" width=\"750\" align=\"center\"></a>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<b>References</b>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[https://cloud.ibm.com/apidocs/speech-to-text?code=python](https://cloud.ibm.com/apidocs/speech-to-text?code=python&cm_mmc=Email_Newsletter-_-Developer_Ed%2BTech-_-WW_WW-_-SkillsNetwork-Courses-IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork-19487395&cm_mmca1=000026UJ&cm_mmca2=10006555&cm_mmca3=M12345678&cvosrc=email.Newsletter.M12345678&cvo_campaign=000026UJ)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[https://cloud.ibm.com/apidocs/language-translator?code=python](https://cloud.ibm.com/apidocs/language-translator?code=python&cm_mmc=Email_Newsletter-_-Developer_Ed%2BTech-_-WW_WW-_-SkillsNetwork-Courses-IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork-19487395&cm_mmca1=000026UJ&cm_mmca2=10006555&cm_mmca3=M12345678&cvosrc=email.Newsletter.M12345678&cvo_campaign=000026UJ)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<hr>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Authors:\n",
"\n",
" [Joseph Santarcangelo](https://www.linkedin.com/in/joseph-s-50398b136?cm_mmc=Email_Newsletter-_-Developer_Ed%2BTech-_-WW_WW-_-SkillsNetwork-Courses-IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork-19487395&cm_mmca1=000026UJ&cm_mmca2=10006555&cm_mmca3=M12345678&cvosrc=email.Newsletter.M12345678&cvo_campaign=000026UJ&cm_mmc=Email_Newsletter-_-Developer_Ed%2BTech-_-WW_WW-_-SkillsNetwork-Courses-IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork-19487395&cm_mmca1=000026UJ&cm_mmca2=10006555&cm_mmca3=M12345678&cvosrc=email.Newsletter.M12345678&cvo_campaign=000026UJ) \n",
"\n",
"Joseph Santarcangelo has a PhD in Electrical Engineering, his research focused on using machine learning, signal processing, and computer vision to determine how videos impact human cognition. Joseph has been working for IBM since he completed his PhD.\n",
"\n",
"## Other Contributor(s)\n",
"\n",
"<a href=\"https://www.linkedin.com/in/fanjiang0619/\">Fan Jiang</a>\n",
"\n",
"## Change Log\n",
"\n",
"| Date (YYYY-MM-DD) | Version | Changed By | Change Description |\n",
"| ----------------- | ------- | ---------- | ---------------------------------- |\n",
"| 2021-01-05 | 2.1 | Malika | Added a library |\n",
"| 2020-08-26 | 2.0 | Lavanya | Moved lab to course repo in GitLab |\n",
"| | | | |\n",
"| | | | |\n",
"\n",
"<hr/>\n",
"\n",
"## <h3 align=\"center\"> © IBM Corporation 2020. All rights reserved. <h3/>\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python",
"language": "python",
"name": "conda-env-python-py"
},
"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.6.12"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment