Skip to content

Commit

Permalink
Commit from GitHub Actions (Generate Site)
Browse files Browse the repository at this point in the history
  • Loading branch information
pkbullock committed Jan 27, 2025
1 parent 6ba52e9 commit 36b88ea
Show file tree
Hide file tree
Showing 10 changed files with 8,209 additions and 7,719 deletions.
311 changes: 311 additions & 0 deletions azure-ocr-openai-json-invoice-extractor/README.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,311 @@
<!DOCTYPE html>
<!--[if IE]><![endif]-->
<html lang="en">

<head>
<meta charset="utf-8">
<title>Extract Invoice Details from Any File Format Using Azure OCR and OpenAI | PnP Samples </title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" "="">
<meta name="title" content="Extract Invoice Details from Any File Format Using Azure OCR and OpenAI | PnP Samples ">
<meta name="generator" content="docfx 2.56.7.0">
<meta name="description" content="A sample gallery of scripts to manage all things Microsoft 365.">

<!-- Social Media -->
<meta property="og:locale" content="en_GB">
<meta property="og:title" content="Extract Invoice Details from Any File Format Using Azure OCR and OpenAI | PnP Samples ">
<meta property="og:type" content="website">
<meta property="og:url" content="https://pnp.github.io/script-samples">
<meta property="og:image" content="https://pnp.github.io/script-samples/assets/social-media-screenshot.png">
<meta property="og:image:alt" content="Screenshot example of the PnP Script Samples Site">
<meta property="og:site_name" content="PnP Script Samples">
<meta name="msvalidate.01" content="04EFEDA66EC42C08A1247C979E2F9ADD">
<meta property="og:description" content="A sample gallery of scripts to manage all things Microsoft 365.">

<link rel="shortcut icon" href="https://c.s-microsoft.com/favicon.ico?v2">
<link rel="stylesheet" href="../styles/docfx.vendor.css">
<link rel="stylesheet" href="../styles/docfx.css">
<link rel="stylesheet" href="../styles/main.css">
<link rel="stylesheet" href="../styles/extra.css">
<link rel="stylesheet" href="../styles/filter.css">
<link rel="stylesheet" href="../styles/site.css">
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">
<meta property="docfx:navrel" content="../toc.html">
<meta property="docfx:tocrel" content="../toc.html">


<meta property="docfx:newtab" content="true">
<script type="text/javascript">
(function(c,l,a,r,i,t,y){
c[a]=c[a]||function(){(c[a].q=c[a].q||[]).push(arguments)};
t=l.createElement(r);t.async=1;t.src="https://www.clarity.ms/tag/"+i;
y=l.getElementsByTagName(r)[0];y.parentNode.insertBefore(t,y);
})(window, document, "clarity", "script", "8j6pedl344");
</script>
</head> <body data-spy="scroll" data-target="#affix" data-offset="120">
<div id="wrapper">
<header>

<nav id="autocollapse" class="navbar navbar-inverse ng-scope" role="navigation">
<div class="container">
<div class="github-mark">
<a href="https://github.com/pnp/script-samples" title="Go to repository">
<div class="github-wrapper">
<div class="github-icon">
<img src="https://pnp.github.io/script-samples/assets/icons/github-light-64px.png" alt="GitHub Icon Image">
</div>
<div class="github-repository">
<span>GitHub</span>
<ul class="github-facts">
<li>
<i class="fa fa-code-fork" aria-hidden="true"></i>
<span class="github-forks"></span>
</li>
<li>
<i class="fa fa-star-o" aria-hidden="true"></i>
<span class="github-stars"></span>
</li>
</ul>
</div>
</div>
</a>
</div>
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="../index.html">
<img id="logo" class="svg" src="../assets/logo.svg" alt="">
</a> </div>
<div class="collapse navbar-collapse" id="navbar">
<form class="navbar-form navbar-right" role="search" id="search">
<div class="form-group">
<label class="glyphicon glyphicon-search" for="search-query"></label>
<input type="text" class="form-control" id="search-query" placeholder="Search" autocomplete="off">
</div>
</form>
</div>
</div>
</nav> </header>
<div role="main" class="container body-content hide-when-search">
<div class="article row grid">
<div class="col-md-12">
<article class="content wrap" id="_content" data-uid="">
<div class="contribution-panel mobile-hide pull-right">
<a href="https://github.com/pnp/script-samples/blob/main/scripts/azure-ocr-openai-json-invoice-extractor/README.md/#L1" title="Improve this Doc" class="improve-doc-lg"><i class="glyphicon glyphicon-pencil"></i></a>
</div>
<h1 id="extract-invoice-details-from-any-file-format-using-azure-ocr-and-openai">Extract Invoice Details from Any File Format Using Azure OCR and OpenAI</h1>

