Deploy a Text Analytics container to Azure Container Instances

Learn how to deploy the Cognitive Services Text Analytics container to Azure Container Instances. This procedure exemplifies the creation of a Text Analytics resource, the creation of an associated Sentiment Analysis image and the ability to exercise this orchestration of the two from a browser. Using containers can shift the developers' attention away from managing infrastructure to instead focusing on application development.

Prerequisites

  • Use an Azure subscription. If you don't have an Azure subscription, create a free account before you begin.

Create a Cognitive Services Text Analytics resource

  1. Sign in to the Azure portal.

  2. Select Create a resource, and then go to AI + Machine Learning > Text Analytics. Or, go to Create Text Analytics.

  3. Enter all the required settings:

    Setting Value
    Name Enter a name (2-64 characters).
    Subscription Select the appropriate subscription.
    Location Select a nearby location.
    Pricing tier Enter S, the standard pricing tier.
    Resource group Select an available resource group.
  4. Select Create, and wait for the resource to be created. Your browser automatically redirects to the newly created resource page.

  5. Collect the configured endpoint and an API key:

    Resource tab in portal Setting Value
    Overview Endpoint Copy the endpoint. It appears similar to https://northeurope.api.cognitive.microsoft.com/text/analytics/v2.0.
    Keys API Key Copy one of the two keys. It's a 32-character alphanumeric string with no spaces or dashes: <xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx>.

Create an Azure Container Instance resource

  1. Go to the Create page for Container Instances.

  2. On the Basics tab, enter the following details:

    Setting Value
    Subscription Select your subscription.
    Resource group Select the available resource group or create a new one such as cognitive-services.
    Container name Enter a name such as cognitive-container-instance. The name must be in lower caps.
    Location Select a region for deployment.
    Image type If your container image is stored in a container registry that doesn’t require credentials, choose Public. If accessing your container image requires credentials, choose Private. Refer to container repositories and images for details on whether or not the container image is Public or Private ("Public Preview").
    Image name Enter the Cognitive Services container location. The location is what's used as an argument to the docker pull command. Refer to the container repositories and images for the available image names and their corresponding repository.

    The image name must be fully qualified specifying three parts. First, the container registry, then the repository, finally the image name: <container-registry>/<repository>/<image-name>.

    Here is an example, mcr.microsoft.com/azure-cognitive-services/keyphrase would represent the Key Phrase Extraction image in the Microsoft Container Registry under the Azure Cognitive Services repository. Another example is, containerpreview.azurecr.io/microsoft/cognitive-services-speech-to-text which would represent the Speech to Text image in the Microsoft repository of the Container Preview container registry.
    OS type Linux
    Size Change size to the suggested recommendations for your specific Cognitive Service container:
    2 CPU cores
    4 GB
  3. On the Networking tab, enter the following details:

    Setting Value
    Ports Set the TCP port to 5000. Exposes the container on port 5000.
  4. On the Advanced tab, enter the required Environment Variables for the container billing settings of the Azure Container Instance resource:

    Key Value
    apikey Copied from the Keys page of the resource. It is a 32 alphanumeric-character string with no spaces or dashes, xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.
    billing Copied from the Overview page of the resource.
    eula accept
  5. Click Review and Create

  6. After validation passes, click Create to finish the creation process

  7. When the resource is successfully deployed, it's ready

Verify the Key Phrase Extraction container instance

  1. Select the Overview tab, and copy the IP address.

  2. Open a new browser tab, and enter the IP address. For example, enter http://<IP-address>:5000 (http://55.55.55.55:5000). The container's home page is displayed, which lets you know the container is running.

    View the container home page to verify that it's running

  3. Select the Service API Description link to go to the container's Swagger page.

  4. Choose any of the POST APIs, and select Try it out. The parameters are displayed, which includes this example input:

    {
      "documents": [
        {
          "id": "1",
          "text": "Hello world"
        },
        {
          "id": "2",
          "text": "Bonjour tout le monde"
        },
        {
          "id": "3",
          "text": "La carretera estaba atascada. Había mucho tráfico el día de ayer."
        },
        {
          "id": "4",
          "text": ":) :( :D"
        }
      ]
    }
    
  5. Replace the input with the following JSON content:

    {
      "documents": [
        {
          "language": "en",
          "id": "7",
          "text": "I was fortunate to attend the KubeCon Conference in Barcelona, it is one of the best conferences I have ever attended. Great people, great sessions and I thoroughly enjoyed it!"
        }
      ]
    }
    
  6. Set showStats to true.

  7. Select Execute to determine the sentiment of the text.

    The model that's packaged in the container generates a score that ranges from 0 to 1, where 0 is negative and 1 is positive.

    The JSON response that's returned includes sentiment for the updated text input:

    {
      "documents": [
        {
          "id": "7",
          "keyPhrases": [
            "Great people",
            "great sessions",
            "KubeCon Conference",
            "Barcelona",
            "best conferences"
          ],
          "statistics": {
            "charactersCount": 176,
            "transactionsCount": 1
          }
        }
      ],
      "errors": [],
      "statistics": {
        "documentsCount": 1,
        "validDocumentsCount": 1,
        "erroneousDocumentsCount": 0,
        "transactionsCount": 1
      }
    }
    

We can now correlate the document id of the response payload's JSON data to the original request payload document id. The resulting document has a keyPhrases array, which contains the list of key phrases that have been extracted from the corresponding input document. Additionally, there are various statistics such as characterCount and transactionCount for each resulting document.

Next steps