Skip to content

Commit b7dd15c

Browse files
[QA] update samples to show how to get the exported project (Azure#27898)
* update samples to show how to get the exported project * don't run samples for now * fix pyright
1 parent cab9486 commit b7dd15c

File tree

3 files changed

+50
-69
lines changed

3 files changed

+50
-69
lines changed

scripts/devops_tasks/test_run_samples.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,10 @@
135135
"sample_begin_translation_with_filters.py",
136136
"sample_begin_translation_with_filters_async.py"
137137
],
138-
"azure-ai-language-questionanswering": ["sample_chat.py"],
138+
"azure-ai-language-questionanswering": [
139+
"sample_export_import_project.py",
140+
"sample_export_import_project_async.py"
141+
],
139142
"azure-ai-textanalytics": [
140143
"sample_analyze_healthcare_entities_with_cancellation.py",
141144
"sample_analyze_healthcare_entities_with_cancellation_async.py",

sdk/cognitivelanguage/azure-ai-language-questionanswering/samples/authoring/async_samples/sample_export_import_project_async.py

Lines changed: 24 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -16,66 +16,56 @@
1616
Set the environment variables with your own values before running the sample:
1717
1) AZURE_QUESTIONANSWERING_ENDPOINT - the endpoint to your QuestionAnswering resource.
1818
2) AZURE_QUESTIONANSWERING_KEY - your QuestionAnswering API key.
19+
3) AZURE_QUESTIONANSWERING_PROJECT - your existing Question Answering project to export/import.
1920
"""
2021

2122
import asyncio
2223

24+
2325
async def sample_export_import_project_async():
2426
# [START export_import_project]
2527
import os
28+
import io
29+
import zipfile
30+
from azure.core.rest import HttpRequest
2631
from azure.core.credentials import AzureKeyCredential
2732
from azure.ai.language.questionanswering.authoring.aio import AuthoringClient
2833

2934
# get service secrets
3035
endpoint = os.environ["AZURE_QUESTIONANSWERING_ENDPOINT"]
3136
key = os.environ["AZURE_QUESTIONANSWERING_KEY"]
37+
project_name = os.environ["AZURE_QUESTIONANSWERING_PROJECT"]
3238

3339
# create client
3440
client = AuthoringClient(endpoint, AzureKeyCredential(key))
3541
async with client:
3642

37-
# create project
38-
project_name = "IssacNewton"
39-
await client.create_project(
40-
project_name=project_name,
41-
options={
42-
"description": "biography of Sir Issac Newton",
43-
"language": "en",
44-
"multilingualResource": True,
45-
"settings": {
46-
"defaultAnswer": "no answer"
47-
}
48-
})
49-
5043
# export
44+
export_format = "json"
5145
export_poller = await client.begin_export(
5246
project_name=project_name,
53-
file_format="json"
47+
file_format=export_format
5448
)
5549
export_result = await export_poller.result()
5650
export_url = export_result["resultUrl"]
51+
request = HttpRequest("GET", export_url)
52+
exported_project = None
53+
54+
if export_format == "json":
55+
response = await client.send_request(request)
56+
exported_project = response.json()
57+
elif export_format == "excel" or export_format == "tsv":
58+
response = await client.send_request(request, stream=True)
59+
exported_project = zipfile.ZipFile(io.BytesIO(await response.read()))
60+
exported_project.extractall("./ExportedProject")
61+
exported_project.close()
62+
print(f"{export_format} project files written to ./ExportedProject.")
63+
return
5764

5865
# import project
59-
project = {
60-
"Metadata": {
61-
"ProjectName": "IssacNewton",
62-
"Description": "biography of Sir Issac Newton",
63-
"Language": "en",
64-
"DefaultAnswer": None,
65-
"MultilingualResource": False,
66-
"CreatedDateTime": "2022-01-25T13:10:08Z",
67-
"LastModifiedDateTime": "2022-01-25T13:10:08Z",
68-
"LastDeployedDateTime": None,
69-
"Settings": {
70-
"DefaultAnswer": "no answer",
71-
"EnableHierarchicalExtraction": None,
72-
"DefaultAnswerUsedForExtraction": None
73-
}
74-
}
75-
}
7666
import_poller = await client.begin_import_assets(
77-
project_name=project_name,
78-
options=project
67+
project_name=f"{project_name}-imported",
68+
options=exported_project
7969
)
8070
await import_poller.result()
8171

@@ -91,5 +81,4 @@ async def sample_export_import_project_async():
9181
# [END export_import_project]
9282

9383
if __name__ == '__main__':
94-
loop = asyncio.get_event_loop()
95-
loop.run_until_complete(sample_export_import_project_async())
84+
asyncio.run(sample_export_import_project_async())

sdk/cognitivelanguage/azure-ai-language-questionanswering/samples/authoring/sample_export_import_project.py

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -16,65 +16,54 @@
1616
Set the environment variables with your own values before running the sample:
1717
1) AZURE_QUESTIONANSWERING_ENDPOINT - the endpoint to your QuestionAnswering resource.
1818
2) AZURE_QUESTIONANSWERING_KEY - your QuestionAnswering API key.
19+
3) AZURE_QUESTIONANSWERING_PROJECT - your existing Question Answering project to export/import.
1920
"""
2021

