Q: How do I get my Translator containers to work the same way as Azure Translator?
A: Integrate it with the text-analytics container.
This is part 3 of a series on cognitive services, specifically on how to leverage Translator in Azure and in containers so if you would like to catch up please check out the previous posts here:
Part 1: How do I use Azure Cognitive Services like Translator? -
We have come a long ways but the issue we’re going to address today is getting our Translator containers to work without specifying ‘from’ just like the good ole’ Azure Translator resource does it. First though you might ask yourself why it is even worth spending a bunch of effort to avoid passing one parameter. In this case it really matters for several reasons:
The steps will be like deploying the Translator container. We’re going to deploy the Azure resource, then use the information from the resource in Azure to deploy the text-analytics container. Text analytics is far more capable than just detecting languages, it can do sentiment analysis, key phrase extraction, text summarization, and much more but for our purposes we’re just going to have it tell us the language of the text we give it. Once we have text-analytics we can setup Translator to automatically work with text-analytics on our behalf so that it all works just like we’re used to in Azure.
Disclaimer
The sample scripts are not supported under any Microsoft standard support program or service. The sample scripts are provided AS IS without warranty of any kind. Microsoft further disclaims all implied warranties including, without limitation, any implied warranties of merchantability or of fitness for a particular purpose. The entire risk arising out of the use or performance of the sample scripts and documentation remains with you. In no event shall Microsoft, its authors, or anyone else involved in the creation, production, or delivery of the scripts be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the sample scripts or documentation, even if Microsoft has been advised of the possibility of such damages.
From the same VM that we were in for Part 2 we’ll run this command to get the textanalytics container
sudo docker pull mcr.microsoft.com/azure-cognitive-services/textanalytics/language:latest
sudo docker run --rm -it -p 5000:5000 --memory 4g --cpus 1 mcr.microsoft.com/azure-cognitive-services/textanalytics/language Eula=accept Billing=https://paulanalyticsforblog.cognitiveservices.azure.com/ ApiKey=<put_your_own_key_here>
sudo apt-get update
sudo apt-get install docker-compose-plugin
I created a file in /home/George called TranslatorComposer.yaml with the following contents. It specifies how I want different containers to be deployed.
version: "3.3"
services:
text-translation:
image: "mcr.microsoft.com/azure-cognitive-services/translator/text-translation:latest"
volumes:
- /mnt/d/TranslatorContainer:/usr/local/models
ports:
- 5000:5000
environment:
- EULA=accept
- Languages=en,fr,es
- ApiKey=I_AM_NOT_SHARING_MY_KEY_BUT_PUT_YOURS_HERE
- Billing=https://paultestforblog.cognitiveservices.azure.com/
- LADURL=http://language-detection:5000
language-detection:
image: "mcr.microsoft.com/azure-cognitive-services/textanalytics/language"
ports:
- 5100:5000
environment:
- EULA=accept
- ApiKey= I_AM_NOT_SHARING_MY_KEY_BUT_PUT_YOURS_HERE
- Billing=https://paulanalyticsforblog.cognitiveservices.azure.com/
sudo docker compose -f /home/george/TranslatorComposer.yaml up
#Variable section
$baseURI = 'http://MyVMIPAddress:5000/'
$Key = '<I_don’t_share_my_key>'
$Region = 'eastus2'
$TextToTranslate = 'I like bananas and accordions.'
#Test Translation:
$to = 'es'
$URI = $baseURI + 'translate?api-version=3.0&to=' + $to
$resp = Invoke-WebRequest -Uri $URI -Method Post -Headers $([hashtable]@{"Ocp-Apim-Subscription-Key"=$Key;"Ocp-Apim-Subscription-Region"=$Region}) -Body "[ {'Text':'$TextToTranslate'} ]" -ContentType "application/json; charset=utf-8" -ErrorAction Inquire
$trans = ($resp.Content | ConvertFrom-Json).translations.text
$trans
Me gustan los plátanos y los acordeones.
Hurrah, now we have Azure Cognitive Services running in a container anywhere we want, and it behaves just like the resources we know and love in Azure.
Have fun scripting!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.