DSP Blog

Portable Sentiment Analysis with Azure Cognitive Services - Part 2

Written by Phil Brown | 30-Apr-2020 09:42:32

In the previous blog we talked through a basic 'getting started' process on Azure Cognitive Services.  Now we are going to take the service from Azure and run it somewhere else.  There are a few reasons why you might want to do this:

  • To use Text Analytics on secure documents which can’t be placed in or sent to the Cloud
  • To use Text Analytics in a different Cloud platform or On-Premises
  • To run Text Analytics in Azure in a region which it isn’t currently available
 

These are all valid use cases and Azure enables you to download the docker image of the Text Analytics service and use it anywhere.  This isn’t going to be a tutorial on Docker, but we will cover the basic steps of running Docker on Linux.

 

How to run Docker on Linux

I’ve used another Cloud platform for this demonstration and spun up a VM. Installing Docker is pretty straight forward now and can be done with a single command:

[root@vm01-azure-cognative-text ~]# yum install docker-engine

 

We then run the classic Hello-World container from Docker to validate the installation:

[opc@vm01-azure-cognative-text ~]$ sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
Trying to pull repository docker.io/library/hello-world ...
latest: Pulling from docker.io/library/hello-world
1b930d010525: Pull complete
Digest: sha256:f9dfddf63636d84ef479d645ab5885156ae030f611a56f3a7ac7f2fdd86d7e4e
Status: Downloaded newer image for hello-world:latest
 
Hello from Docker!
This message shows that your installation appears to be working correctly.

 

So now we have a docker running we can pull down the latest Docker Text Analytics service from Microsoft:

[opc@vm01-azure-cognative-text ~]$ sudo docker pull mcr.microsoft.com/azure-cognitive-services/sentiment:latest

 

Then we just need to run it:

[opc@vm01-azure-cognative-text ~]$ sudo docker run --rm -it -p 5000:5000 --memory 4g --cpus 1 mcr.microsoft.com/azure-cognitive-services/sentiment Eula=accept Billing=https://azu-txta-paas1.cognitiveservices.azure.com/ ApiKey=<don’t look>

 

The docker image requires just a few parameters which enable it to run, all of which are well documented.  Also the docker image will need to be able to communicate with Azure to ensure all billing metrics are captured; if it can’t communicate with Azure then it will not process the Text Analytics API calls. 

I’m doing this on a VM with a Public IP but this could equally be done on a private subnet with outbound communications enabled via a NAT gateway.  The -p 5000:5000 is the port translation element of docker, meaning that to communicate with the actual API it will be on port 5000, but this could be changed to 80 or something else entirely. 

 

Portability outside of Azure

Once started we can directly send API requests to the service. Interestingly, if you’re not familiar with tools like Postman, Azure ship the container with Swagger to help testing of the API calls.  The below image is what you will see; if you can’t call the home page (http://<IP>:5000/swagger) then check the following:

  • Listener Port (netstat -na | grep LIST)
  • Host Level Firewalls (firewall-cmd --list-all)
  • Cloud platform network ingress rules

 

Now we have the Azure Text Analytics service running outside of Azure - pretty neat! Let’s celebrate with a song.  Maybe I’m showing my age but a classic tune for me is Red Hot Chilli Peppers Under The Bridge.

Text Analytics has this as having largely a negative sentiment:

Had my parents known about sentiment analysis, they would have worked out what made me such a moody teenager. 

 

SSL setup and other Cloud stuff...

One issue with running the docker image is that now it’s gone from a ‘managed’ service hosted in Azure with SSL certificates to be running on a standalone VM without any SSL.  However, this can be easily fixed with Cloud services-provided managed Load Balancers which can also handle SSL termination.

To generate a SSL self-signed certificate you can do the following in OpenSSL on a Linux VM.

openssl genrsa -des3 -passout pass:x -out server.pass.key 2048
openssl rsa -passin pass:x -in server.pass.key -out server.key
openssl req -new -key server.key -out server.csr
openssl x509 -req -sha256 -days 365 -in server.csr -signkey server.key -out server.crt 
 

When you upload this to a Load Balancer you browser will still say it’s an 'Invalid Certificate' as it doesn’t recognise the certificate authority that created the certificate (i.e. you).  You can use OpenSSL to create a 'certificate authority certificate' and this will bypass some issues. If you then add that certificate to your browser's 'trusted certificate authority certificates'...it's a bit of a mouth full.  But this is a blog about Azure Text Analytics, so now I have my docker container running and terminated with SSL I will celebrate with one more song.

How about 'Get Off My Cloud' (see what I did there) by the Rolling Stones; well it’s a song you probably know, and it seems in terms of sentiment it’s neither positive or negative.

 

Conclusion

In conclusion I think the element that I’m most interested to share is the portability of Cloud services outside their native Cloud platform.  Azure has done an amazing job in helping customers utilise Cloud services even if there are significant blockers against said customers moving to a Cloud platform, or if they have already decided to use a different Cloud provider.  The above is just one of a number of services which have this level of portability. 

To read more about how DSP-Explorer are exploring AI technologies, check out our blog predicting the results of the Rugby World Cup: https://content.dsp.co.uk/rugby-world-cup-oracle-machine-learning. You can also get in touch with one of our specialists at enquiries@dsp.co.uk.