示例#1
0
        public async Task CopyModel(bool useTokenCredential)
        {
            var sourceClient = CreateDocumentModelAdministrationClient(useTokenCredential);
            var targetClient = CreateDocumentModelAdministrationClient(useTokenCredential);
            var modelId      = Recording.GenerateId();

            await using var trainedModel = await CreateDisposableBuildModelAsync(modelId);

            var targetModelId            = Recording.GenerateId();
            CopyAuthorization targetAuth = await targetClient.GetCopyAuthorizationAsync(targetModelId);

            CopyModelOperation operation = await sourceClient.StartCopyModelToAsync(trainedModel.ModelId, targetAuth);

            await operation.WaitForCompletionAsync();

            Assert.IsTrue(operation.HasValue);

            DocumentModel copiedModel = operation.Value;

            ValidateDocumentModel(copiedModel);
            Assert.AreEqual(targetAuth.TargetModelId, copiedModel.ModelId);
            Assert.AreNotEqual(trainedModel.ModelId, copiedModel.ModelId);

            Assert.AreEqual(1, copiedModel.DocTypes.Count);
            Assert.IsTrue(copiedModel.DocTypes.ContainsKey(modelId));

            DocTypeInfo docType = copiedModel.DocTypes[modelId];

            Assert.AreEqual(DocumentBuildMode.Template, docType.BuildMode);
        }
示例#2
0
        public async Task StartCopyModelFailsWithWrongRegion()
        {
            var sourceClient = CreateFormTrainingClient();
            var targetClient = CreateFormTrainingClient();
            var resourceId   = TestEnvironment.TargetResourceId;
            var regionA      = "regionA";
            var regionB      = "regionB";

            switch (TestEnvironment.AuthorityHostUrl)
            {
            case "https://login.microsoftonline.com/":
                regionA = "westcentralus";
                regionB = "eastus2";
                break;

            case "https://login.microsoftonline.us/":
                regionA = "usgovarizona";
                regionB = "usgovvirginia";
                break;

            default:
                regionA = "westcentralus";
                regionB = "eastus2";
                break;
            }
            var wrongRegion = TestEnvironment.TargetResourceRegion == regionA ? regionB : regionA;

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

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

            var operation = await sourceClient.StartCopyModelAsync(trainedModel.ModelId, targetAuth);

            Assert.ThrowsAsync <RequestFailedException>(async() => await operation.WaitForCompletionAsync());
        }
        public async Task CopyModelAsync()
        {
            #region Snippet:FormRecognizerSampleCreateCopySourceClient
#if SNIPPET
            string sourceEndpoint = "<source_endpoint>";
            string sourceApiKey   = "<source_apiKey>";
#else
            string sourceEndpoint = TestEnvironment.Endpoint;
            string sourceApiKey   = TestEnvironment.ApiKey;
#endif
            var sourcecredential = new AzureKeyCredential(sourceApiKey);
            var sourceClient     = new DocumentModelAdministrationClient(new Uri(sourceEndpoint), new AzureKeyCredential(sourceApiKey));
            #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 = new Uri("<trainingFileUri>");
#else
            Uri trainingFileUri = new Uri(TestEnvironment.BlobContainerSasUrl);
#endif
            BuildModelOperation operation = await sourceClient.StartBuildModelAsync(trainingFileUri, DocumentBuildMode.Template);

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

            DocumentModel model = operationResponse.Value;

            #region Snippet:FormRecognizerSampleCreateCopyTargetClient
#if SNIPPET
            string targetEndpoint = "<target_endpoint>";
            string targetApiKey   = "<target_apiKey>";
#else
            string targetEndpoint = TestEnvironment.Endpoint;
            string targetApiKey   = TestEnvironment.ApiKey;
#endif
            var targetCredential = new AzureKeyCredential(targetApiKey);
            var targetClient     = new DocumentModelAdministrationClient(new Uri(targetEndpoint), new AzureKeyCredential(targetApiKey));
            #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.StartCopyModelToAsync(modelId, targetAuth);

            await newModelOperation.WaitForCompletionAsync();

            DocumentModel newModel = newModelOperation.Value;

            Console.WriteLine($"Original model ID => {modelId}");
            Console.WriteLine($"Copied model ID => {newModel.ModelId}");
            #endregion
        }
示例#4
0
        public async Task CopyModelWithoutLabelsAndModelName()
        {
            var sourceClient = CreateFormTrainingClient();
            var targetClient = CreateFormTrainingClient();
            var resourceId   = TestEnvironment.TargetResourceId;
            var region       = TestEnvironment.TargetResourceRegion;

            string modelName = "My model to copy";

            await using var trainedModel = await CreateDisposableTrainedModelAsync(useTrainingLabels : false, modelName : modelName);

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

            CopyModelOperation operation = await sourceClient.StartCopyModelAsync(trainedModel.ModelId, targetAuth);

            await operation.WaitForCompletionAsync(PollingInterval);

            Assert.IsTrue(operation.HasValue);

            CustomFormModelInfo modelCopied = operation.Value;

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

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

            Assert.AreEqual(modelName, modelCopiedFullInfo.ModelName);
        }
        public async Task CopyModel()
        {
            var sourceClient = CreateInstrumentedFormTrainingClient();
            var targetClient = CreateInstrumentedFormTrainingClient();
            var resourceID   = TestEnvironment.TargetResourceId;
            var region       = TestEnvironment.TargetResourceRegion;

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

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

            CopyModelOperation operation;

            // TODO: sanitize body and enable body recording here.
            using (Recording.DisableRequestBodyRecording())
            {
                operation = await sourceClient.StartCopyModelAsync(trainedModel.ModelId, targetAuth);
            }

            await operation.WaitForCompletionAsync();

            Assert.IsTrue(operation.HasValue);

            CustomFormModelInfo modelCopied = operation.Value;

            Assert.IsNotNull(modelCopied.CompletedOn);
            Assert.IsNotNull(modelCopied.RequestedOn);
            Assert.AreEqual(targetAuth.ModelId, modelCopied.ModelId);
            Assert.AreNotEqual(trainedModel.ModelId, modelCopied.ModelId);
        }
        public async Task CopyModel()
        {
            string endpoint        = TestEnvironment.Endpoint;
            string apiKey          = TestEnvironment.ApiKey;
            Uri    trainingFileUri = new Uri(TestEnvironment.BlobContainerSasUrl);
            string resourceId      = TestEnvironment.TargetResourceId;
            string resourceRegion  = TestEnvironment.TargetResourceRegion;

            #region Snippet:FormRecognizerSampleCreateCopySourceClient
            //@@ string endpoint = "<source_endpoint>";
            //@@ string apiKey = "<source_apiKey>";
            var sourcecredential = new AzureKeyCredential(apiKey);
            var sourceClient     = new FormTrainingClient(new Uri(endpoint), sourcecredential);
            #endregion

            // For the purpose of this sample, we are going to create a trained model to copy. Please note that
            // if you already have a model, this is not necessary.
            //@@ Uri trainingFileUri = <trainingFileUri>;
            TrainingOperation operation = await sourceClient.StartTrainingAsync(trainingFileUri, useTrainingLabels : false);

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

            CustomFormModel model = operationResponse.Value;

            string modelId = model.ModelId;

            #region Snippet:FormRecognizerSampleCreateCopyTargetClient
            //@@ string endpoint = "<target_endpoint>";
            //@@ string apiKey = "<target_apiKey>";
            var targetCredential = new AzureKeyCredential(apiKey);
            var targetClient     = new FormTrainingClient(new Uri(endpoint), targetCredential);
            #endregion

            #region Snippet:FormRecognizerSampleGetCopyAuthorization
            //@@ string resourceId = "<resourceId>";
            //@@ string resourceRegion = "<region>";
            CopyAuthorization targetAuth = await targetClient.GetCopyAuthorizationAsync(resourceId, resourceRegion);

            #endregion

            #region Snippet:FormRecognizerSampleToJson
            string jsonTargetAuth = targetAuth.ToJson();
            #endregion

            #region Snippet:FormRecognizerSampleFromJson
            CopyAuthorization targetCopyAuth = CopyAuthorization.FromJson(jsonTargetAuth);
            #endregion

            #region Snippet:FormRecognizerSampleCopyModel
            //@@ string modelId = "<source_modelId>";
            CustomFormModelInfo newModel = await sourceClient.StartCopyModelAsync(modelId, targetCopyAuth).WaitForCompletionAsync();

            Console.WriteLine($"Original model ID => {modelId}");
            Console.WriteLine($"Copied model ID => {newModel.ModelId}");
            #endregion
        }
        public void CopyModelError()
        {
            var sourceClient = CreateFormTrainingClient();
            var targetClient = CreateFormTrainingClient();
            var resourceId   = TestEnvironment.TargetResourceId;
            var region       = TestEnvironment.TargetResourceRegion;

            CopyAuthorization targetAuth = CopyAuthorization.FromJson("{\"modelId\":\"328c3b7d - a563 - 4ba2 - 8c2f - 2f26d664486a\",\"accessToken\":\"5b5685e4 - 2f24 - 4423 - ab18 - 000000000000\",\"expirationDateTimeTicks\":1591932653,\"resourceId\":\"resourceId\",\"resourceRegion\":\"westcentralus\"}");

            Assert.ThrowsAsync <RequestFailedException>(async() => await sourceClient.StartCopyModelAsync("00000000-0000-0000-0000-000000000000", targetAuth));
        }
        public async Task CopyModelError()
        {
            var sourceClient = CreateInstrumentedFormTrainingClient();
            var targetClient = CreateInstrumentedFormTrainingClient();
            var resourceID   = TestEnvironment.TargetResourceId;
            var region       = TestEnvironment.TargetResourceRegion;

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

            Assert.ThrowsAsync <RequestFailedException>(async() => await sourceClient.StartCopyModelAsync("00000000-0000-0000-0000-000000000000", targetAuth));
        }
