Thursday, June 29, 2023

Hackathon Posts - Oracle AI Service: OCI Language

 Few posts planned for use in expected Hackathons. 

We want to use Oracle AI  Service of OCI Language. (OCI - Oracle Cloud Infrastructure)

A nice workshop from Oracle Live-Labs: Get started with Oracle Cloud Infrastructure Language

The Language AI Service documentation is here.

Permissions

First, we need to gain access to the service. It can be granted to all OCI users of the tenancy or specific group you belong to. For this we need to create a Policy that allows to use ai-service-language-family in tenancy.


    To create a Policy, in the burger menu go to "Identity & Security" and select Policies. There press "Create Policy". Once you did enter Name and description of the Policy and turn on the "show manual editor" under Policy Builder. Now you have 2 options to allow use for specific group or all the users. Enter one of the following:
  •  allow group <group-name> to use ai-service-language-family in tenancy
  • allow any-user to use ai-service-language-family in tenancy 
In case you plan to use later the Oracle Cloud shell, you might want to do the same with "cloud-shell in tenancy". For example: allow any-user to use cloud-shell in tenancy
 

 Intro

Now we can access the AI Service - Language (under the Analytics & AI menu).
 

There are quite a few options there:
  • Using pretrained models
  • Text translation
  • Custom Models
For a given text we can do:
  • Language detection - Identify the language of your text from more than 75 languages.
  • Text classification - Classifies text content into more than 600 categories.
  • Sentiment analysis - Extract sentiment expressed towards product and product aspects.
  • Key phrase extraction - Identify the most salient talking points in your text.
  • Named entity recognition - Identify entities such as people, places, organization, date, etc in text.
  • PII Detection - Detect PII entities and mask, replace or remove them from text records.
At the moment most of the functionality (accept detection) is for English and Spanish languages. 

How can we overcome it? The translation service supports a much longer list of languages, currently (Jun2-2023) those are: 
  • Arabic
  • Brazilian-Portugese
  • Czech
  • Danish 
  • Dutch
  • English 
  • Finnish 
  • French 
  • French-Canadian 
  • German 
  • Greek 
  • Hebrew 
  • Italian 
  • Japanese 
  • Korean 
  • Norwegian 
  • Polish 
  • Romanian 
  • Simplified-Chinese 
  • Spanish 
  • Swedish 
  • Traditional-Chinese 
  • Turkish
 

Now for any of the above languages we can first translate them to English and then analyze.

There are many options to consume the services:

  • Console UI
  • OCI CLI
  • Language SDK
  • Rest API
  • From DataSience Notebook Session


Results in Console

I copied a text I was reading in Businessinsider.com (https://www.businessinsider.com/oracle-chooses-cohere-ai-startup-best-alternative-openai-2023-6)  and analyzed it. As you can see in the image, we can get plain layout or JSON:


Here are the results:

 




 

Actual Work

We can do the same with:

OCI CLI see here in the above mentioned Workshop - Lab 1 Task 1. And Language CLI documentation here.

 Using Language SDK with Java, Python, TypeScript and JavaScript, .NET, Go, Ruby.  See here for links to documentation and a example in Python (Workshop Lab 1 Task 2) see also API reference of AIServiceLanguageClient (that includes translation and many more options) here.

General SDK and CLI documentation (Not only Language) is here.

Using REST APIs (with POSTMAN). See here Workshop Lab 1 Task 4

Language API documentation is here

An example of working Notebooks of "Sentiment Analysis" and "Named Entity Recognition" from Oracle DayaScience Service, see here Workshop Lab 1 Task 3.

 

Custom Models

As part of the Language service we can create custom classification, custom Named entity recognition (NER) and more. In the Workshop Lab 2, we can learn how to create and train Custom models. 

In the Lab, we learn to 

  • Create project.
  • Train a custom NER and custom Text Classification model.
  • Create endpoint and analyze text using OCI console and Python SDK

Please notice, this part requires additional visit to the "Identity & Security" section. 

There creating a dynamic group with the following matching rule:
all {resource.type='ailanguagemodel'}

And  a new policy with the following statements:

  • allow any-user to manage ai-service-language-family in tenancy
  • allow dynamic-group <dynamic-group> to manage objects in tenancy

(<dynamic_group> is the group name you just created). As before we can set it to a group and not any-user.

 For extra reading see this blog and product documentation (that includes supported languages as well).


Translation

Beyond the described in the Intro, Lab 3 of the Workshop presents translating text with Python SDK and using the OCI Console example.


Wednesday, June 28, 2023

Hackathon Posts - Autonomous DB and Python connection – without a Wallet

Few posts planned for use in expected Hackathons.

We want to use Oracle Autonomous DB (ADB) from Python. Preferably without need to worry about wallets.

Here are 2 links to Oracle documentation:

and a very nice blog by Oracle PM, Sharad Chandran R:

 
Below is the Connecting Python Without Wallet option guidelines:

 Install on the client oracledb for python connection to the DB: 

py -m pip install oracledb

if needed

python -m pip install oracledb --upgrade

Under Network of ADB update the Access type:

You can select “add my IP address” (or input addresses manually, separated by comma.)



 

If you prefer no security at all you can set CIDR Block with value 0.0.0.0/0

 

Next under the same Network entry in ADB, in Network / Mutual TLS (mTLS) authentication, Uncheck the "Required" check-mark.


In Python, copy the low connection string, from database connection, into the bellow described “cs” variable add ‘’’ before and after the connection as described below. Run the 3 script lines:

 


import oracledb

 

cs='''(description= (retry_count=20)(retry_delay=3)(address=(protocol=tcps)(port=1522)(host=adb.eu-frankfurt-1.oraclecloud.com))(connect_data=(service_name=q4gsn76smrbsdns_badp4sba1ab3ut26_low.adb.oraclecloud.com))(security=(ssl_server_dn_match=yes)))'''

 

connection=oracledb.connect(user="DB_USER_NAME", password="USER_PASSWORD", dsn=cs)

The important part is the connection. 

As I said, we insert the value of Oracle connection string we copied to the cs variable.


For those who worry about hacking, the above described DB does not exist anymore.