Text Analytics with REST APIs for Autonomous Databases

Loïc Lefèvre
db-one
Published in
3 min readSep 23, 2021

--

Text analytics is a very interesting topic and not that obvious.

Oracle recently announced the Oracle Machine Learning Services in their public cloud. With this comes a set of REST APIs to access the capabilities available.

To summarize, there are 3 families of APIs:

  • Machine Learning models “market place”: list, discover, and store models (even those created using open source frameworks: Tensorflow, Keras… thanks to the ONNX format)
  • Machine Learning models deployment: deploy the models inside Oracle databases for real-time scoring… very useful for MLOps!
  • Cognitive Text: the subject of this post!

These APIs can be accessed from PL/SQL using the APEX_WEB_SERVICE package (from Autonomous Databases for example).

OAuth2 Authentication

The very first step is to authenticate in order to get a token for all the next REST API calls (this token will last one hour and then will need to be refreshed).

For this very first step, you’ll need to gather the following information:

  • database username: easy
  • database password: my_super_password2021
  • autonomous database cloud service instance URL: this is adb.<region>.oraclecloud.com (see a list of the available regions here)
  • OCI tenant OCID: see the documentation
  • database name: this is the name of your database (example with loic in the screenshot, the case is not important)

With these information, you can now build your REST API call as following:

The body will be a JSON document:

{"grant_type":"password","username":"easy","password":"my_super_password2021"}

The URL will be:

https://adb.eu-frankfurt-1.oraclecloud.com/omlusers/tenants/ocid1.tenancy.oc1..../databases/loic/api/oauth2/v1/token

Two headers:

Content-Type: application/json
Accept: application/json

Now you can perform the call and retrieve your OAuth 2.0 token:

curl -i -k -X POST --header 'Content-Type: application/json' --header 'Accept: application/json'\ 
-d '{"grant_type":"password", "username":"easy", "password":"my_super_password2021"}'\
"<autonomous-database-cloud-service-instance-url>/omlusers/tenants/${tenant_name}/databases/${database_name}/api/oauth2/v1/token"

Response example:

{
"accessToken": "eyJhb...==",
"expiresIn": 3600,
"tokenType": "Bearer"
}

The accessToken field contains the token to keep. You’ll use it for your next REST API calls for Bearer authentication this time.

Cognitive Text APIS

And now the fun, you have now access to the following REST APIs:

Note that several languages are supported such as AMERICAN, FRENCH, and SPANISH.

Example of Sentiment Analysis

--

--