Skip to content

Commit d04bcfe

Browse files
author
Aaron Hill
committed
fixing merge conflict
2 parents 9f8f714 + e7c152c commit d04bcfe

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+2494
-688
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,9 @@ ENV/
9999

100100
# mypy
101101
.mypy_cache/
102+
103+
# idea files
104+
.idea/
105+
106+
#DS store
107+
.DS_Store

README.md

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
---
2-
services: cognitive-services
3-
platforms: python
4-
author: lmazuel
2+
page_type: sample
3+
languages:
4+
- python
5+
products:
6+
- azure
7+
description: "These samples will show you how to get up and running using the Python SDKs for various Cognitive Services services."
8+
urlFragment: cognitive-services-python-sdk-samples
59
---
610

711
# Cognitive Services Python SDK Samples
@@ -12,16 +16,21 @@ These samples will show you how to get up and running using the Python SDKs for
1216

1317
This project framework provides examples for the following services:
1418

19+
### Knowledge
20+
* Using the **QnA SDK** [azure-cognitiveservices-knowledge-qnamaker](http://pypi.python.org/pypi/azure-cognitiveservices-knowledge-qnamaker) for the [QnA API](https://azure.microsoft.com/en-us/services/cognitive-services/qna-maker/)
21+
22+
1523
### Language
1624

1725
* Using the **LUIS SDK** [azure-cognitiveservices-language-luis](http://pypi.python.org/pypi/azure-cognitiveservices-language-luis) for the [LUIS API](https://azure.microsoft.com/services/cognitive-services/language-understanding-intelligent-service/)
1826
* Using the **Bing Spell Check SDK** [azure-cognitiveservices-language-spellcheck](http://pypi.python.org/pypi/azure-cognitiveservices-language-spellcheck) for the [Bing Spell Check API](https://azure.microsoft.com/services/cognitive-services/spell-check/)
19-
* Using the **Text Analytics SDK** [azure-cognitiveservices-language-textanalytics](http://pypi.python.org/pypi/azure-cognitiveservices-language-textanalytics) for the [Tet Analytics API](https://azure.microsoft.com/services/cognitive-services/text-analytics/)
27+
* Using the **Text Analytics SDK** [azure-cognitiveservices-language-textanalytics](http://pypi.python.org/pypi/azure-cognitiveservices-language-textanalytics) for the [Text Analytics API](https://azure.microsoft.com/services/cognitive-services/text-analytics/)
2028

2129
### Search
2230

23-
* Using the **Bing Autosuggest SDK** [azure-cognitiveservices-search-customsearch](http://pypi.python.org/pypi/azure-cognitiveservices-search-autosuggest) for the [Autosuggest API](https://azure.microsoft.com/services/cognitive-services/autosuggest/)
31+
* Using the **Bing Autosuggest SDK** [azure-cognitiveservices-search-autosuggest](http://pypi.python.org/pypi/azure-cognitiveservices-search-autosuggest) for the [Autosuggest API](https://azure.microsoft.com/services/cognitive-services/autosuggest/)
2432
* Using the **Bing Custom Search SDK** [azure-cognitiveservices-search-customsearch](http://pypi.python.org/pypi/azure-cognitiveservices-search-customsearch) for the [Custom Search API](https://azure.microsoft.com/services/cognitive-services/bing-custom-search/)
33+
* Using the **Bing Custom Image Search SDK** [azure-cognitiveservices-search-customimagesearch](http://pypi.python.org/pypi/azure-cognitiveservices-search-customimagesearch) for the [Custom Image Search API](https://azure.microsoft.com/services/cognitive-services/bing-custom-search/)
2534
* Using the **Bing Entity Search SDK** [azure-cognitiveservices-search-entitysearch](http://pypi.python.org/pypi/azure-cognitiveservices-search-entitysearch) for the [Entity Search API](https://azure.microsoft.com/services/cognitive-services/bing-entity-search-api/)
2635
* Using the **Bing Image Search SDK** [azure-cognitiveservices-search-imagesearch](http://pypi.python.org/pypi/azure-cognitiveservices-search-imagesearch) for the [Image Search API](https://azure.microsoft.com/services/cognitive-services/bing-image-search-api/)
2736
* Using the **Bing News Search SDK** [azure-cognitiveservices-search-newssearch](http://pypi.python.org/pypi/azure-cognitiveservices-search-newssearch) for the [News Search API](https://azure.microsoft.com/services/cognitive-services/bing-news-search-api/)
@@ -31,9 +40,11 @@ This project framework provides examples for the following services:
3140

3241
### Vision
3342

43+
* Using the **Face SDK** [azure-cognitiveservices-vision-face](http://pypi.python.org/pypi/azure-cognitiveservices-vision-face) for the [Face API](https://azure.microsoft.com/services/cognitive-services/face/)
3444
* Using the **Computer Vision SDK** [azure-cognitiveservices-vision-computervision](http://pypi.python.org/pypi/azure-cognitiveservices-vision-computervision) for the [Computer Vision API](https://azure.microsoft.com/services/cognitive-services/computer-vision/)
3545
* Using the **Content Moderator SDK** [azure-cognitiveservices-vision-contentmoderator](http://pypi.python.org/pypi/azure-cognitiveservices-vision-contentmoderator) for the [Content Moderator API](https://azure.microsoft.com/services/cognitive-services/content-moderator/)
3646
* Using the **Custom Vision SDK** [azure-cognitiveservices-vision-customvision](http://pypi.python.org/pypi/azure-cognitiveservices-vision-customvision) for the [Custom Vision API](https://azure.microsoft.com/services/cognitive-services/custom-vision-service/)
47+
* Using the **Ink Recognizer SDK** [azure-cognitiveservices-inkrecognizer](https://pypi.org/project/azure-cognitiveservices-inkrecognizer/) for the [Ink Recognizer API](https://azure.microsoft.com/services/cognitive-services/ink-recognizer/)
3748

3849
We provide several meta-packages to help you install several packages at a time. Please note that meta-packages are only recommended for development purpose. It's recommended in production to always pin specific version of individual packages.
3950

@@ -84,6 +95,7 @@ We provide several meta-packages to help you install several packages at a time.
8495
4. Set up the environment variable `TEXTANALYTICS_SUBSCRIPTION_KEY` with your key if you want to execute TextAnalytics tests. You might override too `TEXTANALYTICS_LOCATION` (westcentralus by default).
8596
3. Set up the environment variable `AUTOSUGGEST_SUBSCRIPTION_KEY` with your key if you want to execute Autosuggest tests.
8697
3. Set up the environment variable `CUSTOMSEARCH_SUBSCRIPTION_KEY` with your key if you want to execute CustomSearch tests.
98+
3. Set up the environment variable `CUSTOMIMAGESEARCH_SUBSCRIPTION_KEY` with your key if you want to execute CustomImageSearch tests.
8799
3. Set up the environment variable `ENTITYSEARCH_SUBSCRIPTION_KEY` with your key if you want to execute EntitySearch tests.
88100
4. Set up the environment variable `IMAGESEARCH_SUBSCRIPTION_KEY` with your key if you want to execute ImageSearch tests.
89101
4. Set up the environment variable `NEWSSEARCH_SUBSCRIPTION_KEY` with your key if you want to execute NewsSearch tests.
@@ -94,6 +106,7 @@ We provide several meta-packages to help you install several packages at a time.
94106
4. Set up the environment variable `CONTENTMODERATOR_SUBSCRIPTION_KEY` with your key if you want to execute Content Moderator tests. You might override too `CONTENTMODERATOR_LOCATION` (westcentralus by default).
95107
4. Set up the environment variable `CUSTOMVISION_TRAINING_KEY` with your key if you want to execute CustomVision Training tests.
96108
4. Set up the environment variable `CUSTOMVISION_PREDICTION_KEY` with your key if you want to execute CustomVision Prediction tests.
109+
4. Set up the environment variable `INK_RECOGNIZER_SUBSCRIPTION_KEY` with your key if you want to execute ink recognition tests.
97110
98111
## Demo
99112
@@ -103,9 +116,10 @@ To run the complete demo, execute `python example.py`
103116
104117
To run each individual demo, point directly to the file. For example (i.e. not complete list):
105118
106-
2. `python samples/language/spellcheck_samples.py`
107-
1. `python samples/search/entity_search_samples.py`
108-
2. `python samples/search/video_search_samples.py`
119+
1. `python samples/language/spellcheck_samples.py`
120+
2. `python samples/search/entity_search_samples.py`
121+
3. `python samples/search/video_search_samples.py`
122+
4. `python samples/vision/inkrecognizer_sample.py`
109123
110124
To see the code of each example, simply look at the examples in the Samples folder. They are written to be isolated in scope so that you can see only what you're interested in.
111125

example.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,27 @@
1212

1313
import samples.tools
1414

15+
1516
def run_all_samples():
1617
for _, section_name_name, ispkg in pkgutil.walk_packages(samples.__path__):
1718
if not ispkg:
1819
continue
1920
section_package_name = "samples."+section_name_name
2021
section_package = importlib.import_module(section_package_name)
2122
for _, sample_name, _ in pkgutil.iter_modules(section_package.__path__):
22-
sample_module = importlib.import_module(section_package_name+"."+sample_name)
23-
subkey_env_name = getattr(sample_module, "SUBSCRIPTION_KEY_ENV_NAME", None)
23+
sample_module = importlib.import_module(
24+
section_package_name+"."+sample_name)
25+
subkey_env_name = getattr(
26+
sample_module, "SUBSCRIPTION_KEY_ENV_NAME", None)
2427
if not subkey_env_name:
2528
continue
2629
print("Executing sample from ", sample_name)
2730
try:
28-
samples.tools.execute_samples(sample_module.__dict__, subkey_env_name)
31+
samples.tools.execute_samples(
32+
sample_module.__dict__, subkey_env_name)
2933
except samples.tools.SubscriptionKeyError as err:
3034
print("{}\n".format(err))
3135

36+
3237
if __name__ == "__main__":
3338
run_all_samples()

requirements.txt

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
1+
azure-cognitiveservices-knowledge-qnamaker
12
azure-cognitiveservices-language-luis
23
azure-cognitiveservices-language-spellcheck
3-
azure-cognitiveservices-language-textanalytics
4+
azure-cognitiveservices-language-textanalytics>=0.2.0 # sample won't work with previous versions
45
azure-cognitiveservices-search-autosuggest
5-
azure-cognitiveservices-search-customsearch
6+
azure-cognitiveservices-search-customimagesearch
7+
azure-cognitiveservices-search-customsearch>=0.2.0 # sample won't work with previous versions
68
azure-cognitiveservices-search-entitysearch
79
azure-cognitiveservices-search-imagesearch
810
azure-cognitiveservices-search-newssearch
911
azure-cognitiveservices-search-videosearch
10-
azure-cognitiveservices-search-visualsearch
12+
azure-cognitiveservices-search-visualsearch>=0.2.0 # sample won't work with previous versions
1113
azure-cognitiveservices-search-websearch
12-
azure-cognitiveservices-vision-computervision>=0.2.0 # sample won't work with previous versions
13-
azure-cognitiveservices-vision-contentmoderator
14-
azure-cognitiveservices-vision-customvision
14+
azure-cognitiveservices-vision-computervision>=0.3.0 # sample won't work with previous versions
15+
azure-cognitiveservices-vision-contentmoderator>=1.0.0 # sample won't work with previous versions
16+
azure-cognitiveservices-vision-customvision>=0.4.0 # sample won't work with previous versions
17+
azure-cognitiveservices-vision-face
18+
azure-cognitiveservices-anomalydetector>=0.2.0 # sample won't work with previous versions
19+
azure-cognitiveservices-inkrecognizer>=1.0.0b1
20+
pandas
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
from azure.cognitiveservices.anomalydetector import AnomalyDetectorClient
2+
from azure.cognitiveservices.anomalydetector.models import Request, Point, Granularity, \
3+
APIErrorException
4+
from datetime import datetime, timezone
5+
from msrest.authentication import CognitiveServicesCredentials
6+
import pandas as pd
7+
import os
8+
9+
# Add your Azure Anomaly Detector subscription key to your environment variables.
10+
SUBSCRIPTION_KEY = os.environ.get["ANOMALY_DETECTOR_SUBSCRIPTION_KEY"]
11+
12+
CSV_FOLDER = os.path.join(os.path.dirname(
13+
os.path.realpath(__file__)), "csv_files")
14+
15+
16+
def get_series_from_file(path):
17+
df = pd.read_csv(path, header=None, encoding="utf-8", parse_dates=[0])
18+
series = []
19+
for index, row in df.iterrows():
20+
series.append(Point(timestamp=row[0], value=row[1]))
21+
return series
22+
23+
24+
def get_request():
25+
series = get_series_from_file(os.path.join(
26+
CSV_FOLDER, "anomaly_detector_daily_series.csv"))
27+
return Request(series=series, granularity=Granularity.daily)
28+
29+
30+
def entire_detect(subscription_key):
31+
print("Sample of detecting anomalies in the entire series.")
32+
# Add your Azure Anomaly Detector subscription key to your environment variables.
33+
endpoint = os.environ.get["ANOMALY_DETECTOR_ENDPOINT"]
34+
35+
try:
36+
client = AnomalyDetectorClient(
37+
endpoint, CognitiveServicesCredentials(subscription_key))
38+
request = get_request()
39+
response = client.entire_detect(request)
40+
if True in response.is_anomaly:
41+
print("Anomaly was detected from the series at index:")
42+
for i in range(len(request.series)):
43+
if response.is_anomaly[i]:
44+
print(i)
45+
else:
46+
print("There is no anomaly detected from the series.")
47+
except Exception as e:
48+
if isinstance(e, APIErrorException):
49+
print("Error code: {}".format(e.error.code))
50+
print("Error message: {}".format(e.error.message))
51+
else:
52+
print(e)
53+
54+
55+
def last_detect(subscription_key):
56+
print("Sample of detecting whether the latest point in series is anomaly.")
57+
# Add your Azure Anomaly Detector subscription key to your environment variables.
58+
endpoint = os.environ.get["ANOMALY_DETECTOR_ENDPOINT"]
59+
60+
try:
61+
client = AnomalyDetectorClient(
62+
endpoint, CognitiveServicesCredentials(subscription_key))
63+
request = get_request()
64+
response = client.last_detect(request)
65+
if response.is_anomaly:
66+
print("The latest point is detected as anomaly.")
67+
else:
68+
print("The latest point is not detected as anomaly.")
69+
except Exception as e:
70+
if isinstance(e, APIErrorException):
71+
print("Error code: {}".format(e.error.code))
72+
print("Error message: {}".format(e.error.message))
73+
else:
74+
print(e)
75+
76+
77+
if __name__ == "__main__":
78+
import sys
79+
import os.path
80+
sys.path.append(os.path.abspath(os.path.join(__file__, "..", "..")))
81+
from tools import execute_samples
82+
execute_samples(globals(), SUBSCRIPTION_KEY)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2018-03-01T00:00:00Z,32858923
2+
2018-03-02T00:00:00Z,29615278
3+
2018-03-03T00:00:00Z,22839355
4+
2018-03-04T00:00:00Z,25948736
5+
2018-03-05T00:00:00Z,34139159
6+
2018-03-06T00:00:00Z,33843985
7+
2018-03-07T00:00:00Z,33637661
8+
2018-03-08T00:00:00Z,32627350
9+
2018-03-09T00:00:00Z,29881076
10+
2018-03-10T00:00:00Z,22681575
11+
2018-03-11T00:00:00Z,24629393
12+
2018-03-12T00:00:00Z,34010679
13+
2018-03-13T00:00:00Z,33893888
14+
2018-03-14T00:00:00Z,33760076
15+
2018-03-15T00:00:00Z,33093515
16+
2018-03-16T00:00:00Z,29945555
17+
2018-03-17T00:00:00Z,22676212
18+
2018-03-18T00:00:00Z,25262514
19+
2018-03-19T00:00:00Z,33631649
20+
2018-03-20T00:00:00Z,34468310
21+
2018-03-21T00:00:00Z,34212281
22+
2018-03-22T00:00:00Z,38144434
23+
2018-03-23T00:00:00Z,34662949
24+
2018-03-24T00:00:00Z,24623684
25+
2018-03-25T00:00:00Z,26530491
26+
2018-03-26T00:00:00Z,35445003
27+
2018-03-27T00:00:00Z,34250789
28+
2018-03-28T00:00:00Z,33423012
29+
2018-03-29T00:00:00Z,30744783
30+
2018-03-30T00:00:00Z,25825128
31+
2018-03-31T00:00:00Z,21244209
32+
2018-04-01T00:00:00Z,22576956
33+
2018-04-02T00:00:00Z,31957221
34+
2018-04-03T00:00:00Z,33841228
35+
2018-04-04T00:00:00Z,33554483
36+
2018-04-05T00:00:00Z,32383350
37+
2018-04-06T00:00:00Z,29494850
38+
2018-04-07T00:00:00Z,22815534
39+
2018-04-08T00:00:00Z,25557267
40+
2018-04-09T00:00:00Z,34858252
41+
2018-04-10T00:00:00Z,34750597
42+
2018-04-11T00:00:00Z,34717956
43+
2018-04-12T00:00:00Z,34132534
44+
2018-04-13T00:00:00Z,30762236
45+
2018-04-14T00:00:00Z,22504059
46+
2018-04-15T00:00:00Z,26149060
47+
2018-04-16T00:00:00Z,35250105

samples/knowledge/__init__.py

Whitespace-only changes.
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import os
2+
import time
3+
4+
from azure.cognitiveservices.knowledge.qnamaker import QnAMakerClient
5+
from azure.cognitiveservices.knowledge.qnamaker.models import QnADTO, MetadataDTO, CreateKbDTO, OperationStateType, UpdateKbOperationDTO, UpdateKbOperationDTOAdd
6+
from msrest.authentication import CognitiveServicesCredentials
7+
8+
# Add your QnaMaker subscription key and endpoint to your environment variables.
9+
SUBSCRIPTION_KEY = os.environ['QNA_MAKER_SUBSCRIPTION_KEY']
10+
QNA_ENDPOINT = os.environ['QNA_MAKER_ENDPOINT']
11+
12+
13+
def knowledge_based_crud_sample(subscription_key):
14+
"""KnowledgeBasedCRUDSample.
15+
16+
This will create, update, publish, download, then delete a knowledge base.
17+
"""
18+
def _create_sample_kb(client):
19+
"""Helper function for knowledge_based_crud_sample.
20+
21+
This helper function takes in a QnAMakerClient and returns an operation of a created knowledge base.
22+
"""
23+
qna = QnADTO(
24+
answer="You can use our REST APIs to manage your knowledge base.",
25+
questions=["How do I manage my knowledgebase?"],
26+
metadata=[MetadataDTO(name="Category", value="api")]
27+
)
28+
urls = [
29+
"https://docs.microsoft.com/en-in/azure/cognitive-services/qnamaker/faqs"]
30+
create_kb_dto = CreateKbDTO(
31+
name="QnA Maker FAQ from quickstart",
32+
qna_list=[qna],
33+
urls=urls
34+
)
35+
create_op = client.knowledgebase.create(
36+
create_kb_payload=create_kb_dto)
37+
create_op = _monitor_operation(client=client, operation=create_op)
38+
return create_op.resource_location.replace("/knowledgebases/", "")
39+
40+
def _monitor_operation(client, operation):
41+
"""Helper function for knowledge_based_crud_sample.
42+
43+
This helper function takes in a QnAMakerClient and an operation, and loops until the operation has either succeeded
44+
or failed and returns the operation.
45+
"""
46+
for i in range(20):
47+
if operation.operation_state in [OperationStateType.not_started, OperationStateType.running]:
48+
print("Waiting for operation: {} to complete.".format(
49+
operation.operation_id))
50+
time.sleep(5)
51+
operation = client.operations.get_details(
52+
operation_id=operation.operation_id)
53+
else:
54+
break
55+
if operation.operation_state != OperationStateType.succeeded:
56+
raise Exception("Operation {} failed to complete.".format(
57+
operation.operation_id))
58+
return operation
59+
60+
client = QnAMakerClient(endpoint=QNA_ENDPOINT, credentials=CognitiveServicesCredentials(subscription_key))
61+
62+
# Create a KB
63+
print("Creating KB...")
64+
kb_id = _create_sample_kb(client=client)
65+
print("Created KB with ID: {}".format(kb_id))
66+
67+
# Update the KB
68+
print("Updating KB...")
69+
update_kb_operation_dto = UpdateKbOperationDTO(
70+
add=UpdateKbOperationDTOAdd(
71+
qna_list=[
72+
QnADTO(questions=["bye"], answer="goodbye")
73+
]
74+
)
75+
)
76+
update_op = client.knowledgebase.update(
77+
kb_id=kb_id, update_kb=update_kb_operation_dto)
78+
_monitor_operation(client=client, operation=update_op)
79+
80+
# Publish the KB
81+
print("Publishing KB...")
82+
client.knowledgebase.publish(kb_id=kb_id)
83+
print("KB Published.")
84+
85+
# Download the KB
86+
print("Downloading KB...")
87+
kb_data = client.knowledgebase.download(kb_id=kb_id, environment="Prod")
88+
print("KB Downloaded. It has {} QnAs.".format(len(kb_data.qna_documents)))
89+
90+
# Delete the KB
91+
print("Deleting KB...")
92+
client.knowledgebase.delete(kb_id=kb_id)
93+
print("KB Deleted.")
94+
95+
96+
if __name__ == "__main__":
97+
import sys
98+
import os.path
99+
sys.path.append(os.path.abspath(os.path.join(__file__, "..", "..")))
100+
from tools import execute_samples
101+
execute_samples(globals(), SUBSCRIPTION_KEY)

0 commit comments

Comments
 (0)