Skip to content

Instantly share code, notes, and snippets.

@B046090010
Created February 9, 2022 02:17
Show Gist options
  • Save B046090010/f1324b3dd127e467aa63e9ecaa1842e5 to your computer and use it in GitHub Desktop.
Save B046090010/f1324b3dd127e467aa63e9ecaa1842e5 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "azure_form.ipynb",
"provenance": [],
"collapsed_sections": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "code",
"source": [
"## Connect to google drive\n",
"import os, sys\n",
"from google.colab import drive\n",
"drive.mount('/content/drive')\n",
"mydrive = \"YOUR FOLDER PATH\"\n",
"os.chdir(mydrive)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "8qV7nIYy3G50",
"outputId": "0f8b696d-6f97-4b2e-ba3e-e47ff31ab50c"
},
"execution_count": 5,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount(\"/content/drive\", force_remount=True).\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"!pip install azure-ai-formrecognizer --pre --upgrade"
],
"metadata": {
"id": "PvOp5yeA0WTS",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "a460a218-abda-4c5d-d06b-47af36a3a3a1"
},
"execution_count": 6,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Collecting azure-ai-formrecognizer\n",
" Downloading azure_ai_formrecognizer-3.2.0b2-py2.py3-none-any.whl (219 kB)\n",
"\u001b[K |████████████████████████████████| 219 kB 5.0 MB/s \n",
"\u001b[?25hCollecting azure-common~=1.1\n",
" Downloading azure_common-1.1.28-py2.py3-none-any.whl (14 kB)\n",
"Collecting azure-core<2.0.0,>=1.13.0\n",
" Downloading azure_core-1.22.0-py3-none-any.whl (178 kB)\n",
"\u001b[K |████████████████████████████████| 178 kB 48.9 MB/s \n",
"\u001b[?25hCollecting msrest>=0.6.21\n",
" Downloading msrest-0.6.21-py2.py3-none-any.whl (85 kB)\n",
"\u001b[K |████████████████████████████████| 85 kB 3.6 MB/s \n",
"\u001b[?25hRequirement already satisfied: six>=1.11.0 in /usr/local/lib/python3.7/dist-packages (from azure-ai-formrecognizer) (1.15.0)\n",
"Requirement already satisfied: requests>=2.18.4 in /usr/local/lib/python3.7/dist-packages (from azure-core<2.0.0,>=1.13.0->azure-ai-formrecognizer) (2.23.0)\n",
"Requirement already satisfied: requests-oauthlib>=0.5.0 in /usr/local/lib/python3.7/dist-packages (from msrest>=0.6.21->azure-ai-formrecognizer) (1.3.1)\n",
"Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.7/dist-packages (from msrest>=0.6.21->azure-ai-formrecognizer) (2021.10.8)\n",
"Collecting isodate>=0.6.0\n",
" Downloading isodate-0.6.1-py2.py3-none-any.whl (41 kB)\n",
"\u001b[K |████████████████████████████████| 41 kB 446 kB/s \n",
"\u001b[?25hRequirement already satisfied: idna<3,>=2.5 in /usr/local/lib/python3.7/dist-packages (from requests>=2.18.4->azure-core<2.0.0,>=1.13.0->azure-ai-formrecognizer) (2.10)\n",
"Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /usr/local/lib/python3.7/dist-packages (from requests>=2.18.4->azure-core<2.0.0,>=1.13.0->azure-ai-formrecognizer) (1.24.3)\n",
"Requirement already satisfied: chardet<4,>=3.0.2 in /usr/local/lib/python3.7/dist-packages (from requests>=2.18.4->azure-core<2.0.0,>=1.13.0->azure-ai-formrecognizer) (3.0.4)\n",
"Requirement already satisfied: oauthlib>=3.0.0 in /usr/local/lib/python3.7/dist-packages (from requests-oauthlib>=0.5.0->msrest>=0.6.21->azure-ai-formrecognizer) (3.2.0)\n",
"Installing collected packages: isodate, msrest, azure-core, azure-common, azure-ai-formrecognizer\n",
"Successfully installed azure-ai-formrecognizer-3.2.0b2 azure-common-1.1.28 azure-core-1.22.0 isodate-0.6.1 msrest-0.6.21\n"
]
}
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "9iMJunZz0L_J"
},
"outputs": [],
"source": [
"\"\"\"\n",
"This code sample shows Custom Model operations with the Azure Form Recognizer client library. \n",
"The async versions of the samples require Python 3.6 or later.\n",
"\n",
"To learn more, please visit the documentation - Quickstart: Form Recognizer Python client library SDKs v3.0\n",
"https://docs.microsoft.com/en-us/azure/applied-ai-services/form-recognizer/quickstarts/try-v3-python-sdk\n",
"\"\"\"\n",
"\n",
"from azure.core.credentials import AzureKeyCredential\n",
"from azure.ai.formrecognizer import DocumentAnalysisClient\n",
"\n",
"endpoint = \"YOUR_FORM_RECOGNIZER_ENDPOINT_URL\"\n",
"key = \"YOUR_FORM_RECOGNIZER_SUBSCRIPTION_KEY\"\n",
"model_id = \"YOUR_CUSTOM_BUILT_MODEL_ID\"\n",
"\n",
"formFile = \"YOUR FILE PATH\"\n",
"\n",
"document_analysis_client = DocumentAnalysisClient(\n",
" endpoint=endpoint, credential=AzureKeyCredential(key)\n",
")\n",
"\n",
"# Make sure your document's type is included in the list of document types the custom model can analyze\n",
"with open(formFile, \"rb\") as f:\n",
" poller = document_analysis_client.begin_analyze_document(model_id, document=f)\n",
"result = poller.result()"
]
},
{
"cell_type": "code",
"source": [
"for idx, document in enumerate(result.documents): # Analyze all the documents in order\n",
" print(\"--------Analyzing document #{}--------\".format(idx + 1))\n",
" print(\"Document was analyzed by model with ID {}\".format(result.model_id))\n",
" for name, field in document.fields.items():\n",
" field_value = \"\".join(field.value.split(\" \")) # Delete whitespace in value\n",
" print(\"......found field of '{}' with value '{}' and with confidence {}\".format(name, field_value, field.confidence))"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "2bDU2K1JM_Zt",
"outputId": "57f859dc-b0c1-44cf-9b8a-806abebcba99"
},
"execution_count": 19,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"--------Analyzing document #1--------\n",
"Document was analyzed by model with ID resumeschool_02081329\n",
"......found field of 'school' with value '國立政治大學' and with confidence 0.939\n"
]
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment