Skip to content
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 4 commits into from
Sep 29, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
360 changes: 0 additions & 360 deletions sdk/formrecognizer/Azure.AI.FormRecognizer/README.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ You can set `endpoint` and `apiKey` based on an environment variable, a configur
### Source client
The source client that contains the custom model we want to copy.

```C# Snippet:FormRecognizerSampleCreateCopySourceClient
```C# Snippet:FormRecognizerSampleCreateCopySourceClientV3
Copy link
Member

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 ?

Copy link
Member Author

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?

Copy link
Member

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 and v3.2-beta

Do you think this would be better?

Copy link
Member Author

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"

string endpoint = "<source_endpoint>";
string apiKey = "<source_apiKey>";
var sourcecredential = new AzureKeyCredential(apiKey);
Expand All @@ -30,7 +30,7 @@ var sourceClient = new FormTrainingClient(new Uri(endpoint), sourcecredential, n
### Target client
The target client where we want to copy the custom model to.

```C# Snippet:FormRecognizerSampleCreateCopyTargetClient
```C# Snippet:FormRecognizerSampleCreateCopyTargetClientV3
string endpoint = "<target_endpoint>";
string apiKey = "<target_apiKey>";
var targetCredential = new AzureKeyCredential(apiKey);
Expand All @@ -39,7 +39,7 @@ var targetClient = new FormTrainingClient(new Uri(endpoint), targetCredential, n

### Authorize the copy
Before starting the copy, we need to get a `CopyAuthorization` from the target Form Recognizer resource that will give us permission to execute the copy.
```C# Snippet:FormRecognizerSampleGetCopyAuthorization
```C# Snippet:FormRecognizerSampleGetCopyAuthorizationV3
string resourceId = "<resourceId>";
string resourceRegion = "<region>";
CopyAuthorization targetAuth = await targetClient.GetCopyAuthorizationAsync(resourceId, resourceRegion);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ CustomFormModel cleaningSuppliesModel = cleaningOperationResponse.Value;

When a purchase order happens, the employee in charge uploads the form to our application. The application then needs to recognize the form to extract the total value of the purchase order. Instead of asking the user to look for the specific `modelId` according to the nature of the form, you can create a composed model that aggregates the previous models, and use that model in `StartRecognizeCustomForms` and let the service decide which model fits best according to the form provided.

```C# Snippet:FormRecognizerSampleCreateComposedModel
```C# Snippet:FormRecognizerSampleCreateComposedModelV3
List<string> modelIds = new List<string>()
{
officeSuppliesModel.ModelId,
Expand Down
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
}
}
}
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);
}
}
}
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
}
}
}
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);
}
}
}
Loading