将文本分析容器部署到 Azure 容器实例Deploy a Text Analytics container to Azure Container Instances

了解如何将认知服务文本分析容器部署到 Azure 容器实例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

  • 使用 Azure 订阅。Use an Azure subscription. 如果没有 Azure 订阅,可在开始前创建一个试用帐户If you don't have an Azure subscription, create a Trial before you begin.

创建认知服务文本分析资源Create a Cognitive Services Text Analytics resource

  1. 登录 Azure 门户Sign in to the Azure portal.

  2. 选择“创建资源” ,然后转到 “AI + 机器学习” > “文本分析” 。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 输入名称(2-64 个字符)。Enter a name (2-64 characters).
    订阅Subscription 选择适当的订阅。Select the appropriate subscription.
    位置Location 选择附近的位置。Select a nearby location.
    定价层Pricing tier 输入 S (即标准定价层)。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. 收集配置的 endpoint 和 API 密钥:Collect the configured endpoint and an API key:

    门户中的“资源”选项卡Resource tab in portal 设置Setting Value
    概述Overview 端点Endpoint 复制终结点。Copy the endpoint. 其外观类似于 https://chinaeast2.api.cognitive.azure.cn/text/analytics/v2.0It appears similar to https://chinaeast2.api.cognitive.azure.cn/text/analytics/v2.0
    “键”Keys API 密钥API Key 复制两个密钥中的一个。Copy one of the two keys. 它是一个 32 个字符的字母数字字符串(不包含空格或短划线):<xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx>。It's a 32-character alphanumeric string with no spaces or dashes: <xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx>.

创建 Azure 容器实例资源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 ValueValue
    订阅Subscription 选择订阅。Select your subscription.
    资源组Resource group 选择可用的资源组,或者创建一个新的,例如 cognitive-servicesSelect the available resource group or create a new one such as cognitive-services.
    容器名称Container name 输入一个名称,例如 cognitive-container-instanceEnter a name such as cognitive-container-instance. 此名称必须小写。The name must be in lower caps.
    LocationLocation 选择要部署的区域。Select a region for deployment.
    映像类型Image type Public
    映像名称Image name 输入认知服务容器位置。Enter the Cognitive Services container location.
    OS 类型OS type Linux
    大小Size 对于特定的认知服务容器,请将大小更改为建议的设置:Change size to the suggested recommendations for your specific Cognitive Service container:
    2 个CPU 核心2 CPU cores
    4 GB4 GB
  3. 在“网络”选项卡上,输入以下详细信息: On the Networking tab, enter the following details:

    设置Setting ValueValue
    端口Ports 将 TCP 端口设置为 5000Set the TCP port to 5000. 在端口 5000 上公开此容器。Exposes the container on port 5000.
  4. 在“高级”选项卡上, 输入所需的环境变量作为 ACI 资源的容器计费设置:On the Advanced tab, enter the required Environment Variables for the container billing settings of the ACI resource:

    Key ValueValue
    apikey 从资源的“键”页复制。 Copied from the Keys page of the resource. 它是一个由 32 个字母数字组成的字符串(不包含空格或短划线),即 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxIt 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. 选择“概述”选项卡,并复制 IP 地址。Select the Overview tab, and copy the IP address.

  2. 打开新的浏览器选项卡,并输入 IP 地址。Open a new browser tab, and enter the IP address. 例如,输入 http://<IP-address>:5000 (http://55.55.55.55:5000)。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.

    查看容器主页,以验证它是否处于运行状态

  3. 选择“服务 API 说明”链接,以转到该容器的 Swagger 页。Select the Service API Description link to go to the container's Swagger page.

  4. 选择任意一个 POST API,然后选择“试用”。此时将显示参数,其中包括以下示例输入: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. 将该输入替换为以下 JSON 内容: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. 将“showStats”设置为 trueSet showStats to true.

  7. 选择“执行”以确定文本的情绪。Select Execute to determine the sentiment of the text.

    在容器中打包的模型会生成范围为从 0 到 1 的分数,其中,0 表示消极,而 1 表示积极。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.

    返回的 JSON 响应包括更新的文本输入的情绪: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
      }
    }
    

我们现在可以将响应有效负载的 JSON 数据的文档 id 关联到原始请求有效负载文档 idWe can now correlate the document id of the response payload's JSON data to the original request payload document id. 生成的文档有一个 keyPhrases 数组,其中包含已从相应输入文档中提取的关键短语的列表。The resulting document has a keyPhrases array, which contains the list of key phrases that have been extracted from the corresponding input document. 此外,每个生成的文档都有各种统计信息(例如 characterCounttransactionCount)。Additionally, there are various statistics such as characterCount and transactionCount for each resulting document.

后续步骤Next steps