示例#9
0
        public async Task CopyModelErrorAsync()
        {
            var sourceClient = CreateDocumentModelAdministrationClient();
            var targetClient = CreateDocumentModelAdministrationClient();

            var modelId = Recording.GenerateId();
            CopyAuthorization targetAuth = await targetClient.GetCopyAuthorizationAsync(modelId);

            RequestFailedException ex = Assert.ThrowsAsync <RequestFailedException>(async() => await sourceClient.StartCopyModelToAsync(modelId, targetAuth));

            Assert.AreEqual("InvalidRequest", ex.ErrorCode);
        }
示例#10
0
        public async Task GetCopyAuthorization()
        {
            var targetClient = CreateDocumentModelAdministrationClient();
            var modelId      = Recording.GenerateId();

            CopyAuthorization targetAuth = await targetClient.GetCopyAuthorizationAsync(modelId);

            Assert.IsNotNull(targetAuth.TargetModelId);
            Assert.AreEqual(modelId, targetAuth.TargetModelId);
            Assert.IsNotNull(targetAuth.AccessToken);
            Assert.IsNotNull(targetAuth.TargetResourceId);
            Assert.IsNotNull(targetAuth.TargetResourceRegion);
        }
        public async Task GetCopyAuthorization()
        {
            var targetClient = CreateFormTrainingClient();
            var resourceId   = TestEnvironment.TargetResourceId;
            var region       = TestEnvironment.TargetResourceRegion;

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

            Assert.IsNotNull(targetAuth.ModelId);
            Assert.IsNotNull(targetAuth.AccessToken);
            Assert.IsNotNull(targetAuth.ExpiresOn);
            Assert.AreEqual(resourceId, targetAuth.ResourceId);
            Assert.AreEqual(region, targetAuth.Region);
        }
