Skip to content

Commit f1a55b3

Browse files
add sample showing how to set model_version (Azure#22132)
1 parent 3e3fbe8 commit f1a55b3

File tree

3 files changed

+164
-0
lines changed

3 files changed

+164
-0
lines changed

sdk/textanalytics/azure-ai-textanalytics/samples/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ These sample programs show common scenarios for the Text Analytics client's offe
3434
|[sample_recognize_custom_entities.py][recognize_custom_entities_sample] and [sample_recognize_custom_entities_async.py][recognize_custom_entities_sample_async]|Use a custom model to recognize custom entities in documents|
3535
|[sample_single_category_classify.py][single_category_classify_sample] and [sample_single_category_classify_async.py][single_category_classify_sample_async]|Use a custom model to classify documents into a single category|
3636
|[sample_multi_category_classify.py][multi_category_classify_sample] and [sample_multi_category_classify_async.py][multi_category_classify_sample_async]|Use a custom model to classify documents into multiple categories|
37+
|[sample_model_version.py][sample_model_version] and [sample_model_version_async.py][sample_model_version_async]|Set the model version for pre-built Text Analytics models|
3738

3839
## Prerequisites
3940
* Python 2.7, or 3.6 or later is required to use this package (3.6 or later if using asyncio)
@@ -110,6 +111,8 @@ what you can do with the Azure Text Analytics client library.
110111
[single_category_classify_sample_async]: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_single_category_classify_async.py
111112
[multi_category_classify_sample]: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/textanalytics/azure-ai-textanalytics/samples/sample_multi_category_classify.py
112113
[multi_category_classify_sample_async]: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_multi_category_classify_async.py
114+
[sample_model_version]: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/textanalytics/azure-ai-textanalytics/samples/sample_model_version.py
115+
[sample_model_version_async]: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_model_version_async.py
113116
[pip]: https://pypi.org/project/pip/
114117
[azure_subscription]: https://azure.microsoft.com/free/
115118
[azure_text_analytics_account]: https://docs.microsoft.com/azure/cognitive-services/cognitive-services-apis-create-account?tabs=singleservice%2Cwindows
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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())
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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.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.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+
31+
32+
def sample_model_version():
33+
print("--------------Choosing model_version sample--------------")
34+
from azure.core.credentials import AzureKeyCredential
35+
from azure.ai.textanalytics import TextAnalyticsClient, RecognizeEntitiesAction
36+
37+
endpoint = os.environ["AZURE_TEXT_ANALYTICS_ENDPOINT"]
38+
key = os.environ["AZURE_TEXT_ANALYTICS_KEY"]
39+
40+
text_analytics_client = TextAnalyticsClient(endpoint=endpoint, credential=AzureKeyCredential(key))
41+
documents = [
42+
"I work for Foo Company, and we hired Contoso for our annual founding ceremony. The food \
43+
was amazing and we all can't say enough good words about the quality and the level of service."
44+
]
45+
46+
print("\nSetting model_version='latest' with recognize_entities")
47+
result = text_analytics_client.recognize_entities(documents, model_version="latest")
48+
result = [review for review in result if not review.is_error]
49+
50+
print("...Results of Recognize Entities:")
51+
for review in result:
52+
for entity in review.entities:
53+
print("......Entity '{}' has category '{}'".format(entity.text, entity.category))
54+
55+
print("\nSetting model_version='latest' with recognize entities action in begin_analyze_actions")
56+
poller = text_analytics_client.begin_analyze_actions(
57+
documents,
58+
actions=[
59+
RecognizeEntitiesAction(model_version="latest")
60+
]
61+
)
62+
63+
print("...Results of Recognize Entities Action:")
64+
document_results = poller.result()
65+
for action_results in document_results:
66+
recognize_entities_result = action_results[0]
67+
if recognize_entities_result.is_error:
68+
print("......Is an error with code '{}' and message '{}'".format(
69+
recognize_entities_result.code, recognize_entities_result.message
70+
))
71+
else:
72+
for entity in recognize_entities_result.entities:
73+
print("......Entity '{}' has category '{}'".format(entity.text, entity.category))
74+
75+
76+
if __name__ == '__main__':
77+
sample_model_version()

0 commit comments

Comments
 (0)