private async Task <Guid> CreateNewScenarioAsync()
        {
            CloudBlobContainer formRecognizerCloudContainer = null;

            try
            {
                ScenarioSetupState = FormRecognizerScenarioSetupState.Running;

                // create cloud storage container and upload local files
                formRecognizerCloudContainer = AzureBlobHelper.GetCloudBlobContainer(StorageAccount, StorageKey, Guid.NewGuid().ToString());
                await AzureBlobHelper.UploadStorageFilesToContainerAsync(LocalFileCollection, formRecognizerCloudContainer);

                string containerFullSASUrl = AzureBlobHelper.GetContainerSasToken(formRecognizerCloudContainer, sharedAccessStartTimeInMinutes: 5, sharedAccessExpiryTimeInMinutes: 5);

                // create and train the custom model
                Guid modelId = await this.formRecognizerService.TrainCustomModelAsync(containerFullSASUrl);

                ModelResultResponse model = await this.formRecognizerService.GetCustomModelAsync(modelId);

                // check creation status
                string status = (model?.ModelInfo?.Status ?? string.Empty).ToLower();
                switch (status)
                {
                case "created":
                case "ready":
                    ScenarioSetupState = FormRecognizerScenarioSetupState.Completed;
                    return(modelId);

                case "invalid":
                default:
                    ScenarioSetupState = FormRecognizerScenarioSetupState.Error;
                    break;
                }
            }
            catch (Exception ex)
            {
                ScenarioCreationErrorDetails = ex.Message;
                ScenarioSetupState           = FormRecognizerScenarioSetupState.Error;
            }
            finally
            {
                if (formRecognizerCloudContainer != null)
                {
                    await formRecognizerCloudContainer.DeleteIfExistsAsync();
                }
            }

            return(Guid.Empty);
        }
Пример #2
0
        private async Task <Guid> GetResultFromLocationResponse(HttpResponseMessage response)
        {
            // Process operation
            if (response.Headers.Contains(this.HEADER_LOCATION_KEY) == false)
            {
                throw new InvalidOperationException("No location value returned from initial request.");
            }

            Uri locationUri = new Uri(response.Headers.GetValues(this.HEADER_LOCATION_KEY).First());

            var opResult = new ModelResultResponse();

            int i = 0;

            while (i++ < RETRY_COUNT)
            {
                // Get the operation result
                opResult = await HttpClientUtility.GetAsync <ModelResultResponse>(locationUri, this.RequestHeaders);

                // Wait if operation is running or has not started
                if (opResult.ModelInfo.Status == "creating")
                {
                    await Task.Delay(RETRY_DELAY);
                }
                else
                {
                    break;
                }
            }

            if (opResult.ModelInfo.Status != "ready")
            {
                throw new Exception($"Form recognition operation was not successful with status: {opResult.ModelInfo.Status}");
            }

            return(opResult.ModelInfo.ModelId);
        }