-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Form Recognizer Copy API #12182
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Form Recognizer Copy API #12182
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d41316e
implementation + apifeedback
maririos 745eea1
documentation
maririos b5311fd
changes for CI
maririos 86129b5
bad merge
maririos 20f75fb
fix region
maririos a40f401
disable tests
maririos e2d1076
PR feedback
maririos c4fd8a7
sample explanation
maririos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
sdk/formrecognizer/Azure.AI.FormRecognizer/samples/Sample6_CopyCustomModel.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| # Copy a custom model between Form Recognizer resources | ||
|
|
||
| This sample demonstrates how to copy a custom model between Form Recognizer resources. | ||
|
|
||
| To get started you'll need a Cognitive Services resource or a Form Recognizer resource. See [README][README] for prerequisites and instructions. | ||
|
|
||
| ## Copy a custom model | ||
| There are several scenarios that require the models to be copied between Form Recognizer resources, like for example, to keep a backup of the created models. | ||
| Copies can be made: | ||
| - Within same Form Recognizer resource | ||
| - Across other Form Recognizer resources that exist in any other supported region. | ||
|
|
||
| For this sample, you will copy a model across Form recognizer resource. It assumes you have the credentials for both the source and the target Form Recognizer resources. | ||
|
|
||
| ## Creating the source and target `FormTrainingClient` | ||
|
|
||
| To create a new `FormTrainingClient` you need the endpoint and credentials from your resource. In the sample below you'll use a Form Recognizer API key credential by creating an `AzureKeyCredential` object. | ||
| You can set `endpoint` and `apiKey` based on an environment variable, a configuration setting, or any way that works for your application. | ||
|
|
||
| ### Source client | ||
| The source client that contains the custom model we want to copy. | ||
|
|
||
| ```C# Snippet:FormRecognizerSample6CreateCopySourceClient | ||
| string endpoint = "<source_endpoint>"; | ||
| string apiKey = "<source_apiKey>"; | ||
| var sourcecredential = new AzureKeyCredential(apiKey); | ||
| var sourceClient = new FormTrainingClient(new Uri(endpoint), sourcecredential); | ||
| ``` | ||
|
|
||
| ### Target client | ||
| The target client where we want to copy the custom model to. | ||
|
|
||
| ```C# Snippet:FormRecognizerSample6CreateCopyTargetClient | ||
| string endpoint = "<target_endpoint>"; | ||
| string apiKey = "<target_apiKey>"; | ||
| var targetCredential = new AzureKeyCredential(apiKey); | ||
| var targetClient = new FormTrainingClient(new Uri(endpoint), targetCredential); | ||
| ``` | ||
|
|
||
| ### Authorize the copy | ||
| Before starting the copy, we need to get a `CopyAuthorization` from the target Form Recognizer resource that will give us permission to execute the copy. | ||
| ```C# Snippet:FormRecognizerSample6GetCopyAuthorization | ||
| string resourceId = "<resourceId>"; | ||
| string resourceRegion = "<region>"; | ||
| CopyAuthorization targetAuth = await targetClient.GetCopyAuthorizationAsync(resourceId, resourceRegion); | ||
| ``` | ||
|
|
||
| `CopyAuthorization` provides the convenience method `ToJson` that will serialize the authorization properties into a json format string. | ||
| ```C# Snippet:FormRecognizerSample6ToJson | ||
| string jsonTargetAuth = targetAuth.ToJson(); | ||
| ``` | ||
|
|
||
| To deserealize a string that contains authorizaiton information, use the `FromJson` method from `CopyAuthorization`. | ||
| ```C# Snippet:FormRecognizerSample6FromJson | ||
| CopyAuthorization targetCopyAuth = CopyAuthorization.FromJson(jsonTargetAuth); | ||
| ``` | ||
|
|
||
| ### Execute the copy | ||
| Now that we have authorization from the target Form Recognizer resource, we execute the copy from the `sourceClient` where the model to copy lives. | ||
|
|
||
| ```C# Snippet:FormRecognizerSample6CopyModel | ||
| string modelId = "<source_modelId>"; | ||
| CustomFormModelInfo newModel = await sourceClient.StartCopyModelAsync(modelId, targetCopyAuth).WaitForCompletionAsync(); | ||
|
|
||
| Console.WriteLine($"Original modelID => {modelId}"); | ||
| Console.WriteLine($"Copied modelID => {newModel.ModelId}"); | ||
| ``` | ||
|
|
||
|
|
||
| To see the full example source files, see: | ||
| * [Copy custom models](https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/formrecognizer/Azure.AI.FormRecognizer/tests/samples/Sample7_CopyModel.cs) | ||
|
|
||
| [README]: https://github.com/Azure/azure-sdk-for-net/tree/master/sdk/formrecognizer/Azure.AI.FormRecognizer#getting-started |
84 changes: 84 additions & 0 deletions
84
sdk/formrecognizer/Azure.AI.FormRecognizer/src/CopyAuthorization.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System.Text.Json; | ||
|
|
||
| namespace Azure.AI.FormRecognizer.Training | ||
| { | ||
| /// <summary> | ||
| /// Authorization for copying a custom model into the target Form Recognizer resource. | ||
| /// </summary> | ||
| public class CopyAuthorization | ||
| { | ||
| /// <summary>Model identifier in the target Form Recognizer Resource. </summary> | ||
| public string ModelId { get; } | ||
| /// <summary> The time when the access token expires. The date is represented as the number of seconds from 1970-01-01T0:0:0Z UTC until the expiration time. </summary> | ||
| //public DateTimeOffset ExpiresOn { get; set; } | ||
| public long ExpiresOn { get; } | ||
| /// <summary> Token claim used to authorize the request. </summary> | ||
| internal string _accessToken { get; } | ||
maririos marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /// <summary> Azure Resource Id of the target Form Recognizer resource where the model is copied to. </summary> | ||
| internal string _resourceId { get; } | ||
| /// <summary> Location of the target Form Recognizer resource. A valid Azure region name supported by Cognitive Services. </summary> | ||
| internal string _region { get; } | ||
|
|
||
| internal CopyAuthorization(string modelId, string accessToken, long expirationDateTimeTicks, string resourceId, string region) | ||
| { | ||
| ModelId = modelId; | ||
| _accessToken = accessToken; | ||
| //ExpiresOn = DateTimeOffset.FromUnixTimeSeconds(expirationDateTimeTicks); | ||
| ExpiresOn = expirationDateTimeTicks; | ||
| _resourceId = resourceId; | ||
| _region = region; | ||
| } | ||
|
|
||
| internal CopyAuthorization(CopyAuthorizationResult copyAuth, string resourceId, string region) | ||
| : this(copyAuth.ModelId, copyAuth.AccessToken, copyAuth.ExpirationDateTimeTicks, resourceId, region) { } | ||
|
|
||
| /// <summary> | ||
| /// Deserializes an opaque string into a <see cref="CopyAuthorization"/> object. | ||
| /// </summary> | ||
| /// <param name="accessToken">Opaque string with the access token information for a specific model.</param> | ||
| public static CopyAuthorization FromJson(string accessToken) | ||
| { | ||
| CopyAuthorizationParse parse = JsonSerializer.Deserialize<CopyAuthorizationParse>(accessToken); | ||
| return new CopyAuthorization( | ||
maririos marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| parse.modelId, | ||
| parse.accessToken, | ||
| //DateTimeOffset.FromUnixTimeSeconds(parse.expirationDateTimeTicks), | ||
| parse.expirationDateTimeTicks, | ||
| parse.resourceId, | ||
| parse.resourceRegion); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Converts the CopyAuthorization object to its equivalent json representation. | ||
| /// </summary> | ||
| public string ToJson() | ||
| { | ||
| var toParse = new CopyAuthorizationParse(this); | ||
| return JsonSerializer.Serialize(toParse); | ||
| } | ||
|
|
||
| private class CopyAuthorizationParse | ||
| { | ||
| public string modelId { get; set; } | ||
| public string accessToken { get; set; } | ||
| public long expirationDateTimeTicks { get; set; } | ||
| public string resourceId { get; set; } | ||
| public string resourceRegion { get; set; } | ||
|
|
||
| public CopyAuthorizationParse() { } | ||
|
|
||
| public CopyAuthorizationParse(CopyAuthorization target) | ||
| { | ||
| modelId = target.ModelId; | ||
| accessToken = target._accessToken; | ||
| //expirationDateTimeTicks = target.ExpiresOn.ToUnixTimeSeconds(); | ||
| expirationDateTimeTicks = target.ExpiresOn; | ||
| resourceId = target._resourceId; | ||
| resourceRegion = target._region; | ||
| } | ||
| } | ||
| } | ||
| } | ||
12 changes: 12 additions & 0 deletions
12
sdk/formrecognizer/Azure.AI.FormRecognizer/src/CopyAuthorizationResult.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using Azure.Core; | ||
|
|
||
| namespace Azure.AI.FormRecognizer.Training | ||
| { | ||
| [CodeGenModel("CopyAuthorizationResult")] | ||
| internal partial class CopyAuthorizationResult | ||
| { | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Issue #12181