コード例 #1
0
        public async Task ManageModelsAsync()
        {
            string endpoint = TestEnvironment.Endpoint;
            string apiKey   = TestEnvironment.ApiKey;

            #region Snippet:FormRecognizerSampleManageModelsAsync

            var client = new DocumentModelAdministrationClient(new Uri(endpoint), new AzureKeyCredential(apiKey));

            // Check number of custom models in the FormRecognizer account, and the maximum number of models that can be stored.
            AccountProperties accountProperties = await client.GetAccountPropertiesAsync();

            Console.WriteLine($"Account has {accountProperties.DocumentModelCount} models.");
            Console.WriteLine($"It can have at most {accountProperties.DocumentModelLimit} models.");

            // List the first ten or fewer models currently stored in the account.
            AsyncPageable <DocumentModelInfo> models = client.GetModelsAsync();

            int count = 0;
            await foreach (DocumentModelInfo modelInfo in models)
            {
                Console.WriteLine($"Custom Model Info:");
                Console.WriteLine($"  Model Id: {modelInfo.ModelId}");
                if (string.IsNullOrEmpty(modelInfo.Description))
                {
                    Console.WriteLine($"  Model description: {modelInfo.Description}");
                }
                Console.WriteLine($"  Created on: {modelInfo.CreatedOn}");
                if (++count == 10)
                {
                    break;
                }
            }

            // Create a new model to store in the account
#if SNIPPET
            Uri trainingFileUri = new Uri("<trainingFileUri>");
#else
            Uri trainingFileUri = new Uri(TestEnvironment.BlobContainerSasUrl);
#endif
            BuildModelOperation operation = await client.StartBuildModelAsync(trainingFileUri, DocumentBuildMode.Template);

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

            DocumentModel model = operationResponse.Value;

            // Get the model that was just created
            DocumentModel newCreatedModel = await client.GetModelAsync(model.ModelId);

            Console.WriteLine($"Custom Model with Id {newCreatedModel.ModelId} has the following information:");

            Console.WriteLine($"  Model Id: {newCreatedModel.ModelId}");
            if (string.IsNullOrEmpty(newCreatedModel.Description))
            {
                Console.WriteLine($"  Model description: {newCreatedModel.Description}");
            }
            Console.WriteLine($"  Created on: {newCreatedModel.CreatedOn}");

            // Delete the model from the account.
            await client.DeleteModelAsync(newCreatedModel.ModelId);

            #endregion
        }
コード例 #2
0
        public async Task GetAndListOperationsAsync()
        {
            string endpoint = TestEnvironment.Endpoint;
            string apiKey   = TestEnvironment.ApiKey;

            #region Snippet:FormRecognizerSampleGetAndListOperations

            var client = new DocumentModelAdministrationClient(new Uri(endpoint), new AzureKeyCredential(apiKey));

            // Make sure there is at least one operation, so we are going to build a custom model.
#if SNIPPET
            Uri trainingFileUri = < trainingFileUri >;
#else
            Uri trainingFileUri = new Uri(TestEnvironment.BlobContainerSasUrl);
#endif
            BuildModelOperation operation = await client.StartBuildModelAsync(trainingFileUri, DocumentBuildMode.Template);

            await operation.WaitForCompletionAsync();

            // List the first ten or fewer operations that have been executed in the last 24h.
            AsyncPageable <ModelOperationInfo> modelOperations = client.GetOperationsAsync();

            string operationId = string.Empty;
            int    count       = 0;
            await foreach (ModelOperationInfo modelOperationInfo in modelOperations)
            {
                Console.WriteLine($"Model operation info:");
                Console.WriteLine($"  Id: {modelOperationInfo.OperationId}");
                Console.WriteLine($"  Kind: {modelOperationInfo.Kind}");
                Console.WriteLine($"  Status: {modelOperationInfo.Status}");
                Console.WriteLine($"  Percent completed: {modelOperationInfo.PercentCompleted}");
                Console.WriteLine($"  Created on: {modelOperationInfo.CreatedOn}");
                Console.WriteLine($"  LastUpdated on: {modelOperationInfo.LastUpdatedOn}");
                Console.WriteLine($"  Resource location of successful operation: {modelOperationInfo.ResourceLocation}");

                if (count == 0)
                {
                    operationId = modelOperationInfo.OperationId;
                }

                if (++count == 10)
                {
                    break;
                }
            }

            // Get an operation by ID
            ModelOperation specificOperation = await client.GetOperationAsync(operationId);

            if (specificOperation.Status == DocumentOperationStatus.Succeeded)
            {
                Console.WriteLine($"My {specificOperation.Kind} operation is completed.");
                DocumentModel result = specificOperation.Result;
                Console.WriteLine($"Model ID: {result.ModelId}");
            }
            else if (specificOperation.Status == DocumentOperationStatus.Failed)
            {
                Console.WriteLine($"My {specificOperation.Kind} operation failed.");
                ResponseError error = specificOperation.Error;
                Console.WriteLine($"Code: {error.Code}: Message: {error.Message}");
            }
            else
            {
                Console.WriteLine($"My {specificOperation.Kind} operation status is {specificOperation.Status}");
            }
            #endregion
        }