Posted January 20, 20231 yr Q: How do I use Azure Cognitive Services like Translator? A: There are many ways! I’ll show you some examples to get you started. Cognitive Services refers to a wide variety of PaaS solutions that leverage AI. There are many services including speech to text, text to speech, sentiment analysis, conversational language understanding, translation, computer vision, Face API, anomaly detection, content moderator, and others. That is a lot of stuff and I’m not going to dive into all of it today but want to walk through how I’ve used Azure Translator today. Next week we’ll dive into how folks can deploy their own translator containers, then later we’ll cover how to make translator containers and text-analytics containers work together, then finally cover offline translation containers to cover the last of your edge cases and allowing you to leverage Azure without even connecting to Azure. Before diving into deploying or using Translator in Azure folks might ask is: There are Azure Cognitive Services available in Azure so why would I want to have my own containers running cognitive services in the first place? There are a few reasons and probably some that I’m haven’t thought of yet: My clients have poor network connectivity compared to the amount of data they need worked on so it would be too difficult to send and receive all my translations over the internet. This can also be the case for other cognitive services like sending large numbers of images for face detection over the internet. I have very latency sensitive workloads that are not near the cloud. For certain use cases like near real-time feedback to clients at remote sites leveraging computer vision they may want to minimize latency as much as possible by bringing processing from the cloud to their site. For workloads like these connected containers provide the best solution. There is no internet access at all on my client’s network. There are some clients like some research labs or government networks that have disconnected networks that don’t connect to the internet at all so they can’t directly query the cloud. For these workloads disconnected containers can leverage the power of Azure without directly connecting to Azure. Those are all big problems for using the cloud but Microsoft is a cloud company now so it is nice that Azure can be leveraged no matter which unique situation your clients come up with. Even if you want to use fancy containers for your production environment it makes sense to leverage Azure Translator in the cloud for dev or pre-prod. Testing in Azure is very easy to deploy without the need for resources to host containers and you can use the same code against Azure, connected containers, and disconnected containers. Testing in Azure is also great because there is a free tier so pre-production testing doesn’t cost much if anything extra. All the documentation you could want for Translation in Azure is available here: Azure Cognitive Services Translator documentation - quickstarts, tutorials, API reference - Azure Cognitive Services Finally, now we’re going to deploy and use Azure Translator the way nearly everyone will do it by leveraging Translator in Azure. Log into the Azure portal then search for and click on the “Translators” service. Select “Create” then fill in a few fields. I’m all about free stuff so it is nice that translation can be performed for free. Click “Next” a few times then “Create” and now you have your Azure Translator resource. That was almost too easy. Let’s try using our new resource a bit to make sure it works as expected. We can see in the “Keys and Endpoint” section all the info we’ll need to do translations. You can get all the information you need to write your own REST calls here: Translator V3.0 Reference - Azure Cognitive Services I’ll provide a few examples here to show you how I like to do it with my favorite PowerShell and a little prettier output. I know a lot of you prefer curl, but there are already more examples covering curl in the official documentation, so I’ll show PowerShell here. First, I initialize variables based on the “Keys and Endpoint” section my Translator resource. These variables are the only items you will need to change if you’re testing code in Azure, then running it later against connected or disconnected containers. 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. #Variable section $baseURI = 'Azure Cognitive Services Translator documentation - quickstarts, tutorials, API reference - Azure Cognitive Services' $Key = 'I_AM_NOT_SHARING_MY_KEY_BUT_PUT_YOURS_HERE' $Region = 'eastus2' $TextToTranslate = 'I like bananas and accordions.' Next, I do a sample translation. If you know which language you’re translating from then you can specify “from” as documented in the API reference link above however if you do not specify it then it will automatically detect the source language for you. #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. Next, I do language detection. Language detection is for I’m not sure what language I was provided and simply want to detect the language. #Test language detection: $URI = $baseURI + 'detect?api-version=3.0' $resp = Invoke-WebRequest -Uri $URI -Method Post -Headers $([hashtable]@{"Ocp-Apim-Subscription-Key"=$Key;"Ocp-Apim-Subscription-Region"="EastUS2"}) -Body "[ {'Text':'$TextToTranslate'} ]" -ContentType "application/json; charset=utf-8" -ErrorAction Inquire $trans = ($resp.Content | ConvertFrom-Json).language $trans en Next, I list all the languages available. Listing the languages available is also helpful if you don’t recognize the information returned in language detection. If I didn’t know “en” meant English then I would just look it up in the table I output below. There are several languages where the “Native Name” is not output in a human readable format. This is not a concern for me since I don’t need those languages on this computer however if you have the right languages loaded they will display properly. $URI = $baseURI + 'languages?api-version=3.0' $resp = Invoke-WebRequest -Uri $URI -Method Get -Headers $([hashtable]@{"accept-language"="en"}) -ContentType "application/json; charset=utf-8" -ErrorAction Inquire $trans = ($resp.Content | ConvertFrom-Json) $Names = $trans.translation | Get-Member -MemberType NoteProperty $Names.name | Select @{N='Abbreviation';E={$_}},@{N='Name';E={$trans.translation.($_).Name}},@{N='NativeName';E={$trans.translation.($_).NativeName}} ------------ ---- ---------- af Afrikaans Afrikaans am Amharic አማáˆáŠ› ar Arabic العربية as Assamese ঠসমীয়া az Azerbaijani AzÉ™rbaycan ba Bashkir Bashkir bg Bulgarian БългарÑки bn Bangla বাংলা bo Tibetan བོད་སà¾à½‘་ bs Bosnian Bosnian ca Catalan Català cs Czech ÄŒeÅ¡tina cy Welsh Cymraeg da Danish Dansk de German Deutsch dv Divehi Þ‹Þ¨ÞˆÞ¬Þ€Þ¨Þ„Þ¦ÞÞ° el Greek Ελληνικά en English English es Spanish Español et Estonian Eesti eu Basque Euskara fa Persian Ùارسی fi Finnish Suomi fil Filipino Filipino fj Fijian Na Vosa Vakaviti fo Faroese Føroyskt fr French Français fr-CA French (Canada) Français (Canada) ga Irish Gaeilge gl Galician Galego gu Gujarati ગà«àªœàª°àª¾àª¤à«€ he Hebrew עברית hi Hindi हिनà¥à¤¦à¥€ hr Croatian Hrvatski hsb Upper Sorbian Hornjoserbšćina ht Haitian Creole Haitian Creole hu Hungarian Magyar hy Armenian Õ€Õ¡ÕµÕ¥Ö€Õ¥Õ¶ id Indonesian Indonesia ikt Inuinnaqtun Inuinnaqtun is Icelandic Ãslenska it Italian Italiano iu Inuktitut áƒá“„ᒃᑎá‘ᑦ iu-Latn Inuktitut (Latin) Inuktitut (Latin) ja Japanese 日本語 ka Georgian ქáƒáƒ თული kk Kazakh Қазақ Тілі km Khmer ážáŸ’មែរ kmr Kurdish (Northern) Kurdî (Bakur) kn Kannada ಕನà³à²¨à²¡ ko Korean í•œêµì–´ ku Kurdish (Central) Kurdî (Navîn) ky Kyrgyz Кыргызча lo Lao ລາວ lt Lithuanian Lietuvių lv Latvian LatvieÅ¡u lzh Chinese (Literary) ä¸æ–‡ (文言文) mg Malagasy Malagasy mi MÄori Te Reo MÄori mk Macedonian МакедонÑки ml Malayalam മലയാളം mn-Cyrl Mongolian (Cyrillic) Mongolian (Cyrillic) mn-Mong Mongolian (Traditional) á ®á £á ©á á £á ¯ á ¬á ¡á ¯á ¡ mr Marathi मराठी ms Malay Melayu mt Maltese Malti mww Hmong Daw Hmong Daw my Myanmar (Burmese) မြန်မာ nb Norwegian Norsk BokmÃ¥l ne Nepali नेपाली nl Dutch Nederlands or Odia ଓଡ଼ିଆ otq Querétaro Otomi Hñähñu pa Punjabi ਪੰਜਾਬੀ pl Polish Polski prs Dari دری ps Pashto پښتو pt Portuguese (Brazil) Português (Brasil) pt-PT Portuguese (Portugal) Português (Portugal) ro Romanian Română ru Russian РуÑÑкий sk Slovak SlovenÄina sl Slovenian SlovenÅ¡Äina sm Samoan Gagana SÄmoa so Somali Soomaali sq Albanian Shqip sr-Cyrl Serbian (Cyrillic) СрпÑки (ћирилица) sr-Latn Serbian (Latin) Srpski (latinica) sv Swedish Svenska sw Swahili Kiswahili ta Tamil தமிழ௠te Telugu తెలà±à°—à± th Thai ไทย ti Tigrinya ትáŒáˆ tk Turkmen Türkmen Dili tlh-Latn Klingon (Latin) Klingon (Latin) tlh-Piqd Klingon (pIqaD) Klingon (pIqaD) to Tongan Lea Fakatonga tr Turkish Türkçe tt Tatar Татар ty Tahitian Reo Tahiti ug Uyghur ئۇيغۇرچە uk Ukrainian УкраїнÑька ur Urdu اردو uz Uzbek (Latin) Uzbek (Latin) vi Vietnamese Tiếng Việt yua Yucatec Maya Yucatec Maya yue Cantonese (Traditional) 粵語 (ç¹é«”) zh-Hans Chinese Simplified ä¸æ–‡ (简体) zh-Hant Chinese Traditional ç¹é«”ä¸æ–‡ (ç¹é«”) zu Zulu Isi-Zulu As you can see above some of the output doesn’t look right. I’m not fluent in these languages but some of that output doesn’t look correct. Uyghur, Cantonese, Chinese and others have a bunch of rectangles. This happened because the default output encoding for PowerShell 5 doesn’t properly display Unicode characters. Fortunately, if you run this in Windows Terminal or PowerShell 7 which I normally run in VS Code then characters are displayed properly as shown below. Abbreviation Name NativeName ------------ ---- ---------- af Afrikaans Afrikaans am Amharic አማርኛ ar Arabic العربية as Assamese অসমীয়া az Azerbaijani Azərbaycan ba Bashkir Bashkir bg Bulgarian Български bn Bangla বাংলা bo Tibetan བོད་སྐད་ bs Bosnian Bosnian ca Catalan Català cs Czech Čeština cy Welsh Cymraeg da Danish Dansk de German Deutsch dv Divehi ދިވެހިބަސް el Greek Ελληνικά en English English es Spanish Español et Estonian Eesti eu Basque Euskara fa Persian فارسی fi Finnish Suomi fil Filipino Filipino fj Fijian Na Vosa Vakaviti fo Faroese Føroyskt fr French Français fr-CA French (Canada) Français (Canada) ga Irish Gaeilge gl Galician Galego gu Gujarati ગુજરાતી he Hebrew עברית hi Hindi हिन्दी hr Croatian Hrvatski hsb Upper Sorbian Hornjoserbšćina ht Haitian Creole Haitian Creole hu Hungarian Magyar hy Armenian Հայերեն id Indonesian Indonesia ikt Inuinnaqtun Inuinnaqtun is Icelandic Íslenska it Italian Italiano iu Inuktitut ᐃᓄᒃᑎᑐᑦ iu-Latn Inuktitut (Latin) Inuktitut (Latin) ja Japanese 日本語 ka Georgian ქართული kk Kazakh Қазақ Тілі km Khmer ខ្មែរ kmr Kurdish (Northern) Kurdî (Bakur) kn Kannada ಕನ್ನಡ ko Korean 한국어 ku Kurdish (Central) Kurdî (Navîn) ky Kyrgyz Кыргызча lo Lao ລາວ lt Lithuanian Lietuvių lv Latvian Latviešu lzh Chinese (Literary) 中文 (文言文) mg Malagasy Malagasy mi Māori Te Reo Māori mk Macedonian Македонски ml Malayalam മലയാളം mn-Cyrl Mongolian (Cyrillic) Mongolian (Cyrillic) mn-Mong Mongolian (Traditional) ᠮᠣᠩᠭᠣᠯ ᠬᠡᠯᠡ mr Marathi मराठी ms Malay Melayu mt Maltese Malti mww Hmong Daw Hmong Daw my Myanmar (Burmese) မြန်မာ nb Norwegian Norsk Bokmål ne Nepali नेपाली nl Dutch Nederlands or Odia ଓଡ଼ିଆ otq Querétaro Otomi Hñähñu pa Punjabi ਪੰਜਾਬੀ pl Polish Polski prs Dari دری ps Pashto پښتو pt Portuguese (Brazil) Português (Brasil) pt-PT Portuguese (Portugal) Português (Portugal) ro Romanian Română ru Russian Русский sk Slovak Slovenčina sl Slovenian Slovenščina sm Samoan Gagana Sāmoa so Somali Soomaali sq Albanian Shqip sr-Cyrl Serbian (Cyrillic) Српски (ћирилица) sr-Latn Serbian (Latin) Srpski (latinica) sv Swedish Svenska sw Swahili Kiswahili ta Tamil தமிழ் te Telugu తెలుగు th Thai ไทย ti Tigrinya ትግር tk Turkmen Türkmen Dili tlh-Latn Klingon (Latin) Klingon (Latin) tlh-Piqd Klingon (pIqaD) Klingon (pIqaD) to Tongan Lea Fakatonga tr Turkish Türkçe tt Tatar Татар ty Tahitian Reo Tahiti ug Uyghur ئۇيغۇرچە uk Ukrainian Українська ur Urdu اردو uz Uzbek (Latin) Uzbek (Latin) vi Vietnamese Tiếng Việt yua Yucatec Maya Yucatec Maya yue Cantonese (Traditional) 粵語 (繁體) zh-Hans Chinese Simplified 中文 (简体) zh-Hant Chinese Traditional 繁體中文 (繁體) zu Zulu Isi-Zulu These Azure cognitive services are very powerful and easy to use so they can be stitched together with a few REST calls to make some powerful applications. I look forward to seeing how more clients integrate these into their applications. Have fun scripting! References: Cognitive Services documentation: Azure Cognitive Services documentation Translator documentation: Azure Cognitive Services Translator documentation - quickstarts, tutorials, API reference - Azure Cognitive Services Translator v3 API: Translator V3.0 Reference - Azure Cognitive Services Continue reading...
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.