“If you’re not using deep learning, you should be.” – Jeff Dean, Google Fellow.

Google has started out with a mission to empower everything and everyone with Artificial Intelligence (A.I.).  It has open-sourced Tensorflow and supporting libraries to enable developers and enterprises to build and train models, and infer (predict) using those.  Building useful enterprise services with this may take time.

Google has also exposed many of the A.I services (via Cloud Machine learning APIs) that can be quite useful in your applications. Extracting features and text from images, translating text from one language to another, sentiment analysis on text can help you to improve user experience dramatically. There are new businesses and business models enabled by these services alone. Google has done the hard work of building and training the model.  You simply call a REST API to exploit their machine learning APIs.   These APIs simply act as a declarative service invocation method without needed to understand or maintain the deep infrastructure underneath.

Their machine learning APIs include:

    1. Google Vision API
    1. Google Speech API
    1. Google Natural Language API
    1. Google Translation API
    1. Google Video Intelligence API
  1. And probably many more services by the time this gets published.

These are all provided as REST APIs.  See the Google site below for documentation and details of the service.

In Couchbase 5.0, N1QL R&D engineer, Isha Kandaswamy has developed and has written about the CURL() functionality.  Using CURL(), you can use any of REST services with JSON endpoint. An example of using Google map API is shown here. The JSON result from the services can be naturally processed in N1QL because N1QL is designed to be SQL for JSON.

Note: You can use CURL() use A.I or other services from any publicly available service.

This blog shows the way to use Google Cloud AI API directly on the data you have in Couchbase, directly with a simple N1QL statement.  All of the Google Cloud Machine Learning APIs return JSON.  So, CURL() can be used to call any of the services.  I’ll show you examples of using Google Vision API, Google translation API, and Google Natural Language API.

Note: You need to setup your project and enable each of the services and obtain the API-KEY which you need to pass. In the examples below, I’ve removed my key. You should replace PUT YOUR KEY HERE with your key.

Let’s explore using some of the APIs.

1. Using Google Vision API:

You could be an insurance company where customers upload photos of their cars and identifications. You could be a college receiving images of transcripts.   You want to extract the text in the image for veracity, improved user experience and making it easier to search for those when needed.

Google has gone from organizing world’s text to world’s multi-media content. Vision API gives you powerful features to extract a lot of metadata within an image and correlate that information to information and other images available on the web.

Let’s get the full analysis of the image using Google Vision API.

This query returns a huge 120K response describing all the polygons, associated texts.  Here’s the response.  Let’s issue the query again to and project the things we need.  Vision API returns the concatenated list of strings.  You can use the SPLIT() or TOKENS() function of N1QL to get each string separately.

2. Google Translation API

This API does what it says: Translate from one language to another.  It can automatically detect the source language.  Arguments to this API is simply the source content and target language.  In this example, let’s translate the customer reviews for a hotel from English to French. 

The result of the query, which includes translation to French is:

Since I don’t understand French well, I had the review translated to Kannada, my mother tongue.  I must say, the translation was decent for a machine.  Some sentences were convoluted (almost sounded old Kannada), but overall understandable.  Here’s that translation.

3. Google Natural Language API

The natural language API does sentiment analysis, entity analysis, parse the intent and much more. You can invoke these services just be changing the parameters to the service.

Let’s use the hotel review data, which does have some emotional content!

We’ve only projected the fields we’re interested here, review, magnitude, and sentiment score. See the Google docs for full description of what these mean and rest of the data it generates.

Google natural language API can also extract entities, their web references.  Let’s look at an example of that as well.

The entity analysis API correctly identifies the location and gives us the web references (URLs) to the landmark. When the references are not quite specific, like the William Team Rooms here, the API can give very high-level generic references.

Summary:

Couchbase N1QL applications can use Google machine learning APIs easily to improve the customer experience. CURL() gives a flexible method to use the A.I services not only from Google but also from other vendors. Try it out.

References:

1. N1QL:  https://docs.couchbase.com/server/5.5/n1ql/n1ql-language-reference/index.html

2. CURL() documentation: https://developer.couchbase.com/documentation/server/current/n1ql/n1ql-language-reference/curl.html

3. CURL comes to N1QL: https://dzone.com/articles/curl-comes-to-n1ql-querying-external-json-data

Author

Posted by Keshav Murthy

Keshav Murthy is a Vice President at Couchbase R&D. Previously, he was at MapR, IBM, Informix, Sybase, with more than 20 years of experience in database design & development. He lead the SQL and NoSQL R&D team at IBM Informix. He has received two President's Club awards at Couchbase, two Outstanding Technical Achievement Awards at IBM. Keshav has a bachelor's degree in Computer Science and Engineering from the University of Mysore, India, holds ten US patents and has three US patents pending.

Leave a reply