Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 25 additions & 23 deletions notebooks/analyzer_training.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -83,57 +83,59 @@
"metadata": {},
"outputs": [],
"source": [
"from datetime import datetime\n",
"import logging\n",
"import json\n",
"import os\n",
"import sys\n",
"import time\n",
"import uuid\n",
"from typing import Any, Optional\n",
"from dotenv import find_dotenv, load_dotenv\n",
"from azure.storage.blob import ContainerSasPermissions\n",
"# Add the parent directory to the Python path to import the sample_helper module\n",
"sys.path.append(os.path.join(os.path.dirname(os.getcwd()), 'python'))\n",
"from content_understanding_client import AzureContentUnderstandingClient\n",
"from extension.document_processor import DocumentProcessor\n",
"from extension.sample_helper import save_json_to_file \n",
"from azure.core.credentials import AzureKeyCredential\n",
"from azure.identity import DefaultAzureCredential\n",
"\n",
"load_dotenv(find_dotenv())\n",
"logging.basicConfig(level=logging.INFO)\n",
"\n",
"# API Configuration\n",
"API_VERSION = \"2025-11-01\" # GA version\n",
"\n",
"# For authentication, you can use either token-based auth or subscription key; only one is required\n",
"AZURE_AI_ENDPOINT = os.getenv(\"AZURE_AI_ENDPOINT\")\n",
"# IMPORTANT: Replace with your actual subscription key or set it in your \".env\" file if not using token authentication\n",
"AZURE_AI_API_KEY = os.getenv(\"AZURE_AI_API_KEY\")\n",
"API_VERSION = \"2025-11-01\"\n",
"\n",
"# IMPORTANT: Choose your authentication method\n",
"# Option 1: Using Subscription Key (simpler but less secure)\n",
"if AZURE_AI_API_KEY:\n",
" client = AzureContentUnderstandingClient(\n",
" endpoint=AZURE_AI_ENDPOINT,\n",
" api_version=API_VERSION,\n",
" subscription_key=AZURE_AI_API_KEY\n",
" )\n",
" print(\"✅ AzureContentUnderstandingClient created with subscription key\")\n",
"else:\n",
" # Option 2: Using Azure AD Token Provider (recommended for production)\n",
"# Create token provider for Azure AD authentication\n",
"def token_provider():\n",
" credential = DefaultAzureCredential()\n",
" \n",
" # Create a token provider function that returns the access token\n",
" def get_token():\n",
" token = credential.get_token(\"https://cognitiveservices.azure.com/.default\")\n",
" return token.token\n",
" \n",
" token = credential.get_token(\"https://cognitiveservices.azure.com/.default\")\n",
" return token.token\n",
"\n",
"# Create the Content Understanding client\n",
"try:\n",
" client = AzureContentUnderstandingClient(\n",
" endpoint=AZURE_AI_ENDPOINT,\n",
" api_version=API_VERSION,\n",
" token_provider=get_token\n",
" subscription_key=AZURE_AI_API_KEY,\n",
" token_provider=token_provider if not AZURE_AI_API_KEY else None,\n",
" x_ms_useragent=\"azure-ai-content-understanding-python-sample-ga\" # The user agent is used for tracking sample usage and does not provide identity information. You can change this if you want to opt out of tracking.\n",
" )\n",
" print(\"✅ AzureContentUnderstandingClient created with token provider\")\n",
" credential_type = \"Subscription Key\" if AZURE_AI_API_KEY else \"Azure AD Token\"\n",
" print(f\"✅ Client created successfully\")\n",
" print(f\" Endpoint: {AZURE_AI_ENDPOINT}\")\n",
" print(f\" Credential: {credential_type}\")\n",
" print(f\" API Version: {API_VERSION}\")\n",
"except Exception as e:\n",
" credential_type = \"Subscription Key\" if AZURE_AI_API_KEY else \"Azure AD Token\"\n",
" print(f\"❌ Failed to create client\")\n",
" print(f\" Endpoint: {AZURE_AI_ENDPOINT}\")\n",
" print(f\" Credential: {credential_type}\")\n",
" print(f\" Error: {e}\")\n",
" raise\n",
"\n",
"try:\n",
" processor = DocumentProcessor(client)\n",
Expand Down
56 changes: 32 additions & 24 deletions notebooks/classifier.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -52,54 +52,56 @@
"metadata": {},
"outputs": [],
"source": [
"from datetime import datetime\n",
"import logging\n",
"import json\n",
"import os\n",
"import sys\n",
"import time\n",
"from typing import Any, Optional\n",
"from dotenv import find_dotenv, load_dotenv\n",
"# Add the parent directory to the Python path to import the sample_helper module\n",
"sys.path.append(os.path.join(os.path.dirname(os.getcwd()), 'python'))\n",
"from content_understanding_client import AzureContentUnderstandingClient\n",
"from extension.sample_helper import save_json_to_file \n",
"from azure.core.credentials import AzureKeyCredential\n",
"from azure.identity import DefaultAzureCredential\n",
"\n",
"load_dotenv(find_dotenv())\n",
"logging.basicConfig(level=logging.INFO)\n",
"\n",
"# API Configuration\n",
"API_VERSION = \"2025-11-01\" # GA version\n",
"\n",
"# For authentication, you can use either token-based auth or subscription key; only one is required\n",
"AZURE_AI_ENDPOINT = os.getenv(\"AZURE_AI_ENDPOINT\")\n",
"# IMPORTANT: Replace with your actual subscription key or set it in your \".env\" file if not using token authentication\n",
"AZURE_AI_API_KEY = os.getenv(\"AZURE_AI_API_KEY\")\n",
"API_VERSION = \"2025-11-01\"\n",
"\n",
"# IMPORTANT: Choose your authentication method\n",
"# Option 1: Using Subscription Key (simpler but less secure)\n",
"if AZURE_AI_API_KEY:\n",
" client = AzureContentUnderstandingClient(\n",
" endpoint=AZURE_AI_ENDPOINT,\n",
" api_version=API_VERSION,\n",
" subscription_key=AZURE_AI_API_KEY\n",
" )\n",
" print(\"✅ AzureContentUnderstandingClient created with subscription key\")\n",
"else:\n",
" # Option 2: Using Azure AD Token Provider (recommended for production)\n",
"# Create token provider for Azure AD authentication\n",
"def token_provider():\n",
" credential = DefaultAzureCredential()\n",
" \n",
" # Create a token provider function that returns the access token\n",
" def get_token():\n",
" token = credential.get_token(\"https://cognitiveservices.azure.com/.default\")\n",
" return token.token\n",
" \n",
" token = credential.get_token(\"https://cognitiveservices.azure.com/.default\")\n",
" return token.token\n",
"\n",
"# Create the Content Understanding client\n",
"try:\n",
" client = AzureContentUnderstandingClient(\n",
" endpoint=AZURE_AI_ENDPOINT,\n",
" api_version=API_VERSION,\n",
" token_provider=get_token\n",
" subscription_key=AZURE_AI_API_KEY,\n",
" token_provider=token_provider if not AZURE_AI_API_KEY else None,\n",
" x_ms_useragent=\"azure-ai-content-understanding-python-sample-ga\" # The user agent is used for tracking sample usage and does not provide identity information. You can change this if you want to opt out of tracking.\n",
" )\n",
" print(\"✅ AzureContentUnderstandingClient created with token provider\")"
" credential_type = \"Subscription Key\" if AZURE_AI_API_KEY else \"Azure AD Token\"\n",
" print(f\"✅ Client created successfully\")\n",
" print(f\" Endpoint: {AZURE_AI_ENDPOINT}\")\n",
" print(f\" Credential: {credential_type}\")\n",
" print(f\" API Version: {API_VERSION}\")\n",
"except Exception as e:\n",
" credential_type = \"Subscription Key\" if AZURE_AI_API_KEY else \"Azure AD Token\"\n",
" print(f\"❌ Failed to create client\")\n",
" print(f\" Endpoint: {AZURE_AI_ENDPOINT}\")\n",
" print(f\" Credential: {credential_type}\")\n",
" print(f\" Error: {e}\")\n",
" raise"
]
},
{
Expand Down Expand Up @@ -193,7 +195,13 @@
"1. Create a custom classifier\n",
"2. Classify a document from a remote URL\n",
"3. Save the classification result to a file\n",
"4. Clean up the created classifier"
"4. Clean up the created classifier\n",
"\n",
"In Azure AI Content Understanding, classification is integrated directly into the analyzer operation rather than requiring a separate API. To create a classifier, you define **`contentCategories`** within the analyzer's configuration, specifying up to 200 category names and descriptions that the service will use to categorize your input files. \n",
"\n",
"The **`enableSegment`** parameter controls how the classifier handles multi-document files: when set to `true`, it automatically splits and classifies different document types within a single file (useful for processing combined documents like a loan application package containing multiple forms), while setting it to `false` treats the entire file as a single document. \n",
"\n",
"For more detailed information about classification capabilities, best practices, and advanced scenarios, see the [Content Understanding classification documentation](https://learn.microsoft.com/en-us/azure/ai-services/content-understanding/concepts/classifier)."
]
},
{
Expand Down
6 changes: 2 additions & 4 deletions notebooks/content_extraction.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,8 @@
"source": [
"from datetime import datetime\n",
"import logging\n",
"import json\n",
"import os\n",
"import sys\n",
"import asyncio\n",
"import re\n",
"from typing import Any, Optional\n",
"from dotenv import find_dotenv, load_dotenv\n",
"\n",
Expand Down Expand Up @@ -93,7 +90,8 @@
" endpoint=AZURE_AI_ENDPOINT,\n",
" api_version=API_VERSION,\n",
" subscription_key=AZURE_AI_API_KEY,\n",
" token_provider=token_provider if not AZURE_AI_API_KEY else None\n",
" token_provider=token_provider if not AZURE_AI_API_KEY else None,\n",
" x_ms_useragent=\"azure-ai-content-understanding-python-sample-ga\" # The user agent is used for tracking sample usage and does not provide identity information. You can change this if you want to opt out of tracking.\n",
" )\n",
" credential_type = \"Subscription Key\" if AZURE_AI_API_KEY else \"Azure AD Token\"\n",
" print(f\"✅ Client created successfully\")\n",
Expand Down
28 changes: 21 additions & 7 deletions notebooks/field_extraction.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"import os\n",
"import sys\n",
"import asyncio\n",
"from typing import Any, Optional\n",
"from dotenv import find_dotenv, load_dotenv\n",
"\n",
"# Add the parent directory to the Python path to import the sample_helper module\n",
Expand All @@ -87,13 +88,26 @@
" return token.token\n",
"\n",
"# Create the Content Understanding client\n",
"client = AzureContentUnderstandingClient(\n",
" endpoint=AZURE_AI_ENDPOINT,\n",
" api_version=API_VERSION,\n",
" subscription_key=AZURE_AI_API_KEY,\n",
" token_provider=token_provider if not AZURE_AI_API_KEY else None\n",
")\n",
"print(\"✅ ContentUnderstandingClient created successfully\")\n",
"try:\n",
" client = AzureContentUnderstandingClient(\n",
" endpoint=AZURE_AI_ENDPOINT,\n",
" api_version=API_VERSION,\n",
" subscription_key=AZURE_AI_API_KEY,\n",
" token_provider=token_provider if not AZURE_AI_API_KEY else None,\n",
" x_ms_useragent=\"azure-ai-content-understanding-python-sample-ga\" # The user agent is used for tracking sample usage and does not provide identity information. You can change this if you want to opt out of tracking.\n",
" )\n",
" credential_type = \"Subscription Key\" if AZURE_AI_API_KEY else \"Azure AD Token\"\n",
" print(f\"✅ Client created successfully\")\n",
" print(f\" Endpoint: {AZURE_AI_ENDPOINT}\")\n",
" print(f\" Credential: {credential_type}\")\n",
" print(f\" API Version: {API_VERSION}\")\n",
"except Exception as e:\n",
" credential_type = \"Subscription Key\" if AZURE_AI_API_KEY else \"Azure AD Token\"\n",
" print(f\"❌ Failed to create client\")\n",
" print(f\" Endpoint: {AZURE_AI_ENDPOINT}\")\n",
" print(f\" Credential: {credential_type}\")\n",
" print(f\" Error: {e}\")\n",
" raise\n",
"\n",
"try:\n",
" processor = DocumentProcessor(client)\n",
Expand Down
5 changes: 4 additions & 1 deletion notebooks/management.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,11 @@
"metadata": {},
"outputs": [],
"source": [
"from datetime import datetime\n",
"import logging\n",
"import os\n",
"import sys\n",
"from typing import Any, Optional\n",
"from dotenv import find_dotenv, load_dotenv\n",
"\n",
"# Add the parent directory to the Python path to import the sample_helper module\n",
Expand Down Expand Up @@ -87,7 +89,8 @@
" endpoint=AZURE_AI_ENDPOINT,\n",
" api_version=API_VERSION,\n",
" subscription_key=AZURE_AI_API_KEY,\n",
" token_provider=token_provider if not AZURE_AI_API_KEY else None\n",
" token_provider=token_provider if not AZURE_AI_API_KEY else None,\n",
" x_ms_useragent=\"azure-ai-content-understanding-python-sample-ga\" # The user agent is used for tracking sample usage and does not provide identity information. You can change this if you want to opt out of tracking.\n",
" )\n",
" credential_type = \"Subscription Key\" if AZURE_AI_API_KEY else \"Azure AD Token\"\n",
" print(f\"✅ Client created successfully\")\n",
Expand Down
Loading