The Azure SDK for Python is a collection of libraries
that enables you to quickly,
easily and consistently integrate Azure services into your cloud solutions.
You authenticate your users and applications with the Azure Identity
Library.
You store your application data with the Azure storage libraries.
You integrate artificial intelligence into your apps with the cognitive
service libraries. You implement robust messaging systems with the Azure
messaging libraries.
We have libraries to meet all your cloud solution needs
and they can be found at azure.com/SDK.
As you use the new Azure SDK for Python libraries,
you'll notice that they feel just like any other Python
library.
We put a lot of effort into ensuring that we
follow Python design guidelines,
conventions and best practices. We also integrate with the Python
ecosystem and allow you to consume packages from either
pypi or conda.
Today we're going to discuss a common health care industry
scenario.
We have thousands of scanned invoices and health records that
we want to extract text from,
convert to json and store in persistent storage such as
the Azure blob storage service.
This example will use our new identity storage,
messaging, and cognitive service libraries. We'll divide the application into
two parts.
The PDF ingestor and the PDF processor.
That way we'll be able to decouple those two
responsibilities and scale them independently.
The Azure messaging service is the glue that will bind
those application components together.
First, we'll write a program that uploads the PDF files
to blob storage to be later processed. While uploading
the documents will send custom telemetry events to event hubs,
which is a service that is commonly used in massive
scale telemetry systems.
A new blob created event will be triggered when a
new PDF is uploaded.
We'll configure blob storage to forward those events to Event
Grid,
which is an Azure native eventing system where you can
listen for Azure events or define your own events.
Event Grid will send a message to service bus using
the official cloud Native Computing Foundation cloud event schema.
Our PDF processor will receive the service bus message and
then call the new cognitive service API's to extract
invoice and patient information from the PDF files.
It will transform the data into the format needed by
later steps in the application,
serialize it to json and save it to blob storage.
Let's have a quick look at the type of documents
we'll be processing.
This is an example of an invoice that can be
processed by the new cognitive services
invoices API. As you will see in a bit,
the API is capable of extracting all this data into
an object model,
including the details of each invoice line item. On the
left
you can see the original PDF invoice and on the
right you can see the resulting json model that would
be stored in Azure blob storage.
The other type of PDF document that will process is
a scan medical record.
The Cognitive Services health APIs can extract all the medical
conditions that are described in the doctor's notes.
On the left you can see the original doctors notes
in PDF form.
And on the right you can see the extracted data
in json format needed for our application.
Let's have a look at our PDF ingestion code,
which reads are PDF files from disk,
uploads them to blob storage,
and writes events to event Hub. For this part of
our solution will use Azure SDK packages from Conda.
Let's open up Conda and install the Microsoft Source and
Azure packages.
I've opened the Anaconda Navigator and have selected the conda
environment that I previously created.
In order to add the Azure packages,
I first need to add the Microsoft source.
So click on channels.
Then click Add. Enter Microsoft.
And hit enter.
Then click update channels.
Now select not installed and search for azure-.
We need to install the Azure storage and Azure event
packages,
so select both of those and click apply.
It will then show you dependent packages that will be
installed,
click apply.
Once that completes, select installed to verify that all the
correct packages were installed.
You'll notice that Azure identity and Azure core were automatically
pulled in as dependent packages. Now that our conda environment
is set up,
let's switch over to VS Code and run the app.
I'm now in VS Code in the app is loaded.
You'll notice that I have squiggly lines under the Azure
imports.
That's because I have not selected the Python virtual environment
yet,
so let's go ahead and do that.
I'm going to click down here in the toolbar.
And I'm going to select the Pycon virtual environment. We
successfully created the conda virtual environment,
added the Azure packages, have loaded the virtual environment into
VS Code.
Now let's run the app.
You'll notice here that we're using DefaultAzureCredential, Blob
Service
Client, EventHubProducerclient,
and EventData classes from the Azure libraries.
Then we new up in Azure Identity DefaultAzureCredential
object,
which is a convenient wrapper around all the complex Oauth
workflows.
Azure Identity has many authentication methods available to you. When
run locally,
DefaultAzureCredential will use your dev credentials such as
credentials from Azure CLI or PowerShell.
When deployed to production, it will use manage identity or
service principle information you have configured via well known environment
variables.
We then new up a blob service client,
pass the endpoint in the credential object we created earlier.
The client will use that credential object to request a
token from Azure Active Directory before performing actions that require
authentication.
We then get the container clients for both invoices an
records and new up an event producer
client. Event Hub sends messages in batches,
so we'll create a batch.
Then we will iterate through each of the invoice PDFs
and their record PDFs and upload them to Azure blob
storage.
After the documents have been uploaded,
we'll call the send batch method of the Event Hub
client to send
all those events to the Event Hub. When the PDF
is uploaded,
Event Grid will automatically send a message to the service
bus queue in the cloud events format.
We actually don't need any code for this step.
All you need to do is configure the event grid
system topic to watch for Blob created events and set
the destination to the service bus queue.
You can create this configuration via the Portal, CLI,
PowerShell, ARM, Bicep, or any other infrastructure as code method.
Let's now dive into the PDF file processors.
We have one processor for invoices and
one for patient records. Both processors will look for service
bus messages and use the cognitive services APIs to extract
the text,
convert to json, and then save to blob storage. For
the processors we're going to use a standard Python virtual
environment and install our packages with pip and a requirements.txt
file.
We're back in VS Code and we have the invoice
processor code open.
Will new up all the necessary Azure SDK clients,
including the form recognizer client,
which is the cognitive service that allows you to extract
text from documents.
We'll new up a service bus client to monitor messages
sent from Event Grid to service bus.
And by iterating over the service bus receiver we'll automatically
receive new messages. For each new message,
we're going to deserialize it as a cloud event and
then we're going to pass the URL of the PDF
stored in blob storage to the begin
recognize invoices from URL function of form recognizer client.
This is going to use the Azure SDK long polling
operation design that allows you to make a method call
and then it will automatically pull the results. Once we
have the results, we're going to do a custom
Invoice object. And then we're going to upload that.
And then we're going to complete the message.
You can use Azure Storage Explorer to view the files
that have been uploaded to blob storage.
Here's the original PDF, and here's the json version of
that PDF file.
We can double click those to save them locally,
and here are the files side by side. On the
left you have the original PDF file on the right
you have the json representation of that file which was
extracted using the cognitive services invoice APIs.
Now let's take a look at the patient record processor.
We're going to use the form recognizer client again to
extract the text.
Then we're going to use the text analytics client to
extract the health data from that text.
You'll notice that each of these clients are using the
default Azure credential.
You also have the option of passing a key to
the client instead of a credential object.
In this case, we're going to use Azure Key Vault
to store that key and then pass it to the
text analytics client.
Once again, we're looping through all the messages sent to
the queue,
deserializing into a cloud event,
and then passing the PDF URL to the begin recognized
content from URL method.
We will pull for the results.
And then we will deserialize those results into a custom
record object,
we'll then upload the result to blob storage,
we'll then use the begin analyze healthcare entities method to
extract the well known healthcare terms from the patient
notes. We'll create a patient object from those results,
upload them to blob storage,
and then complete the message. Back in Azure Storage Explorer
we can see those documents.
Here's the original PDF, the raw notes from the record
and the patient data in the schema we need for
our application.
Once again, you can double click on these to open
them up.
On the left you have the original PDF health record
form, on the upper right you have the raw notes
extracted from the text, and on the lower right you
have all the terms that were extracted using the healthcare
APIs.
In this video we gave you a brief introduction to
the Azure SDK for Python.
We learned how to use Azure identity,
Azure storage, Azure messaging and Azure Cognitive Services to extract
invoice impatient data from PDF files and store the resulting
json in blob storage.
You can find links to all the packages docs in
code at azure.com/SDK.
Follow us on Twitter at twitter.com/azureSDK to get notified
of all the new SDK features and releases.
Subscribe to our blog at aka.ms/azsdk
blog for behind the scenes look at how the
Azure SDKs are built.
Thanks for watching and have a great day.