예제 #1
0
        public async Task CreateComposedModelOperationCreatesDiagnosticScopeOnUpdate()
        {
            using var testListener = new ClientDiagnosticListener(DiagnosticNamespace);
            using var stream       = new MemoryStream(Encoding.UTF8.GetBytes(@"
                {
                    ""modelInfo"": {
                        ""status"": ""creating"",
                        ""modelId"": ""00000000-0000-0000-0000-000000000000""
                    }
                }"));

            var mockResponse = new MockResponse(200);

            mockResponse.ContentStream = stream;

            var mockTransport = new MockTransport(new[] { mockResponse, mockResponse });
            var options       = new FormRecognizerClientOptions()
            {
                Transport = mockTransport
            };
            var client = CreateFormTrainingClient(options);

            var operation = new CreateComposedModelOperation("00000000-0000-0000-0000-000000000000", client);

            if (IsAsync)
            {
                await operation.UpdateStatusAsync();
            }
            else
            {
                operation.UpdateStatus();
            }

            testListener.AssertScope($"{nameof(CreateCustomFormModelOperation)}.{nameof(CreateCustomFormModelOperation.UpdateStatus)}");
        }
예제 #2
0
        public async Task CreateComposedModelOperationCanPollFromNewObject()
        {
            var client = CreateFormTrainingClient(out var nonInstrumentedClient);

            await using var trainedModelA = await CreateDisposableTrainedModelAsync(useTrainingLabels : true);

            await using var trainedModelB = await CreateDisposableTrainedModelAsync(useTrainingLabels : true);

            var operation = await client.StartCreateComposedModelAsync(new List <string> {
                trainedModelA.ModelId, trainedModelB.ModelId
            });

            var sameOperation = new CreateComposedModelOperation(operation.Id, nonInstrumentedClient);
            await sameOperation.WaitForCompletionAsync(PollingInterval);

            Assert.IsTrue(sameOperation.HasValue);
            Assert.AreEqual(CustomFormModelStatus.Ready, sameOperation.Value.Status);
        }
예제 #3
0
        public async Task CopyComposedModel(bool useTokenCredential)
        {
            var sourceClient = CreateFormTrainingClient(useTokenCredential);
            var targetClient = CreateFormTrainingClient(useTokenCredential);
            var resourceId   = TestEnvironment.TargetResourceId;
            var region       = TestEnvironment.TargetResourceRegion;

            await using var trainedModelA = await CreateDisposableTrainedModelAsync(useTrainingLabels : true);

            await using var trainedModelB = await CreateDisposableTrainedModelAsync(useTrainingLabels : true);

            var modelIds = new List <string> {
                trainedModelA.ModelId, trainedModelB.ModelId
            };

            string modelName = "My composed model";
            CreateComposedModelOperation operation = await sourceClient.StartCreateComposedModelAsync(modelIds, modelName);

            await operation.WaitForCompletionAsync(PollingInterval);

            Assert.IsTrue(operation.HasValue);
            CustomFormModel composedModel = operation.Value;

            CopyAuthorization targetAuth = await targetClient.GetCopyAuthorizationAsync(resourceId, region);

            CopyModelOperation copyOperation = await sourceClient.StartCopyModelAsync(composedModel.ModelId, targetAuth);

            await copyOperation.WaitForCompletionAsync(PollingInterval);

            Assert.IsTrue(copyOperation.HasValue);
            CustomFormModelInfo modelCopied = copyOperation.Value;

            Assert.AreEqual(targetAuth.ModelId, modelCopied.ModelId);
            Assert.AreNotEqual(composedModel.ModelId, modelCopied.ModelId);

            CustomFormModel modelCopiedFullInfo = await sourceClient.GetCustomModelAsync(modelCopied.ModelId).ConfigureAwait(false);

            Assert.AreEqual(modelName, modelCopiedFullInfo.ModelName);
            foreach (var submodel in modelCopiedFullInfo.Submodels)
            {
                Assert.IsTrue(modelIds.Contains(submodel.ModelId));
            }
        }
예제 #4
0
        public async Task CreateComposedModelOperationCanPollFromNewObject()
        {
            // Skip instrumenting here because the internal service client passed to the operation object would be made null otherwise,
            // making the test fail.

            var client = CreateFormTrainingClient(skipInstrumenting: true);

            await using var trainedModelA = await CreateDisposableTrainedModelAsync(useTrainingLabels : true);

            await using var trainedModelB = await CreateDisposableTrainedModelAsync(useTrainingLabels : true);

            var operation = await client.StartCreateComposedModelAsync(new List <string> {
                trainedModelA.ModelId, trainedModelB.ModelId
            });

            var sameOperation = new CreateComposedModelOperation(operation.Id, client);
            await sameOperation.WaitForCompletionAsync(PollingInterval);

            Assert.IsTrue(sameOperation.HasValue);
            Assert.AreEqual(CustomFormModelStatus.Ready, sameOperation.Value.Status);
        }
예제 #5
0
        public async Task StartCreateComposedModel(bool useTokenCredential)
        {
            var client = CreateFormTrainingClient(useTokenCredential);

            await using var trainedModelA = await CreateDisposableTrainedModelAsync(useTrainingLabels : true);

            await using var trainedModelB = await CreateDisposableTrainedModelAsync(useTrainingLabels : true);

            var modelIds = new List <string> {
                trainedModelA.ModelId, trainedModelB.ModelId
            };

            CreateComposedModelOperation operation = await client.StartCreateComposedModelAsync(modelIds, "My composed model");

            await operation.WaitForCompletionAsync(PollingInterval);

            Assert.IsTrue(operation.HasValue);

            CustomFormModel composedModel = operation.Value;

            Assert.IsNotNull(composedModel.ModelId);
            Assert.IsNotNull(composedModel.Properties);
            Assert.IsTrue(composedModel.Properties.IsComposedModel);
            Assert.IsNotNull(composedModel.TrainingStartedOn);
            Assert.IsNotNull(composedModel.TrainingCompletedOn);
            Assert.AreEqual(CustomFormModelStatus.Ready, composedModel.Status);
            Assert.IsNotNull(composedModel.Errors);
            Assert.AreEqual(0, composedModel.Errors.Count);

            Dictionary <string, List <TrainingDocumentInfo> > trainingDocsPerModel = composedModel.TrainingDocuments.GroupBy(doc => doc.ModelId).ToDictionary(g => g.Key, g => g.ToList());

            Assert.AreEqual(2, trainingDocsPerModel.Count);
            Assert.IsTrue(trainingDocsPerModel.ContainsKey(trainedModelA.ModelId));
            Assert.IsTrue(trainingDocsPerModel.ContainsKey(trainedModelB.ModelId));

            //Check training documents in modelA
            foreach (TrainingDocumentInfo doc in trainingDocsPerModel[trainedModelA.ModelId])
            {
                Assert.IsNotNull(doc.Name);
                Assert.AreEqual(trainedModelA.ModelId, doc.ModelId);
                Assert.IsNotNull(doc.PageCount);
                Assert.AreEqual(TrainingStatus.Succeeded, doc.Status);
                Assert.IsNotNull(doc.Errors);
                Assert.AreEqual(0, doc.Errors.Count);
            }

            //Check training documents in modelB
            foreach (TrainingDocumentInfo doc in trainingDocsPerModel[trainedModelB.ModelId])
            {
                Assert.IsNotNull(doc.Name);
                Assert.AreEqual(trainedModelB.ModelId, doc.ModelId);
                Assert.IsNotNull(doc.PageCount);
                Assert.AreEqual(TrainingStatus.Succeeded, doc.Status);
                Assert.IsNotNull(doc.Errors);
                Assert.AreEqual(0, doc.Errors.Count);
            }

            Assert.AreEqual(2, composedModel.Submodels.Count);
            foreach (var submodel in composedModel.Submodels)
            {
                Assert.IsNotNull(submodel.FormType);
                Assert.IsNotNull(submodel.ModelId);
                Assert.IsTrue(modelIds.Contains(submodel.ModelId));
                foreach (var fields in submodel.Fields)
                {
                    Assert.IsNotNull(fields.Value.Name);
                    Assert.IsNotNull(fields.Value.Accuracy);
                }
            }
        }
        public async Task CreateComposedModel()
        {
            string endpoint        = TestEnvironment.Endpoint;
            string apiKey          = TestEnvironment.ApiKey;
            string trainingFileUrl = TestEnvironment.BlobContainerSasUrlV2;

            FormTrainingClient client = new FormTrainingClient(new Uri(endpoint), new AzureKeyCredential(apiKey));

            #region Snippet:FormRecognizerSampleTrainVariousModels
            // 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

            bool useLabels = true;

#if SNIPPET
            Uri officeSuppliesUri = new Uri("<purchaseOrderOfficeSuppliesUri>");
#else
            Uri officeSuppliesUri = new Uri(trainingFileUrl);
#endif
            string suppliesModelName = "Purchase order - Office supplies";

            TrainingOperation suppliesOperation = await client.StartTrainingAsync(officeSuppliesUri, useLabels, suppliesModelName);

            Response <CustomFormModel> suppliesOperationResponse = await suppliesOperation.WaitForCompletionAsync();

            CustomFormModel officeSuppliesModel = suppliesOperationResponse.Value;

#if SNIPPET
            Uri officeEquipmentUri = new Uri("<purchaseOrderOfficeEquipmentUri>");
#else
            Uri officeEquipmentUri = new Uri(trainingFileUrl);
#endif
            string equipmentModelName = "Purchase order - Office Equipment";

            TrainingOperation equipmentOperation = await client.StartTrainingAsync(officeEquipmentUri, useLabels, equipmentModelName);

            Response <CustomFormModel> equipmentOperationResponse = await equipmentOperation.WaitForCompletionAsync();

            CustomFormModel officeEquipmentModel = equipmentOperationResponse.Value;

#if SNIPPET
            Uri furnitureUri = new Uri("<purchaseOrderFurnitureUri>");
#else
            Uri furnitureUri = new Uri(trainingFileUrl);
#endif
            string furnitureModelName = "Purchase order - Furniture";

            TrainingOperation furnitureOperation = await client.StartTrainingAsync(furnitureUri, useLabels, furnitureModelName);

            Response <CustomFormModel> furnitureOperationResponse = await furnitureOperation.WaitForCompletionAsync();

            CustomFormModel furnitureModel = furnitureOperationResponse.Value;

#if SNIPPET
            Uri cleaningSuppliesUri = new Uri("<purchaseOrderCleaningSuppliesUri>");
#else
            Uri cleaningSuppliesUri = new Uri(trainingFileUrl);
#endif
            string cleaningModelName = "Purchase order - Cleaning Supplies";

            TrainingOperation cleaningOperation = await client.StartTrainingAsync(cleaningSuppliesUri, useLabels, cleaningModelName);

            Response <CustomFormModel> cleaningOperationResponse = await cleaningOperation.WaitForCompletionAsync();

            CustomFormModel cleaningSuppliesModel = cleaningOperationResponse.Value;

            #endregion

            #region Snippet:FormRecognizerSampleCreateComposedModelV3

            List <string> modelIds = new List <string>()
            {
                officeSuppliesModel.ModelId,
                officeEquipmentModel.ModelId,
                furnitureModel.ModelId,
                cleaningSuppliesModel.ModelId
            };

            string purchaseModelName = "Composed Purchase order";
            CreateComposedModelOperation operation = await client.StartCreateComposedModelAsync(modelIds, purchaseModelName);

            Response <CustomFormModel> operationResponse = await operation.WaitForCompletionAsync();

            CustomFormModel purchaseOrderModel = operationResponse.Value;

            Console.WriteLine($"Purchase Order Model Info:");
            Console.WriteLine($"  Is composed model: {purchaseOrderModel.Properties.IsComposedModel}");
            Console.WriteLine($"  Model Id: {purchaseOrderModel.ModelId}");
            Console.WriteLine($"  Model name: {purchaseOrderModel.ModelName}");
            Console.WriteLine($"  Model Status: {purchaseOrderModel.Status}");
            Console.WriteLine($"  Create model started on: {purchaseOrderModel.TrainingStartedOn}");
            Console.WriteLine($"  Create model completed on: {purchaseOrderModel.TrainingCompletedOn}");

            #endregion

            #region Snippet:FormRecognizerSampleSubmodelsInComposedModel

            Dictionary <string, List <TrainingDocumentInfo> > trainingDocsPerModel;
            trainingDocsPerModel = purchaseOrderModel.TrainingDocuments.GroupBy(doc => doc.ModelId).ToDictionary(g => g.Key, g => g.ToList());

            Console.WriteLine($"The purchase order model is based on {purchaseOrderModel.Submodels.Count} models");

            foreach (CustomFormSubmodel model in purchaseOrderModel.Submodels)
            {
                Console.WriteLine($"  Model Id: {model.ModelId}");
                Console.WriteLine("  The documents used to trained the model are: ");
                foreach (var doc in trainingDocsPerModel[model.ModelId])
                {
                    Console.WriteLine($"    {doc.Name}");
                }
            }

            #endregion

            #region Snippet:FormRecognizerSampleRecognizeCustomFormWithComposedModel

#if SNIPPET
            string purchaseOrderFilePath = "<purchaseOrderFilePath>";
#else
            string purchaseOrderFilePath = FormRecognizerTestEnvironment.CreatePath("Form_1.jpg");
#endif
            FormRecognizerClient recognizeClient = client.GetFormRecognizerClient();
            using var stream = new FileStream(purchaseOrderFilePath, FileMode.Open);

            RecognizeCustomFormsOperation recognizeOperation = await recognizeClient.StartRecognizeCustomFormsAsync(purchaseOrderModel.ModelId, stream);

            Response <RecognizedFormCollection> recognizeOperationResponse = await recognizeOperation.WaitForCompletionAsync();

            RecognizedFormCollection forms = recognizeOperationResponse.Value;

            // Find labeled field.
            foreach (RecognizedForm form in forms)
            {
                // Setting an arbitrary confidence level
                if (form.FormTypeConfidence.Value > 0.9)
                {
                    if (form.Fields.TryGetValue("Total", out FormField field))
                    {
                        Console.WriteLine($"Total value in the form `{form.FormType}` is `{field.ValueData.Text}`");
                    }
                }
                else
                {
                    Console.WriteLine("Unable to recognize form.");
                }
            }

            #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);
        }