| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Threading; |
| | 6 | | using System.Threading.Tasks; |
| | 7 | | using Azure.AI.FormRecognizer.Models; |
| | 8 | | using Azure.Core; |
| | 9 | | using Azure.Core.Pipeline; |
| | 10 | |
|
| | 11 | | namespace Azure.AI.FormRecognizer.Training |
| | 12 | | { |
| | 13 | | /// <summary> |
| | 14 | | /// The client to use to connect with the Form Recognizer Azure Cognitive Service to train models from |
| | 15 | | /// custom forms. It also supports listing and deleting trained models, as well as accessing account |
| | 16 | | /// properties. |
| | 17 | | /// </summary> |
| | 18 | | public class FormTrainingClient |
| | 19 | | { |
| | 20 | | /// <summary>Provides communication with the Form Recognizer Azure Cognitive Service through its REST API.</summ |
| | 21 | | internal readonly ServiceRestClient ServiceClient; |
| | 22 | |
|
| | 23 | | /// <summary>Provides tools for exception creation in case of failure.</summary> |
| | 24 | | internal readonly ClientDiagnostics Diagnostics; |
| | 25 | |
|
| | 26 | | /// <summary> |
| | 27 | | /// Initializes a new instance of the <see cref="FormTrainingClient"/> class. |
| | 28 | | /// </summary> |
| 200 | 29 | | protected FormTrainingClient() |
| | 30 | | { |
| 200 | 31 | | } |
| | 32 | |
|
| | 33 | | /// <summary> |
| | 34 | | /// Initializes a new instance of the <see cref="FormTrainingClient"/> class. |
| | 35 | | /// </summary> |
| | 36 | | /// <param name="endpoint">The endpoint to use for connecting to the Form Recognizer Azure Cognitive Service.</p |
| | 37 | | /// <param name="credential">A credential used to authenticate to an Azure Service.</param> |
| | 38 | | /// <remarks> |
| | 39 | | /// Both the <paramref name="endpoint"/> URI string and the <paramref name="credential"/> <c>string</c> key |
| | 40 | | /// can be found in the Azure Portal. |
| | 41 | | /// </remarks> |
| | 42 | | /// <seealso href="https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/formrecognizer/Azure.AI.FormRecogn |
| 32 | 43 | | public FormTrainingClient(Uri endpoint, AzureKeyCredential credential) : this(endpoint, credential, new FormReco |
| | 44 | | { |
| 28 | 45 | | } |
| | 46 | |
|
| | 47 | | /// <summary> |
| | 48 | | /// Initializes a new instance of the <see cref="FormTrainingClient"/> class. |
| | 49 | | /// </summary> |
| | 50 | | /// <param name="endpoint">The endpoint to use for connecting to the Form Recognizer Azure Cognitive Service.</p |
| | 51 | | /// <param name="credential">A credential used to authenticate to an Azure Service.</param> |
| | 52 | | /// <param name="options">A set of options to apply when configuring the client.</param> |
| | 53 | | /// <remarks> |
| | 54 | | /// Both the <paramref name="endpoint"/> URI string and the <paramref name="credential"/> <c>string</c> key |
| | 55 | | /// can be found in the Azure Portal. |
| | 56 | | /// </remarks> |
| | 57 | | /// <seealso href="https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/formrecognizer/Azure.AI.FormRecogn |
| 224 | 58 | | public FormTrainingClient(Uri endpoint, AzureKeyCredential credential, FormRecognizerClientOptions options) |
| | 59 | | { |
| 224 | 60 | | Argument.AssertNotNull(endpoint, nameof(endpoint)); |
| 216 | 61 | | Argument.AssertNotNull(credential, nameof(credential)); |
| 216 | 62 | | Argument.AssertNotNull(options, nameof(options)); |
| | 63 | |
|
| 216 | 64 | | Diagnostics = new ClientDiagnostics(options); |
| 216 | 65 | | HttpPipeline pipeline = HttpPipelineBuilder.Build(options, new AzureKeyCredentialPolicy(credential, Constant |
| 216 | 66 | | ServiceClient = new ServiceRestClient(Diagnostics, pipeline, endpoint.AbsoluteUri); |
| 216 | 67 | | } |
| | 68 | |
|
| | 69 | | /// <summary> |
| | 70 | | /// Initializes a new instance of the <see cref="FormTrainingClient"/> class. |
| | 71 | | /// </summary> |
| | 72 | | /// <param name="endpoint">The endpoint to use for connecting to the Form Recognizer Azure Cognitive Service.</p |
| | 73 | | /// <param name="credential">A credential used to authenticate to an Azure Service.</param> |
| | 74 | | /// <remarks> |
| | 75 | | /// The <paramref name="endpoint"/> URI string can be found in the Azure Portal. |
| | 76 | | /// </remarks> |
| | 77 | | /// <seealso href="https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/formrecognizer/Azure.AI.FormRecogn |
| | 78 | | public FormTrainingClient(Uri endpoint, TokenCredential credential) |
| 8 | 79 | | : this(endpoint, credential, new FormRecognizerClientOptions()) |
| | 80 | | { |
| 0 | 81 | | } |
| | 82 | |
|
| | 83 | | /// <summary> |
| | 84 | | /// Initializes a new instance of the <see cref="FormTrainingClient"/> class. |
| | 85 | | /// </summary> |
| | 86 | | /// <param name="endpoint">The endpoint to use for connecting to the Form Recognizer Azure Cognitive Service.</p |
| | 87 | | /// <param name="credential">A credential used to authenticate to an Azure Service.</param> |
| | 88 | | /// <param name="options">A set of options to apply when configuring the client.</param> |
| | 89 | | /// <remarks> |
| | 90 | | /// The <paramref name="endpoint"/> URI string can be found in the Azure Portal. |
| | 91 | | /// </remarks> |
| | 92 | | /// <seealso href="https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/formrecognizer/Azure.AI.FormRecogn |
| 20 | 93 | | public FormTrainingClient(Uri endpoint, TokenCredential credential, FormRecognizerClientOptions options) |
| | 94 | | { |
| 20 | 95 | | Argument.AssertNotNull(endpoint, nameof(endpoint)); |
| 12 | 96 | | Argument.AssertNotNull(credential, nameof(credential)); |
| 4 | 97 | | Argument.AssertNotNull(options, nameof(options)); |
| | 98 | |
|
| 4 | 99 | | Diagnostics = new ClientDiagnostics(options); |
| 4 | 100 | | var pipeline = HttpPipelineBuilder.Build(options, new BearerTokenAuthenticationPolicy(credential, Constants. |
| 4 | 101 | | ServiceClient = new ServiceRestClient(Diagnostics, pipeline, endpoint.AbsoluteUri); |
| 4 | 102 | | } |
| | 103 | |
|
| | 104 | | #region Training |
| | 105 | |
|
| | 106 | | /// <summary> |
| | 107 | | /// Trains a model from a collection of custom forms in a blob storage container. |
| | 108 | | /// </summary> |
| | 109 | | /// <param name="trainingFilesUri">An externally accessible Azure storage blob container Uri. |
| | 110 | | /// For more information see <a href="https://docs.microsoft.com/azure/cognitive-services/form-recognizer/build- |
| | 111 | | /// <param name="useTrainingLabels">If <c>true</c>, use a label file created in the <link-to-label-tool-doc&g |
| | 112 | | /// <param name="trainingOptions">A set of options available for configuring the training request.</param> |
| | 113 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param> |
| | 114 | | /// <returns> |
| | 115 | | /// <para>A <see cref="TrainingOperation"/> to wait on this long-running operation. Its <see cref="TrainingOpera |
| | 116 | | /// completion will contain meta-data about the trained model.</para> |
| | 117 | | /// <para>Even if training fails, a model is created in the Form Recognizer account with an "invalid" status. |
| | 118 | | /// A <see cref="RequestFailedException"/> will be raised containing the modelId to access this invalid model.</ |
| | 119 | | /// </returns> |
| | 120 | | public virtual TrainingOperation StartTraining(Uri trainingFilesUri, bool useTrainingLabels, TrainingOptions tra |
| | 121 | | { |
| 74 | 122 | | Argument.AssertNotNull(trainingFilesUri, nameof(trainingFilesUri)); |
| 72 | 123 | | trainingOptions ??= new TrainingOptions(); |
| | 124 | |
|
| 72 | 125 | | using DiagnosticScope scope = Diagnostics.CreateScope($"{nameof(FormTrainingClient)}.{nameof(StartTraining)} |
| 72 | 126 | | scope.Start(); |
| | 127 | |
|
| | 128 | | try |
| | 129 | | { |
| 72 | 130 | | var trainRequest = new TrainRequest(trainingFilesUri.AbsoluteUri) { SourceFilter = trainingOptions.Train |
| | 131 | |
|
| 72 | 132 | | ResponseWithHeaders<ServiceTrainCustomModelAsyncHeaders> response = ServiceClient.TrainCustomModelAsync( |
| 72 | 133 | | return new TrainingOperation(response.Headers.Location, ServiceClient, Diagnostics); |
| | 134 | | } |
| 0 | 135 | | catch (Exception e) |
| | 136 | | { |
| 0 | 137 | | scope.Failed(e); |
| 0 | 138 | | throw; |
| | 139 | | } |
| 72 | 140 | | } |
| | 141 | |
|
| | 142 | | /// <summary> |
| | 143 | | /// Trains a model from a collection of custom forms in a blob storage container. |
| | 144 | | /// </summary> |
| | 145 | | /// <param name="trainingFilesUri">An externally accessible Azure storage blob container Uri. |
| | 146 | | /// For more information see <a href="https://docs.microsoft.com/azure/cognitive-services/form-recognizer/build- |
| | 147 | | /// <param name="useTrainingLabels">If <c>true</c>, use a label file created in the <link-to-label-tool-doc&g |
| | 148 | | /// <param name="trainingOptions">A set of options available for configuring the training request.</param> |
| | 149 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param> |
| | 150 | | /// <returns> |
| | 151 | | /// <para>A <see cref="TrainingOperation"/> to wait on this long-running operation. Its <see cref="TrainingOpera |
| | 152 | | /// completion will contain meta-data about the trained model.</para> |
| | 153 | | /// <para>Even if training fails, a model is created in the Form Recognizer account with an "invalid" status. |
| | 154 | | /// A <see cref="RequestFailedException"/> will be raised containing the modelId to access this invalid model.</ |
| | 155 | | /// </returns> |
| | 156 | | public virtual async Task<TrainingOperation> StartTrainingAsync(Uri trainingFilesUri, bool useTrainingLabels, Tr |
| | 157 | | { |
| 78 | 158 | | Argument.AssertNotNull(trainingFilesUri, nameof(trainingFilesUri)); |
| 76 | 159 | | trainingOptions ??= new TrainingOptions(); |
| | 160 | |
|
| 76 | 161 | | using DiagnosticScope scope = Diagnostics.CreateScope($"{nameof(FormTrainingClient)}.{nameof(StartTraining)} |
| 76 | 162 | | scope.Start(); |
| | 163 | |
|
| | 164 | | try |
| | 165 | | { |
| 76 | 166 | | var trainRequest = new TrainRequest(trainingFilesUri.AbsoluteUri) { SourceFilter = trainingOptions.Train |
| | 167 | |
|
| 76 | 168 | | ResponseWithHeaders<ServiceTrainCustomModelAsyncHeaders> response = await ServiceClient.TrainCustomModel |
| 76 | 169 | | return new TrainingOperation(response.Headers.Location, ServiceClient, Diagnostics); |
| | 170 | | } |
| 0 | 171 | | catch (Exception e) |
| | 172 | | { |
| 0 | 173 | | scope.Failed(e); |
| 0 | 174 | | throw; |
| | 175 | | } |
| 76 | 176 | | } |
| | 177 | |
|
| | 178 | | #endregion |
| | 179 | |
|
| | 180 | | #region Management Ops |
| | 181 | |
|
| | 182 | | /// <summary> |
| | 183 | | /// Gets a description of a custom model, including the types of forms it can recognize and the fields it will e |
| | 184 | | /// </summary> |
| | 185 | | /// <param name="modelId">The ID of the model to retrieve.</param> |
| | 186 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param> |
| | 187 | | /// <returns>A <see cref="Response{T}"/> representing the result of the operation. It can be cast to a <see cref |
| | 188 | | /// information about the requested model.</returns> |
| | 189 | | public virtual Response<CustomFormModel> GetCustomModel(string modelId, CancellationToken cancellationToken = de |
| | 190 | | { |
| 16 | 191 | | Argument.AssertNotNullOrEmpty(modelId, nameof(modelId)); |
| | 192 | |
|
| 12 | 193 | | using DiagnosticScope scope = Diagnostics.CreateScope($"{nameof(FormTrainingClient)}.{nameof(GetCustomModel) |
| 12 | 194 | | scope.Start(); |
| | 195 | |
|
| | 196 | | try |
| | 197 | | { |
| 12 | 198 | | Guid guid = ClientCommon.ValidateModelId(modelId, nameof(modelId)); |
| | 199 | |
|
| 10 | 200 | | Response<Model> response = ServiceClient.GetCustomModel(guid, includeKeys: true, cancellationToken); |
| 6 | 201 | | return Response.FromValue(new CustomFormModel(response.Value), response.GetRawResponse()); |
| | 202 | | } |
| 6 | 203 | | catch (Exception e) |
| | 204 | | { |
| 6 | 205 | | scope.Failed(e); |
| 6 | 206 | | throw; |
| | 207 | | } |
| 6 | 208 | | } |
| | 209 | |
|
| | 210 | | /// <summary> |
| | 211 | | /// Gets a description of a custom model, including the types of forms it can recognize and the fields it will e |
| | 212 | | /// </summary> |
| | 213 | | /// <param name="modelId">The ID of the model to retrieve.</param> |
| | 214 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param> |
| | 215 | | /// <returns>A <see cref="Response{T}"/> representing the result of the operation. It can be cast to a <see cref |
| | 216 | | /// information about the requested model.</returns> |
| | 217 | | public virtual async Task<Response<CustomFormModel>> GetCustomModelAsync(string modelId, CancellationToken cance |
| | 218 | | { |
| 16 | 219 | | Argument.AssertNotNullOrEmpty(modelId, nameof(modelId)); |
| | 220 | |
|
| 12 | 221 | | using DiagnosticScope scope = Diagnostics.CreateScope($"{nameof(FormTrainingClient)}.{nameof(GetCustomModel) |
| 12 | 222 | | scope.Start(); |
| | 223 | |
|
| | 224 | | try |
| | 225 | | { |
| 12 | 226 | | Guid guid = ClientCommon.ValidateModelId(modelId, nameof(modelId)); |
| | 227 | |
|
| 10 | 228 | | Response<Model> response = await ServiceClient.GetCustomModelAsync(guid, includeKeys: true, cancellation |
| 6 | 229 | | return Response.FromValue(new CustomFormModel(response.Value), response.GetRawResponse()); |
| | 230 | | } |
| 6 | 231 | | catch (Exception e) |
| | 232 | | { |
| 6 | 233 | | scope.Failed(e); |
| 6 | 234 | | throw; |
| | 235 | | } |
| 6 | 236 | | } |
| | 237 | |
|
| | 238 | | /// <summary> |
| | 239 | | /// Deletes the model with the specified model ID. |
| | 240 | | /// </summary> |
| | 241 | | /// <param name="modelId">The ID of the model to delete.</param> |
| | 242 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param> |
| | 243 | | /// <returns>A <see cref="Response"/> representing the result of the operation.</returns> |
| | 244 | | public virtual Response DeleteModel(string modelId, CancellationToken cancellationToken = default) |
| | 245 | | { |
| 60 | 246 | | Argument.AssertNotNullOrEmpty(modelId, nameof(modelId)); |
| | 247 | |
|
| 56 | 248 | | using DiagnosticScope scope = Diagnostics.CreateScope($"{nameof(FormTrainingClient)}.{nameof(DeleteModel)}") |
| 56 | 249 | | scope.Start(); |
| | 250 | |
|
| | 251 | | try |
| | 252 | | { |
| 56 | 253 | | Guid guid = ClientCommon.ValidateModelId(modelId, nameof(modelId)); |
| 54 | 254 | | return ServiceClient.DeleteCustomModel(guid, cancellationToken); |
| | 255 | | } |
| 4 | 256 | | catch (Exception e) |
| | 257 | | { |
| 4 | 258 | | scope.Failed(e); |
| 4 | 259 | | throw; |
| | 260 | | } |
| 52 | 261 | | } |
| | 262 | |
|
| | 263 | | /// <summary> |
| | 264 | | /// Deletes the model with the specified model ID. |
| | 265 | | /// </summary> |
| | 266 | | /// <param name="modelId">The ID of the model to delete.</param> |
| | 267 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param> |
| | 268 | | /// <returns>A <see cref="Response"/> representing the result of the operation.</returns> |
| | 269 | | public virtual async Task<Response> DeleteModelAsync(string modelId, CancellationToken cancellationToken = defau |
| | 270 | | { |
| 60 | 271 | | Argument.AssertNotNullOrEmpty(modelId, nameof(modelId)); |
| | 272 | |
|
| 56 | 273 | | using DiagnosticScope scope = Diagnostics.CreateScope($"{nameof(FormTrainingClient)}.{nameof(DeleteModel)}") |
| 56 | 274 | | scope.Start(); |
| | 275 | |
|
| | 276 | | try |
| | 277 | | { |
| 56 | 278 | | Guid guid = ClientCommon.ValidateModelId(modelId, nameof(modelId)); |
| 54 | 279 | | return await ServiceClient.DeleteCustomModelAsync(guid, cancellationToken).ConfigureAwait(false); |
| | 280 | | } |
| 4 | 281 | | catch (Exception e) |
| | 282 | | { |
| 4 | 283 | | scope.Failed(e); |
| 4 | 284 | | throw; |
| | 285 | | } |
| 52 | 286 | | } |
| | 287 | |
|
| | 288 | | /// <summary> |
| | 289 | | /// Gets a collection of items describing the models trained on this Cognitive Services Account |
| | 290 | | /// and their training status. |
| | 291 | | /// </summary> |
| | 292 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param> |
| | 293 | | /// <returns>A collection of <see cref="CustomFormModelInfo"/> items.</returns> |
| | 294 | | public virtual Pageable<CustomFormModelInfo> GetCustomModels(CancellationToken cancellationToken = default) |
| | 295 | | { |
| | 296 | | Page<CustomFormModelInfo> FirstPageFunc(int? pageSizeHint) |
| | 297 | | { |
| 4 | 298 | | using DiagnosticScope scope = Diagnostics.CreateScope($"{nameof(FormTrainingClient)}.{nameof(GetCustomMo |
| 4 | 299 | | scope.Start(); |
| | 300 | |
|
| | 301 | | try |
| | 302 | | { |
| 4 | 303 | | Response<Models.Models> response = ServiceClient.ListCustomModels(cancellationToken); |
| 4 | 304 | | return Page.FromValues(response.Value.ModelList, response.Value.NextLink, response.GetRawResponse()) |
| | 305 | | } |
| 0 | 306 | | catch (Exception e) |
| | 307 | | { |
| 0 | 308 | | scope.Failed(e); |
| 0 | 309 | | throw; |
| | 310 | | } |
| 4 | 311 | | } |
| | 312 | |
|
| | 313 | | Page<CustomFormModelInfo> NextPageFunc(string nextLink, int? pageSizeHint) |
| | 314 | | { |
| 0 | 315 | | using DiagnosticScope scope = Diagnostics.CreateScope($"{nameof(FormTrainingClient)}.{nameof(GetCustomMo |
| 0 | 316 | | scope.Start(); |
| | 317 | |
|
| | 318 | | try |
| | 319 | | { |
| 0 | 320 | | Response<Models.Models> response = ServiceClient.ListCustomModelsNextPage(nextLink, cancellationToke |
| 0 | 321 | | return Page.FromValues(response.Value.ModelList, response.Value.NextLink, response.GetRawResponse()) |
| | 322 | | } |
| 0 | 323 | | catch (Exception e) |
| | 324 | | { |
| 0 | 325 | | scope.Failed(e); |
| 0 | 326 | | throw; |
| | 327 | | } |
| 0 | 328 | | } |
| | 329 | |
|
| 4 | 330 | | return PageableHelpers.CreateEnumerable(FirstPageFunc, NextPageFunc); |
| | 331 | | } |
| | 332 | |
|
| | 333 | | /// <summary> |
| | 334 | | /// Gets a collection of items describing the models trained on this Cognitive Services Account |
| | 335 | | /// and their training status. |
| | 336 | | /// </summary> |
| | 337 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param> |
| | 338 | | /// <returns>A collection of <see cref="CustomFormModelInfo"/> items.</returns> |
| | 339 | | public virtual AsyncPageable<CustomFormModelInfo> GetCustomModelsAsync(CancellationToken cancellationToken = def |
| | 340 | | { |
| | 341 | | async Task<Page<CustomFormModelInfo>> FirstPageFunc(int? pageSizeHint) |
| | 342 | | { |
| 4 | 343 | | using DiagnosticScope scope = Diagnostics.CreateScope($"{nameof(FormTrainingClient)}.{nameof(GetCustomMo |
| 4 | 344 | | scope.Start(); |
| | 345 | |
|
| | 346 | | try |
| | 347 | | { |
| 4 | 348 | | Response<Models.Models> response = await ServiceClient.ListCustomModelsAsync(cancellationToken).Conf |
| 4 | 349 | | return Page.FromValues(response.Value.ModelList, response.Value.NextLink, response.GetRawResponse()) |
| | 350 | | } |
| 0 | 351 | | catch (Exception e) |
| | 352 | | { |
| 0 | 353 | | scope.Failed(e); |
| 0 | 354 | | throw; |
| | 355 | | } |
| 4 | 356 | | } |
| | 357 | |
|
| | 358 | | async Task<Page<CustomFormModelInfo>> NextPageFunc(string nextLink, int? pageSizeHint) |
| | 359 | | { |
| 4 | 360 | | using DiagnosticScope scope = Diagnostics.CreateScope($"{nameof(FormTrainingClient)}.{nameof(GetCustomMo |
| 4 | 361 | | scope.Start(); |
| | 362 | |
|
| | 363 | | try |
| | 364 | | { |
| 4 | 365 | | Response<Models.Models> response = await ServiceClient.ListCustomModelsNextPageAsync(nextLink, cance |
| 4 | 366 | | return Page.FromValues(response.Value.ModelList, response.Value.NextLink, response.GetRawResponse()) |
| | 367 | | } |
| 0 | 368 | | catch (Exception e) |
| | 369 | | { |
| 0 | 370 | | scope.Failed(e); |
| 0 | 371 | | throw; |
| | 372 | | } |
| 4 | 373 | | } |
| | 374 | |
|
| 4 | 375 | | return PageableHelpers.CreateAsyncEnumerable(FirstPageFunc, NextPageFunc); |
| | 376 | | } |
| | 377 | |
|
| | 378 | | /// <summary> |
| | 379 | | /// Gets the number of models trained on this Cognitive Services Account and the account limits. |
| | 380 | | /// </summary> |
| | 381 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param> |
| | 382 | | /// <returns>A <see cref="Response{T}"/> representing the result of the operation. It can be cast to an <see cre |
| | 383 | | /// the account properties.</returns> |
| | 384 | | public virtual Response<AccountProperties> GetAccountProperties(CancellationToken cancellationToken = default) |
| | 385 | | { |
| 8 | 386 | | using DiagnosticScope scope = Diagnostics.CreateScope($"{nameof(FormTrainingClient)}.{nameof(GetAccountPrope |
| 8 | 387 | | scope.Start(); |
| | 388 | |
|
| | 389 | | try |
| | 390 | | { |
| 8 | 391 | | Response<Models.Models> response = ServiceClient.GetCustomModels(cancellationToken); |
| 4 | 392 | | return Response.FromValue(new AccountProperties(response.Value.Summary), response.GetRawResponse()); |
| | 393 | | } |
| 4 | 394 | | catch (Exception e) |
| | 395 | | { |
| 4 | 396 | | scope.Failed(e); |
| 4 | 397 | | throw; |
| | 398 | | } |
| 4 | 399 | | } |
| | 400 | |
|
| | 401 | | /// <summary> |
| | 402 | | /// Gets the number of models trained on this Cognitive Services Account and the account limits. |
| | 403 | | /// </summary> |
| | 404 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param> |
| | 405 | | /// <returns>A <see cref="Response{T}"/> representing the result of the operation. It can be cast to an <see cre |
| | 406 | | /// the account properties.</returns> |
| | 407 | | public virtual async Task<Response<AccountProperties>> GetAccountPropertiesAsync(CancellationToken cancellationT |
| | 408 | | { |
| 8 | 409 | | using DiagnosticScope scope = Diagnostics.CreateScope($"{nameof(FormTrainingClient)}.{nameof(GetAccountPrope |
| 8 | 410 | | scope.Start(); |
| | 411 | |
|
| | 412 | | try |
| | 413 | | { |
| 8 | 414 | | Response<Models.Models> response = await ServiceClient.GetCustomModelsAsync(cancellationToken).Configure |
| 4 | 415 | | return Response.FromValue(new AccountProperties(response.Value.Summary), response.GetRawResponse()); |
| | 416 | | } |
| 4 | 417 | | catch (Exception e) |
| | 418 | | { |
| 4 | 419 | | scope.Failed(e); |
| 4 | 420 | | throw; |
| | 421 | | } |
| 4 | 422 | | } |
| | 423 | |
|
| | 424 | | #endregion |
| | 425 | |
|
| | 426 | | #region Copy |
| | 427 | | /// <summary> |
| | 428 | | /// Copy a custom model stored in this resource (the source) to the user specified |
| | 429 | | /// target Form Recognizer resource. |
| | 430 | | /// </summary> |
| | 431 | | /// <param name="modelId">Model identifier of the model to copy to the target Form Recognizer resource.</param> |
| | 432 | | /// <param name="target">A <see cref="CopyAuthorization"/> with the copy authorization to the target Form Recogn |
| | 433 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param> |
| | 434 | | /// <returns>A <see cref="CopyModelOperation"/> to wait on this long-running operation. Its <see cref="CopyMode |
| | 435 | | /// completion will contain meta-data about the model copied.</returns> |
| | 436 | | public virtual CopyModelOperation StartCopyModel(string modelId, CopyAuthorization target, CancellationToken can |
| | 437 | | { |
| 12 | 438 | | Argument.AssertNotNullOrEmpty(modelId, nameof(modelId)); |
| 8 | 439 | | Argument.AssertNotNull(target, nameof(target)); |
| | 440 | |
|
| 6 | 441 | | using DiagnosticScope scope = Diagnostics.CreateScope($"{nameof(FormTrainingClient)}.{nameof(StartCopyModel) |
| 6 | 442 | | scope.Start(); |
| | 443 | |
|
| | 444 | | try |
| | 445 | | { |
| 6 | 446 | | Guid guid = ClientCommon.ValidateModelId(modelId, nameof(modelId)); |
| 4 | 447 | | var request = new CopyRequest(target.ResourceId, |
| 4 | 448 | | target.Region, |
| 4 | 449 | | new CopyAuthorizationResult(target.ModelId, target.AccessToken, target.Exp |
| | 450 | |
|
| 4 | 451 | | Response response = ServiceClient.CopyCustomModel(guid, request, cancellationToken); |
| 4 | 452 | | string location = ClientCommon.GetResponseHeader(response.Headers, Constants.OperationLocationHeader); |
| | 453 | |
|
| 4 | 454 | | return new CopyModelOperation(ServiceClient, Diagnostics, location, target.ModelId); |
| | 455 | | } |
| 2 | 456 | | catch (Exception e) |
| | 457 | | { |
| 2 | 458 | | scope.Failed(e); |
| 2 | 459 | | throw; |
| | 460 | | } |
| 4 | 461 | | } |
| | 462 | |
|
| | 463 | | /// <summary> |
| | 464 | | /// Copy a custom model stored in this resource (the source) to the user specified |
| | 465 | | /// target Form Recognizer resource. |
| | 466 | | /// </summary> |
| | 467 | | /// <param name="modelId">Model identifier of the model to copy to the target Form Recognizer resource.</param> |
| | 468 | | /// <param name="target">A <see cref="CopyAuthorization"/> with the copy authorization to the target Form Recogn |
| | 469 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param> |
| | 470 | | /// <returns>A <see cref="CopyModelOperation"/> to wait on this long-running operation. Its <see cref="CopyMode |
| | 471 | | /// completion will contain meta-data about the model copied.</returns> |
| | 472 | | public virtual async Task<CopyModelOperation> StartCopyModelAsync(string modelId, CopyAuthorization target, Canc |
| | 473 | | { |
| 16 | 474 | | Argument.AssertNotNullOrEmpty(modelId, nameof(modelId)); |
| 12 | 475 | | Argument.AssertNotNull(target, nameof(target)); |
| | 476 | |
|
| 10 | 477 | | using DiagnosticScope scope = Diagnostics.CreateScope($"{nameof(FormTrainingClient)}.{nameof(StartCopyModel) |
| 10 | 478 | | scope.Start(); |
| | 479 | |
|
| | 480 | | try |
| | 481 | | { |
| 10 | 482 | | Guid guid = ClientCommon.ValidateModelId(modelId, nameof(modelId)); |
| 8 | 483 | | var request = new CopyRequest(target.ResourceId, |
| 8 | 484 | | target.Region, |
| 8 | 485 | | new CopyAuthorizationResult(target.ModelId, target.AccessToken, target.Exp |
| | 486 | |
|
| 8 | 487 | | Response response = await ServiceClient.CopyCustomModelAsync(guid, request, cancellationToken).Configure |
| 8 | 488 | | string location = ClientCommon.GetResponseHeader(response.Headers, Constants.OperationLocationHeader); |
| | 489 | |
|
| 8 | 490 | | return new CopyModelOperation(ServiceClient, Diagnostics, location, target.ModelId); |
| | 491 | | } |
| 2 | 492 | | catch (Exception e) |
| | 493 | | { |
| 2 | 494 | | scope.Failed(e); |
| 2 | 495 | | throw; |
| | 496 | | } |
| 8 | 497 | | } |
| | 498 | |
|
| | 499 | | /// <summary> |
| | 500 | | /// Generate authorization for copying a custom model into the target Form Recognizer resource. |
| | 501 | | /// </summary> |
| | 502 | | /// <param name="resourceId">Azure Resource Id of the target Form Recognizer resource where the model will be co |
| | 503 | | /// This information can be found in the Properties section of the Form Recognizer resource in the Azure Portal. |
| | 504 | | /// <param name="resourceRegion">Location of the target Form Recognizer resource. |
| | 505 | | /// This information can be found in the Keys and Endpoint section of the Form Recognizer resource in the Azure |
| | 506 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param> |
| | 507 | | /// <returns>A <see cref="Response{T}"/> representing the result of the operation. It can be cast to <see cref=" |
| | 508 | | /// the authorization information necessary to copy a custom model into a target Form Recognizer resource.</retu |
| | 509 | | public virtual Response<CopyAuthorization> GetCopyAuthorization(string resourceId, string resourceRegion, Cancel |
| | 510 | | { |
| 16 | 511 | | Argument.AssertNotNullOrEmpty(resourceId, nameof(resourceId)); |
| 12 | 512 | | Argument.AssertNotNullOrEmpty(resourceRegion, nameof(resourceRegion)); |
| | 513 | |
|
| 8 | 514 | | using DiagnosticScope scope = Diagnostics.CreateScope($"{nameof(FormTrainingClient)}.{nameof(GetCopyAuthoriz |
| 8 | 515 | | scope.Start(); |
| | 516 | |
|
| | 517 | | try |
| | 518 | | { |
| 8 | 519 | | Response<CopyAuthorizationResult> response = ServiceClient.GenerateModelCopyAuthorization(cancellationTo |
| 8 | 520 | | return Response.FromValue(new CopyAuthorization(response.Value, resourceId, resourceRegion), response.Ge |
| | 521 | | } |
| 0 | 522 | | catch (Exception e) |
| | 523 | | { |
| 0 | 524 | | scope.Failed(e); |
| 0 | 525 | | throw; |
| | 526 | | } |
| 8 | 527 | | } |
| | 528 | |
|
| | 529 | | /// <summary> |
| | 530 | | /// Generate authorization for copying a custom model into the target Form Recognizer resource. |
| | 531 | | /// </summary> |
| | 532 | | /// <param name="resourceId">Azure Resource Id of the target Form Recognizer resource where the model will be co |
| | 533 | | /// This information can be found in the Properties section of the Form Recognizer resource in the Azure Portal. |
| | 534 | | /// <param name="resourceRegion">Location of the target Form Recognizer resource. |
| | 535 | | /// This information can be found in the Keys and Endpoint section of the Form Recognizer resource in the Azure |
| | 536 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param> |
| | 537 | | /// <returns>A <see cref="Response{T}"/> representing the result of the operation. It can be cast to <see cref=" |
| | 538 | | /// the authorization information necessary to copy a custom model into a target Form Recognizer resource.</retu |
| | 539 | | public virtual async Task<Response<CopyAuthorization>> GetCopyAuthorizationAsync(string resourceId, string resou |
| | 540 | | { |
| 20 | 541 | | Argument.AssertNotNullOrEmpty(resourceId, nameof(resourceId)); |
| 16 | 542 | | Argument.AssertNotNullOrEmpty(resourceRegion, nameof(resourceRegion)); |
| | 543 | |
|
| 12 | 544 | | using DiagnosticScope scope = Diagnostics.CreateScope($"{nameof(FormTrainingClient)}.{nameof(GetCopyAuthoriz |
| 12 | 545 | | scope.Start(); |
| | 546 | |
|
| | 547 | | try |
| | 548 | | { |
| 12 | 549 | | Response<CopyAuthorizationResult> response = await ServiceClient.GenerateModelCopyAuthorizationAsync(can |
| 12 | 550 | | return Response.FromValue(new CopyAuthorization(response.Value, resourceId, resourceRegion), response.Ge |
| | 551 | | } |
| 0 | 552 | | catch (Exception e) |
| | 553 | | { |
| 0 | 554 | | scope.Failed(e); |
| 0 | 555 | | throw; |
| | 556 | | } |
| 12 | 557 | | } |
| | 558 | | #endregion Copy |
| | 559 | |
|
| | 560 | | #region Form Recognizer Client |
| | 561 | |
|
| | 562 | | /// <summary> |
| | 563 | | /// Gets an instance of a <see cref="FormRecognizerClient"/> that shares the same endpoint, the same |
| | 564 | | /// credentials and the same set of <see cref="FormRecognizerClientOptions"/> this client has. |
| | 565 | | /// </summary> |
| | 566 | | /// <returns>A new instance of a <see cref="FormRecognizerClient"/>.</returns> |
| 4 | 567 | | public virtual FormRecognizerClient GetFormRecognizerClient() => new FormRecognizerClient(Diagnostics, ServiceCl |
| | 568 | |
|
| | 569 | | #endregion Form Recognizer Client |
| | 570 | | } |
| | 571 | | } |