示例#12
0
        public async Task StartCopyModelFailsWithWrongRegion()
        {
            var sourceClient = CreateFormTrainingClient();
            var targetClient = CreateFormTrainingClient();
            var resourceId   = TestEnvironment.TargetResourceId;
            var wrongRegion  = TestEnvironment.TargetResourceRegion == "westcentralus" ? "eastus2" : "westcentralus";

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

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

            var operation = await sourceClient.StartCopyModelAsync(trainedModel.ModelId, targetAuth);

            Assert.ThrowsAsync <RequestFailedException>(async() => await operation.WaitForCompletionAsync(PollingInterval));
        }
示例#13
0
        public async Task CopyModelOperationCanPollFromNewObject()
        {
            var client     = CreateFormTrainingClient(out var nonInstrumentedClient);
            var resourceId = TestEnvironment.TargetResourceId;
            var region     = TestEnvironment.TargetResourceRegion;

            await using var trainedModel = await CreateDisposableTrainedModelAsync(useTrainingLabels : false);

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

            var operation = await client.StartCopyModelAsync(trainedModel.ModelId, targetAuth);

            var sameOperation = InstrumentOperation(new CopyModelOperation(operation.Id, targetAuth.ModelId, nonInstrumentedClient));
            await sameOperation.WaitForCompletionAsync();

            Assert.IsTrue(sameOperation.HasValue);
            Assert.AreEqual(targetAuth.ModelId, sameOperation.Value.ModelId);
        }
        public async Task SerializeDeserializeCopyAuthorizationAsync()
        {
            var targetClient = CreateFormTrainingClient();
            var resourceId   = TestEnvironment.TargetResourceId;
            var region       = TestEnvironment.TargetResourceRegion;

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

            string jsonTargetAuth = targetAuth.ToJson();

            CopyAuthorization targetAuthFromJson = CopyAuthorization.FromJson(jsonTargetAuth);

            Assert.AreEqual(targetAuth.ModelId, targetAuthFromJson.ModelId);
            Assert.AreEqual(targetAuth.ExpiresOn, targetAuthFromJson.ExpiresOn);
            Assert.AreEqual(targetAuth.AccessToken, targetAuthFromJson.AccessToken);
            Assert.AreEqual(targetAuth.ResourceId, targetAuthFromJson.ResourceId);
            Assert.AreEqual(targetAuth.Region, targetAuthFromJson.Region);
        }
示例#15
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));
            }
        }
示例#16
0
        public async Task CopyModelOperationPercentageCompletedValue()
        {
            var client  = CreateDocumentModelAdministrationClient(out var nonInstrumentedClient);
            var modelId = Recording.GenerateId();

            await using var trainedModel = await CreateDisposableBuildModelAsync(modelId);

            var targetModelId            = Recording.GenerateId();
            CopyAuthorization targetAuth = await client.GetCopyAuthorizationAsync(targetModelId);

            var operation = await client.StartCopyModelAsync(trainedModel.ModelId, targetAuth);

            Assert.AreEqual(0, operation.PercentCompleted);

            await operation.WaitForCompletionAsync();

            Assert.IsTrue(operation.HasValue);
            Assert.AreEqual(100, operation.PercentCompleted);
        }
示例#17
0
        public async Task CopyModelOperationCanPollFromNewObject()
        {
            var client  = CreateDocumentModelAdministrationClient(out var nonInstrumentedClient);
            var modelId = Recording.GenerateId();

            await using var trainedModel = await CreateDisposableBuildModelAsync(modelId);

            var targetModelId            = Recording.GenerateId();
            CopyAuthorization targetAuth = await client.GetCopyAuthorizationAsync(targetModelId);

            var operation = await client.StartCopyModelAsync(trainedModel.ModelId, targetAuth);

            Assert.IsNotNull(operation.GetRawResponse());

            var sameOperation = InstrumentOperation(new CopyModelOperation(operation.Id, nonInstrumentedClient));
            await sameOperation.WaitForCompletionAsync();

            Assert.IsTrue(sameOperation.HasValue);
            Assert.AreEqual(targetModelId, sameOperation.Value.ModelId);
        }
