- Python 3
- Visual Studio Code
- Docker Desktop
- Postman
- Azure Storage Account
- Form Recognizer Resource
- Trained Form Recognizer Model
-
Azure Portal > Create a Function App
-
Visual Studio Code > Create a New Virtual Envrionment (Python).
-
Press: Ctrl + SHIFT + P
-
Search "Create New Project", Select "Azure Functions: Create New Project"
-
Select Python as the language for your Azure Function project.
-
Select HTTP trigger as the template for your projects function.
- Run the following command in your activated environment via Terminal:
pip install azure-storage-blob
- Add the following line to requirements.txt:
azure-storage-blob==1.4.0
This can be copy and pasted into __init__.py
import requests
import logging
import azure.functions as func
from azure.storage.blob import BlockBlobService
SUPPORTED_CONTENT_TYPES = ['application/pdf', 'image/jpeg', 'image/png']
def main(req: func.HttpRequest) -> func.HttpResponse:
# 1. Get Header Values - Azure Blob Storage
storage_account_name = req.headers.get('storage_account_name')
storage_account_key = req.headers.get('storage_account_key')
container_name = req.headers.get('container_name')
blob_name = req.headers.get('blob_name')
# 2. Get Header Values - Form Recognizer Cognitive Service
region = req.headers.get('region')
model_id = req.headers.get('model_id')
subscription_key = req.headers.get('subscription_key')
# 3. Get Form from Blob Storage
blob_service = BlockBlobService(account_name=storage_account_name, account_key=storage_account_key)
blob = blob_service.get_blob_to_bytes(container_name, blob_name)
data = blob.content
content_type = blob.properties.content_settings.content_type
body = None
if content_type in SUPPORTED_CONTENT_TYPES:
# 4. Generate Endpoint and HTTP Header
endpoint = 'https://{0}.api.cognitive.microsoft.com/formrecognizer/v1.0-preview/custom/models/{1}/analyze'.format(region, model_id)
headers = {
'Content-Type': content_type,
'Ocp-Apim-Subscription-Key': subscription_key
}
# 5. Analyze Form
response = requests.post(endpoint, headers=headers, data=data)
body = response.content
# 6. Return HTTP Response
return func.HttpResponse(body,headers={'Content-Type':'application/json'})
-
Execute the following command in Terminal to launch the functions runtime host:
func start
-
Open Postman and create a new request with the following properties:
- Method: GET
- URL:
http://localhost:7071/api/YOUR_FUNCTION_NAME
-
Add the following Headers:
Key Value storage_account_name STORAGE_ACCOUNT_NAME
storage_account_key STORAGE_ACCOUNT_KEY
container_name STORAGE_CONTAINER_NAME
blob_name STORAGE_BLOB_NAME
region FORM_RECOGNIZER_RESOURCE_REGION
model_id FORM_RECOGNIZER_MODEL_ID
subscription_key FORM_RECOGNIZER_SUBSCRIPTION_KEY
-
Click Send
If successful, this should return the results of the form being processed by the Analyze Form Recognizer API.
Execute the following terminal command to publish the current directory contents to an Azure Function App:
func azure functionapp publish NAME_OF_YOUR_FUNCTION_APP --build-native-deps
Check out https://aka.ms/func-python-publish for more information on publishing a Python function to Azure.
Once the deployment has completed successfully, a public invoke url will be returned to the terminal window.
-
Copy the invoke url to the clipboard.
-
Return to Postman and duplicate the existing request by right-clicking on the tab and clicking Duplicate Tab.
-
Replace the http://localhost/api/YOUR_FUNCTION_NAME with the invoke url.
-
Click Send
That's it! If successful, this should return the same results as before but this time, the function will be running in the cloud. We now have a serverless method of processing forms stored on Azure Blob Storage using Azure Functions.