Programming can sometimes be challenging and tedious whether you are a beginner or experienced developer. The frustration comes in spending long periods of time searching for code snippets to unblock coding barriers like errors or finding the best code syntax to solve a problem. Thanks to the breakthroughs in OpenAI Codex GPT-3, developers can use the AI pair programming solution that translates natural language to code in GitHub Copilot in dozens of programming languages including Python, JavaScript, Go, Perl, PHP, Ruby, and more. In this session, we will be exploring how to use GitHub Copilot by simply describing in short sentences what we want, to solve common business problems that enable you to be more efficient and increase productivity from now on. We will use Python to see how GitHub Copilot can help developers integrate with the Azure Form Recognizer API to read contents of a receipt.
Prerequisites:
Using Keyboard Shortcuts
GitHub Copilot provides keyboard shortcuts to make the experience in Visual Studio Code user-friendly across platforms. We will be using Windows shortcuts in this tutorial. If you are using a Mac or Linux platform, refer to shortcuts for your environment.
Importing Libraries
pip install azure-ai-formrecognizer==3.2.0
# import azure form recognizer libraries
As you can see in the image above, Github Copilot generated 10 code suggestions (NOTE: GitHub Copilot hides suggestions that are duplicates). In the first suggestion, it provides two form recognizer libraries. In addition, it knows that we would need to connect to the API, so it includes an AzureKeyCredential class used for azure authentication. Finally, it includes a ResourceNotFoundError class, because it knows we'll need exception handling while accessing azure resources.
The next suggestion, it goes a step further by providing libraries for azure storage for cases where the input files are stored in an azure storage account. In addition, it includes the key vault for encrypting and storing information such as the API Key for security purposes.
So, as you can see, Copilot will suggest the bare minimum code a user needs as well as best practice code recommendations for things the user may not have considered.
Connecting to the Azure Form Recognizer API
# create the client and authenticates with the endpoint and key
# create the client and authenticate with the endpoint and key
Recognizing a Receipt
The Azure Form Recognizer service has pretrained models to analyze and extract data fields or values for different types of forms and documents. For example, tax forms, sales invoices, printed or handwritten receipts, passport or ID cards, etc.
myReceiptUrl = "https://raw.githubusercontent.com/Azure/azure-sdk-for-python/master/sdk/formrecognizer/azure-ai-formrecognizer/tests/sample_forms/receipt/contoso-receipt.png"
# user form recognizer client to recognize image from myReceiptUrl
Based on the two choices, we'll click on “Accept Solution” for the second suggestion.
Print out extracted receipt data
Now that the Form Recognizer has recognized the receipt image. We need to print out the receipt fields and values from the receipt. However, we are not sure what the data structure for the Form Recognizer output result is. So, we are going to rely on Copilot to see if it’ll be able to help us generate the code for printing out the results.
# loop through results and extract data from receipt
Conclusion
As you can see, it only took us 5 short comment sentences to write an application that is able to use Azure Form Recognizer to read a receipt image, analyze and extract the contents of the receipt. In addition, we observed GitHub Copilot's ability to understand a user's intent even when there are misspelling, or the instruction sentence is not very detail. These are OpenAI's natural language processing capabilities that are built-in GitHub Copilot. It does not just read commented instructions and generate code; it pays attention to the context of the user's code to perform autocompletes when the user is typing or generate code based on what the user has coded so far. These are helpful to reduce the time it takes to write repetitive boiler-plate code. This tutorial also illustrated situations where programmers are face with a new API. Trying to figure out an APIs client connectivity or how use its services are not always easy, so GitHub is a great paired programmer to help a developer on syntax. Overall, this is a useful tool for developers to be more productive when programming and implementing applications faster.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.