Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
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);
        }
        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
        }
Ejemplo n.º 4
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 = new CopyModelOperation(operation.Id, targetAuth.ModelId, nonInstrumentedClient);
            await sameOperation.WaitForCompletionAsync(PollingInterval);

            Assert.IsTrue(sameOperation.HasValue);
            Assert.AreEqual(targetAuth.ModelId, sameOperation.Value.ModelId);
        }
Ejemplo n.º 5
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));
            }
        }
Ejemplo n.º 6
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);
        }
Ejemplo n.º 7
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);
        }
Ejemplo n.º 8
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);
        }
        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);
        }