[Build and Deploy Staging Site] Publish from microsoft/azuretipsandtricks-private:main/src/blog

This commit is contained in:
erjuntun75 2021-10-13 15:08:06 +00:00
Родитель c372976e84
Коммит a666235fb3
1 изменённых файлов: 94 добавлений и 0 удалений

94
blog/tip337.md Normal file
Просмотреть файл

@ -0,0 +1,94 @@
---
type: post
title: "Tip 337 - How to run Cognitive Service Text Analytics for Health in a Web App for Containers"
excerpt: "Learn how to run Cognitive Service Text Analytics for Health in a Web App for Containers"
tags: [AI + Machine Learning, Web, Containers]
share: true
date: 2021-10-13 08:00:00
---
::: tip
:fire: Download the FREE Azure Developer Guide eBook [here](http://aka.ms/azuredevebook?WT.mc_id=docs-azuredevtips-azureappsdev).
:bulb: Learn more : [Cognitive Services in containers](https://docs.microsoft.com/azure/cognitive-services/cognitive-services-container-support?WT.mc_id=docs-azuredevtips-azureappsdev).
:tv: Watch the video : [How to run Cognitive Service Text Analytics for Health in a Web App for Containers](https://youtu.be/7GkOJUQojtI?WT.mc_id=youtube-azuredevtips-azureappsdev).
:::
### How to run Cognitive Service Text Analytics for Health in a Web App for Containers
#### Cognitive Services in containers
[Azure Cognitive Services](https://azure.microsoft.com/services/cognitive-services?WT.mc_id=azure-azuredevtips-azureappsdev) provide pre-trained AI capabilities. There are many types of Cognitive Services, including services for Speech, Decision, Language, and Vision. You can simply create a Cognitive Service resource, and use it by calling its API from your application.
You can also deploy [Cognitive Services to Docker containers](https://docs.microsoft.com/azure/cognitive-services/cognitive-services-container-support?WT.mc_id=docs-azuredevtips-azureappsdev). This enables you to increase performance by running Cognitive Services close to your data, control where data gets processed, and use Cognitive Services in a portable solution.
In this post, we'll deploy and run a [Cognitive Service Text Analytics for Health](https://docs.microsoft.com/azure/cognitive-services/text-analytics/how-tos/text-analytics-for-health?WT.mc_id=azure-azuredevtips-azureappsdev) service in a container that runs in an [Azure Web app for Containers](https://azure.microsoft.com/services/app-service/containers?WT.mc_id=azure-azuredevtips-azureappsdev).
#### 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](https://azure.microsoft.com/free/?WT.mc_id=azure-azuredevtips-azureappsdev) before you begin)
#### Use a Cognitive Services container
We are going to deploy an Azure Cognitive Service into a container that runs in an Azure Web App for Containers. The Cognitive Service in the container needs to communicate with a Cognitive Service resource in Azure for billing information. To enable that, we will start by creating a Text Analytics Cognitive Service in the Azure portal.
1. Go to the [Azure portal](https://portal.azure.com/?WT.mc_id=azure-azuredevtips-azureappsdev)
2. Click the **Create a resource** button (the plus-sign in the top left corner)
3. Search for **text analytics**, select the "Text Analytics" result and click **Create**
1. Select a **Resource Group**
2. Type in a **Name**
3. Select a **Pricing tier**. The **free tier** is fine for this exercise
4. Accept the terms of the **Responsible AI notice**
5. Click **Review + create** and then **Create**
<img :src="$withBase('/files/121createservice.png')">
(Create an Azure Text Analytics resource in the Azure portal)
When the Text Analytics resource is created, navigate to it in the Azure portal. Select the **Keys and Endpoint**. In there, copy the **Key** and the **Endpoint**. We'll need to feed this information into the container that we are going to create, so that it can send billing information to the Text Analytics resource.
<img :src="$withBase('/files/121keyendpoint.png')">
(Key and Endpoint of the Azure Text Analytics)
Next, we create a container that contains a Text Analytics resource, and deploy that into a Web App for Containers that we'll create. We do that using a PowerShell script that we execute in the [Azure Cloud Shell](https://portal.azure.com/#cloudshell?WT.mc_id=azure-azuredevtips-azureappsdev). You can also execute this script in a local installation of the [Azure CLI](https://docs.microsoft.com/cli/azure/install-azure-cli?WT.mc_id=docs-azuredevtips-azureappsdev). The container image is one [of several special Cognitive Services images](https://docs.microsoft.com/azure/cognitive-services/containers/container-image-tags?WT.mc_id=docs-azuredevtips-azureappsdev) that you can use. We are using the image for the [Text Analytics for health feature](https://docs.microsoft.com/azure/cognitive-services/text-analytics/how-tos/text-analytics-for-health?WT.mc_id=azure-azuredevtips-azureappsdev). This can detect illnesses, treatments, medications, and more, in any given text.
1. Go to the Azure Cloud Shell ([https://portal.azure.com/#cloudshell](https://portal.azure.com/#cloudshell?WT.mc_id=azure-azuredevtips-azureappsdev))
2. Make sure that PowerShell is selected as the language of the cloud shell
3. Paste the PowerShell code that you find below into the Cloud Shell. Make sure to fill in the subscription name, resource group name, resource location, App Service plan and service name, and the API key and endpoint
```
$subscription_name = "" # THe name of the subscription you want you resource to be created on.
$resource_group_name = "" # The name of the resource group you want the AppServicePlan
# and AppSerivce to be attached to.
$resources_location = "" # This is the location you wish the AppServicePlan to be deployed to.
# You can use the "az account list-locations -o table" command to
# get the list of available locations and location code names.
$appservice_plan_name = "" # This is the AppServicePlan name you wish to have.
$appservice_name = "" # This is the AppService resource name you wish to have.
$TEXT_ANALYTICS_RESOURCE_API_KEY = "" # This should be taken from the Text Analytics resource.
$TEXT_ANALYTICS_RESOURCE_API_ENDPOINT = "" # This should be taken from the Text Analytics resource.
$DOCKER_IMAGE_NAME = "mcr.microsoft.com/azure-cognitive-services/textanalytics/healthcare:latest"
az appservice plan create -n $appservice_plan_name -g $resource_group_name --is-linux -l $resources_location --sku P3V2
az webapp create -g $resource_group_name -p $appservice_plan_name -n $appservice_name -i $DOCKER_IMAGE_NAME
az webapp config appsettings set -g $resource_group_name -n $appservice_name --settings Eula=accept rai_terms=accept Billing=$TEXT_ANALYTICS_RESOURCE_API_ENDPOINT ApiKey=$TEXT_ANALYTICS_RESOURCE_API_KEY
```
<img :src="$withBase('/files/212createwebapp.png')">
(PowerShell script to create a Web App and deploy a Cognitive Services container to it)
4. After 20 minutes or so, the container will be ready. **Navigate to the Web App for Containers** in the Azure portal
5. In the **Overview blade** of the Web App for Containers, you'll find the **URL for the Web App**. Click on it to navigate to it
6. Append the URL with /demo. The URL should look something like https://yourappservicename.azurewebsites.net/demo
7. This opens the Text Analytics for Health demo UI. Type in some text, like "can the flu be treated with vitamin C?", and click **Analyze**
8. The text will be sent to the Cognitive Service that runs in the container. It will analyze the text and return the result. The Health feature of the Text Analytics service identifies terms that indicate illness, treatment, medications, and more
<img :src="$withBase('/files/121result.png')" width="75%">
(The demo UI of the Text Analytics Health feature that runs in a container)
#### Conclusion
Running [Azure Cognitive Services](https://azure.microsoft.com/services/cognitive-services?WT.mc_id=azure-azuredevtips-azureappsdev) in [containers](https://docs.microsoft.com/azure/cognitive-services/cognitive-services-container-support?WT.mc_id=docs-azuredevtips-azureappsdev) enables you to take control of your data, improve speed, and create portable solutions. It's easy to get started with [any of the Cognitive Services container images](https://docs.microsoft.com/azure/cognitive-services/containers/container-image-tags?WT.mc_id=docs-azuredevtips-azureappsdev). Go and check it out!