|
| 1 | +# coding: utf-8 |
| 2 | + |
| 3 | +# ------------------------------------------------------------------------- |
| 4 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 5 | +# Licensed under the MIT License. See License.txt in the project root for |
| 6 | +# license information. |
| 7 | +# -------------------------------------------------------------------------- |
| 8 | + |
| 9 | +""" |
| 10 | +FILE: sample_model_version_async.py |
| 11 | +
|
| 12 | +DESCRIPTION: |
| 13 | + This sample demonstrates how to set the model_version for pre-built Text Analytics models. |
| 14 | + Recognize entities is used in this sample, but the concept applies generally to all pre-built Text Analytics models. |
| 15 | +
|
| 16 | + By default, model_version is set to "latest". This indicates that the latest generally available version |
| 17 | + of the model will be used. Model versions are date based, e.g "2021-06-01". |
| 18 | + See the documentation for a list of all model versions: |
| 19 | + https://docs.microsoft.com/azure/cognitive-services/language-service/named-entity-recognition/how-to-call#specify-the-ner-model |
| 20 | +
|
| 21 | +USAGE: |
| 22 | + python sample_model_version_async.py |
| 23 | +
|
| 24 | + Set the environment variables with your own values before running the sample: |
| 25 | + 1) AZURE_TEXT_ANALYTICS_ENDPOINT - the endpoint to your Cognitive Services resource. |
| 26 | + 2) AZURE_TEXT_ANALYTICS_KEY - your Text Analytics subscription key |
| 27 | +""" |
| 28 | + |
| 29 | +import os |
| 30 | +import asyncio |
| 31 | + |
| 32 | + |
| 33 | +async def sample_model_version_async(): |
| 34 | + print("--------------Choosing model_version sample--------------") |
| 35 | + from azure.core.credentials import AzureKeyCredential |
| 36 | + from azure.ai.textanalytics.aio import TextAnalyticsClient |
| 37 | + from azure.ai.textanalytics import RecognizeEntitiesAction |
| 38 | + |
| 39 | + endpoint = os.environ["AZURE_TEXT_ANALYTICS_ENDPOINT"] |
| 40 | + key = os.environ["AZURE_TEXT_ANALYTICS_KEY"] |
| 41 | + |
| 42 | + text_analytics_client = TextAnalyticsClient(endpoint=endpoint, credential=AzureKeyCredential(key)) |
| 43 | + documents = [ |
| 44 | + "I work for Foo Company, and we hired Contoso for our annual founding ceremony. The food \ |
| 45 | + was amazing and we all can't say enough good words about the quality and the level of service." |
| 46 | + ] |
| 47 | + |
| 48 | + async with text_analytics_client: |
| 49 | + print("\nSetting model_version='latest' with recognize_entities") |
| 50 | + result = await text_analytics_client.recognize_entities(documents, model_version="latest") |
| 51 | + result = [review for review in result if not review.is_error] |
| 52 | + |
| 53 | + print("...Results of Recognize Entities:") |
| 54 | + for review in result: |
| 55 | + for entity in review.entities: |
| 56 | + print("......Entity '{}' has category '{}'".format(entity.text, entity.category)) |
| 57 | + |
| 58 | + print("\nSetting model_version='latest' with recognize entities action in begin_analyze_actions") |
| 59 | + poller = await text_analytics_client.begin_analyze_actions( |
| 60 | + documents, |
| 61 | + actions=[ |
| 62 | + RecognizeEntitiesAction(model_version="latest") |
| 63 | + ] |
| 64 | + ) |
| 65 | + |
| 66 | + print("...Results of Recognize Entities Action:") |
| 67 | + document_results = await poller.result() |
| 68 | + async for action_results in document_results: |
| 69 | + recognize_entities_result = action_results[0] |
| 70 | + if recognize_entities_result.is_error: |
| 71 | + print("......Is an error with code '{}' and message '{}'".format( |
| 72 | + recognize_entities_result.code, recognize_entities_result.message |
| 73 | + )) |
| 74 | + else: |
| 75 | + for entity in recognize_entities_result.entities: |
| 76 | + print("......Entity '{}' has category '{}'".format(entity.text, entity.category)) |
| 77 | + |
| 78 | + |
| 79 | +async def main(): |
| 80 | + await sample_model_version_async() |
| 81 | + |
| 82 | + |
| 83 | +if __name__ == '__main__': |
| 84 | + asyncio.run(main()) |
0 commit comments