|
4 | 4 | "cell_type": "markdown",
|
5 | 5 | "metadata": {},
|
6 | 6 | "source": [
|
7 |
| - "# Extract custom fields in your file" |
| 7 | + "# Extract Custom Fields from Your File" |
8 | 8 | ]
|
9 | 9 | },
|
10 | 10 | {
|
11 | 11 | "cell_type": "markdown",
|
12 | 12 | "metadata": {},
|
13 | 13 | "source": [
|
14 |
| - "This notebook demonstrates how to use analyzers to extract custom fields from your input file." |
| 14 | + "This notebook demonstrates how to use analyzers to extract custom fields from your input files." |
15 | 15 | ]
|
16 | 16 | },
|
17 | 17 | {
|
18 | 18 | "cell_type": "markdown",
|
19 | 19 | "metadata": {},
|
20 | 20 | "source": [
|
21 | 21 | "## Prerequisites\n",
|
22 |
| - "1. Follow steps in [README](../README.md#Configure-Azure-AI-Service-resource) to create `.env` file to configure your Azure AI Service.\n", |
23 |
| - "1. Install packages needed to run the sample" |
| 22 | + "1. Follow the steps in the [README](../README.md#Configure-Azure-AI-Service-resource) to create a `.env` file and configure your Azure AI Service.\n", |
| 23 | + "2. Install the required packages to run the sample." |
24 | 24 | ]
|
25 | 25 | },
|
26 | 26 | {
|
|
36 | 36 | "cell_type": "markdown",
|
37 | 37 | "metadata": {},
|
38 | 38 | "source": [
|
39 |
| - "## Analyzer template examples" |
| 39 | + "## Analyzer Templates" |
40 | 40 | ]
|
41 | 41 | },
|
42 | 42 | {
|
43 | 43 | "cell_type": "markdown",
|
44 | 44 | "metadata": {},
|
45 | 45 | "source": [
|
46 |
| - "Below is a collection of analyzer template examples designed to extract fields from various input file types.\n", |
| 46 | + "Below is a collection of analyzer templates designed to extract fields from various input file types.\n", |
47 | 47 | "\n",
|
48 |
| - "These templates are highly customizable, allowing you to modify them to suit your specific needs. For additional verified templates from Microsoft, please visit [HERE](../analyzer_templates/README.md)." |
| 48 | + "These templates are highly customizable, allowing you to modify them to suit your specific needs. For additional verified templates from Microsoft, please visit [here](../analyzer_templates/README.md)." |
49 | 49 | ]
|
50 | 50 | },
|
51 | 51 | {
|
52 | 52 | "cell_type": "code",
|
53 |
| - "execution_count": 13, |
| 53 | + "execution_count": null, |
54 | 54 | "metadata": {},
|
55 | 55 | "outputs": [],
|
56 | 56 | "source": [
|
57 |
| - "extraction_samples = {\n", |
58 |
| - " \"sample_invoice\": ('../analyzer_templates/invoice.json', '../data/invoice.pdf'),\n", |
59 |
| - " \"sample_chart\": ('../analyzer_templates/image_chart.json', '../data/pieChart.jpg'),\n", |
60 |
| - " \"sample_call_transcript\": ('../analyzer_templates/call_transcript.json', '../data/callCenterRecording.mp3'),\n", |
61 |
| - " \"sample_marketing_video\": ('../analyzer_templates/marketing_video.json', '../data/video.mp4')\n", |
| 57 | + "extraction_templates = {\n", |
| 58 | + " \"invoice\": ('../analyzer_templates/invoice.json', '../data/invoice.pdf' ),\n", |
| 59 | + " \"chart\": ('../analyzer_templates/image_chart.json', '../data/pieChart.jpg' ),\n", |
| 60 | + " \"call_transcript\": ('../analyzer_templates/call_transcript.json', '../data/callCenterRecording.mp3'),\n", |
| 61 | + " \"marketing_video\": ('../analyzer_templates/marketing_video.json', '../data/video.mp4' )\n", |
62 | 62 | "}"
|
63 | 63 | ]
|
64 | 64 | },
|
65 | 65 | {
|
66 | 66 | "cell_type": "markdown",
|
67 | 67 | "metadata": {},
|
68 | 68 | "source": [
|
69 |
| - "Set the target to the sample analyzer that you want to try." |
| 69 | + "Specify the analyzer template you want to use and provide a name for the analyzer to be created based on the template." |
70 | 70 | ]
|
71 | 71 | },
|
72 | 72 | {
|
73 | 73 | "cell_type": "code",
|
74 |
| - "execution_count": 14, |
| 74 | + "execution_count": null, |
75 | 75 | "metadata": {},
|
76 | 76 | "outputs": [],
|
77 | 77 | "source": [
|
78 |
| - "target_sample = \"sample_invoice\"" |
| 78 | + "import uuid\n", |
| 79 | + "\n", |
| 80 | + "ANALYZER_TEMPLATE = \"invoice\"\n", |
| 81 | + "ANALYZER_ID = \"field-extraction-sample-\" + str(uuid.uuid4())\n", |
| 82 | + "\n", |
| 83 | + "(analyzer_template_path, analyzer_sample_file_path) = extraction_templates[ANALYZER_TEMPLATE]" |
79 | 84 | ]
|
80 | 85 | },
|
81 | 86 | {
|
82 | 87 | "cell_type": "markdown",
|
83 | 88 | "metadata": {},
|
84 | 89 | "source": [
|
85 |
| - "## Create Azure content understanding client\n", |
86 |
| - ">The [AzureContentUnderstandingClient](../python/content_understanding_client.py) is utility Class which contain the functions to interact with the Content Understanding server. Before Content Understanding SDK release, we can regard it as a lightweight SDK. Fill the constant **AZURE_AI_ENDPOINT**, **AZURE_AI_API_VERSION**, **AZURE_AI_API_KEY** with the information from your Azure AI Service." |
| 90 | + "## Create Azure AI Content Understanding Client\n", |
| 91 | + "\n", |
| 92 | + "> The [AzureContentUnderstandingClient](../python/content_understanding_client.py) is a utility class containing functions to interact with the Content Understanding API. Before the official release of the Content Understanding SDK, it can be regarded as a lightweight SDK.\n" |
87 | 93 | ]
|
88 | 94 | },
|
89 | 95 | {
|
90 | 96 | "cell_type": "code",
|
91 |
| - "execution_count": 15, |
| 97 | + "execution_count": null, |
92 | 98 | "metadata": {},
|
93 | 99 | "outputs": [],
|
94 | 100 | "source": [
|
|
98 | 104 | "import sys\n",
|
99 | 105 | "from dotenv import find_dotenv, load_dotenv\n",
|
100 | 106 | "\n",
|
101 |
| - "# import utility package from python samples root directory\n", |
| 107 | + "load_dotenv(find_dotenv())\n", |
| 108 | + "logging.basicConfig(level=logging.INFO)\n", |
| 109 | + "\n", |
| 110 | + "AZURE_AI_ENDPOINT = os.getenv(\"AZURE_AI_ENDPOINT\")\n", |
| 111 | + "AZURE_AI_API_KEY = os.getenv(\"AZURE_AI_API_KEY\")\n", |
| 112 | + "AZURE_AI_API_VERSION = os.getenv(\"AZURE_AI_API_VERSION\", \"2024-12-01-preview\")\n", |
| 113 | + "\n", |
| 114 | + "# Import utility package from python samples root directory\n", |
102 | 115 | "py_samples_root_dir = os.path.abspath(os.path.join(os.getcwd(), \"..\"))\n",
|
103 | 116 | "sys.path.append(py_samples_root_dir)\n",
|
104 | 117 | "from python.content_understanding_client import AzureContentUnderstandingClient\n",
|
105 | 118 | "\n",
|
106 |
| - "load_dotenv(find_dotenv())\n", |
107 |
| - "logging.basicConfig(level=logging.INFO)\n", |
108 |
| - "\n", |
109 | 119 | "client = AzureContentUnderstandingClient(\n",
|
110 |
| - " endpoint=os.getenv(\"AZURE_AI_ENDPOINT\"),\n", |
111 |
| - " api_version=os.getenv(\"AZURE_AI_API_VERSION\", \"2024-12-01-preview\"),\n", |
112 |
| - " subscription_key=os.getenv(\"AZURE_AI_API_KEY\"),\n", |
113 |
| - " api_token=os.getenv(\"AZURE_AI_API_TOKEN\"),\n", |
| 120 | + " endpoint=AZURE_AI_ENDPOINT,\n", |
| 121 | + " subscription_key=AZURE_AI_API_KEY,\n", |
| 122 | + " api_version=AZURE_AI_API_VERSION,\n", |
114 | 123 | " x_ms_useragent=\"azure-ai-content-understanding-python/field_extraction\",\n",
|
115 | 124 | ")"
|
116 | 125 | ]
|
|
119 | 128 | "cell_type": "markdown",
|
120 | 129 | "metadata": {},
|
121 | 130 | "source": [
|
122 |
| - "## Create analyzer with defined schema\n", |
123 |
| - "Before creating the custom fields analyzer, you should fill the constant ANALYZER_ID with a business-related name. Here we randomly generate a name for demo purpose." |
| 131 | + "## Create Analyzer from the Template" |
124 | 132 | ]
|
125 | 133 | },
|
126 | 134 | {
|
127 | 135 | "cell_type": "code",
|
128 |
| - "execution_count": 16, |
| 136 | + "execution_count": null, |
129 | 137 | "metadata": {},
|
130 | 138 | "outputs": [
|
131 | 139 | {
|
|
188 | 196 | }
|
189 | 197 | ],
|
190 | 198 | "source": [
|
191 |
| - "import uuid\n", |
192 |
| - "ANALYZER_ID = \"extraction-sample-\" + str(uuid.uuid4())\n", |
193 |
| - "\n", |
194 |
| - "response = client.begin_create_analyzer(ANALYZER_ID, analyzer_schema_path=extraction_samples[target_sample][0])\n", |
| 199 | + "response = client.begin_create_analyzer(ANALYZER_ID, analyzer_template_path)\n", |
195 | 200 | "result = client.poll_result(response)\n",
|
196 | 201 | "\n",
|
197 |
| - "logging.info(json.dumps(result, indent=2))" |
| 202 | + "print(json.dumps(result, indent=2))" |
198 | 203 | ]
|
199 | 204 | },
|
200 | 205 | {
|
201 | 206 | "cell_type": "markdown",
|
202 | 207 | "metadata": {},
|
203 | 208 | "source": [
|
204 |
| - "## Use created analyzer to extract document content\n" |
| 209 | + "## Extract Fields Using the Analyzer" |
205 | 210 | ]
|
206 | 211 | },
|
207 | 212 | {
|
|
213 | 218 | },
|
214 | 219 | {
|
215 | 220 | "cell_type": "code",
|
216 |
| - "execution_count": 17, |
| 221 | + "execution_count": null, |
217 | 222 | "metadata": {},
|
218 | 223 | "outputs": [
|
219 | 224 | {
|
|
362 | 367 | }
|
363 | 368 | ],
|
364 | 369 | "source": [
|
365 |
| - "response = client.begin_analyze(ANALYZER_ID, file_location=extraction_samples[target_sample][1])\n", |
| 370 | + "response = client.begin_analyze(ANALYZER_ID, file_location=analyzer_sample_file_path)\n", |
366 | 371 | "result = client.poll_result(response)\n",
|
367 | 372 | "\n",
|
368 |
| - "logging.info(json.dumps(result, indent=2))" |
| 373 | + "json.dumps(result, indent=2)" |
369 | 374 | ]
|
370 | 375 | },
|
371 | 376 | {
|
372 | 377 | "cell_type": "markdown",
|
373 | 378 | "metadata": {},
|
374 | 379 | "source": [
|
375 |
| - "## Delete exist analyzer in Content Understanding Service\n", |
376 |
| - "This snippet is not required, but it's only used to prevent the testing analyzer from residing in your service. The custom fields analyzer could be stored in your service for reusing by subsequent business in real usage scenarios.\n", |
377 |
| - "\n" |
| 380 | + "## Clean Up\n", |
| 381 | + "Optionally, delete the sample analyzer from your resource. In typical usage scenarios, you would analyze multiple files using the same analyzer." |
378 | 382 | ]
|
379 | 383 | },
|
380 | 384 | {
|
|
0 commit comments