-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ai-form-recognizer] Rework samples for 3.1.0-beta.3 (#14689)
* [ai-form-recognizer] Migrate to samples v2 * Move test-assets to assets * Remove dirname based path computation * Backport fix for stronglyTypedRecognizedForm to samples/v3 * Fix aka.ms links for FR data types * Remove old typescript samples * Use newer invoice sample document * Rework samples and tag with metadata * Added package.json metadata * Fixed a tense inconsistency * Disable generation of docs.ms metadata until GA. * Fixed links in sample READMEs * [min/max] Changed asset path from test-assets to assets * addressed feedback
- Loading branch information
1 parent
85c2873
commit fadd5b9
Showing
137 changed files
with
2,925 additions
and
869 deletions.
There are no files selected for viewing
This file contains 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 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
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
Binary file added
BIN
+180 KB
sdk/formrecognizer/ai-form-recognizer/assets/invoice/sample_invoice.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
File renamed without changes
File renamed without changes.
This file contains 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
103 changes: 103 additions & 0 deletions
103
sdk/formrecognizer/ai-form-recognizer/samples-dev/authenticationMethods.ts
This file contains 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,103 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
/** | ||
* This sample demonstrates how to use either an Azure Active Directory (RBAC) | ||
* or an API Key to authenticate a FormRecognizerClient. | ||
* | ||
* @summary authenticate a service client using both Azure Active Directory | ||
* and an API key | ||
* @azsdk-weight 50 | ||
*/ | ||
|
||
// To use an API Key, import `AzureKeyCredential` from the Form Recognizer package | ||
import { FormRecognizerClient, AzureKeyCredential } from "@azure/ai-form-recognizer"; | ||
|
||
// To use Azure AD, import `DefaultAzureCredential` from the `@azure/identity` package | ||
import { DefaultAzureCredential } from "@azure/identity"; | ||
|
||
import * as fs from "fs"; | ||
|
||
// Load the .env file if it exists | ||
import * as dotenv from "dotenv"; | ||
dotenv.config(); | ||
|
||
// You will need to set this environment variables or edit the following values | ||
const endpoint = process.env["FORM_RECOGNIZER_ENDPOINT"] ?? "<cognitive services endpoint>"; | ||
|
||
/** | ||
* Create a client using Azure Active Directory to authenticate | ||
*/ | ||
async function useAad() { | ||
console.log("-- Azure Active Directory --"); | ||
|
||
// DefaultAzureCredential expects the following three environment variables: | ||
// - AZURE_TENANT_ID: The tenant ID in Azure Active Directory | ||
// - AZURE_CLIENT_ID: The application (client) ID registered in the AAD tenant | ||
// - AZURE_CLIENT_SECRET: The client secret for the registered application | ||
const credential = new DefaultAzureCredential(); | ||
|
||
const client = new FormRecognizerClient(endpoint, credential); | ||
|
||
await recognizeContentWithClient(client); | ||
} | ||
|
||
/** | ||
* Create a client using an API Key (Cognitive Services Subscription Key) to authenticate. | ||
*/ | ||
async function useApiKey() { | ||
console.log("-- API Key --"); | ||
|
||
// If using an API Key, you will need to set this environment variable | ||
const apiKey = process.env["FORM_RECOGNIZER_API_KEY"] ?? "<api key>"; | ||
|
||
const credential = new AzureKeyCredential(apiKey); | ||
|
||
const client = new FormRecognizerClient(endpoint, credential); | ||
|
||
await recognizeContentWithClient(client); | ||
} | ||
|
||
/** | ||
* This function will use the client generated by `useApiKey` and `useAad` | ||
* for content recognition and print its output. | ||
*/ | ||
async function recognizeContentWithClient(client: FormRecognizerClient) { | ||
const fileName = "./assets/forms/Form_1.jpg"; | ||
|
||
if (!fs.existsSync(fileName)) { | ||
throw new Error(`Expecting file ${fileName} exists`); | ||
} | ||
|
||
const readStream = fs.createReadStream(fileName); | ||
|
||
const poller = await client.beginRecognizeContent(readStream); | ||
const pages = await poller.pollUntilDone(); | ||
|
||
if (!pages || pages.length === 0) { | ||
throw new Error("Expected to receive a non-empty list of pages!"); | ||
} | ||
|
||
for (const page of pages!) { | ||
console.log( | ||
`Page ${page.pageNumber}: width="${page.width}", and height="${page.height}" (unit: ${page.unit})` | ||
); | ||
for (const table of page.tables!) { | ||
for (const cell of table.cells) { | ||
console.log(`cell [${cell.rowIndex},${cell.columnIndex}] has text "${cell.text}"`); | ||
} | ||
} | ||
} | ||
} | ||
|
||
export async function main() { | ||
console.log("== Client Authentication Methods Sample =="); | ||
|
||
await useAad(); | ||
|
||
await useApiKey(); | ||
} | ||
|
||
main().catch((err) => { | ||
console.error("The sample encountered an error:", err); | ||
}); |
59 changes: 59 additions & 0 deletions
59
sdk/formrecognizer/ai-form-recognizer/samples-dev/copyModel.ts
This file contains 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,59 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
/** | ||
* This sample demonstrates how to copy a model from a source Form Recognizer resource | ||
* to a target Form Recognizer resource. | ||
* | ||
* @summary copy a custom model from one Form Recognizer resource to another | ||
* @azsdk-weight 60 | ||
*/ | ||
|
||
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 | ||
// information about the source Form Recognizer resource | ||
const endpoint = | ||
process.env["FORM_RECOGNIZER_SOURCE_ENDPOINT"] ?? "<source cognitive services endpoint>"; | ||
const apiKey = process.env["FORM_RECOGNIZER_SOURCE_API_KEY"] ?? "<source resource api key>"; | ||
const sourceModelId = | ||
process.env["FORM_RECOGNIZER_SOURCE_MODEL_ID"] ?? "<source custom model id>"; | ||
// information about the target Form Recognizer resource | ||
const targetEndpoint = | ||
process.env["FORM_RECOGNIZER_TARGET_ENDPOINT"] ?? "<target cognitive services endpoint>"; | ||
const targetApiKey = process.env["FORM_RECOGNIZER_TARGET_API_KEY"] ?? "<target resource api key>"; | ||
const targetResourceRegion = | ||
process.env["FORM_RECOGNIZER_TARGET_REGION"] ?? "<target resource region>"; | ||
const targetResourceId = | ||
process.env["FORM_RECOGNIZER_TARGET_RESOURCE_ID"] ?? "<target resource resource id>"; | ||
|
||
const targetClient = new FormTrainingClient(targetEndpoint, new AzureKeyCredential(targetApiKey)); | ||
const authorization = await targetClient.getCopyAuthorization( | ||
targetResourceId, | ||
targetResourceRegion | ||
); | ||
|
||
const sourceClient = new FormTrainingClient(endpoint, new AzureKeyCredential(apiKey)); | ||
const poller = await sourceClient.beginCopyModel(sourceModelId, authorization, { | ||
onProgress: (state) => { | ||
console.log(`Copy model status: ${state.status}`); | ||
} | ||
}); | ||
const result = await poller.pollUntilDone(); | ||
|
||
// now verify that the copy in the target Form Recognizer resource | ||
console.log(`Model ID: ${result.modelId}`); | ||
console.log(`Status : ${result.status}`); | ||
|
||
const model = await targetClient.getCustomModel(result.modelId); | ||
console.log(model); | ||
} | ||
|
||
main().catch((err) => { | ||
console.error("The sample encountered an error:", err); | ||
}); |
107 changes: 107 additions & 0 deletions
107
sdk/formrecognizer/ai-form-recognizer/samples-dev/createComposedModel.ts
This file contains 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,107 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
/** | ||
* This sample demonstrates how to create a composed model from several | ||
* individual labeled models. | ||
* | ||
* We train all of the models used in the compose and then finally create | ||
* the composed model. | ||
* | ||
* NOTE: Only models trained using labels can be composed. Attempting to | ||
* compose an unlabeled model will result in an error. | ||
* | ||
* @summary create a composed model from several individual labeled models | ||
* @azsdk-weight 60 | ||
*/ | ||
|
||
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>"; | ||
|
||
// This object will hold the SAS-encoded URLs to containers that hold | ||
// different types of purchase order documents and their labels. | ||
const purchaseOrderSasUrls = { | ||
supplies: | ||
process.env["PURCHASE_ORDER_OFFICE_SUPPLIES_SAS_URL"] ?? | ||
"<sas url to container with purchase orders for supplies>", | ||
equipment: | ||
process.env["PURCHASE_ORDER_OFFICE_SUPPLIES_SAS_URL"] ?? | ||
"<sas url to container with purchase orders for equipment>", | ||
furniture: | ||
process.env["PURCHASE_ORDER_OFFICE_SUPPLIES_SAS_URL"] ?? | ||
"<sas url to container with purchase orders for furniture>", | ||
cleaningSupplies: | ||
process.env["PURCHASE_ORDER_OFFICE_SUPPLIES_SAS_URL"] ?? | ||
"<sas url to container with purchase orders for cleaning supplies>" | ||
}; | ||
|
||
// Train all of the individual models and extract their model IDs | ||
const trainingClient = new FormTrainingClient(endpoint, new AzureKeyCredential(apiKey)); | ||
|
||
const modelIds = await Promise.all( | ||
Object.entries(purchaseOrderSasUrls) | ||
.map(async ([kind, sasUrl]) => { | ||
const poller = await trainingClient.beginTraining(sasUrl, true, { | ||
onProgress(state) { | ||
console.log(`training model "${kind}": ${state.status}`); | ||
}, | ||
modelName: kind | ||
}); | ||
|
||
return poller.pollUntilDone(); | ||
}) | ||
.map(async (model) => (await model).modelId) | ||
); | ||
|
||
// Finally, create the composed model. | ||
|
||
const poller = await trainingClient.beginCreateComposedModel(modelIds, { | ||
onProgress(state) { | ||
console.log(`composing model "purchase_order": ${state.status}`); | ||
}, | ||
modelName: "purchase_order" | ||
}); | ||
|
||
const composedModel = await poller.pollUntilDone(); | ||
|
||
// Print the model info to console | ||
console.log(`Composed model: ${composedModel.modelName} (${composedModel.modelId}`); | ||
|
||
console.log("Properties:", composedModel.properties); | ||
|
||
// Errors | ||
if (composedModel.errors && composedModel.errors.length > 0) { | ||
console.log("Model Errors:"); | ||
for (const error of composedModel.errors) { | ||
console.log(`- [${error.code}] ${error.message}`); | ||
} | ||
} | ||
|
||
// Submodels | ||
console.log("Submodels:"); | ||
for (const model of composedModel.submodels ?? []) { | ||
console.log(`- ${model.formType} (${model.modelId}) - ${model.accuracy} accuracy`); | ||
console.log(" Fields:"); | ||
for (const [name, field] of Object.entries(model.fields)) { | ||
console.log(` - ${name} (${field.accuracy} accuracy)`); | ||
} | ||
} | ||
|
||
// Training Documents | ||
console.log("Training Documents:"); | ||
for (const info of composedModel.trainingDocuments ?? []) { | ||
console.log(`- ${info.name} (in ${info.modelId})`); | ||
} | ||
} | ||
|
||
main().catch((err) => { | ||
console.error("The sample encountered an error:", err); | ||
}); |
Oops, something went wrong.