[Build and Deploy Staging Site] Publish from microsoft/azuretipsandtricks-private:main/src/blog
This commit is contained in:
Родитель
f2a20186bf
Коммит
502e9b357b
|
@ -0,0 +1,105 @@
|
|||
---
|
||||
type: post
|
||||
title: "Tip 321 - How to deploy Azure Cognitive Services in containers"
|
||||
excerpt: "Learn how to deploy Azure Cognitive Services in containers"
|
||||
tags: [Containers, AI + Machine Learning]
|
||||
share: true
|
||||
date: 2021-6-23 12:00:00
|
||||
---
|
||||
|
||||
::: tip
|
||||
|
||||
:fire: Make sure you [star the repo](https://github.com/microsoft/azuretipsandtricks) to keep up to date with new tips and tricks.
|
||||
|
||||
:bulb: Learn more : [Azure Cognitive Services documentation](https://docs.microsoft.com/azure/cognitive-services/?WT.mc_id=docs-azuredevtips-azureappsdev).
|
||||
|
||||
:bulb: Checkout [Azure AI resources for developers](https://azure.microsoft.com/en-us/overview/ai-platform/dev-resources/?WT.mc_id=docs-azuredevtips-azureappsdev).
|
||||
|
||||
:bulb: Read a blog : [How to use Cognitive Services and containers](https://techcommunity.microsoft.com/t5/azure-ai/how-to-use-cognitive-services-and-containers/ba-p/2113684?WT.mc_id=docs-azuredevtips-azureappsdev).
|
||||
|
||||
:tv: Watch the video : [How to deploy Azure Cognitive Services in containers](https://youtu.be/XLQLNazid4I?WT.mc_id=youtube-azuredevtips-azureappsdev).
|
||||
|
||||
:::
|
||||
|
||||
### How to deploy Azure Cognitive Services in containers
|
||||
|
||||
#### Run Cognitive Services anywhere
|
||||
[Azure Cognitive Services](https://docs.microsoft.com/azure/cognitive-services/what-are-cognitive-services?WT.mc_id=docs-azuredevtips-azureappsdev) provide a broad range of pre-configured Artificial Intelligence capabilities in the categories of [Vision, Speech, Language, Decision and Search](https://docs.microsoft.com/azure/cognitive-services/what-are-cognitive-services?WT.mc_id=docs-azuredevtips-azureappsdev). 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](https://docs.microsoft.com/azure/cognitive-services/cognitive-services-container-support?WT.mc_id=docs-azuredevtips-azureappsdev), 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](https://azure.microsoft.com/free/?WT.mc_id=azure-azuredevtips-azureappsdev) before you begin)
|
||||
* [Docker Desktop](https://docs.docker.com/docker-for-windows/install/?WT.mc_id=other-azuredevtips-azureappsdev)
|
||||
|
||||
#### 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](https://docs.microsoft.com/azure/cognitive-services/text-analytics/how-tos/text-analytics-how-to-sentiment-analysis?WT.mc_id=docs-azuredevtips-azureappsdev). 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
|
||||
|
||||
<img :src="$withBase('/files/107create.png')">
|
||||
|
||||
(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.
|
||||
|
||||
<img :src="$withBase('/files/107keys.png')">
|
||||
|
||||
(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
|
||||
```
|
||||
|
||||
2. 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}
|
||||
```
|
||||
3. Now that the container is running, you can access the API through https://localhost:5000. Open a browser and navigate to the URL
|
||||
|
||||
<img :src="$withBase('/files/107runninglocal.png')">
|
||||
|
||||
(Cognitive Service running locally in a Docker container)
|
||||
|
||||
4. 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
|
||||
5. Select the **/text/analytics/v3.0/sentiment** endpoint
|
||||
6. Click the **Try it out!** button
|
||||
7. In the body parameter, paste in the following JSON data:
|
||||
|
||||
```
|
||||
{
|
||||
"documents": [
|
||||
{
|
||||
"language": "en",
|
||||
"id": "1",
|
||||
"text": "I love this"
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
8. Change the **Parameter content type** to **application/json**
|
||||
9. Click **Execute** to try it out
|
||||
10. 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
|
||||
|
||||
<img :src="$withBase('/files/107response.png')">
|
||||
|
||||
(Result of querying the Cognitive Service in a container)
|
||||
|
||||
#### Conclusion
|
||||
[Azure Cognitive Services in Docker containers](https://docs.microsoft.com/azure/cognitive-services/cognitive-services-container-support?WT.mc_id=docs-azuredevtips-azureappsdev) enable you to run your cognitive applications anywhere. Go and check it out!
|
Загрузка…
Ссылка в новой задаче