<commit_before>{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h1> Using Machine Learning APIs </h1>\n",
    "\n",
    "First, visit <a href=\"http://console.cloud.google.com/apis\">API console</a>, choose \"Credentials\" on the left-hand menu.  Choose \"Create Credentials\" and generate an API key for your application. You should probably restrict it by IP address to prevent abuse, but for now, just  leave that field blank and delete the API key after trying out this demo.\n",
    "\n",
    "Copy-paste your API Key here:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "APIKEY=\"AIzaSyCedn34DrOD6c8ybV-1LmHS1CMtSGhX7EA\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<b> Note: Make sure you got an API Key and pasted it above. Mine won't work for you </b>\n",
    "\n",
    "From the same API console, choose \"Dashboard\" on the left-hand menu and \"Enable API\".\n",
    "\n",
    "Enable the following APIs for your project (search for them) if they are not already enabled:\n",
    "<ol>\n",
    "<li> Google Translate API </li>\n",
    "<li> Google Cloud Vision API </li>\n",
    "<li> Google Natural Language API </li>\n",
    "<li> Google Cloud Speech API </li>\n",
    "</ol>\n",
    "\n",
    "Finally, because we are calling the APIs from Python (clients in many other languages are available), let's install the Python package (it's not installed by default on Datalab)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Collecting google-api-python-client\n",
      "  Downloading google_api_python_client-1.5.1-py2.py3-none-any.whl (50kB)\n",
      "\u001b[K    100% |################################| 51kB 577kB/s \n",
      "\u001b[?25hCollecting oauth2client (from google-api-python-client)\n",
      "  Downloading oauth2client-3.0.0.tar.gz (77kB)\n",
      "\u001b[K    100% |################################| 81kB 1.5MB/s \n",
      "\u001b[?25hRequirement already up-to-date: six<2,>=1.6.1 in /usr/local/lib/python2.7/dist-packages (from google-api-python-client)\n",
      "Requirement already up-to-date: httplib2<1,>=0.8 in /usr/local/lib/python2.7/dist-packages (from google-api-python-client)\n",
      "Collecting uritemplate<1,>=0.6 (from google-api-python-client)\n",
      "  Downloading uritemplate-0.6.tar.gz\n",
      "Requirement already up-to-date: pyasn1>=0.1.7 in /usr/local/lib/python2.7/dist-packages (from oauth2client->google-api-python-client)\n",
      "Requirement already up-to-date: pyasn1-modules>=0.0.5 in /usr/local/lib/python2.7/dist-packages (from oauth2client->google-api-python-client)\n",
      "Requirement already up-to-date: rsa>=3.1.4 in /usr/local/lib/python2.7/dist-packages (from oauth2client->google-api-python-client)\n",
      "Collecting simplejson>=2.5.0 (from uritemplate<1,>=0.6->google-api-python-client)\n",
      "  Downloading simplejson-3.8.2.tar.gz (76kB)\n",
      "\u001b[K    100% |################################| 81kB 4.2MB/s \n",
      "\u001b[?25hBuilding wheels for collected packages: oauth2client, uritemplate, simplejson\n",
      "  Running setup.py bdist_wheel for oauth2client ... \u001b[?25l-\b \bdone\n",
      "\u001b[?25h  Stored in directory: /root/.cache/pip/wheels/b3/44/e9/e56c5a2ca8869305f38254df012fb16b2807eb9d5d55291e8b\n",
      "  Running setup.py bdist_wheel for uritemplate ... \u001b[?25l-\b \bdone\n",
      "\u001b[?25h  Stored in directory: /root/.cache/pip/wheels/2d/dc/57/124fcb62028d04cf74f6c7f5261f5deb29b3f6022eec179064\n",
      "  Running setup.py bdist_wheel for simplejson ... \u001b[?25l-\b \b\\\b \bdone\n",
      "\u001b[?25h  Stored in directory: /root/.cache/pip/wheels/e4/32/71/60b361b0d05433eb9d1dd3d47619931c08cc4e387dc494ad3c\n",
      "Successfully built oauth2client uritemplate simplejson\n",
      "Installing collected packages: oauth2client, simplejson, uritemplate, google-api-python-client\n",
      "  Found existing installation: oauth2client 2.0.2\n",
      "    Uninstalling oauth2client-2.0.2:\n",
      "      Successfully uninstalled oauth2client-2.0.2\n",
      "Successfully installed google-api-python-client-1.5.1 oauth2client-3.0.0 simplejson-3.8.2 uritemplate-0.6\n"
     ]
    }
   ],
   "source": [
    "# Copyright 2016 Google Inc.\n",
    "# Licensed under the Apache License, Version 2.0 (the \"License\"); you may not use this file except in compliance with the License. You may obtain a copy of the License at\n",
    "# http://www.apache.org/licenses/LICENSE-2.0\n",
    "# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.\n",
    "!pip install --upgrade google-api-python-client"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h2> Invoke Translate API </h2>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "is it really this easy? -> est-ce vraiment ce facile?\n",
      "amazing technology -> technologie étonnante\n",
      "wow -> sensationnel\n"
     ]
    }
   ],
   "source": [
    "# running Translate API\n",
    "from googleapiclient.discovery import build\n",
    "service = build('translate', 'v2', developerKey=APIKEY)\n",
    "\n",
    "# use the service\n",
    "inputs = ['is it really this easy?', 'amazing technology', 'wow']\n",
    "outputs = service.translations().list(source='en', target='fr', q=inputs).execute()\n",
    "# print outputs\n",
    "for input, output in zip(inputs, outputs['translations']):\n",
    "  print u\"{0} -> {1}\".format(input, output['translatedText'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h2> Invoke Vision API </h2>\n",
    "\n",
    "The Vision API can work off an image in Cloud Storage or embedded directly into a POST message. I'll use Cloud Storage and do OCR on this image: <img src=\"https://storage.googleapis.com/cloud-training-demos/vision/sign2.jpg\" width=\"200\" />.  That photograph is from http://www.publicdomainpictures.net/view-image.php?image=15842\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{u'responses': [{u'textAnnotations': [{u'locale': u'zh', u'description': u'\\u8bf7\\u60a8\\u7231\\u62a4\\u548c\\u4fdd\\n\\u62a4\\u536b\\u751f\\u521b\\u5efa\\u4f18\\n\\u7f8e\\u6c34\\u73af\\u5883\\n', u'boundingPoly': {u'vertices': [{u'y': 103, u'x': 150}, {u'y': 103, u'x': 1084}, {u'y': 654, u'x': 1084}, {u'y': 654, u'x': 150}]}}, {u'description': u'\\u8bf7', u'boundingPoly': {u'vertices': [{u'y': 103, u'x': 178}, {u'y': 103, u'x': 322}, {u'y': 241, u'x': 322}, {u'y': 241, u'x': 178}]}}, {u'description': u'\\u60a8', u'boundingPoly': {u'vertices': [{u'y': 107, u'x': 327}, {u'y': 107, u'x': 471}, {u'y': 241, u'x': 471}, {u'y': 241, u'x': 327}]}}, {u'description': u'\\u7231\\u62a4', u'boundingPoly': {u'vertices': [{u'y': 107, u'x': 481}, {u'y': 107, u'x': 774}, {u'y': 245, u'x': 774}, {u'y': 245, u'x': 481}]}}, {u'description': u'\\u548c', u'boundingPoly': {u'vertices': [{u'y': 110, u'x': 784}, {u'y': 110, u'x': 924}, {u'y': 245, u'x': 924}, {u'y': 245, u'x': 784}]}}, {u'description': u'\\u4fdd', u'boundingPoly': {u'vertices': [{u'y': 103, u'x': 933}, {u'y': 103, u'x': 1078}, {u'y': 237, u'x': 1078}, {u'y': 237, u'x': 933}]}}, {u'description': u'\\u62a4', u'boundingPoly': {u'vertices': [{u'y': 299, u'x': 164}, {u'y': 299, u'x': 311}, {u'y': 441, u'x': 311}, {u'y': 441, u'x': 164}]}}, {u'description': u'\\u536b\\u751f', u'boundingPoly': {u'vertices': [{u'y': 303, u'x': 317}, {u'y': 303, u'x': 619}, {u'y': 441, u'x': 619}, {u'y': 441, u'x': 317}]}}, {u'description': u'\\u521b\\u5efa', u'boundingPoly': {u'vertices': [{u'y': 303, u'x': 628}, {u'y': 303, u'x': 930}, {u'y': 444, u'x': 930}, {u'y': 444, u'x': 628}]}}, {u'description': u'\\u4f18', u'boundingPoly': {u'vertices': [{u'y': 299, u'x': 936}, {u'y': 299, u'x': 1084}, {u'y': 441, u'x': 1084}, {u'y': 441, u'x': 936}]}}, {u'description': u'\\u7f8e', u'boundingPoly': {u'vertices': [{u'y': 503, u'x': 150}, {u'y': 503, u'x': 303}, {u'y': 651, u'x': 303}, {u'y': 651, u'x': 150}]}}, {u'description': u'\\u6c34', u'boundingPoly': {u'vertices': [{u'y': 503, u'x': 305}, {u'y': 503, u'x': 456}, {u'y': 651, u'x': 456}, {u'y': 651, u'x': 305}]}}, {u'description': u'\\u73af\\u5883', u'boundingPoly': {u'vertices': [{u'y': 507, u'x': 461}, {u'y': 507, u'x': 764}, {u'y': 654, u'x': 764}, {u'y': 654, u'x': 461}]}}]}]}\n"
     ]
    }
   ],
   "source": [
    "# Running Vision API\n",
    "import base64\n",
    "IMAGE=\"gs://cloud-training-demos/vision/sign2.jpg\"\n",
    "vservice = build('vision', 'v1', developerKey=APIKEY)\n",
    "request = vservice.images().annotate(body={\n",
    "        'requests': [{\n",
    "                'image': {\n",
    "                    'source': {\n",
    "                        'gcs_image_uri': IMAGE\n",
    "                    }\n",
    "                },\n",
    "                'features': [{\n",
    "                    'type': 'TEXT_DETECTION',\n",
    "                    'maxResults': 3,\n",
    "                }]\n",
    "            }],\n",
    "        })\n",
    "responses = request.execute(num_retries=3)\n",
    "print responses"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "zh 请您爱护和保\n",
      "护卫生创建优\n",
      "美水环境\n",
      "\n"
     ]
    }
   ],
   "source": [
    "foreigntext = responses['responses'][0]['textAnnotations'][0]['description']\n",
    "foreignlang = responses['responses'][0]['textAnnotations'][0]['locale']\n",
    "print foreignlang, foreigntext"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h2> Translate sign </h2>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请您爱护和保\n",
      "护卫生创建优\n",
      "美水环境\n",
      " -> Please take good care of your health and create a beautiful water environment protection\n"
     ]
    }
   ],
   "source": [
    "inputs=[foreigntext]\n",
    "outputs = service.translations().list(source=foreignlang, target='en', q=inputs).execute()\n",
    "# print outputs\n",
    "for input, output in zip(inputs, outputs['translations']):\n",
    "  print u\"{0} -> {1}\".format(input, output['translatedText'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h2> Sentiment analysis with Language API </h2>\n",
    "\n",
    "Let's evaluate the sentiment of some famous quotes using Google Cloud Natural Language API."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "POLARITY=1 MAGNITUDE=0.9 for To succeed, you must have tremendous perseverance, tremendous will.\n",
      "POLARITY=1 MAGNITUDE=0.3 for It’s not that I’m so smart, it’s just that I stay with problems longer.\n",
      "POLARITY=1 MAGNITUDE=0.8 for Love is quivering happiness.\n",
      "POLARITY=1 MAGNITUDE=0.9 for Love is of all passions the strongest, for it attacks simultaneously the head, the heart, and the senses.\n",
      "POLARITY=-1 MAGNITUDE=0.5 for What difference does it make to the dead, the orphans and the homeless, whether the mad destruction is wrought under the name of totalitarianism or in the holy name of liberty or democracy?\n",
      "POLARITY=-1 MAGNITUDE=0.8 for When someone you love dies, and you’re not expecting it, you don’t lose her all at once; you lose her in pieces over a long time — the way the mail stops coming, and her scent fades from the pillows and even from the clothes in her closet and drawers. \n"
     ]
    }
   ],
   "source": [
    "lservice = build('language', 'v1beta1', developerKey=APIKEY)\n",
    "quotes = [\n",
    "  'To succeed, you must have tremendous perseverance, tremendous will.',\n",
    "  'It’s not that I’m so smart, it’s just that I stay with problems longer.',\n",
    "  'Love is quivering happiness.',\n",
    "  'Love is of all passions the strongest, for it attacks simultaneously the head, the heart, and the senses.',\n",
    "  'What difference does it make to the dead, the orphans and the homeless, whether the mad destruction is wrought under the name of totalitarianism or in the holy name of liberty or democracy?',\n",
    "  'When someone you love dies, and you’re not expecting it, you don’t lose her all at once; you lose her in pieces over a long time — the way the mail stops coming, and her scent fades from the pillows and even from the clothes in her closet and drawers. '\n",
    "]\n",
    "for quote in quotes:\n",
    "  response = lservice.documents().analyzeSentiment(\n",
    "    body={\n",
    "      'document': {\n",
    "         'type': 'PLAIN_TEXT',\n",
    "         'content': quote\n",
    "      }\n",
    "    }).execute()\n",
    "  polarity = response['documentSentiment']['polarity']\n",
    "  magnitude = response['documentSentiment']['magnitude']\n",
    "  print('POLARITY=%s MAGNITUDE=%s for %s' % (polarity, magnitude, quote))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h2> Speech API </h2>\n",
    "\n",
    "The Speech API can work on streaming data, audio content encoded and embedded directly into the POST message, or on a file on Cloud Storage. Here I'll pass in this <a href=\"https://storage.googleapis.com/cloud-training-demos/vision/audio.raw\">audio file</a> in Cloud Storage."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{u'results': [{u'alternatives': [{u'confidence': 0.98267895, u'transcript': u'how old is the Brooklyn Bridge'}]}]}\n"
     ]
    }
   ],
   "source": [
    "sservice = build('speech', 'v1beta1', developerKey=APIKEY)\n",
    "response = sservice.speech().syncrecognize(\n",
    "    body={\n",
    "        'config': {\n",
    "            'encoding': 'LINEAR16',\n",
    "            'sampleRate': 16000\n",
    "        },\n",
    "        'audio': {\n",
    "            'uri': 'gs://cloud-training-demos/vision/audio.raw'\n",
    "            }\n",
    "        }).execute()\n",
    "print response"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "how old is the Brooklyn Bridge\n",
      "Confidence=0.982679\n"
     ]
    }
   ],
   "source": [
    "print response['results'][0]['alternatives'][0]['transcript']\n",
    "print 'Confidence=%f' % response['results'][0]['alternatives'][0]['confidence']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h2> Clean up </h2>\n",
    "\n",
    "Remember to delete the API key by visiting <a href=\"http://code.google.com/apis/console\">API console</a>.\n",
    "\n",
    "If necessary, commit all your notebooks to git.\n",
    "\n",
    "If you are running Datalab on a Compute Engine VM or delegating to one, remember to stop or shut it down so that you are not charged.\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
<commit_msg>More change to match new cloud console<commit_after>{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h1> Using Machine Learning APIs </h1>\n",
    "\n",
    "First, visit <a href=\"http://console.cloud.google.com/apis\">API console</a>, choose \"Credentials\" on the left-hand menu.  Choose \"Create Credentials\" and generate an API key for your application. You should probably restrict it by IP address to prevent abuse, but for now, just  leave that field blank and delete the API key after trying out this demo.\n",
    "\n",
    "Copy-paste your API Key here:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "APIKEY=\"AIzaSyCedn34DrOD6c8ybV-1LmHS1CMtSGhX7EA\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<b> Note: Make sure you got an API Key and pasted it above. Mine won't work for you </b>\n",
    "\n",
    "From the same API console, choose \"Dashboard\" on the left-hand menu and \"Enable API\".\n",
    "\n",
    "Enable the following APIs for your project (search for them) if they are not already enabled:\n",
    "<ol>\n",
    "<li> Google Translate API </li>\n",
    "<li> Google Cloud Vision API </li>\n",
    "<li> Google Natural Language API </li>\n",
    "<li> Google Cloud Speech API </li>\n",
    "</ol>\n",
    "\n",
    "Finally, because we are calling the APIs from Python (clients in many other languages are available), let's install the Python package (it's not installed by default on Datalab)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Collecting google-api-python-client\n",
      "  Downloading google_api_python_client-1.5.1-py2.py3-none-any.whl (50kB)\n",
      "\u001b[K    100% |################################| 51kB 577kB/s \n",
      "\u001b[?25hCollecting oauth2client (from google-api-python-client)\n",
      "  Downloading oauth2client-3.0.0.tar.gz (77kB)\n",
      "\u001b[K    100% |################################| 81kB 1.5MB/s \n",
      "\u001b[?25hRequirement already up-to-date: six<2,>=1.6.1 in /usr/local/lib/python2.7/dist-packages (from google-api-python-client)\n",
      "Requirement already up-to-date: httplib2<1,>=0.8 in /usr/local/lib/python2.7/dist-packages (from google-api-python-client)\n",
      "Collecting uritemplate<1,>=0.6 (from google-api-python-client)\n",
      "  Downloading uritemplate-0.6.tar.gz\n",
      "Requirement already up-to-date: pyasn1>=0.1.7 in /usr/local/lib/python2.7/dist-packages (from oauth2client->google-api-python-client)\n",
      "Requirement already up-to-date: pyasn1-modules>=0.0.5 in /usr/local/lib/python2.7/dist-packages (from oauth2client->google-api-python-client)\n",
      "Requirement already up-to-date: rsa>=3.1.4 in /usr/local/lib/python2.7/dist-packages (from oauth2client->google-api-python-client)\n",
      "Collecting simplejson>=2.5.0 (from uritemplate<1,>=0.6->google-api-python-client)\n",
      "  Downloading simplejson-3.8.2.tar.gz (76kB)\n",
      "\u001b[K    100% |################################| 81kB 4.2MB/s \n",
      "\u001b[?25hBuilding wheels for collected packages: oauth2client, uritemplate, simplejson\n",
      "  Running setup.py bdist_wheel for oauth2client ... \u001b[?25l-\b \bdone\n",
      "\u001b[?25h  Stored in directory: /root/.cache/pip/wheels/b3/44/e9/e56c5a2ca8869305f38254df012fb16b2807eb9d5d55291e8b\n",
      "  Running setup.py bdist_wheel for uritemplate ... \u001b[?25l-\b \bdone\n",
      "\u001b[?25h  Stored in directory: /root/.cache/pip/wheels/2d/dc/57/124fcb62028d04cf74f6c7f5261f5deb29b3f6022eec179064\n",
      "  Running setup.py bdist_wheel for simplejson ... \u001b[?25l-\b \b\\\b \bdone\n",
      "\u001b[?25h  Stored in directory: /root/.cache/pip/wheels/e4/32/71/60b361b0d05433eb9d1dd3d47619931c08cc4e387dc494ad3c\n",
      "Successfully built oauth2client uritemplate simplejson\n",
      "Installing collected packages: oauth2client, simplejson, uritemplate, google-api-python-client\n",
      "  Found existing installation: oauth2client 2.0.2\n",
      "    Uninstalling oauth2client-2.0.2:\n",
      "      Successfully uninstalled oauth2client-2.0.2\n",
      "Successfully installed google-api-python-client-1.5.1 oauth2client-3.0.0 simplejson-3.8.2 uritemplate-0.6\n"
     ]
    }
   ],
   "source": [
    "# Copyright 2016 Google Inc.\n",
    "# Licensed under the Apache License, Version 2.0 (the \"License\"); you may not use this file except in compliance with the License. You may obtain a copy of the License at\n",
    "# http://www.apache.org/licenses/LICENSE-2.0\n",
    "# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.\n",
    "!pip install --upgrade google-api-python-client"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h2> Invoke Translate API </h2>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "is it really this easy? -> est-ce vraiment ce facile?\n",
      "amazing technology -> technologie étonnante\n",
      "wow -> sensationnel\n"
     ]
    }
   ],
   "source": [
    "# running Translate API\n",
    "from googleapiclient.discovery import build\n",
    "service = build('translate', 'v2', developerKey=APIKEY)\n",
    "\n",
    "# use the service\n",
    "inputs = ['is it really this easy?', 'amazing technology', 'wow']\n",
    "outputs = service.translations().list(source='en', target='fr', q=inputs).execute()\n",
    "# print outputs\n",
    "for input, output in zip(inputs, outputs['translations']):\n",
    "  print u\"{0} -> {1}\".format(input, output['translatedText'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h2> Invoke Vision API </h2>\n",
    "\n",
    "The Vision API can work off an image in Cloud Storage or embedded directly into a POST message. I'll use Cloud Storage and do OCR on this image: <img src=\"https://storage.googleapis.com/cloud-training-demos/vision/sign2.jpg\" width=\"200\" />.  That photograph is from http://www.publicdomainpictures.net/view-image.php?image=15842\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{u'responses': [{u'textAnnotations': [{u'locale': u'zh', u'description': u'\\u8bf7\\u60a8\\u7231\\u62a4\\u548c\\u4fdd\\n\\u62a4\\u536b\\u751f\\u521b\\u5efa\\u4f18\\n\\u7f8e\\u6c34\\u73af\\u5883\\n', u'boundingPoly': {u'vertices': [{u'y': 103, u'x': 150}, {u'y': 103, u'x': 1084}, {u'y': 654, u'x': 1084}, {u'y': 654, u'x': 150}]}}, {u'description': u'\\u8bf7', u'boundingPoly': {u'vertices': [{u'y': 103, u'x': 178}, {u'y': 103, u'x': 322}, {u'y': 241, u'x': 322}, {u'y': 241, u'x': 178}]}}, {u'description': u'\\u60a8', u'boundingPoly': {u'vertices': [{u'y': 107, u'x': 327}, {u'y': 107, u'x': 471}, {u'y': 241, u'x': 471}, {u'y': 241, u'x': 327}]}}, {u'description': u'\\u7231\\u62a4', u'boundingPoly': {u'vertices': [{u'y': 107, u'x': 481}, {u'y': 107, u'x': 774}, {u'y': 245, u'x': 774}, {u'y': 245, u'x': 481}]}}, {u'description': u'\\u548c', u'boundingPoly': {u'vertices': [{u'y': 110, u'x': 784}, {u'y': 110, u'x': 924}, {u'y': 245, u'x': 924}, {u'y': 245, u'x': 784}]}}, {u'description': u'\\u4fdd', u'boundingPoly': {u'vertices': [{u'y': 103, u'x': 933}, {u'y': 103, u'x': 1078}, {u'y': 237, u'x': 1078}, {u'y': 237, u'x': 933}]}}, {u'description': u'\\u62a4', u'boundingPoly': {u'vertices': [{u'y': 299, u'x': 164}, {u'y': 299, u'x': 311}, {u'y': 441, u'x': 311}, {u'y': 441, u'x': 164}]}}, {u'description': u'\\u536b\\u751f', u'boundingPoly': {u'vertices': [{u'y': 303, u'x': 317}, {u'y': 303, u'x': 619}, {u'y': 441, u'x': 619}, {u'y': 441, u'x': 317}]}}, {u'description': u'\\u521b\\u5efa', u'boundingPoly': {u'vertices': [{u'y': 303, u'x': 628}, {u'y': 303, u'x': 930}, {u'y': 444, u'x': 930}, {u'y': 444, u'x': 628}]}}, {u'description': u'\\u4f18', u'boundingPoly': {u'vertices': [{u'y': 299, u'x': 936}, {u'y': 299, u'x': 1084}, {u'y': 441, u'x': 1084}, {u'y': 441, u'x': 936}]}}, {u'description': u'\\u7f8e', u'boundingPoly': {u'vertices': [{u'y': 503, u'x': 150}, {u'y': 503, u'x': 303}, {u'y': 651, u'x': 303}, {u'y': 651, u'x': 150}]}}, {u'description': u'\\u6c34', u'boundingPoly': {u'vertices': [{u'y': 503, u'x': 305}, {u'y': 503, u'x': 456}, {u'y': 651, u'x': 456}, {u'y': 651, u'x': 305}]}}, {u'description': u'\\u73af\\u5883', u'boundingPoly': {u'vertices': [{u'y': 507, u'x': 461}, {u'y': 507, u'x': 764}, {u'y': 654, u'x': 764}, {u'y': 654, u'x': 461}]}}]}]}\n"
     ]
    }
   ],
   "source": [
    "# Running Vision API\n",
    "import base64\n",
    "IMAGE=\"gs://cloud-training-demos/vision/sign2.jpg\"\n",
    "vservice = build('vision', 'v1', developerKey=APIKEY)\n",
    "request = vservice.images().annotate(body={\n",
    "        'requests': [{\n",
    "                'image': {\n",
    "                    'source': {\n",
    "                        'gcs_image_uri': IMAGE\n",
    "                    }\n",
    "                },\n",
    "                'features': [{\n",
    "                    'type': 'TEXT_DETECTION',\n",
    "                    'maxResults': 3,\n",
    "                }]\n",
    "            }],\n",
    "        })\n",
    "responses = request.execute(num_retries=3)\n",
    "print responses"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "zh 请您爱护和保\n",
      "护卫生创建优\n",
      "美水环境\n",
      "\n"
     ]
    }
   ],
   "source": [
    "foreigntext = responses['responses'][0]['textAnnotations'][0]['description']\n",
    "foreignlang = responses['responses'][0]['textAnnotations'][0]['locale']\n",
    "print foreignlang, foreigntext"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h2> Translate sign </h2>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请您爱护和保\n",
      "护卫生创建优\n",
      "美水环境\n",
      " -> Please take good care of your health and create a beautiful water environment protection\n"
     ]
    }
   ],
   "source": [
    "inputs=[foreigntext]\n",
    "outputs = service.translations().list(source=foreignlang, target='en', q=inputs).execute()\n",
    "# print outputs\n",
    "for input, output in zip(inputs, outputs['translations']):\n",
    "  print u\"{0} -> {1}\".format(input, output['translatedText'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h2> Sentiment analysis with Language API </h2>\n",
    "\n",
    "Let's evaluate the sentiment of some famous quotes using Google Cloud Natural Language API."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "POLARITY=1 MAGNITUDE=0.9 for To succeed, you must have tremendous perseverance, tremendous will.\n",
      "POLARITY=1 MAGNITUDE=0.3 for It’s not that I’m so smart, it’s just that I stay with problems longer.\n",
      "POLARITY=1 MAGNITUDE=0.8 for Love is quivering happiness.\n",
      "POLARITY=1 MAGNITUDE=0.9 for Love is of all passions the strongest, for it attacks simultaneously the head, the heart, and the senses.\n",
      "POLARITY=-1 MAGNITUDE=0.5 for What difference does it make to the dead, the orphans and the homeless, whether the mad destruction is wrought under the name of totalitarianism or in the holy name of liberty or democracy?\n",
      "POLARITY=-1 MAGNITUDE=0.8 for When someone you love dies, and you’re not expecting it, you don’t lose her all at once; you lose her in pieces over a long time — the way the mail stops coming, and her scent fades from the pillows and even from the clothes in her closet and drawers. \n"
     ]
    }
   ],
   "source": [
    "lservice = build('language', 'v1beta1', developerKey=APIKEY)\n",
    "quotes = [\n",
    "  'To succeed, you must have tremendous perseverance, tremendous will.',\n",
    "  'It’s not that I’m so smart, it’s just that I stay with problems longer.',\n",
    "  'Love is quivering happiness.',\n",
    "  'Love is of all passions the strongest, for it attacks simultaneously the head, the heart, and the senses.',\n",
    "  'What difference does it make to the dead, the orphans and the homeless, whether the mad destruction is wrought under the name of totalitarianism or in the holy name of liberty or democracy?',\n",
    "  'When someone you love dies, and you’re not expecting it, you don’t lose her all at once; you lose her in pieces over a long time — the way the mail stops coming, and her scent fades from the pillows and even from the clothes in her closet and drawers. '\n",
    "]\n",
    "for quote in quotes:\n",
    "  response = lservice.documents().analyzeSentiment(\n",
    "    body={\n",
    "      'document': {\n",
    "         'type': 'PLAIN_TEXT',\n",
    "         'content': quote\n",
    "      }\n",
    "    }).execute()\n",
    "  polarity = response['documentSentiment']['polarity']\n",
    "  magnitude = response['documentSentiment']['magnitude']\n",
    "  print('POLARITY=%s MAGNITUDE=%s for %s' % (polarity, magnitude, quote))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h2> Speech API </h2>\n",
    "\n",
    "The Speech API can work on streaming data, audio content encoded and embedded directly into the POST message, or on a file on Cloud Storage. Here I'll pass in this <a href=\"https://storage.googleapis.com/cloud-training-demos/vision/audio.raw\">audio file</a> in Cloud Storage."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{u'results': [{u'alternatives': [{u'confidence': 0.98267895, u'transcript': u'how old is the Brooklyn Bridge'}]}]}\n"
     ]
    }
   ],
   "source": [
    "sservice = build('speech', 'v1beta1', developerKey=APIKEY)\n",
    "response = sservice.speech().syncrecognize(\n",
    "    body={\n",
    "        'config': {\n",
    "            'encoding': 'LINEAR16',\n",
    "            'sampleRate': 16000\n",
    "        },\n",
    "        'audio': {\n",
    "            'uri': 'gs://cloud-training-demos/vision/audio.raw'\n",
    "            }\n",
    "        }).execute()\n",
    "print response"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "how old is the Brooklyn Bridge\n",
      "Confidence=0.982679\n"
     ]
    }
   ],
   "source": [
    "print response['results'][0]['alternatives'][0]['transcript']\n",
    "print 'Confidence=%f' % response['results'][0]['alternatives'][0]['confidence']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h2> Clean up </h2>\n",
    "\n",
    "Remember to delete the API key by visiting <a href=\"http://console.cloud.google.com/apis\">API console</a>.\n",
    "\n",
    "If necessary, commit all your notebooks to git.\n",
    "\n",
    "If you are running Datalab on a Compute Engine VM or delegating to one, remember to stop or shut it down so that you are not charged.\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}

{'fraction_non_alphanumeric': 0.20224583433953153, 'fraction_numerical': 0.04823714078724946, 'mean_word_length': 2.990964943982653, 'pattern_counts': {'":': 240, '<': 77, '<?xml version=': 0, '>': 105, 'https://': 4, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6506456', 'n_tokens_mistral': 13241, 'n_tokens_neox': 12186, 'n_words': 3038}