<h2 id="summary">Summary</h2>
<p>This sample script integrates Azure's Document Intelligence and OpenAI services to extract from either images or pdfs or embedded images from any unstructured data and to use this script for any other purpose , please change the system prompt in the script.</p>
<h3 id="functionality"><strong>Functionality</strong></h3>
<ul>
<li><strong>Text Extraction</strong>: Utilizes Azure's Document Intelligence Read OCR model to extract text from documents, including PDFs and images.</li>
<li><strong>Data Processing</strong>: Employs Azure OpenAI's GPT models to analyze the extracted text and identify key invoice details, such as <strong>Invoice Number</strong>, <strong>Sales Person</strong>, <strong>Date of Invoice</strong>, <strong>SubTotal</strong>, <strong>Tax</strong>, and <strong>Total</strong>.</li>
<li><strong>JSON Output</strong>: Structures the identified information into a JSON format for streamlined data handling and integration.</li>
</ul>
<hr>
<h2 id="prerequisites">Prerequisites</h2>
<ul>
<li><strong>OpenAI API Key</strong>: Create an account <a href="https://platform.openai.com/signup/">here</a> to obtain the API key.</li>
<li><strong>Document Intelligence API Key</strong>: Create a key in the <a href="https://documentintelligence.ai.azure.com/studio/">Document Intelligence Studio</a>.</li>
<li><strong>Settings</strong>: The settings of the script is part of set Settings-Agreements.json , ensure the script and its settings file are in same location</li>
</ul>
<hr>
<h2 id="settings">Settings</h2>
<pre><code class="lang-json">{
&quot;APIKeys&quot;: {
&quot;OpenAI&quot;: &quot;&lt;OCRKeyFromAzureOpenAI&gt;&quot;,
&quot;DocumentIntelligence&quot;: &quot;&lt;OCRKeyFromADocumentIntelligence&gt;&quot;&quot;
},
&quot;Endpoints&quot;: {
&quot;OpenAI&quot;: &quot;https://&lt;openaiendpoint&gt;.openai.azure.com/openai/deployments/gpt-4o/chat/completions?api-version=2024-08-01-preview&quot;,
&quot;DocumentIntelligence&quot;: &quot;https://&lt;docintelligendeendpoint&gt;.cognitiveservices.azure.com&quot;
},
&quot;FilePath&quot;: &quot;/Users/divyaakula/Documents/VidesoReview/SyntexDemo/AI Builder Document Processing Sample Data/Invoices/Adatum/Train/Adatum 1.pdf&quot;,
&quot;Base64String&quot;: null,
&quot;OCRVersion&quot;: &quot;2024-07-31-preview&quot;,
&quot;ChunkSize&quot;: 4194304
}

</code></pre>
<hr>
<h2 id="example-screenshot">Example Screenshot</h2>
<p><img src="assets/output.png" alt="Example Screenshot"></p>
<hr>
<div class="tabGroup" id="tabgroup_CeZOj-G++Q">
<ul role="tablist">
<li role="presentation">
<a href="#tabpanel_CeZOj-G++Q_powershell" role="tab" aria-controls="tabpanel_CeZOj-G++Q_powershell" data-tab="powershell" tabindex="0" aria-selected="true">PowerShell</a>
</li>
</ul>
<section id="tabpanel_CeZOj-G++Q_powershell" role="tabpanel" data-tab="powershell"><button class="article-clipboard" title="Copy to clipboard" data-toggle="tooltip" data-placement="top" data-clipboard-target="section[id^='tabpanel'][data-tab='powershell'] pre">
<span class="glyphicon glyphicon-copy" aria-hidden="true"></span>
<span class="article-clipboard__message done">Copied</span>
</button>

<pre><code class="lang-powershell"># A PowerShell script to extract invoice details using Azure OCR and OpenAI.

# Path to your config file
$configFilePath = &quot;Settings-Agreements.json&quot;
if (-not (Test-Path $configFilePath)) {
throw &quot;Configuration file not found at $configFilePath&quot;
}

# Load configuration from JSON file
$Config = Get-Content -Path $configFilePath | ConvertFrom-Json

# Extract Values from Config
$OpenAI_Key = $Config.APIKeys.OpenAI
$OCR_Key = $Config.APIKeys.DocumentIntelligence
$OCR_Endpoint = $Config.Endpoints.DocumentIntelligence
$OpenAI_Endpoint = $Config.Endpoints.OpenAI
$OCR_Version = $Config.OCRVersion
$FilePath = $Config.FilePath
$Base64String = $Config.Base64String

