/
trainLabeledModel.ts
41 lines (34 loc) · 1.49 KB
/
trainLabeledModel.ts
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
/**
* This sample demonstrates how to train a custom model with labeled data.
* See recognizeForm.ts to recognize forms using a custom model.
*/
import { FormTrainingClient, AzureKeyCredential } from "@azure/ai-form-recognizer";
// Load the .env file if it exists
import * as dotenv from "dotenv";
dotenv.config();
export async function main() {
// You will need to set these environment variables or edit the following values
const endpoint = process.env["FORM_RECOGNIZER_ENDPOINT"] || "<cognitive services endpoint>";
const apiKey = process.env["FORM_RECOGNIZER_API_KEY"] || "<api key>";
const containerSasUrl =
process.env["LABELED_CONTAINER_SAS_URL"] ||
"<url to Azure blob container storing the labeled training documents>";
const trainingClient = new FormTrainingClient(endpoint, new AzureKeyCredential(apiKey));
// The second positional argument to `beginTraining` indidcates whether or
// not the training process should look for label data in the training
// container
const poller = await trainingClient.beginTraining(containerSasUrl, true, {
// Model name is optional, but recommended
modelName: "trainLabeledModel.ts test model",
onProgress: (state) => {
console.log(`training status: ${state.status}`);
}
});
const model = await poller.pollUntilDone();
console.dir(model, { depth: 4 });
}
main().catch((err) => {
console.error("The sample encountered an error:", err);
});