TIP
š„ Make sure you star the repo (opens new window) to keep up to date with new tips and tricks.
š” Learn more : Azure Cognitive Services documentation (opens new window).
š”Ā CheckoutĀ AzureĀ AIĀ resources forĀ developers (opens new window).
š” Read a blog : How to use Cognitive Services and containers (opens new window).
šŗ Watch the video : How to deploy Azure Cognitive Services in containers (opens new window).
# How to deploy Azure Cognitive Services in containers
# Run Cognitive Services anywhere
Azure Cognitive Services (opens new window) provide a broad range of pre-configured Artificial Intelligence capabilities in the categories of Vision, Speech, Language, Decision and Search (opens new window). You can use these capabilities by calling the Cognitive Services REST APIs, which are hosted in Azure. However, sometimes, you want to bring AI capabilities to your on-premises environment, to bring them closer to your data for compliance or security. You can do that by running Cognitive Services in Docker containers (opens new window), which can run anywhere that containers can run.
In this post, we'll deploy an Azure Cognitive Services container and query its API.
# Prerequisites
If you want to follow along, you'll need the following:
- An Azure subscription (If you don't have an Azure subscription, create a free account (opens new window) before you begin)
- Docker Desktop (opens new window)
# Download and run an Cognitive Services Docker Image
Cognitive Services APIs can run in Docker containers. In a container, the APIs can run anywhere, but they will still need to connect to an Azure Cognitive Services resource to send billing information. First, we'll create an Azure Cognitive Services resource using the Azure portal.
- Click the Create a resource button (the plus-sign in the top left corner)
- In this example, we'll use the Text Analytics Sentiment Analysis API (opens new window). Search for Text Analytics, select the "Text Analytics" result and click Create
- This brings you to the Create Text Analytics blade
- Select a Resource Group
- Fill in a Name for the service
- Pick a Pricing tier
- Click Review + create and Create after that to create the service
(Create Text Analytics in the Azure portal)
When the Text Analytics Cognitive Service is created, navigate to it in the Azure portal. In the Keys and Endpoint blade, copy the Access key and the Endpoint for the service. We'll need those later.
(Keys and Endpoints blade in the Azure portal)
Now that we have a Cognitive Service in Azure, we can download and run a Cognitive Service in a Docker container locally.
- On your local machine (or wherever you have installed Docker), open a Command Prompt and type the following command to pull the container image for the sentiment analysis API
docker pull mcr.microsoft.com/azure-cognitive-services/textanalytics/sentiment:3.0-en
- When the container is downloaded, you need to start it by running the following command, which includes the access key and endpoint that you've copied from the Azure portal
docker run --rm -it -p 5000:5000 --memory 8g --cpus 1
mcr.microsoft.com/azure-cognitive-services/textanalytics/sentiment:3.0-en
Eula=accept
Billing={ENDPOINT_URI}
ApiKey={API_KEY}
2
3
4
5
- Now that the container is running, you can access the API through https://localhost:5000. Open a browser and navigate to the URL
(Cognitive Service running locally in a Docker container)
- Click the Service API Description link. This opens the swagger page for the API. In here, you can browse the API endpoints and try them out
- Select the /text/analytics/v3.0/sentiment endpoint
- Click the Try it out! button
- In the body parameter, paste in the following JSON data:
{
"documents": [
{
"language": "en",
"id": "1",
"text": "I love this"
}
]
}
2
3
4
5
6
7
8
9
- Change the Parameter content type to application/json
- Click Execute to try it out
- Scroll down to see the results. This will show that the sentence "I love this" is positive. You can play around with the service by changing the sentence to "I hate this" and check the result
(Result of querying the Cognitive Service in a container)
# Conclusion
Azure Cognitive Services in Docker containers (opens new window) enable you to run your cognitive applications anywhere. Go and check it out!