From d0e3fed7f3a14e1941adce9e66d0f4c03c2fdda3 Mon Sep 17 00:00:00 2001
From: Timna Brown <24630902+brown9804@users.noreply.github.com>
Date: Mon, 21 Jul 2025 08:45:24 -0600
Subject: [PATCH 1/8] arch diagram
---
docs/automatedPDFLayoutprocessingFunctionAppDocIntellig.drawio | 0
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 docs/automatedPDFLayoutprocessingFunctionAppDocIntellig.drawio
diff --git a/docs/automatedPDFLayoutprocessingFunctionAppDocIntellig.drawio b/docs/automatedPDFLayoutprocessingFunctionAppDocIntellig.drawio
new file mode 100644
index 0000000..e69de29
From 8af97c8c386e63f897358fda3e4f7dbb9fef2463 Mon Sep 17 00:00:00 2001
From: Timna Brown <24630902+brown9804@users.noreply.github.com>
Date: Mon, 21 Jul 2025 08:45:46 -0600
Subject: [PATCH 2/8] cleaning
---
...g-FunctionApp-DocIntellig-AI-Vision.drawio | 105 ------------------
1 file changed, 105 deletions(-)
delete mode 100644 docs/automated-PDFLayoutprocessing-FunctionApp-DocIntellig-AI-Vision.drawio
diff --git a/docs/automated-PDFLayoutprocessing-FunctionApp-DocIntellig-AI-Vision.drawio b/docs/automated-PDFLayoutprocessing-FunctionApp-DocIntellig-AI-Vision.drawio
deleted file mode 100644
index b05d1f0..0000000
--- a/docs/automated-PDFLayoutprocessing-FunctionApp-DocIntellig-AI-Vision.drawio
+++ /dev/null
@@ -1,105 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
From 485a529824ec60edd4afd0854f778f873db86e93 Mon Sep 17 00:00:00 2001
From: Timna Brown <24630902+brown9804@users.noreply.github.com>
Date: Mon, 21 Jul 2025 08:46:58 -0600
Subject: [PATCH 3/8] cleaning
---
README.md | 453 ------------------------------------------------------
1 file changed, 453 deletions(-)
diff --git a/README.md b/README.md
index 58ac866..e69de29 100644
--- a/README.md
+++ b/README.md
@@ -1,453 +0,0 @@
-# Demo: PDF Layout Extraction with Doc Intelligence
Supporting Multiple Document Versions with Visual Selection Cues (full-code approach)
-
-`Azure Storage + Document Intelligence + Function App + Cosmos DB`
-
-Costa Rica
-
-[](https://github.com)
-[](https://github.com/)
-[brown9804](https://github.com/brown9804)
-
-Last updated: 2025-07-21
-
------------------------------
-
-> This solution is designed to be flexible and robust, supporting multiple versions of PDF documents with varying layouts—including those that use visual selection cues such as gray fills, hand-drawn Xs, checkmarks, or circles. By building on the [PDFs-Layouts-Processing-Fapp-DocIntelligence](https://github.com/MicrosoftCloudEssentials-LearningHub/PDFs-Layouts-Processing-Fapp-DocIntelligence) repository, we ensure that:
-
-- Table structure and text are extracted using Azure Document Intelligence (Layout model).
-- Visual selection cues are detected using Azure AI Vision or image preprocessing.
-- Visual indicators are mapped to structured data, returning only the selected values in a clean JSON format.
-- The logic is abstracted to support multiple layout variations, so the system adapts easily to new document formats and selection styles.
-
-> [!IMPORTANT]
-> This example is based on a `public network site and is intended for demonstration purposes only`. It showcases how several Azure resources can work together to achieve the desired result. Consider the section below about [Important Considerations for Production Environment](#important-considerations-for-production-environment). Please note that `these demos are intended as a guide and are based on my personal experiences. For official guidance, support, or more detailed information, please refer to Microsoft's official documentation or contact Microsoft directly`: [Microsoft Sales and Support](https://support.microsoft.com/contactus?ContactUsExperienceEntryPointAssetId=S.HP.SMC-HOME)
-
-
-List of References (Click to expand)
-
-- [Use Azure AI services with SynapseML in Microsoft Fabric](https://learn.microsoft.com/en-us/fabric/data-science/how-to-use-ai-services-with-synapseml)
-- [Plan and manage costs for Azure AI Foundry](https://learn.microsoft.com/en-us/azure/ai-foundry/how-to/costs-plan-manage)
-- [Azure AI Document Intelligence documentation](https://learn.microsoft.com/en-us/azure/ai-services/document-intelligence/?view=doc-intel-4.0.0)
-- [Get started with the Document Intelligence Sample Labeling tool](https://learn.microsoft.com/en-us/azure/ai-services/document-intelligence/v21/try-sample-label-tool?view=doc-intel-2.1.0#prerequisites-for-training-a-custom-form-model)
-- [Document Intelligence Sample Labeling tool](https://fott-2-1.azurewebsites.net/)
-- [Assign an Azure role for access to blob data](https://learn.microsoft.com/en-us/azure/storage/blobs/assign-azure-role-data-access?tabs=portal)
-- [Build and train a custom extraction model](https://learn.microsoft.com/en-us/azure/ai-services/document-intelligence/how-to-guides/build-a-custom-model?view=doc-intel-2.1.0)
-- [Compose custom models - Document Intelligence](https://learn.microsoft.com/en-us/azure/ai-services/document-intelligence/how-to-guides/compose-custom-models?view=doc-intel-2.1.0&tabs=studio)
-- [Deploy the Sample Labeling tool](https://learn.microsoft.com/en-us/azure/ai-services/document-intelligence/v21/deploy-label-tool?view=doc-intel-2.1.0)
-- [Train a custom model using the Sample Labeling tool](https://learn.microsoft.com/en-us/azure/ai-services/document-intelligence/v21/label-tool?view=doc-intel-2.1.0)
-- [Train models with the sample-labeling tool](https://learn.microsoft.com/en-us/azure/ai-services/document-intelligence/v21/supervised-table-tags?view=doc-intel-2.1.0)
-- [Azure Cosmos DB - Database for the AI Era](https://learn.microsoft.com/en-us/azure/cosmos-db/introduction)
-- [Consistency levels in Azure Cosmos DB](https://learn.microsoft.com/en-us/azure/cosmos-db/consistency-levels)
-- [Azure Cosmos DB SQL API client library for Python](https://learn.microsoft.com/en-us/python/api/overview/azure/cosmos-readme?view=azure-python)
-- [CosmosClient class documentation](https://learn.microsoft.com/en-us/python/api/azure-cosmos/azure.cosmos.cosmos_client.cosmosclient?view=azure-python)
-- [Cosmos AAD Authentication](https://learn.microsoft.com/en-us/python/api/overview/azure/cosmos-readme?view=azure-python#aad-authentication)
-- [Cosmos python examples](https://learn.microsoft.com/en-us/python/api/overview/azure/cosmos-readme?view=azure-python#examples)
-- [Use control plane role-based access control with Azure Cosmos DB for NoSQL](https://learn.microsoft.com/en-us/azure/cosmos-db/nosql/security/how-to-grant-control-plane-role-based-access?tabs=built-in-definition%2Ccsharp&pivots=azure-interface-portal)
-- [Use data plane role-based access control with Azure Cosmos DB for NoSQL](https://learn.microsoft.com/en-us/azure/cosmos-db/nosql/security/how-to-grant-data-plane-role-based-access?tabs=built-in-definition%2Ccsharp&pivots=azure-interface-cli)
-- [Create or update Azure custom roles using Azure CLI](https://learn.microsoft.com/en-us/azure/role-based-access-control/custom-roles-cli)
-- [Document Intelligence query field extraction](https://learn.microsoft.com/en-us/azure/ai-services/document-intelligence/concept/query-fields?view=doc-intel-4.0.0)
-- [What's new in Azure AI Document Intelligence](https://learn.microsoft.com/en-us/azure/ai-services/document-intelligence/whats-new?view=doc-intel-4.0.0)
-- [Managed identities for Document Intelligence](https://learn.microsoft.com/en-us/azure/ai-services/document-intelligence/authentication/managed-identities?view=doc-intel-4.0.0)
-
-
-
-
-Table of Content (Click to expand)
-
-- [Important Considerations for Production Environment](#important-considerations-for-production-environment)
-- [Prerequisites](#prerequisites)
-- [Where to start?](#where-to-start)
-- [Overview](#overview)
-- [Function App Hosting Options](#function-app-hosting-options)
-- [Function App: Configure/Validate the Environment variables](#function-app-configurevalidate-the-environment-variables)
-- [Function App: Develop the logic](#function-app-develop-the-logic)
-- [Test the solution](#test-the-solution)
-
-
-
-> How to extract layout elements from PDFs stored in an Azure Storage Account, process them using Azure Document Intelligence, and store the results in Cosmos DB for further analysis.
->
-> 1. Upload your PDFs to an Azure Blob Storage container.
-> 2. An Azure Function is triggered by the upload, which calls the Azure Document Intelligence Layout API to analyze the document structure.
-> 3. The extracted layout data (such as tables, checkboxes, and text) is parsed and subsequently stored in a Cosmos DB database, ensuring a seamless and automated workflow from document upload to data storage.
-
-> [!NOTE]
-> Advantages of Document Intelligence for organizations handling with large volumes of documents:
->
-> - Utilizes natural language processing, computer vision, deep learning, and machine learning.
-> - Handles structured, semi-structured, and unstructured documents.
-> - Automates the extraction and transformation of layout data into usable formats like JSON or CSV.
-
-
-

-
-
-> [!NOTE]
-> Azure Event Grid System Topics are free to create and manage, a System Topic is automatically created and managed by Azure for certain Azure services that emit events. It represents a source of events from an Azure resource (like a Storage Account, Key Vault, or Azure Maps). `You don't need to create or manage the topic yourself, Azure does it for you when you enable event publishing on a supported resource.`
->
-> - Emits predefined event types (e.g., Microsoft.Storage.BlobCreated, Microsoft.Resources.ResourceWriteSuccess).
-> - You can attach event handlers (like Azure Functions, Logic Apps, Webhooks) to respond to these events.
-> - Works seamlessly with serverless architectures for real-time automation.
-> For example:
-> Suppose you have a Storage Account and want to trigger a function every time a new blob is uploaded:
-> - Azure automatically creates a System Topic for the Storage Account.
-> - You subscribe to the BlobCreated event.
-> - When a blob is uploaded, Event Grid routes the event to your Azure Function.
-
-
-

-
-
-## Important Considerations for Production Environment
-
-
- Private Network Configuration
-
- > For enhanced security, consider configuring your Azure resources to operate within a private network. This can be achieved using Azure Virtual Network (VNet) to isolate your resources and control inbound and outbound traffic. Implementing private endpoints for services like Azure Blob Storage and Azure Functions can further secure your data by restricting access to your VNet.
-
-
-
-
- Security
-
- > Ensure that you implement appropriate security measures when deploying this solution in a production environment. This includes:
- >
- > - Securing Access: Use Azure Entra ID (formerly known as Azure Active Directory or Azure AD) for authentication and role-based access control (RBAC) to manage permissions.
- > - Managing Secrets: Store sensitive information such as connection strings and API keys in Azure Key Vault.
- > - Data Encryption: Enable encryption for data at rest and in transit to protect sensitive information.
-
-
-
-
- Scalability
-
- > While this example provides a basic setup, you may need to scale the resources based on your specific requirements. Azure services offer various scaling options to handle increased workloads. Consider using:
- >
- > - Auto-scaling: Configure auto-scaling for Azure Functions and other services to automatically adjust based on demand.
- > - Load Balancing: Use Azure Load Balancer or Application Gateway to distribute traffic and ensure high availability.
-
-
-
-
- Cost Management
-
- > Monitor and manage the costs associated with your Azure resources. Use Azure Cost Management and Billing to track usage and optimize resource allocation.
-
-
-
-
- Compliance
-
- > Ensure that your deployment complies with relevant regulations and standards. Use Azure Policy to enforce compliance and governance policies across your resources.
-
-
-
- Disaster Recovery
-
-> Implement a disaster recovery plan to ensure business continuity in case of failures. Use Azure Site Recovery and backup solutions to protect your data and applications.
-
-
-
-## Prerequisites
-
-- An `Azure subscription is required`. All other resources, including instructions for creating a Resource Group, are provided in this workshop.
-- `Contributor role assigned or any custom role that allows`: access to manage all resources, and the ability to deploy resources within subscription.
-- If you choose to use the Terraform approach, please ensure that:
- - [Terraform is installed on your local machine](https://developer.hashicorp.com/terraform/tutorials/azure-get-started/install-cli#install-terraform).
- - [Install the Azure CLI](https://learn.microsoft.com/en-us/cli/azure/install-azure-cli) to work with both Terraform and Azure commands.
-
-## Where to start?
-
-1. Please follow the [Terraform guide](./terraform-infrastructure/) to deploy the necessary Azure resources for the workshop.
-2. Next, as this method `skips the creation of each resource` manually. Proceed with the configuration from [Configure/Validate the Environment variables](#function-app-configurevalidate-the-environment-variables).
-
-> [!IMPORTANT]
-> Regarding `Networking`, this example will cover `Public access configuration`, and `system-managed identity`. However, please ensure you `review your privacy requirements and adjust network and access settings as necessary for your specific case`.
-
-## Overview
-
-> Using Cosmos DB provides you with a flexible, scalable, and globally distributed database solution that can handle both structured and semi-structured data efficiently.
->
-> - `Azure Blob Storage`: Store the PDF invoices.
-> - `Azure Functions`: Trigger on new PDF uploads, extract data, and process it.
-> - `Azure SQL Database or Cosmos DB`: Store the extracted data for querying and analytics.
-
-| Resource | Recommendation |
-|---------------------------|----------------------------------------------------------------------------------------------------------------------|
-| **Azure Blob Storage** | Use for storing the PDF files. This keeps your file storage separate from your data storage, which is a common best practice. |
-| **Azure SQL Database** | Use if your data is highly structured and you need complex queries and transactions. |
-| **Azure Cosmos DB** | Use if you need a globally distributed database with low latency and the ability to handle semi-structured data. |
-
-## Function App Hosting Options
-
-> In the context of Azure Function Apps, a `hosting option refers to the plan you choose to run your function app`. This choice affects how your function app is scaled, the resources available to each function app instance, and the support for advanced functionalities like virtual network connectivity and container support.
-
-> [!TIP]
->
-> - `Scale to Zero`: Indicates whether the service can automatically scale down to zero instances when idle.
-> - **IDLE** stands for:
-> - **I** – Inactive
-> - **D** – During
-> - **L** – Low
-> - **E** – Engagement
-> - In other words, when the application is not actively handling requests or events (it's in a low-activity or paused state).
-> - `Scale Behavior`: Describes how the service scales (e.g., `event-driven`, `dedicated`, or `containerized`).
-> - `Virtual Networking`: Whether the service supports integration with virtual networks for secure communication.
-> - `Dedicated Compute & Reserved Cold Start`: Availability of always-on compute to avoid cold starts and ensure low latency.
-> - `Max Scale Out (Instances)`: Maximum number of instances the service can scale out to.
-> - `Example AI Use Cases`: Real-world scenarios where each plan excels.
-
-
-Flex Consumption
-
-| Feature | Description |
-|--------|-------------|
-| **Scale to Zero** | `Yes` |
-| **Scale Behavior** | `Fast event-driven` |
-| **Virtual Networking** | `Optional` |
-| **Dedicated Compute & Reserved Cold Start** | `Optional (Always Ready)` |
-| **Max Scale Out (Instances)** | `1000` |
-| **Example AI Use Cases** | `Real-time data processing` for AI models, `high-traffic AI-powered APIs`, `event-driven AI microservices`. Ideal for fraud detection, real-time recommendations, NLP, and computer vision services. |
-
-
-
-
-Consumption
-
-| Feature | Description |
-|--------|-------------|
-| **Scale to Zero** | `Yes` |
-| **Scale Behavior** | `Event-driven` |
-| **Virtual Networking** | `Optional` |
-| **Dedicated Compute & Reserved Cold Start** | `No` |
-| **Max Scale Out (Instances)** | `200` |
-| **Example AI Use Cases** | `Lightweight AI APIs`, `scheduled AI tasks`, `low-traffic AI event processing`. Great for sentiment analysis, simple image recognition, and batch ML tasks. |
-
-
-
-
-Functions Premium
-
-| Feature | Description |
-|--------|-------------|
-| **Scale to Zero** | `No` |
-| **Scale Behavior** | `Event-driven with premium options` |
-| **Virtual Networking** | `Yes` |
-| **Dedicated Compute & Reserved Cold Start** | `Yes` |
-| **Max Scale Out (Instances)** | `100` |
-| **Example AI Use Cases** | `Enterprise AI applications`, `low-latency AI APIs`, `VNet integration`. Ideal for secure, high-performance AI services like customer support and analytics. |
-
-
-
-
-App Service
-
-| Feature | Description |
-|--------|-------------|
-| **Scale to Zero** | `No` |
-| **Scale Behavior** | `Dedicated VMs` |
-| **Virtual Networking** | `Yes` |
-| **Dedicated Compute & Reserved Cold Start** | `Yes` |
-| **Max Scale Out (Instances)** | `Varies` |
-| **Example AI Use Cases** | `AI-powered web applications`, `dedicated resources`. Great for chatbots, personalized content, and intensive AI inference. |
-
-
-
-
-Container Apps Env.
-
-| Feature | Description |
-|--------|-------------|
-| **Scale to Zero** | `No` |
-| **Scale Behavior** | `Containerized microservices environment` |
-| **Virtual Networking** | `Yes` |
-| **Dedicated Compute & Reserved Cold Start** | `Yes` |
-| **Max Scale Out (Instances)** | `Varies` |
-| **Example AI Use Cases** | `AI microservices architecture`, `containerized AI workloads`, `complex AI workflows`. Ideal for orchestrating AI services like image processing, text analysis, and real-time analytics. |
-
-
-
-## Function App: Configure/Validate the Environment variables
-
-> [!NOTE]
-> This example is using system-assigned managed identity to assign RBACs (Role-based Access Control).
-
-- Under `Settings`, go to `Environment variables`. And `+ Add` the following variables:
-
- - `COSMOS_DB_ENDPOINT`: Your Cosmos DB account endpoint 🡢 `Review the existence of this, if not create it`
- - `COSMOS_DB_KEY`: Your Cosmos DB account key 🡢 `Review the existence of this, if not create it`
- - `COSMOS_DB_CONNECTION_STRING`: Your Cosmos DB connection string 🡢 `Review the existence of this, if not create it`
- - `invoicecontosostorage_STORAGE`: Your Storage Account connection string 🡢 `Review the existence of this, if not create it`
- - `FORM_RECOGNIZER_ENDPOINT`: For example: `https://.cognitiveservices.azure.com/` 🡢 `Review the existence of this, if not create it`
- - `FORM_RECOGNIZER_KEY`: Your Documment Intelligence Key (Form Recognizer). 🡢
- - `FUNCTIONS_EXTENSION_VERSION`: `~4` 🡢 `Review the existence of this, if not create it`
- - `WEBSITE_RUN_FROM_PACKAGE`: `1` 🡢 `Review the existence of this, if not create it`
- - `FUNCTIONS_WORKER_RUNTIME`: `python` 🡢 `Review the existence of this, if not create it`
- - `FUNCTIONS_NODE_BLOCK_ON_ENTRY_POINT_ERROR`: `true` (This setting ensures that all entry point errors are visible in your application insights logs). 🡢 `Review the existence of this, if not create it`
-
-
-
-
-
-
-
-
-
- - Click on `Apply` to save your configuration.
-
-
-
-## Function App: Develop the logic
-
-- You need to install [VSCode](https://code.visualstudio.com/download)
-- Install python from Microsoft store:
-
-
-
-- Open VSCode, and install some extensions: `python`, and `Azure Tools`.
-
-
-
-
-
-- Click on the `Azure` icon, and `sign in` into your account. Allow the extension `Azure Resources` to sign in using Microsoft, it will open a browser window. After doing so, you will be able to see your subscription and resources.
-
-
-
-- Under Workspace, click on `Create Function Project`, and choose a path in your local computer to develop your function.
-
-
-
-- Choose the language, in this case is `python`:
-
-
-
-- Select the model version, for this example let's use `v2`:
-
-
-
-- For the python interpreter, let's use the one installed via `Microsoft Store`:
-
-
-
-- Choose a template (e.g., **Blob trigger**) and configure it to trigger on new PDF uploads in your Blob container.
-
-
-
-- Provide a function name, like `BlobTriggerContosoPDFInvoicesDocIntelligence`:
-
-
-
-- Next, it will prompt you for the path of the blob container where you expect the function to be triggered after a file is uploaded. In this case is `pdfinvoices` as was previously created.
-
-
-
-- Click on `Create new local app settings`, and then choose your subscription.
-
-
-
-- Choose `Azure Storage Account for remote storage`, and select one. I'll be using the `invoicecontosostorage`.
-
-
-
-- Then click on `Open in the current window`. You will see something like this:
-
-
-
-- Now we need to update the function code to extract data from PDFs and store it in Cosmos DB, use this an example:
-
- > 1. **PDF Upload**: A PDF file is uploaded to the Azure Blob Storage container (`pdfinvoices`).
- > 2. **Trigger Azure Function**: The upload triggers the Azure Function `BlobTriggerContosoPDFLayoutsDocIntelligence`.
- > 3. **Initialize Clients**: Sets up connections to Azure Document Intelligence and Cosmos DB.
- > - Initializes the `DocumentAnalysisClient` using the `FORM_RECOGNIZER_ENDPOINT` and `FORM_RECOGNIZER_KEY` environment variables.
- > - Initializes the `CosmosClient` using Azure Active Directory (AAD) via `DefaultAzureCredential`.
- > 4. **Read PDF from Blob Storage**: Reads the PDF content from the blob into a byte stream.
- > 5. **Analyze PDF**: Uses Azure Document Intelligence to analyze the layout of the PDF.
- > - Calls `begin_analyze_document` with the `prebuilt-layout` model.
- > - Waits for the analysis to complete and retrieves the layout result.
- > 6. **Extract Layout Data**: Parses and structures the layout data from the analysis result.
- > - Extracts lines, tables, and selection marks from each page.
- > - Logs styles (e.g., handwritten content) and organizes data into a structured dictionary.
- > 7. **Save Data to Cosmos DB**: Saves the structured layout data to Cosmos DB.
- > - Ensures the database (`ContosoDBDocIntellig`) and container (`Layouts`) exist or creates them.
- > - Inserts or updates the layout data using `upsert_item`.
- > 8. **Logging (Process and Errors)**: Logs each step of the process, including success messages and detailed error handling for debugging and monitoring.
-
- - Update the function_app.py, for example [see the code used in this demo](./src/function_app.py):
-
- | Template Blob Trigger | Function Code updated |
- | --- | --- |
- |
|
|
-
- - Now, let's update the `requirements.txt`, [see the code used in this demo](./src/requirements.txt):
-
- | Template `requirements.txt` | Updated `requirements.txt` |
- | --- | --- |
- |
|
|
-
- - Since this function has already been tested, you can deploy your code to the function app in your subscription. If you want to test, you can use run your function locally for testing.
- - Click on the `Azure` icon.
- - Under `workspace`, click on the `Function App` icon.
- - Click on `Deploy to Azure`.
-
-
-
- - Select your `subscription`, your `function app`, and accept the prompt to overwrite:
-
-
-
- - After completing, you see the status in your terminal:
-
-
-
-
-
-> [!IMPORTANT]
-> If you need further assistance with the code, please click [here to view all the function code](./src/).
-
-> [!NOTE]
-> Please ensure that all specified roles are assigned to the Function App. The provided example used `System assigned` for the Function App to facilitate the role assignment.
-
-## Test the solution
-
-> [!IMPORTANT]
-> Please ensure that the user/system admin responsible for uploading the PDFs to the blob container has the necessary permissions. The error below illustrates what might occur if these roles are missing.
->
-> In that case, go to `Access Control (IAM)`, click on `+ Add`, and `Add role assignment`:
->
-> Search for `Storage Blob Data Contributor`, click `Next`.
->
-> Then, click on `select members` and search for your user/systen admin. Finally click on `Review + assign`.
-
-> Upload sample PDF invoices to the Blob container and verify that data is correctly ingested and stored in Cosmos DB.
-
-- Click on `Upload`, then select `Browse for files` and choose your PDF invoices to be stored in the blob container, which will trigger the function app to parse them.
-
-
-
-- Check the logs, and traces from your function with `Application Insights`:
-
-
-
-- Under `Investigate`, click on `Performance`. Filter by time range, and `drill into the samples`. Sort the results by date (if you have many, like in my case) and click on the last one.
-
-
-
-- Click on `View all`:
-
-
-
-- Check all the logs, and traces generated. Also review the information parsed:
-
-
-
-- Validate that the information was uploaded to the Cosmos DB. Under `Data Explorer`, check your `Database`.
-
-
-
-
-
-

-
Refresh Date: 2025-07-21
-
-
From 87f3945eda12ebbb033391aa14f8ce148f195d6f Mon Sep 17 00:00:00 2001
From: Timna Brown <24630902+brown9804@users.noreply.github.com>
Date: Mon, 21 Jul 2025 08:48:02 -0600
Subject: [PATCH 4/8] updated
---
README.md | 446 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 446 insertions(+)
diff --git a/README.md b/README.md
index e69de29..c85ac63 100644
--- a/README.md
+++ b/README.md
@@ -0,0 +1,446 @@
+# Demo: PDF Layout Extraction with Doc Intelligence (full-code approach)
+
+`Azure Storage + Document Intelligence + Function App + Cosmos DB`
+
+Costa Rica
+
+[](https://github.com)
+[](https://github.com/)
+[brown9804](https://github.com/brown9804)
+
+Last updated: 2025-07-16
+
+----------
+
+> [!IMPORTANT]
+> This example is based on a `public network site and is intended for demonstration purposes only`. It showcases how several Azure resources can work together to achieve the desired result. Consider the section below about [Important Considerations for Production Environment](#important-considerations-for-production-environment). Please note that `these demos are intended as a guide and are based on my personal experiences. For official guidance, support, or more detailed information, please refer to Microsoft's official documentation or contact Microsoft directly`: [Microsoft Sales and Support](https://support.microsoft.com/contactus?ContactUsExperienceEntryPointAssetId=S.HP.SMC-HOME)
+
+
+List of References (Click to expand)
+
+- [Use Azure AI services with SynapseML in Microsoft Fabric](https://learn.microsoft.com/en-us/fabric/data-science/how-to-use-ai-services-with-synapseml)
+- [Plan and manage costs for Azure AI Foundry](https://learn.microsoft.com/en-us/azure/ai-foundry/how-to/costs-plan-manage)
+- [Azure AI Document Intelligence documentation](https://learn.microsoft.com/en-us/azure/ai-services/document-intelligence/?view=doc-intel-4.0.0)
+- [Get started with the Document Intelligence Sample Labeling tool](https://learn.microsoft.com/en-us/azure/ai-services/document-intelligence/v21/try-sample-label-tool?view=doc-intel-2.1.0#prerequisites-for-training-a-custom-form-model)
+- [Document Intelligence Sample Labeling tool](https://fott-2-1.azurewebsites.net/)
+- [Assign an Azure role for access to blob data](https://learn.microsoft.com/en-us/azure/storage/blobs/assign-azure-role-data-access?tabs=portal)
+- [Build and train a custom extraction model](https://learn.microsoft.com/en-us/azure/ai-services/document-intelligence/how-to-guides/build-a-custom-model?view=doc-intel-2.1.0)
+- [Compose custom models - Document Intelligence](https://learn.microsoft.com/en-us/azure/ai-services/document-intelligence/how-to-guides/compose-custom-models?view=doc-intel-2.1.0&tabs=studio)
+- [Deploy the Sample Labeling tool](https://learn.microsoft.com/en-us/azure/ai-services/document-intelligence/v21/deploy-label-tool?view=doc-intel-2.1.0)
+- [Train a custom model using the Sample Labeling tool](https://learn.microsoft.com/en-us/azure/ai-services/document-intelligence/v21/label-tool?view=doc-intel-2.1.0)
+- [Train models with the sample-labeling tool](https://learn.microsoft.com/en-us/azure/ai-services/document-intelligence/v21/supervised-table-tags?view=doc-intel-2.1.0)
+- [Azure Cosmos DB - Database for the AI Era](https://learn.microsoft.com/en-us/azure/cosmos-db/introduction)
+- [Consistency levels in Azure Cosmos DB](https://learn.microsoft.com/en-us/azure/cosmos-db/consistency-levels)
+- [Azure Cosmos DB SQL API client library for Python](https://learn.microsoft.com/en-us/python/api/overview/azure/cosmos-readme?view=azure-python)
+- [CosmosClient class documentation](https://learn.microsoft.com/en-us/python/api/azure-cosmos/azure.cosmos.cosmos_client.cosmosclient?view=azure-python)
+- [Cosmos AAD Authentication](https://learn.microsoft.com/en-us/python/api/overview/azure/cosmos-readme?view=azure-python#aad-authentication)
+- [Cosmos python examples](https://learn.microsoft.com/en-us/python/api/overview/azure/cosmos-readme?view=azure-python#examples)
+- [Use control plane role-based access control with Azure Cosmos DB for NoSQL](https://learn.microsoft.com/en-us/azure/cosmos-db/nosql/security/how-to-grant-control-plane-role-based-access?tabs=built-in-definition%2Ccsharp&pivots=azure-interface-portal)
+- [Use data plane role-based access control with Azure Cosmos DB for NoSQL](https://learn.microsoft.com/en-us/azure/cosmos-db/nosql/security/how-to-grant-data-plane-role-based-access?tabs=built-in-definition%2Ccsharp&pivots=azure-interface-cli)
+- [Create or update Azure custom roles using Azure CLI](https://learn.microsoft.com/en-us/azure/role-based-access-control/custom-roles-cli)
+- [Document Intelligence query field extraction](https://learn.microsoft.com/en-us/azure/ai-services/document-intelligence/concept/query-fields?view=doc-intel-4.0.0)
+- [What's new in Azure AI Document Intelligence](https://learn.microsoft.com/en-us/azure/ai-services/document-intelligence/whats-new?view=doc-intel-4.0.0)
+- [Managed identities for Document Intelligence](https://learn.microsoft.com/en-us/azure/ai-services/document-intelligence/authentication/managed-identities?view=doc-intel-4.0.0)
+
+
+
+
+Table of Content (Click to expand)
+
+- [Important Considerations for Production Environment](#important-considerations-for-production-environment)
+- [Prerequisites](#prerequisites)
+- [Where to start?](#where-to-start)
+- [Overview](#overview)
+- [Function App Hosting Options](#function-app-hosting-options)
+- [Function App: Configure/Validate the Environment variables](#function-app-configurevalidate-the-environment-variables)
+- [Function App: Develop the logic](#function-app-develop-the-logic)
+- [Test the solution](#test-the-solution)
+
+
+
+> How to extract layout elements from PDFs stored in an Azure Storage Account, process them using Azure Document Intelligence, and store the results in Cosmos DB for further analysis.
+>
+> 1. Upload your PDFs to an Azure Blob Storage container.
+> 2. An Azure Function is triggered by the upload, which calls the Azure Document Intelligence Layout API to analyze the document structure.
+> 3. The extracted layout data (such as tables, checkboxes, and text) is parsed and subsequently stored in a Cosmos DB database, ensuring a seamless and automated workflow from document upload to data storage.
+
+> [!NOTE]
+> Advantages of Document Intelligence for organizations handling with large volumes of documents:
+>
+> - Utilizes natural language processing, computer vision, deep learning, and machine learning.
+> - Handles structured, semi-structured, and unstructured documents.
+> - Automates the extraction and transformation of layout data into usable formats like JSON or CSV.
+
+
+

+
+
+> [!NOTE]
+> Azure Event Grid System Topics are free to create and manage, a System Topic is automatically created and managed by Azure for certain Azure services that emit events. It represents a source of events from an Azure resource (like a Storage Account, Key Vault, or Azure Maps). `You don't need to create or manage the topic yourself, Azure does it for you when you enable event publishing on a supported resource.`
+>
+> - Emits predefined event types (e.g., Microsoft.Storage.BlobCreated, Microsoft.Resources.ResourceWriteSuccess).
+> - You can attach event handlers (like Azure Functions, Logic Apps, Webhooks) to respond to these events.
+> - Works seamlessly with serverless architectures for real-time automation.
+> For example:
+> Suppose you have a Storage Account and want to trigger a function every time a new blob is uploaded:
+> - Azure automatically creates a System Topic for the Storage Account.
+> - You subscribe to the BlobCreated event.
+> - When a blob is uploaded, Event Grid routes the event to your Azure Function.
+
+
+

+
+
+## Important Considerations for Production Environment
+
+
+ Private Network Configuration
+
+ > For enhanced security, consider configuring your Azure resources to operate within a private network. This can be achieved using Azure Virtual Network (VNet) to isolate your resources and control inbound and outbound traffic. Implementing private endpoints for services like Azure Blob Storage and Azure Functions can further secure your data by restricting access to your VNet.
+
+
+
+
+ Security
+
+ > Ensure that you implement appropriate security measures when deploying this solution in a production environment. This includes:
+ >
+ > - Securing Access: Use Azure Entra ID (formerly known as Azure Active Directory or Azure AD) for authentication and role-based access control (RBAC) to manage permissions.
+ > - Managing Secrets: Store sensitive information such as connection strings and API keys in Azure Key Vault.
+ > - Data Encryption: Enable encryption for data at rest and in transit to protect sensitive information.
+
+
+
+
+ Scalability
+
+ > While this example provides a basic setup, you may need to scale the resources based on your specific requirements. Azure services offer various scaling options to handle increased workloads. Consider using:
+ >
+ > - Auto-scaling: Configure auto-scaling for Azure Functions and other services to automatically adjust based on demand.
+ > - Load Balancing: Use Azure Load Balancer or Application Gateway to distribute traffic and ensure high availability.
+
+
+
+
+ Cost Management
+
+ > Monitor and manage the costs associated with your Azure resources. Use Azure Cost Management and Billing to track usage and optimize resource allocation.
+
+
+
+
+ Compliance
+
+ > Ensure that your deployment complies with relevant regulations and standards. Use Azure Policy to enforce compliance and governance policies across your resources.
+
+
+
+ Disaster Recovery
+
+> Implement a disaster recovery plan to ensure business continuity in case of failures. Use Azure Site Recovery and backup solutions to protect your data and applications.
+
+
+
+## Prerequisites
+
+- An `Azure subscription is required`. All other resources, including instructions for creating a Resource Group, are provided in this workshop.
+- `Contributor role assigned or any custom role that allows`: access to manage all resources, and the ability to deploy resources within subscription.
+- If you choose to use the Terraform approach, please ensure that:
+ - [Terraform is installed on your local machine](https://developer.hashicorp.com/terraform/tutorials/azure-get-started/install-cli#install-terraform).
+ - [Install the Azure CLI](https://learn.microsoft.com/en-us/cli/azure/install-azure-cli) to work with both Terraform and Azure commands.
+
+## Where to start?
+
+1. Please follow the [Terraform guide](./terraform-infrastructure/) to deploy the necessary Azure resources for the workshop.
+2. Next, as this method `skips the creation of each resource` manually. Proceed with the configuration from [Configure/Validate the Environment variables](#function-app-configurevalidate-the-environment-variables).
+
+> [!IMPORTANT]
+> Regarding `Networking`, this example will cover `Public access configuration`, and `system-managed identity`. However, please ensure you `review your privacy requirements and adjust network and access settings as necessary for your specific case`.
+
+## Overview
+
+> Using Cosmos DB provides you with a flexible, scalable, and globally distributed database solution that can handle both structured and semi-structured data efficiently.
+>
+> - `Azure Blob Storage`: Store the PDF invoices.
+> - `Azure Functions`: Trigger on new PDF uploads, extract data, and process it.
+> - `Azure SQL Database or Cosmos DB`: Store the extracted data for querying and analytics.
+
+| Resource | Recommendation |
+|---------------------------|----------------------------------------------------------------------------------------------------------------------|
+| **Azure Blob Storage** | Use for storing the PDF files. This keeps your file storage separate from your data storage, which is a common best practice. |
+| **Azure SQL Database** | Use if your data is highly structured and you need complex queries and transactions. |
+| **Azure Cosmos DB** | Use if you need a globally distributed database with low latency and the ability to handle semi-structured data. |
+
+## Function App Hosting Options
+
+> In the context of Azure Function Apps, a `hosting option refers to the plan you choose to run your function app`. This choice affects how your function app is scaled, the resources available to each function app instance, and the support for advanced functionalities like virtual network connectivity and container support.
+
+> [!TIP]
+>
+> - `Scale to Zero`: Indicates whether the service can automatically scale down to zero instances when idle.
+> - **IDLE** stands for:
+> - **I** – Inactive
+> - **D** – During
+> - **L** – Low
+> - **E** – Engagement
+> - In other words, when the application is not actively handling requests or events (it's in a low-activity or paused state).
+> - `Scale Behavior`: Describes how the service scales (e.g., `event-driven`, `dedicated`, or `containerized`).
+> - `Virtual Networking`: Whether the service supports integration with virtual networks for secure communication.
+> - `Dedicated Compute & Reserved Cold Start`: Availability of always-on compute to avoid cold starts and ensure low latency.
+> - `Max Scale Out (Instances)`: Maximum number of instances the service can scale out to.
+> - `Example AI Use Cases`: Real-world scenarios where each plan excels.
+
+
+Flex Consumption
+
+| Feature | Description |
+|--------|-------------|
+| **Scale to Zero** | `Yes` |
+| **Scale Behavior** | `Fast event-driven` |
+| **Virtual Networking** | `Optional` |
+| **Dedicated Compute & Reserved Cold Start** | `Optional (Always Ready)` |
+| **Max Scale Out (Instances)** | `1000` |
+| **Example AI Use Cases** | `Real-time data processing` for AI models, `high-traffic AI-powered APIs`, `event-driven AI microservices`. Ideal for fraud detection, real-time recommendations, NLP, and computer vision services. |
+
+
+
+
+Consumption
+
+| Feature | Description |
+|--------|-------------|
+| **Scale to Zero** | `Yes` |
+| **Scale Behavior** | `Event-driven` |
+| **Virtual Networking** | `Optional` |
+| **Dedicated Compute & Reserved Cold Start** | `No` |
+| **Max Scale Out (Instances)** | `200` |
+| **Example AI Use Cases** | `Lightweight AI APIs`, `scheduled AI tasks`, `low-traffic AI event processing`. Great for sentiment analysis, simple image recognition, and batch ML tasks. |
+
+
+
+
+Functions Premium
+
+| Feature | Description |
+|--------|-------------|
+| **Scale to Zero** | `No` |
+| **Scale Behavior** | `Event-driven with premium options` |
+| **Virtual Networking** | `Yes` |
+| **Dedicated Compute & Reserved Cold Start** | `Yes` |
+| **Max Scale Out (Instances)** | `100` |
+| **Example AI Use Cases** | `Enterprise AI applications`, `low-latency AI APIs`, `VNet integration`. Ideal for secure, high-performance AI services like customer support and analytics. |
+
+
+
+
+App Service
+
+| Feature | Description |
+|--------|-------------|
+| **Scale to Zero** | `No` |
+| **Scale Behavior** | `Dedicated VMs` |
+| **Virtual Networking** | `Yes` |
+| **Dedicated Compute & Reserved Cold Start** | `Yes` |
+| **Max Scale Out (Instances)** | `Varies` |
+| **Example AI Use Cases** | `AI-powered web applications`, `dedicated resources`. Great for chatbots, personalized content, and intensive AI inference. |
+
+
+
+
+Container Apps Env.
+
+| Feature | Description |
+|--------|-------------|
+| **Scale to Zero** | `No` |
+| **Scale Behavior** | `Containerized microservices environment` |
+| **Virtual Networking** | `Yes` |
+| **Dedicated Compute & Reserved Cold Start** | `Yes` |
+| **Max Scale Out (Instances)** | `Varies` |
+| **Example AI Use Cases** | `AI microservices architecture`, `containerized AI workloads`, `complex AI workflows`. Ideal for orchestrating AI services like image processing, text analysis, and real-time analytics. |
+
+
+
+## Function App: Configure/Validate the Environment variables
+
+> [!NOTE]
+> This example is using system-assigned managed identity to assign RBACs (Role-based Access Control).
+
+- Under `Settings`, go to `Environment variables`. And `+ Add` the following variables:
+
+ - `COSMOS_DB_ENDPOINT`: Your Cosmos DB account endpoint 🡢 `Review the existence of this, if not create it`
+ - `COSMOS_DB_KEY`: Your Cosmos DB account key 🡢 `Review the existence of this, if not create it`
+ - `COSMOS_DB_CONNECTION_STRING`: Your Cosmos DB connection string 🡢 `Review the existence of this, if not create it`
+ - `invoicecontosostorage_STORAGE`: Your Storage Account connection string 🡢 `Review the existence of this, if not create it`
+ - `FORM_RECOGNIZER_ENDPOINT`: For example: `https://.cognitiveservices.azure.com/` 🡢 `Review the existence of this, if not create it`
+ - `FORM_RECOGNIZER_KEY`: Your Documment Intelligence Key (Form Recognizer). 🡢
+ - `FUNCTIONS_EXTENSION_VERSION`: `~4` 🡢 `Review the existence of this, if not create it`
+ - `WEBSITE_RUN_FROM_PACKAGE`: `1` 🡢 `Review the existence of this, if not create it`
+ - `FUNCTIONS_WORKER_RUNTIME`: `python` 🡢 `Review the existence of this, if not create it`
+ - `FUNCTIONS_NODE_BLOCK_ON_ENTRY_POINT_ERROR`: `true` (This setting ensures that all entry point errors are visible in your application insights logs). 🡢 `Review the existence of this, if not create it`
+
+
+
+
+
+
+
+
+
+ - Click on `Apply` to save your configuration.
+
+
+
+## Function App: Develop the logic
+
+- You need to install [VSCode](https://code.visualstudio.com/download)
+- Install python from Microsoft store:
+
+
+
+- Open VSCode, and install some extensions: `python`, and `Azure Tools`.
+
+
+
+
+
+- Click on the `Azure` icon, and `sign in` into your account. Allow the extension `Azure Resources` to sign in using Microsoft, it will open a browser window. After doing so, you will be able to see your subscription and resources.
+
+
+
+- Under Workspace, click on `Create Function Project`, and choose a path in your local computer to develop your function.
+
+
+
+- Choose the language, in this case is `python`:
+
+
+
+- Select the model version, for this example let's use `v2`:
+
+
+
+- For the python interpreter, let's use the one installed via `Microsoft Store`:
+
+
+
+- Choose a template (e.g., **Blob trigger**) and configure it to trigger on new PDF uploads in your Blob container.
+
+
+
+- Provide a function name, like `BlobTriggerContosoPDFInvoicesDocIntelligence`:
+
+
+
+- Next, it will prompt you for the path of the blob container where you expect the function to be triggered after a file is uploaded. In this case is `pdfinvoices` as was previously created.
+
+
+
+- Click on `Create new local app settings`, and then choose your subscription.
+
+
+
+- Choose `Azure Storage Account for remote storage`, and select one. I'll be using the `invoicecontosostorage`.
+
+
+
+- Then click on `Open in the current window`. You will see something like this:
+
+
+
+- Now we need to update the function code to extract data from PDFs and store it in Cosmos DB, use this an example:
+
+ > 1. **PDF Upload**: A PDF file is uploaded to the Azure Blob Storage container (`pdfinvoices`).
+ > 2. **Trigger Azure Function**: The upload triggers the Azure Function `BlobTriggerContosoPDFLayoutsDocIntelligence`.
+ > 3. **Initialize Clients**: Sets up connections to Azure Document Intelligence and Cosmos DB.
+ > - Initializes the `DocumentAnalysisClient` using the `FORM_RECOGNIZER_ENDPOINT` and `FORM_RECOGNIZER_KEY` environment variables.
+ > - Initializes the `CosmosClient` using Azure Active Directory (AAD) via `DefaultAzureCredential`.
+ > 4. **Read PDF from Blob Storage**: Reads the PDF content from the blob into a byte stream.
+ > 5. **Analyze PDF**: Uses Azure Document Intelligence to analyze the layout of the PDF.
+ > - Calls `begin_analyze_document` with the `prebuilt-layout` model.
+ > - Waits for the analysis to complete and retrieves the layout result.
+ > 6. **Extract Layout Data**: Parses and structures the layout data from the analysis result.
+ > - Extracts lines, tables, and selection marks from each page.
+ > - Logs styles (e.g., handwritten content) and organizes data into a structured dictionary.
+ > 7. **Save Data to Cosmos DB**: Saves the structured layout data to Cosmos DB.
+ > - Ensures the database (`ContosoDBDocIntellig`) and container (`Layouts`) exist or creates them.
+ > - Inserts or updates the layout data using `upsert_item`.
+ > 8. **Logging (Process and Errors)**: Logs each step of the process, including success messages and detailed error handling for debugging and monitoring.
+
+ - Update the function_app.py, for example [see the code used in this demo](./src/function_app.py):
+
+ | Template Blob Trigger | Function Code updated |
+ | --- | --- |
+ |
|
|
+
+ - Now, let's update the `requirements.txt`, [see the code used in this demo](./src/requirements.txt):
+
+ | Template `requirements.txt` | Updated `requirements.txt` |
+ | --- | --- |
+ |
|
|
+
+ - Since this function has already been tested, you can deploy your code to the function app in your subscription. If you want to test, you can use run your function locally for testing.
+ - Click on the `Azure` icon.
+ - Under `workspace`, click on the `Function App` icon.
+ - Click on `Deploy to Azure`.
+
+
+
+ - Select your `subscription`, your `function app`, and accept the prompt to overwrite:
+
+
+
+ - After completing, you see the status in your terminal:
+
+
+
+
+
+> [!IMPORTANT]
+> If you need further assistance with the code, please click [here to view all the function code](./src/).
+
+> [!NOTE]
+> Please ensure that all specified roles are assigned to the Function App. The provided example used `System assigned` for the Function App to facilitate the role assignment.
+
+## Test the solution
+
+> [!IMPORTANT]
+> Please ensure that the user/system admin responsible for uploading the PDFs to the blob container has the necessary permissions. The error below illustrates what might occur if these roles are missing.
+>
+> In that case, go to `Access Control (IAM)`, click on `+ Add`, and `Add role assignment`:
+>
+> Search for `Storage Blob Data Contributor`, click `Next`.
+>
+> Then, click on `select members` and search for your user/systen admin. Finally click on `Review + assign`.
+
+> Upload sample PDF invoices to the Blob container and verify that data is correctly ingested and stored in Cosmos DB.
+
+- Click on `Upload`, then select `Browse for files` and choose your PDF invoices to be stored in the blob container, which will trigger the function app to parse them.
+
+
+
+- Check the logs, and traces from your function with `Application Insights`:
+
+
+
+- Under `Investigate`, click on `Performance`. Filter by time range, and `drill into the samples`. Sort the results by date (if you have many, like in my case) and click on the last one.
+
+
+
+- Click on `View all`:
+
+
+
+- Check all the logs, and traces generated. Also review the information parsed:
+
+
+
+- Validate that the information was uploaded to the Cosmos DB. Under `Data Explorer`, check your `Database`.
+
+
+
+
+
+

+
Refresh Date: 2025-07-16
+
+
From 2a2b52ff25825dc349b349343a996542fa4f89a6 Mon Sep 17 00:00:00 2001
From: Timna Brown <24630902+brown9804@users.noreply.github.com>
Date: Mon, 21 Jul 2025 08:49:44 -0600
Subject: [PATCH 5/8] adding diagrams
---
...outprocessingFunctionAppDocIntellig.drawio | 95 +++++++++++++++++++
1 file changed, 95 insertions(+)
diff --git a/docs/automatedPDFLayoutprocessingFunctionAppDocIntellig.drawio b/docs/automatedPDFLayoutprocessingFunctionAppDocIntellig.drawio
index e69de29..8a5e0fc 100644
--- a/docs/automatedPDFLayoutprocessingFunctionAppDocIntellig.drawio
+++ b/docs/automatedPDFLayoutprocessingFunctionAppDocIntellig.drawio
@@ -0,0 +1,95 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
From 51cfed346fc29b360553b455c511852e64324b1b Mon Sep 17 00:00:00 2001
From: Timna Brown <24630902+brown9804@users.noreply.github.com>
Date: Mon, 21 Jul 2025 08:54:03 -0600
Subject: [PATCH 6/8] original state
---
src/function_app.py | 67 +++--------------------
terraform-infrastructure/README.md | 6 +-
terraform-infrastructure/main.tf | 22 +-------
terraform-infrastructure/terraform.tfvars | 25 +++------
terraform-infrastructure/variables.tf | 20 -------
5 files changed, 19 insertions(+), 121 deletions(-)
diff --git a/src/function_app.py b/src/function_app.py
index 2efed7e..370c713 100644
--- a/src/function_app.py
+++ b/src/function_app.py
@@ -8,12 +8,6 @@
import uuid
import json
-# For image conversion and vision API
-from typing import List
-from io import BytesIO
-import requests # For REST API to Vision
-from pdf2image import convert_from_bytes # For PDF to image conversion
-
app = func.FunctionApp(http_auth_level=func.AuthLevel.FUNCTION)
## DEFINITIONS
@@ -41,14 +35,13 @@ def analyze_pdf(form_recognizer_client, pdf_bytes):
logging.info(f"Document has {len(result.pages)} page(s), {len(result.tables)} table(s), and {len(result.styles)} style(s).")
return result
-def extract_layout_data(result, visual_cues: List[dict] = None):
+def extract_layout_data(result):
logging.info("Extracting layout data from analysis result.")
layout_data = {
"id": str(uuid.uuid4()),
"pages": []
}
- visual_cues = visual_cues or [] # List of dicts with visual cue info per cell
# Log styles
for idx, style in enumerate(result.styles):
@@ -95,16 +88,12 @@ def extract_layout_data(result, visual_cues: List[dict] = None):
for cell in table.cells:
content = cell.content.strip()
- # Find matching visual cue for this cell (if any)
- cue = next((vc for vc in visual_cues if vc.get("page_number") == page.page_number and vc.get("row_index") == cell.row_index and vc.get("column_index") == cell.column_index), None)
- cell_info = {
+ table_data["cells"].append({
"row_index": cell.row_index,
"column_index": cell.column_index,
- "content": content,
- "visual_cue": cue["cue_type"] if cue else None
- }
- table_data["cells"].append(cell_info)
- logging.info(f"Cell[{cell.row_index}][{cell.column_index}]: '{content}', visual_cue: {cell_info['visual_cue']}")
+ "content": content
+ })
+ logging.info(f"Cell[{cell.row_index}][{cell.column_index}]: '{content}'")
page_data["tables"].append(table_data)
@@ -167,31 +156,6 @@ def save_layout_data_to_cosmos(layout_data):
## MAIN
@app.blob_trigger(arg_name="myblob", path="pdfinvoices/{name}",
connection="invoicecontosostorage_STORAGE")
-def call_vision_api(image_bytes, subscription_key, endpoint):
- vision_url = endpoint + "/vision/v3.2/analyze"
- headers = {
- 'Ocp-Apim-Subscription-Key': subscription_key,
- 'Content-Type': 'application/octet-stream'
- }
- params = {
- 'visualFeatures': 'Objects,Color', # Add more features if needed
- }
- response = requests.post(vision_url, headers=headers, params=params, data=image_bytes)
- response.raise_for_status()
- return response.json()
-
-def extract_visual_cues_from_vision(vision_result, page_number):
- # Example: Detect gray fills, checkmarks, hand-drawn marks
- cues = []
- # This is a placeholder. You need to parse vision_result for your cues.
- # For example, if vision_result['objects'] contains a 'checkmark' or color info for gray fill
- # cues.append({"page_number": page_number, "row_index": ..., "column_index": ..., "cue_type": "gray_fill"})
- return cues
-
-def convert_pdf_to_images(pdf_bytes):
- images = convert_from_bytes(pdf_bytes)
- return images
-
def BlobTriggerContosoPDFLayoutsDocIntelligence(myblob: func.InputStream):
logging.info(f"Python blob trigger function processed blob\n"
f"Name: {myblob.name}\n"
@@ -212,26 +176,9 @@ def BlobTriggerContosoPDFLayoutsDocIntelligence(myblob: func.InputStream):
logging.error(f"Error analyzing PDF: {e}")
return
- # --- Step: Convert PDF to image and call Azure AI Vision ---
- visual_cues = []
- try:
- images = convert_pdf_to_images(pdf_bytes)
- vision_key = os.getenv("VISION_API_KEY")
- vision_endpoint = os.getenv("VISION_API_ENDPOINT")
- for page_num, image in enumerate(images, start=1):
- img_bytes_io = BytesIO()
- image.save(img_bytes_io, format='JPEG')
- img_bytes = img_bytes_io.getvalue()
- vision_result = call_vision_api(img_bytes, vision_key, vision_endpoint)
- cues = extract_visual_cues_from_vision(vision_result, page_num)
- visual_cues.extend(cues)
- logging.info(f"Visual cues extracted: {visual_cues}")
- except Exception as e:
- logging.error(f"Error processing visual cues with AI Vision: {e}")
-
try:
- layout_data = extract_layout_data(result, visual_cues)
- logging.info("Successfully extracted and merged layout data.")
+ layout_data = extract_layout_data(result)
+ logging.info("Successfully extracted layout data.")
except Exception as e:
logging.error(f"Error extracting layout data: {e}")
return
diff --git a/terraform-infrastructure/README.md b/terraform-infrastructure/README.md
index 4fc1bbc..8a56aa7 100644
--- a/terraform-infrastructure/README.md
+++ b/terraform-infrastructure/README.md
@@ -5,7 +5,7 @@ Costa Rica
[](https://github.com/)
[brown9804](https://github.com/brown9804)
-Last updated: 2025-07-21
+Last updated: 2025-07-16
----------
@@ -109,7 +109,7 @@ graph TD;
-

-
Refresh Date: 2025-07-21
+

+
Refresh Date: 2025-07-16
diff --git a/terraform-infrastructure/main.tf b/terraform-infrastructure/main.tf
index e476224..956b5cb 100644
--- a/terraform-infrastructure/main.tf
+++ b/terraform-infrastructure/main.tf
@@ -289,7 +289,6 @@ resource "azurerm_role_assignment" "contributor" {
]
}
-
# Azure Form Recognizer (Document Intelligence)
resource "azurerm_cognitive_account" "form_recognizer" {
name = var.form_recognizer_name
@@ -300,27 +299,12 @@ resource "azurerm_cognitive_account" "form_recognizer" {
depends_on = [azurerm_resource_group.rg]
+ # Output the Form Recognizer name
provisioner "local-exec" {
command = "echo Form Recognizer: ${self.name}"
}
}
-# Azure AI Vision (Cognitive Services)
-resource "azurerm_cognitive_account" "ai_vision" {
- name = var.ai_vision_name
- location = azurerm_resource_group.rg.location
- resource_group_name = azurerm_resource_group.rg.name
- kind = "CognitiveServices"
- sku_name = var.ai_vision_sku
- tags = var.ai_vision_tags
-
- depends_on = [azurerm_resource_group.rg]
-
- provisioner "local-exec" {
- command = "echo AI Vision: ${self.name}"
- }
-}
-
# We need to assign custom or built-in Cosmos DB SQL roles
# (like Cosmos DB Built-in Data Reader, etc.) at the data plane level,
# which is not currently supported directly in Terraform as of now.
@@ -389,10 +373,6 @@ resource "azurerm_linux_function_app" "function_app" {
"APPINSIGHTS_INSTRUMENTATIONKEY" = azurerm_application_insights.appinsights.instrumentation_key
"APPLICATIONINSIGHTS_CONNECTION_STRING" = azurerm_application_insights.appinsights.connection_string
-
- # Azure AI Vision settings
- "VISION_API_ENDPOINT" = azurerm_cognitive_account.ai_vision.endpoint
- "VISION_API_KEY" = azurerm_cognitive_account.ai_vision.primary_access_key
}
depends_on = [
diff --git a/terraform-infrastructure/terraform.tfvars b/terraform-infrastructure/terraform.tfvars
index 2f978b5..12a8bea 100644
--- a/terraform-infrastructure/terraform.tfvars
+++ b/terraform-infrastructure/terraform.tfvars
@@ -1,30 +1,21 @@
# Sample values
-subscription_id = "407f4106-0fd3-42e0-9348-3686dd1e7347" # "your-subscription_id"
-resource_group_name = "RG-PDFLayout-Processing-DocIntelligence" # "your-resource-group-name"
-location = "West US" # "your-location"
+subscription_id = "" # "your-subscription_id"
+resource_group_name = "RG-PDFLayout-Processing-DocIntelligence" # "your-resource-group-name"
+location = "West US" # "your-location"
# Storage Account
-storage_account_name = "storageaccbrownpdfix2" # "your-storage-account-name"
+storage_account_name = "storageaccbrownpdfix2" # "your-storage-account-name"
storage_account_name_runtime = "runtimestorebrownix2" # "your-runtime-storage-account-name"
# Function App
-function_app_name = "fapdfbrownix2" # "your-function-app-name"
+function_app_name = "fapdfbrownix2" # "your-function-app-name"
# App Service Plan
app_service_plan_name = "asppdfbrownix2" # "your-app-service-plan-name"
# Application Insights
-app_insights_name = "apppdfbrownix2" # "your-app-insights-name"
+app_insights_name = "apppdfbrownix2" # "your-app-insights-name"
# Log Analytics Workspace
log_analytics_workspace_name = "logwspdfbrownix2" # "your-log-analytics-workspace-name"
# Key Vault
-key_vault_name = "kvpdfrbrownrix2" # "your-key-vault-name"
+key_vault_name = "kvpdfrbrownix2" # "your-key-vault-name"
# CosmosDB
cosmosdb_account_name = "cosmospdfbrownix2" # "your-cosmosdb-account-name"
# Form Recognizer -> Document Intelligence
-form_recognizer_name = "docintelligt01ix2" # "your-document-intelligence-name"
-
-# AI Vision Service
-ai_vision_name = "aivisionpdfrbrownix2" # "your-ai-vision-name"
-ai_vision_sku = "S0"
-ai_vision_tags = {
- Environment = "Development"
- Project = "PDF Processing"
- Service = "AI Vision"
-}
+form_recognizer_name = "docintelligt01ix2" # "your-document-intelligence-name"
diff --git a/terraform-infrastructure/variables.tf b/terraform-infrastructure/variables.tf
index 33a04dd..a9ea20a 100644
--- a/terraform-infrastructure/variables.tf
+++ b/terraform-infrastructure/variables.tf
@@ -48,26 +48,6 @@ variable "key_vault_name" {
description = "The name of the Key Vault"
type = string
}
-
-variable "ai_vision_name" {
- description = "The name of the AI Vision Cognitive Services account"
- type = string
-}
-
-variable "ai_vision_sku" {
- description = "The SKU of the AI Vision Cognitive Services account"
- type = string
- default = "S0"
-}
-
-variable "ai_vision_tags" {
- description = "Tags to be applied to the AI Vision resource"
- type = map(string)
- default = {
- Environment = "Development"
- Service = "AI Vision"
- }
-}
variable "cosmosdb_account_name" {
description = "The name of the CosmosDB account."
type = string
From 0995adb7800b39793b1c4401fe22e2c668afc827 Mon Sep 17 00:00:00 2001
From: "github-actions[bot]"
Date: Mon, 21 Jul 2025 14:54:45 +0000
Subject: [PATCH 7/8] Update last modified date in Markdown files
---
terraform-infrastructure/README.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/terraform-infrastructure/README.md b/terraform-infrastructure/README.md
index 8a56aa7..144cb71 100644
--- a/terraform-infrastructure/README.md
+++ b/terraform-infrastructure/README.md
@@ -5,7 +5,7 @@ Costa Rica
[](https://github.com/)
[brown9804](https://github.com/brown9804)
-Last updated: 2025-07-16
+Last updated: 2025-07-21
----------
From 7b7d2031851d72b78b4f614df2b6e5f69d103ed7 Mon Sep 17 00:00:00 2001
From: "github-actions[bot]"
Date: Mon, 21 Jul 2025 14:54:46 +0000
Subject: [PATCH 8/8] Update visitor count
---
README.md | 4 ++--
terraform-infrastructure/README.md | 4 ++--
2 files changed, 4 insertions(+), 4 deletions(-)
diff --git a/README.md b/README.md
index c85ac63..210e09c 100644
--- a/README.md
+++ b/README.md
@@ -440,7 +440,7 @@ Last updated: 2025-07-16
-

-
Refresh Date: 2025-07-16
+

+
Refresh Date: 2025-07-21
diff --git a/terraform-infrastructure/README.md b/terraform-infrastructure/README.md
index 8a56aa7..5524177 100644
--- a/terraform-infrastructure/README.md
+++ b/terraform-infrastructure/README.md
@@ -109,7 +109,7 @@ graph TD;
-

-
Refresh Date: 2025-07-16
+

+
Refresh Date: 2025-07-21