# Function to Extract Text from PDF/Image Using Azure OCR
function Get-TextFromDocument($Base64String) {
# Convert Base64 to binary
$BinaryData = [Convert]::FromBase64String($Base64String)

# Set OCR request headers
$Headers = @{
&quot;Ocp-Apim-Subscription-Key&quot; = $OCR_Key
&quot;Content-Type&quot; = &quot;application/pdf&quot; # Change to &quot;image/jpeg&quot; for images
}

# Send file to Azure Document Intelligence OCR API
Invoke-RestMethod -Uri &quot;$OCR_Endpoint/documentintelligence/documentModels/prebuilt-read:analyze?api-version=$OCR_Version&quot; `
-Headers $Headers -Method Post -ResponseHeadersVariable HeaderInfo -Body $BinaryData

if (-not $HeaderInfo) {
throw &quot;Failed to start OCR process.&quot;
}

# Extract operation URL
$OperationUrl = ($HeaderInfo.&quot;Operation-Location&quot;)[0]

# Poll for result
Start-Sleep -Seconds 5 # Wait before checking status
do {
$Result = Invoke-RestMethod -Uri $OperationUrl -Headers @{ &quot;Ocp-Apim-Subscription-Key&quot; = $OCR_Key }
$Status = $Result.status
Start-Sleep -Seconds 2
} while ($Status -ne &quot;succeeded&quot;)

# Extract recognized text
$ExtractedText = ($Result.analyzeResult.pages.lines.content) -join &quot; &quot;
return $ExtractedText
}

# Function to Extract Names Using Azure OpenAI
function Get-Inputs($ExtractedText) {
# OpenAI Request Payload
$OpenAI_Request = @{
messages = @(
@{ role = &quot;system&quot;; content = &quot;Extract Invoice No, Sales Person, Date Of Invoice, SubTotal, Tax and Total and return in plain json format&quot; }
@{ role = &quot;user&quot;; content = &quot;Text: $ExtractedText&quot; }
)
} | ConvertTo-Json -Depth 3

# OpenAI Headers
$OpenAI_Headers = @{
&quot;api-key&quot; = $OpenAI_Key
&quot;Content-Type&quot; = &quot;application/json&quot;
}

# Call OpenAI API
$OpenAI_Response = Invoke-RestMethod -Uri &quot;$OpenAI_Endpoint&quot; `
-Method Post -Headers $OpenAI_Headers -Body $OpenAI_Request

# Extract Names from Response
$responseContent = $OpenAI_Response.choices[0].message.content
# Remove leading and trailing triple backticks and 'json' label
$jsonString = $responseContent -replace '^```json\s*', '' -replace '\s*```$', ''
return $jsonString
}

# Function to Process Base64 Document Input
function Get-Data($Base64String) {
try {
$Base64String = Convert-Base64String $FilePath
# Step 1: Extract text using OCR
$ExtractedText = Get-TextFromDocument -Base64String $Base64String

# Step 2: Extract Names using OpenAI
$Names = Get-Inputs -ExtractedText $ExtractedText
# Step 3: Return as JSON
return $Names
}
catch {
return @{ &quot;error&quot; = $_.Exception.Message } | ConvertTo-Json
}
}

# Function to convert file to Base64 string
function Convert-Base64String($path) {
$Base64Input = [Convert]::ToBase64String([IO.File]::ReadAllBytes($path))
return $Base64Input
}

# Example Usage: Provide a Base64 String of a PDF or Image
$Base64Input = &quot;&quot;
# Call the Function
$Result = Get-Data -Base64String $Base64Input
Write-Output $Result

</code></pre>
<p><a href="https://learn.microsoft.com/en-us/azure/ai-studio/what-is-ai-studio?wt.mc_id=MVP_366830">More about OpenAI API</a></p>
</section>
</div>
<h2 id="contributors">Contributors</h2>
<table>
<thead>
<tr>
<th>Author(s)</th>
<th>Author(s)</th>
</tr>
</thead>
<tbody>
<tr>
<td>Divya Akula</td>
<td><a href="https://www.linkedin.com/in/ms-divya-akula/">https://www.linkedin.com/in/ms-divya-akula/</a></td>
</tr>
</tbody>
</table>
<h2 id="disclaimer">Disclaimer</h2>
<p><strong>THESE SAMPLES ARE PROVIDED <em>AS IS</em> WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.</strong></p>
<img src="https://m365-visitor-stats.azurewebsites.net/script-samples/scripts/azure-ocr-openai-json-invoice-extractor" aria-hidden="true">
</article>
</div>
</div>
</div>

