-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[FR] Samples for DMAC #24300
Merged
Merged
[FR] Samples for DMAC #24300
Changes from 3 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
sdk/formrecognizer/Azure.AI.FormRecognizer/tests/samples/SampleSnippets.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Threading.Tasks; | ||
using Azure.AI.FormRecognizer.DocumentAnalysis.Tests; | ||
using Azure.Core.TestFramework; | ||
using NUnit.Framework; | ||
|
||
namespace Azure.AI.FormRecognizer.DocumentAnalysis.Samples | ||
{ | ||
/// <summary> | ||
/// Samples that are used in the associated README.md file. | ||
/// </summary> | ||
public partial class Snippets : SamplesBase<DocumentAnalysisTestEnvironment> | ||
{ | ||
[Test] | ||
public void CreateDocumentAnalysisClient() | ||
maririos marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
#region Snippet:DocumentAnalysisClient | ||
#if SNIPPET | ||
string endpoint = "<endpoint>"; | ||
string apiKey = "<apiKey>"; | ||
#else | ||
string endpoint = TestEnvironment.Endpoint; | ||
string apiKey = TestEnvironment.ApiKey; | ||
#endif | ||
var credential = new AzureKeyCredential(apiKey); | ||
var client = new DocumentAnalysisClient(new Uri(endpoint), credential); | ||
#endregion | ||
} | ||
|
||
[Test] | ||
[Ignore("AAD not working yet")] | ||
public void CreateDocumentAnalysisClientTokenCredential() | ||
{ | ||
#region Snippet:CreateDocumentAnalysisClientTokenCredential | ||
#if SNIPPET | ||
string endpoint = "<endpoint>"; | ||
#else | ||
string endpoint = TestEnvironment.Endpoint; | ||
#endif | ||
var client = new DocumentAnalysisClient(new Uri(endpoint), new DefaultAzureCredential()); | ||
#endregion | ||
} | ||
|
||
[Test] | ||
public void CreateDocumentModelAdministrationClient() | ||
{ | ||
#region Snippet:CreateDocumentModelAdministrationClient | ||
#if SNIPPET | ||
string endpoint = "<endpoint>"; | ||
string apiKey = "<apiKey>"; | ||
#else | ||
string endpoint = TestEnvironment.Endpoint; | ||
string apiKey = TestEnvironment.ApiKey; | ||
#endif | ||
var credential = new AzureKeyCredential(apiKey); | ||
var client = new DocumentModelAdministrationClient(new Uri(endpoint), credential); | ||
#endregion | ||
} | ||
|
||
[Test] | ||
public async Task BadRequestSnippet() | ||
{ | ||
string endpoint = TestEnvironment.Endpoint; | ||
string apiKey = TestEnvironment.ApiKey; | ||
|
||
var credential = new AzureKeyCredential(apiKey); | ||
var client = new DocumentAnalysisClient(new Uri(endpoint), credential); | ||
|
||
#region Snippet:DocumentAnalysisBadRequest | ||
try | ||
{ | ||
AnalyzeDocumentOperation operation = await client.StartAnalyzeDocumentFromUriAsync("prebuilt-receipt", new Uri("http://invalid.uri")); | ||
await operation.WaitForCompletionAsync(); | ||
} | ||
catch (RequestFailedException e) | ||
{ | ||
Console.WriteLine(e.ToString()); | ||
} | ||
#endregion | ||
} | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
sdk/formrecognizer/Azure.AI.FormRecognizer/tests/samples/Sample_BuildModelAsync.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using Azure.AI.FormRecognizer.DocumentAnalysis.Tests; | ||
using Azure.Core.TestFramework; | ||
using NUnit.Framework; | ||
|
||
namespace Azure.AI.FormRecognizer.DocumentAnalysis.Samples | ||
{ | ||
public partial class DocumentAnalysisSamples : SamplesBase<DocumentAnalysisTestEnvironment> | ||
{ | ||
[Test] | ||
public async Task BuildModelAsync() | ||
{ | ||
string endpoint = TestEnvironment.Endpoint; | ||
string apiKey = TestEnvironment.ApiKey; | ||
|
||
#region Snippet:FormRecognizerSampleBuildModel | ||
// For this sample, you can use the training documents found in the `trainingFiles` folder. | ||
// Upload the forms to your storage container and then generate a container SAS URL. | ||
// For instructions to set up forms for training in an Azure Storage Blob Container, please see: | ||
// https://docs.microsoft.com/azure/cognitive-services/form-recognizer/build-training-data-set#upload-your-training-data | ||
|
||
#if SNIPPET | ||
Uri trainingFileUri = <trainingFileUri>; | ||
#else | ||
Uri trainingFileUri = new Uri(TestEnvironment.BlobContainerSasUrl); | ||
#endif | ||
var client = new DocumentModelAdministrationClient(new Uri(endpoint), new AzureKeyCredential(apiKey)); | ||
|
||
BuildModelOperation operation = await client.StartBuildModelAsync(trainingFileUri); | ||
Response<DocumentModel> operationResponse = await operation.WaitForCompletionAsync(); | ||
DocumentModel model = operationResponse.Value; | ||
|
||
Console.WriteLine($" Model Id: {model.ModelId}"); | ||
if (string.IsNullOrEmpty(model.Description)) | ||
Console.WriteLine($" Model description: {model.Description}"); | ||
Console.WriteLine($" Created on: {model.CreatedOn}"); | ||
Console.WriteLine(" Doc types the model can recognize:"); | ||
foreach (KeyValuePair<string, DocTypeInfo> docType in model.DocTypes) | ||
{ | ||
Console.WriteLine($" Doc type: {docType.Key} which has the following fields:"); | ||
foreach (KeyValuePair<string, DocumentFieldSchema> schema in docType.Value.FieldSchema) | ||
{ | ||
Console.WriteLine($" Field: {schema.Key} has type {schema.Value.Type}"); | ||
} | ||
} | ||
#endregion | ||
|
||
// Delete the model on completion to clean environment. | ||
await client.DeleteModelAsync(model.ModelId); | ||
} | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
sdk/formrecognizer/Azure.AI.FormRecognizer/tests/samples/Sample_CopyModelAsync.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Threading.Tasks; | ||
using Azure.AI.FormRecognizer.DocumentAnalysis.Tests; | ||
using Azure.Core.TestFramework; | ||
using NUnit.Framework; | ||
|
||
namespace Azure.AI.FormRecognizer.DocumentAnalysis.Samples | ||
{ | ||
public partial class DocumentAnalysisSamples : SamplesBase<DocumentAnalysisTestEnvironment> | ||
{ | ||
[Test] | ||
public async Task CopyModelAsync() | ||
{ | ||
string endpoint = TestEnvironment.Endpoint; | ||
string apiKey = TestEnvironment.ApiKey; | ||
|
||
#region Snippet:FormRecognizerSampleCreateCopySourceClient | ||
#if SNIPPET | ||
string endpoint = "<source_endpoint>"; | ||
string apiKey = "<source_apiKey>"; | ||
#endif | ||
var sourcecredential = new AzureKeyCredential(apiKey); | ||
var sourceClient = new DocumentModelAdministrationClient(new Uri(endpoint), new AzureKeyCredential(apiKey)); | ||
#endregion | ||
|
||
// For the purpose of this sample, we are going to create a model to copy. Please note that | ||
// if you already have a model, this is not necessary. | ||
#if SNIPPET | ||
Uri trainingFileUri = <trainingFileUri>; | ||
#else | ||
Uri trainingFileUri = new Uri(TestEnvironment.BlobContainerSasUrl); | ||
#endif | ||
BuildModelOperation operation = await sourceClient.StartBuildModelAsync(trainingFileUri); | ||
Response<DocumentModel> operationResponse = await operation.WaitForCompletionAsync(); | ||
DocumentModel model = operationResponse.Value; | ||
|
||
#region Snippet:FormRecognizerSampleCreateCopyTargetClient | ||
#if SNIPPET | ||
string endpoint = "<target_endpoint>"; | ||
string apiKey = "<target_apiKey>"; | ||
#endif | ||
var targetCredential = new AzureKeyCredential(apiKey); | ||
var targetClient = new DocumentModelAdministrationClient(new Uri(endpoint), new AzureKeyCredential(apiKey)); | ||
#endregion | ||
|
||
#region Snippet:FormRecognizerSampleGetCopyAuthorization | ||
CopyAuthorization targetAuth = await targetClient.GetCopyAuthorizationAsync(); | ||
#endregion | ||
|
||
#region Snippet:FormRecognizerSampleCreateCopyModel | ||
#if SNIPPET | ||
string modelId = "<source_modelId>"; | ||
#else | ||
string modelId = model.ModelId; | ||
#endif | ||
CopyModelOperation newModelOperation = await sourceClient.StartCopyModelAsync(modelId, targetAuth); | ||
await newModelOperation.WaitForCompletionAsync(); | ||
DocumentModel newModel = newModelOperation.Value; | ||
|
||
Console.WriteLine($"Original model ID => {modelId}"); | ||
Console.WriteLine($"Copied model ID => {newModel.ModelId}"); | ||
#endregion | ||
} | ||
} | ||
} |
105 changes: 105 additions & 0 deletions
105
sdk/formrecognizer/Azure.AI.FormRecognizer/tests/samples/Sample_CreateComposedModelAsync.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using Azure.AI.FormRecognizer.DocumentAnalysis.Tests; | ||
using Azure.Core.TestFramework; | ||
using NUnit.Framework; | ||
|
||
namespace Azure.AI.FormRecognizer.DocumentAnalysis.Samples | ||
{ | ||
public partial class DocumentAnalysisSamples : SamplesBase<DocumentAnalysisTestEnvironment> | ||
{ | ||
[Test] | ||
public async Task CreateComposedModelAsync() | ||
{ | ||
string endpoint = TestEnvironment.Endpoint; | ||
string apiKey = TestEnvironment.ApiKey; | ||
string trainingFileUrl = TestEnvironment.BlobContainerSasUrl; | ||
maririos marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
var client = new DocumentModelAdministrationClient(new Uri(endpoint), new AzureKeyCredential(apiKey)); | ||
|
||
#region Snippet:FormRecognizerSampleBuildVariousModels | ||
// For this sample, you can use the training forms found in the `trainingFiles` folder. | ||
// Upload the forms to your storage container and then generate a container SAS URL. | ||
// For instructions on setting up forms for training in an Azure Storage Blob Container, see | ||
// https://docs.microsoft.com/azure/cognitive-services/form-recognizer/build-training-data-set#upload-your-training-data | ||
|
||
#if SNIPPET | ||
Uri officeSuppliesUri = <purchaseOrderOfficeSuppliesUri>; | ||
#else | ||
Uri officeSuppliesUri = new Uri(trainingFileUrl); | ||
#endif | ||
var officeSupplieOptions = new BuildModelOptions() { ModelDescription = "Purchase order - Office supplies" }; | ||
|
||
BuildModelOperation suppliesOperation = await client.StartBuildModelAsync(officeSuppliesUri, buildModelOptions: officeSupplieOptions); | ||
Response<DocumentModel> suppliesOperationResponse = await suppliesOperation.WaitForCompletionAsync(); | ||
DocumentModel officeSuppliesModel = suppliesOperationResponse.Value; | ||
|
||
#if SNIPPET | ||
Uri officeEquipmentUri = <purchaseOrderOfficeEquipmentUri>; | ||
#else | ||
Uri officeEquipmentUri = new Uri(trainingFileUrl); | ||
#endif | ||
var equipmentOptions = new BuildModelOptions() { ModelDescription = "Purchase order - Office Equipment" }; | ||
|
||
BuildModelOperation equipmentOperation = await client.StartBuildModelAsync(officeSuppliesUri, buildModelOptions: equipmentOptions); | ||
Response<DocumentModel> equipmentOperationResponse = await equipmentOperation.WaitForCompletionAsync(); | ||
DocumentModel officeEquipmentModel = equipmentOperationResponse.Value; | ||
|
||
#if SNIPPET | ||
Uri furnitureUri = <purchaseOrderFurnitureUri>; | ||
#else | ||
Uri furnitureUri = new Uri(trainingFileUrl); | ||
#endif | ||
var furnitureOptions = new BuildModelOptions() { ModelDescription = "Purchase order - Furniture" }; | ||
|
||
BuildModelOperation furnitureOperation = await client.StartBuildModelAsync(officeSuppliesUri, buildModelOptions: equipmentOptions); | ||
Response<DocumentModel> furnitureOperationResponse = await furnitureOperation.WaitForCompletionAsync(); | ||
DocumentModel furnitureModel = furnitureOperationResponse.Value; | ||
|
||
#if SNIPPET | ||
Uri cleaningSuppliesUri = <purchaseOrderCleaningSuppliesUri>; | ||
#else | ||
Uri cleaningSuppliesUri = new Uri(trainingFileUrl); | ||
#endif | ||
var cleaningOptions = new BuildModelOptions() { ModelDescription = "Purchase order - Cleaning Supplies" }; | ||
|
||
BuildModelOperation cleaningOperation = await client.StartBuildModelAsync(officeSuppliesUri, buildModelOptions: equipmentOptions); | ||
Response<DocumentModel> cleaningOperationResponse = await cleaningOperation.WaitForCompletionAsync(); | ||
DocumentModel cleaningSuppliesModel = cleaningOperationResponse.Value; | ||
|
||
#endregion | ||
|
||
#region Snippet:FormRecognizerSampleCreateComposedModel | ||
|
||
List<string> modelIds = new List<string>() | ||
{ | ||
officeSuppliesModel.ModelId, | ||
officeEquipmentModel.ModelId, | ||
furnitureModel.ModelId, | ||
cleaningSuppliesModel.ModelId | ||
}; | ||
|
||
BuildModelOperation operation = await client.StartCreateComposedModelAsync(modelIds, modelDescription: "Composed Purchase order"); | ||
Response<DocumentModel> operationResponse = await operation.WaitForCompletionAsync(); | ||
DocumentModel purchaseOrderModel = operationResponse.Value; | ||
|
||
Console.WriteLine($" Model Id: {purchaseOrderModel.ModelId}"); | ||
if (string.IsNullOrEmpty(purchaseOrderModel.Description)) | ||
Console.WriteLine($" Model description: {purchaseOrderModel.Description}"); | ||
Console.WriteLine($" Created on: {purchaseOrderModel.CreatedOn}"); | ||
|
||
#endregion | ||
|
||
// Delete the models on completion to clean environment. | ||
await client.DeleteModelAsync(officeSuppliesModel.ModelId).ConfigureAwait(false); | ||
await client.DeleteModelAsync(officeEquipmentModel.ModelId).ConfigureAwait(false); | ||
await client.DeleteModelAsync(furnitureModel.ModelId).ConfigureAwait(false); | ||
await client.DeleteModelAsync(cleaningSuppliesModel.ModelId).ConfigureAwait(false); | ||
await client.DeleteModelAsync(purchaseOrderModel.ModelId).ConfigureAwait(false); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure if V3 would be appropriate, should we use the date version for the folder name?
And would that be enough without having to append the V3 in
FormRecognizerSampleCreateCopySourceClientV3
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in this case, V3 is reflecting the SDK version. It is following the structure we talked about of having samples in V3 folder and current samples in either a V4 folder or in the main samples folder.
Although snippet strings are internal to us, they are the way for us to know where the code for the sample lives. We have also modified the samples code and moved all "old" samples to a V3 folder
https://github.com/Azure/azure-sdk-for-net/tree/feature/formrecognizerv4/sdk/formrecognizer/Azure.AI.FormRecognizer/tests/samples
Do you think this is clear enough for future developers to know which code/snippet/sample, lives where?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm... I feel v3 or v4 is awkward since we havent used these terms before. Could we use similar wording as what python did
v3.1
andv3.2-beta
Do you think this would be better?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like it! I will change it to
v3.1
everywhere to make it more clear. I will do it in the next PR to keep this one "small-ish"