Integrate Azure AI Document Intelligence
In this section, we're going integrate the Document Intelligence Model with our patient web app to extract the patient's information from an uploaded image.
Updating the Azure Function
- Switch back to VS Code, you should still have the contoso_new_patient_app open in VS Code.
- Open the UploadFile.cs file, located at src/api/NewPatient/UploadFile.cs.
- Scroll down to the // TODO: Call Azure AI Document Intelligence section.
-
Replace the // TODO comment and throw statement with the following code
string? endpoint = Environment.GetEnvironmentVariable("FORM_RECOGNIZER_ENDPOINT"); string? apiKey = Environment.GetEnvironmentVariable("FORM_RECOGNIZER_API_KEY"); string? modelId = Environment.GetEnvironmentVariable("FORM_RECOGNIZER_MODEL_ID"); if (string.IsNullOrEmpty(endpoint) || string.IsNullOrEmpty(apiKey) || string.IsNullOrEmpty(modelId)) { throw new InvalidOperationException("Missing environment variables"); } var credential = new AzureKeyCredential(apiKey); var client = new DocumentAnalysisClient(new Uri(endpoint), credential); AnalyzeDocumentOperation operation = await client.AnalyzeDocumentAsync(WaitUntil.Completed, modelId, file.OpenReadStream()); AnalyzeResult result = operation.Value; var outputs = new Dictionary<string, (string, float?)>(); foreach (AnalyzedDocument document in result.Documents) { foreach ((string fieldName, DocumentField field) in document.Fields) { outputs.Add(fieldName, (field.Content, field.Confidence)); } } return outputs;
-
You must Save the file.
What is this code doing?
Let's take some time to understand what this code is doing by breaking it down piece by piece.string? endpoint = Environment.GetEnvironmentVariable("FORM_RECOGNIZER_ENDPOINT");
string? apiKey = Environment.GetEnvironmentVariable("FORM_RECOGNIZER_API_KEY");
string? modelId = Environment.GetEnvironmentVariable("FORM_RECOGNIZER_MODEL_ID");
if (string.IsNullOrEmpty(endpoint) || string.IsNullOrEmpty(apiKey) || string.IsNullOrEmpty(modelId))
{
throw new InvalidOperationException("Missing environment variables");
}
var credential = new AzureKeyCredential(apiKey);
var client = new DocumentAnalysisClient(new Uri(endpoint), credential);
AnalyzeDocumentOperation operation = await client.AnalyzeDocumentAsync(WaitUntil.Completed, modelId, file.OpenReadStream());
AnalyzeResult result = operation.Value;
var outputs = new Dictionary<string, (string, float?)>();
foreach (AnalyzedDocument document in result.Documents)
{
foreach ((string fieldName, DocumentField field) in document.Fields)
{
outputs.Add(fieldName, (field.Content, field.Confidence));
}
}
return outputs;
Deploy to Azure
Deploy the app to Azure Static Web Apps with the Azure Developer CLI.
- From VS Code, select Ctrl+Shift+` to open a new terminal.
- From the terminal, run the following command to start the function app. This command takes about one minute to deploy the updated function to Azure.
azd deploy
Open the patient registration app in your browser
- From your browser, open the patient registration app at the Web Service URL displayed in the deployment logs.
- Save the patient registration app URL for use in the next section.