<footer>
<div class="grad-bottom"></div>
<div class="footer">
<div class="container">
<span class="pull-right">
<a href="#top">Back to top</a>
</span>
<span>Script Samples<br>Generated by <strong><a href='https://dotnet.github.io/docfx' alt='Doc FX Website'>DocFX</a></strong> with <strong><a href='https://ovasquez.github.io/docfx-material' alt='DocFx Material Theme'>Material UI</a></strong></span>

<!-- <img src="https://telemetry.sharepointpnp.com/@pnp.github.io/index/" alt="spacer"> -->
</div>
</div>
</footer> </div>

<script type="text/javascript" src="../styles/docfx.vendor.js"></script>
<script type="text/javascript" src="../styles/docfx.js"></script>
<script type="text/javascript" src="../styles/main.js"></script>

<script type="text/javascript" src="../styles/clipboard.min.js"></script>
<script type="text/javascript" src="../styles/isotope.pkgd.min.js"></script>
<script type="text/javascript" src="../styles/loadsamples.js"></script>
<script type="text/javascript" src="../styles/filtersamples.js"></script>

</body>
</html>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
61 changes: 61 additions & 0 deletions azure-ocr-openai-json-invoice-extractor/assets/sample.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
[
{
"name": "azure-ocr-openai-json-invoice-extractor",
"source": "openai",
"title": "Get data from OpenAI deployment/model",
"shortDescription": "This sample shows how to use both OpenAI and document intelligene studio to process and extract data in a json format",
"url": "https://pnp.github.io/script-samples/azure-ocr-openai-json-invoice-extractor/README.html",
"longDescription": [
""
],
"creationDateTime": "2025-01-26",
"updateDateTime": "2025-01-26",
"products": [
"OpenAI",
"Document Intelligence Studio"
],
"metadata": [
{
"key": "POWERSHELL",
"value": "7.2.0"
}
],
"categories": [
"Status",
"Summary",
"AI"
],
"tags": [
"Invoke-WebRequest"
],
"thumbnails": [
{
"type": "image",
"order": 100,
"url": "https://raw.githubusercontent.com/pnp/script-samples/main/scripts/azure-ocr-openai-json-invoice-extractor/assets/preview.png",
"alt": "Preview of the sample Get data from OpenAI deployment/model"
}
],
"authors": [
{
"gitHubAccount": "divya-akula",
"company": "",
"pictureUrl": "https://github.com/divya-akula.png",
"name": "Divya Akula"
}
],
"references": [
{
"name": "Want to learn more about Azure OpenAI AI Deployments",
"description": "If you're interested in learning more about OpenAI API and its capabilities, this link provides a list of resources and tools to help you get started. Whether you're a developer looking to integrate Open AI into your applications or a researcher exploring the latest advances in artificial intelligence, this guide offers a range of helpful resources, including documentation, tutorials, and community forums.",
"url": "https://learn.microsoft.com/en-us/azure/ai-studio/what-is-ai-studio"
},
{
"name": "Create and Configure an Azure OpenAI Resource",
"description": "Learn to create and configure an Azure OpenAI resource using the Azure portal for seamless AI integration.",
"url": "https://learn.microsoft.com/en-us/azure/ai-services/openai/how-to/create-resource?pivots=web-portal"
}
]
}
]

7 changes: 6 additions & 1 deletion cmdusage.html
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,11 @@ <h1 id="report-of-samples-command-usage">Report of Samples Command Usage</h1>
<td style="text-align: center;">ConvertFrom-Json, Write-Host, Read-Host</td>
</tr>
<tr>
<td><a href="azure-ocr-openai-json-invoice-extractor/README.html">Get data from OpenAI deployment/model</a></td>
<td style="text-align: center;"></td>
<td style="text-align: center;">Get-Content, ConvertFrom-Json, Invoke-RestMethod, Start-Sleep, ConvertTo-Json, Get-TextFromDocument, Get-Inputs, Get-Data, Write-Output</td>
</tr>
<tr>
<td><a href="azure-open-ai-byod-powershell/README.html">Bring you own data with Azure Open AI example</a></td>
<td style="text-align: center;"></td>
<td style="text-align: center;">Get-Content, ConvertFrom-Json, Write-Host, ConvertTo-Json, Invoke-RestMethod, Out-File</td>
Expand Down Expand Up @@ -1752,7 +1757,7 @@ <h3 id="sharepoint-online-management-shell-usage">SharePoint Online Management S
</tr>
</tbody>
</table>
<p>There are <strong>310</strong> script scenarios with metadata in the site | Generated: 15 Jan 2025 08:47:44</p>
<p>There are <strong>311</strong> script scenarios with metadata in the site | Generated: 27 Jan 2025 06:52:29</p>
</article>
</div>
</div>
Expand Down
Loading

0 comments on commit 36b88ea

Please sign in to comment.