2122

2223
def sample_export_import_project():
2324
# [START export_import_project]
2425
import os
26+
import io
27+
import zipfile
28+
from azure.core.rest import HttpRequest
2529
from azure.core.credentials import AzureKeyCredential
2630
from azure.ai.language.questionanswering.authoring import AuthoringClient
2731

2832
# get service secrets
2933
endpoint = os.environ["AZURE_QUESTIONANSWERING_ENDPOINT"]
3034
key = os.environ["AZURE_QUESTIONANSWERING_KEY"]
35+
project_name = os.environ["AZURE_QUESTIONANSWERING_PROJECT"]
3136

3237
# create client
3338
client = AuthoringClient(endpoint, AzureKeyCredential(key))
3439
with client:
3540

36-
# create project
37-
project_name = "IssacNewton"
38-
client.create_project(
39-
project_name=project_name,
40-
options={
41-
"description": "biography of Sir Issac Newton",
42-
"language": "en",
43-
"multilingualResource": True,
44-
"settings": {
45-
"defaultAnswer": "no answer"
46-
}
47-
})
48-
4941
# export
42+
export_format = "json"
5043
export_poller = client.begin_export(
5144
project_name=project_name,
52-
file_format="json"
45+
file_format=export_format
5346
)
5447
export_result = export_poller.result()
5548
export_url = export_result["resultUrl"]
49+
request = HttpRequest("GET", export_url)
50+
exported_project = None
51+
52+
if export_format == "json":
53+
response = client.send_request(request)
54+
exported_project = response.json()
55+
elif export_format == "excel" or export_format == "tsv":
56+
response = client.send_request(request, stream=True)
57+
exported_project = zipfile.ZipFile(io.BytesIO(response.read()))
58+
exported_project.extractall("./ExportedProject")
59+
exported_project.close()
60+
print(f"{export_format} project files written to ./ExportedProject.")
61+
return
5662

5763
# import project
58-
project = {
59-
"Metadata": {
60-
"ProjectName": "IssacNewton",
61-
"Description": "biography of Sir Issac Newton",
62-
"Language": "en",
63-
"DefaultAnswer": None,
64-
"MultilingualResource": False,
65-
"CreatedDateTime": "2022-01-25T13:10:08Z",
66-
"LastModifiedDateTime": "2022-01-25T13:10:08Z",
67-
"LastDeployedDateTime": None,
68-
"Settings": {
69-
"DefaultAnswer": "no answer",
70-
"EnableHierarchicalExtraction": None,
71-
"DefaultAnswerUsedForExtraction": None
72-
}
73-
}
74-
}
7564
import_poller = client.begin_import_assets(
76-
project_name=project_name,
77-
options=project
65+
project_name=f"{project_name}-imported",
66+
options=exported_project,
7867
)
7968
import_poller.result()
8069

0 commit comments

Comments
 (0)