Azure Bicep: Deploy a Cognitive Services container image for Text Analytics.



Original Source Here

This article will review how to use Azure Bicep to deploy a Cognitive Services resource and an Azure Container Instances resource to create a container image that can be used for text analytics.

Before you move forward, take a moment to read the below article that explains in detail the architecture and objectives.

In this article, we will automate the deployment process of the following reference architecture:

Reference architecture

Reference architecture

Prerequisites.

Let’s get started!

1. Solution Overview

We will author and deploy a Bicep template that creates two resources:

  • A Cognitive Services resource: We will create an Azure Cognitive Services resource instance
  • An Azure Container Instance resource: We will create an Azure Container Instance and configure Environment Variables to consume the Cognitive Services resource.

The solution will include the following files:

  • 📄 main.bicep: This is the Bicep template that will contain the definition of all the resources that are to be created
  • 📄 azuredeploy.parameters.json: This parameter file contains the values to use for deploying your Bicep template.

Let’s analyze the Bicep template.

2. Azure Bicep Template — parameters

Create a new file in your working directory and name it ‘main.bicep’. We will define the following parameters:

@description('That name is the name of our application. It has to be unique.Type a name followed by your resource group name. (<name>-<resourceGroupName>)')
param cognitiveServiceName string = 'AzInsider-CognitiveService-${uniqueString(resourceGroup().id)}'
@description('Location for all resources.')
param location string = resourceGroup().location
@allowed([
'S0'
])
param sku string = 'S0'
@description('Name for the container group')
param name string = 'azinsider-containergroup'
@description('Container image to deploy. Should be of the form repoName/imagename:tag for images stored in public Docker Hub, or a fully qualified URI for other registries. Images from private registries require additional registry credentials.')
param image string = 'mcr.microsoft.com/azure-cognitive-services/textanalytics/language:1.1.013570001-amd64'
@description('Port to open on the container and the public IP address.')
param port int = 5000
@description('The number of CPU cores to allocate to the container.')
param cpuCores int = 4
@description('The amount of memory to allocate to the container in gigabytes.')
param memoryInGb int = 8
@description('The behavior of Azure runtime if container has stopped.')
@allowed([
'Always'
'Never'
'OnFailure'
])
param restartPolicy string = 'OnFailure'

3. Azure Bicep Template — resources

We will define the following resources:

//Create a cognitive services resource
resource cognitiveService 'Microsoft.CognitiveServices/accounts@2021-10-01' = {
name: cognitiveServiceName
location: location
sku: {
name: sku
}
kind: 'CognitiveServices'
properties: {
apiProperties: {
statisticsEnabled: false
}
}
}
output cognitiveServiceEndpoint string = cognitiveService.properties.endpoint
output cognitiveServiceKey string = cognitiveService.listKeys().key1
//Create azure container instance
resource containerGroup 'Microsoft.ContainerInstance/containerGroups@2021-09-01' = {
name: name
location: location
properties: {
containers: [
{
name: name
properties: {
image: image
ports: [
{
port: port
protocol: 'TCP'
}
]
environmentVariables: [
{
name: 'ApiKey'
//secureValue: 'Yes'
value: cognitiveService.listKeys().key1 //Either key for your cognitive services resource
}
{
name: 'Billing'
//secureValue: 'No'
value: cognitiveService.properties.endpoint //The endpoint URI for your cognitive services resource
}
{
name: 'Eula'
//secureValue: 'No'
value: 'accept' //As is
}
]
resources: {
requests: {
cpu: cpuCores
memoryInGB: memoryInGb
}
}
}
}
]
osType: 'Linux'
restartPolicy: restartPolicy
ipAddress: {
type: 'Public'
ports: [
{
port: port
protocol: 'TCP'
}
]
}
}
}
output containerIPv4Address string = containerGroup.properties.ipAddress.ip

Note we declare two resources: the Azure Cognitive Service resource and the Azure Container Instance resource. We include outputs to highlight the values.

As a best practice, use secure values instead.

4. Parameters file

You can create a new file named ‘azuredeploy.parameters.json’ and pass your own values for the name of the resources. The code below shows an example of the definition of the parameters file for a single parameter:

{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"location": {
"value": "eastus"
}
}
}

5. Azure Bicep Template — Deployment

We will use the command below to deploy our Bicep template:

$date = Get-Date -Format "MM-dd-yyyy"
$deploymentName = "AzInsiderDeployment"+"$date"
New-AzResourceGroupDeployment -Name $deploymentName -ResourceGroupName azinsider_demo -TemplateFile .\main.bicep -TemplateParameterFile .\azuredeploy.parameters.json -c

The image below shows the preview of the deployment:

Then we will execute the deployment. The image below shows the deployment output:

6. Use the container.

Now we will run the following script to perform a test.

In the following script, edit the curl command it contains (shown below), replacing <your_ACI_IP_address_or_FQDN> with the IP address or FQDN for your container.

Save your changes to the script.

Note that you do not need to specify the cognitive services endpoint or key — the request is processed by the containerized service. The container in turn communicates periodically with the service in Azure to report usage for billing but does not send request data.

Note the detected language in the above image.

You can find the code of this solution in the following URL, feel free to contribute!

👉 Join the AzInsider email list here.

-Dave R.

AI/ML

Trending AI/ML Article Identified & Digested via Granola by Ramsey Elbasheer; a Machine-Driven RSS Bot

%d bloggers like this: