-
Notifications
You must be signed in to change notification settings - Fork 2k
/
ComposeDocumentModel.java
79 lines (68 loc) · 4.09 KB
/
ComposeDocumentModel.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.azure.ai.formrecognizer.administration;
import com.azure.ai.formrecognizer.documentanalysis.administration.DocumentModelAdministrationClient;
import com.azure.ai.formrecognizer.documentanalysis.administration.DocumentModelAdministrationClientBuilder;
import com.azure.ai.formrecognizer.documentanalysis.administration.models.ComposeDocumentModelOptions;
import com.azure.ai.formrecognizer.documentanalysis.administration.models.DocumentModelBuildMode;
import com.azure.ai.formrecognizer.documentanalysis.administration.models.DocumentModelDetails;
import com.azure.ai.formrecognizer.documentanalysis.models.OperationResult;
import com.azure.core.credential.AzureKeyCredential;
import com.azure.core.util.Context;
import com.azure.core.util.polling.SyncPoller;
import java.time.Duration;
import java.util.Arrays;
/**
* Sample for creating a custom document analysis composed model.
* <p>
* This is useful when you have build different analysis models and want to aggregate a group of
* them into a single model that you (or a user) could use to analyze a custom document. When doing
* so, you can let the service decide which model more accurately represents the document to
* analyze, instead of manually trying each built model against the form and selecting
* the most accurate one.
* </p>
*/
public class ComposeDocumentModel {
/**
* Main method to invoke this demo.
*
* @param args Unused. Arguments to the program.
*/
public static void main(final String[] args) {
// Instantiate a client that will be used to call the service.
DocumentModelAdministrationClient client = new DocumentModelAdministrationClientBuilder()
.credential(new AzureKeyCredential("{key}"))
.endpoint("https://{endpoint}.cognitiveservices.azure.com/")
.buildClient();
// Build custom document analysis model
String model1TrainingFiles = "{SAS_URL_of_your_container_in_blob_storage_for_model_1}";
// The shared access signature (SAS) Url of your Azure Blob Storage container with your forms.
SyncPoller<OperationResult, DocumentModelDetails> model1Poller =
client.beginBuildDocumentModel(model1TrainingFiles, DocumentModelBuildMode.TEMPLATE);
// Build custom document analysis model
String model2TrainingFiles = "{SAS_URL_of_your_container_in_blob_storage_for_model_2}";
// The shared access signature (SAS) Url of your Azure Blob Storage container with your forms.
SyncPoller<OperationResult, DocumentModelDetails> model2Poller =
client.beginBuildDocumentModel(model2TrainingFiles, DocumentModelBuildMode.TEMPLATE);
String labeledModelId1 = model1Poller.getFinalResult().getModelId();
String labeledModelId2 = model2Poller.getFinalResult().getModelId();
String composedModelId = "my-composed-model";
final DocumentModelDetails documentModelDetails =
client.beginComposeDocumentModel(Arrays.asList(labeledModelId1, labeledModelId2),
new ComposeDocumentModelOptions().setModelId(composedModelId).setDescription("my composed model description"),
Context.NONE)
.setPollInterval(Duration.ofSeconds(5))
.getFinalResult();
System.out.printf("Model ID: %s%n", documentModelDetails.getModelId());
System.out.printf("Model description: %s%n", documentModelDetails.getDescription());
System.out.printf("Composed model created on: %s%n", documentModelDetails.getCreatedOn());
System.out.println("Document Fields:");
documentModelDetails.getDocumentTypes().forEach((key, documentTypeDetails) -> {
documentTypeDetails.getFieldSchema().forEach((field, documentFieldSchema) -> {
System.out.printf("Field: %s", field);
System.out.printf("Field type: %s", documentFieldSchema.getType());
System.out.printf("Field confidence: %.2f", documentTypeDetails.getFieldConfidence().get(field));
});
});
}
}