示例#18
0
        public async Task CopyModelOperationCanPollFromNewObject()
        {
            // 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);
            var resourceId = TestEnvironment.TargetResourceId;
            var region     = TestEnvironment.TargetResourceRegion;

            await using var trainedModel = await CreateDisposableTrainedModelAsync(useTrainingLabels : false);

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

            var operation = await client.StartCopyModelAsync(trainedModel.ModelId, targetAuth);

            var sameOperation = new CopyModelOperation(operation.Id, targetAuth.ModelId, client);
            await sameOperation.WaitForCompletionAsync(PollingInterval);

            Assert.IsTrue(sameOperation.HasValue);
            Assert.AreEqual(targetAuth.ModelId, sameOperation.Value.ModelId);
        }
示例#19
0
        public async Task CopyModel(bool useTokenCredential)
        {
            var sourceClient = CreateDocumentModelAdministrationClient(useTokenCredential);
            var targetClient = CreateDocumentModelAdministrationClient(useTokenCredential);
            var modelId      = Recording.GenerateId();

            await using var trainedModel = await CreateDisposableBuildModelAsync(modelId);

            var targetModelId            = Recording.GenerateId();
            CopyAuthorization targetAuth = await targetClient.GetCopyAuthorizationAsync(targetModelId);

            CopyModelOperation operation = await sourceClient.StartCopyModelAsync(trainedModel.ModelId, targetAuth);

            await operation.WaitForCompletionAsync();

            Assert.IsTrue(operation.HasValue);

            DocumentModel modelCopied = operation.Value;

            ValidateDocumentModel(modelCopied);
            Assert.AreEqual(targetAuth.TargetModelId, modelCopied.ModelId);
            Assert.AreNotEqual(trainedModel.ModelId, modelCopied.ModelId);
        }
示例#20
0
        public async Task CopyModelWithTags()
        {
            var sourceClient = CreateDocumentModelAdministrationClient();
            var targetClient = CreateDocumentModelAdministrationClient();
            var modelId      = Recording.GenerateId();

            await using var trainedModel = await CreateDisposableBuildModelAsync(modelId);

            var tags = TestingTags.ToDictionary(t => t.Key, t => t.Value);

            var targetModelId            = Recording.GenerateId();
            CopyAuthorization targetAuth = await targetClient.GetCopyAuthorizationAsync(targetModelId, tags : tags);

            CopyModelOperation operation = await sourceClient.StartCopyModelToAsync(trainedModel.ModelId, targetAuth);

            await operation.WaitForCompletionAsync();

            DocumentModel copiedModel = operation.Value;

            CollectionAssert.AreEquivalent(TestingTags, copiedModel.Tags);

            await sourceClient.DeleteModelAsync(targetModelId);
        }
        public async Task CopyModel()
        {
            var sourceClient = CreateFormTrainingClient();
            var targetClient = CreateFormTrainingClient();
            var resourceId   = TestEnvironment.TargetResourceId;
            var region       = TestEnvironment.TargetResourceRegion;

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

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

            CopyModelOperation operation = await sourceClient.StartCopyModelAsync(trainedModel.ModelId, targetAuth);

            await operation.WaitForCompletionAsync(PollingInterval);

            Assert.IsTrue(operation.HasValue);

            CustomFormModelInfo modelCopied = operation.Value;

            Assert.IsNotNull(modelCopied.TrainingCompletedOn);
            Assert.IsNotNull(modelCopied.TrainingStartedOn);
            Assert.AreEqual(targetAuth.ModelId, modelCopied.ModelId);
            Assert.AreNotEqual(trainedModel.ModelId, modelCopied.ModelId);
        }