# 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:

# 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.

  1. Click the Create a resource button (the plus-sign in the top left corner)
  2. 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
    1. This brings you to the Create Text Analytics blade
    2. Select a Resource Group
    3. Fill in a Name for the service
    4. Pick a Pricing tier
    5. 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.

  1. 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
1
  1. 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}
1
2
3
4
5
  1. 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)

  1. 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
  2. Select the /text/analytics/v3.0/sentiment endpoint
  3. Click the Try it out! button
  4. In the body parameter, paste in the following JSON data:
{
    "documents": [
        {
            "language": "en",
            "id": "1",
            "text": "I love this"
        }
    ]
}
1
2
3
4
5
6
7
8
9
  1. Change the Parameter content type to application/json
  2. Click Execute to try it out
  3. 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!