Overview
This exercise demonstrates how to create a custom text classification model using Azure AI Language Service. The model is trained to categorize text into predefined classes.
Steps & Configuration Details
1. Provision an Azure AI Language Resource
- Open Azure Portal (https://portal.azure.com) and sign in.
- Select Create a resource → Search for Language Service → Click Create.
- Configuration Items:
- Subscription: Your Azure subscription.
- Resource Group: Select or create a resource group.
- Region: Choose from:
- Australia East
- Central India
- East US
- North Europe
- South Central US
- Switzerland North
- UK South
- West Europe
- West US 2
- West US 3
- Name: Enter a unique name.
- Pricing Tier:
F0 (Free)
orS (Standard)
. - Storage Account: Create a new storage account.
- Storage Account Type:
Standard LRS
. - Responsible AI Notice: Agree.
After provisioning, navigate to Keys and Endpoint in the Resource Management section.
2. Configure Storage Access
- Open Azure Portal → Navigate to Storage Account.
- Select Access Control (IAM) → Add Role Assignment.
- Configuration Items:
- Role:
Storage Blob Data Contributor
- Assign Access To:
User, group, or service principal
- Select Members: Choose your user account.
- Role:
3. Upload Sample Articles
- Download sample articles:
https://aka.ms/classification-articles
- Extract files and upload them to Azure Blob Storage:
- Create a Blob Container named
articles
. - Set Anonymous Access Level to
Container (anonymous read access for containers and blobs)
. - Upload extracted
.txt
files.
- Create a Blob Container named
4. Create a Custom Text Classification Project
- Open Language Studio (https://language.cognitive.azure.com) and sign in.
- Select Custom Text Classification → Create a project.
- Configuration Items:
- Project Type:
Single Label Classification
- Project Name:
ClassifyLab
- Primary Language:
English (US)
- Description:
Custom text classification lab
- Blob Store Container:
articles
- Labeling Option:
No, I need to label my files as part of this project
- Project Type:
5. Label Training Data
- Navigate to Data Labeling.
- Define four classes:
Classifieds
Sports
News
Entertainment
- Assign labels to articles:
Article 1 → Sports (Training) Article 10 → News (Training) Article 11 → Entertainment (Testing) Article 12 → News (Testing) Article 13 → Sports (Testing) Article 2 → Sports (Training) Article 3 → Classifieds (Training) Article 4 → Classifieds (Training) Article 5 → Entertainment (Training) Article 6 → Entertainment (Training) Article 7 → News (Training) Article 8 → News (Training) Article 9 → Entertainment (Training)
- Save labels.
6. Train the Model
- Navigate to Training Jobs → Start a Training Job.
- Configuration Items:
- Model Name:
ClassifyArticles
- Training Mode:
Manual Split
- Model Name:
- Click Train.
7. Evaluate the Model
- Navigate to Model Performance → Select
ClassifyArticles
. - Review Accuracy Metrics.
- If errors exist, check Test Set Details.
8. Deploy the Model
- Navigate to Deploying Model → Add Deployment.
- Configuration Items:
- Deployment Name:
articles
- Model:
ClassifyArticles
- Deployment Name:
- Click Deploy.
9. Configure Your Application
- Clone the repository:
git clone https://github.com/MicrosoftLearning/mslearn-ai-language
- Open the folder in Visual Studio Code.
- Install dependencies:
- C#:
dotnet add package Azure.AI.TextAnalytics --version 5.3.0
- Python:
pip install azure-ai-textanalytics==5.3.0
- C#:
- Open the configuration file:
- C#:
appsettings.json
- Python:
.env
- C#:
- Update Configuration Values:
- Azure AI Language Endpoint
- API Key
- Project Name (
ClassifyLab
) - Deployment Name (
articles
)
- Save the configuration file.
10. Add Code to Classify Documents
- Open the code file:
- C#:
Program.cs
- Python:
classify-text.py
- C#:
- Add references:
- C#:
using Azure; using Azure.AI.TextAnalytics;
- Python:
from azure.core.credentials import AzureKeyCredential from azure.ai.textanalytics import TextAnalyticsClient
- C#:
- Create the AI Language client:
- C#:
AzureKeyCredential credentials = new AzureKeyCredential(aiSvcKey); Uri endpoint = new Uri(aiSvcEndpoint); TextAnalyticsClient aiClient = new TextAnalyticsClient(endpoint, credentials);
- Python:
credential = AzureKeyCredential(ai_key) ai_client = TextAnalyticsClient(endpoint=ai_endpoint, credential=credential)
- C#:
- Submit classification requests:
- C#:
ClassifyDocumentOperation operation = await aiClient.SingleLabelClassifyAsync(WaitUntil.Completed, batchedDocuments, projectName, deploymentName); foreach (ClassifyDocumentResult documentResult in operation.Value) { Console.WriteLine($"Category: {documentResult.ClassificationCategories[0].Category}"); Console.WriteLine($"Confidence Score: {documentResult.ClassificationCategories[0].ConfidenceScore}"); }
- Python:
operation = ai_client.begin_single_label_classify(batchedDocuments, project_name=project_name, deployment_name=deployment_name) document_results = operation.result() for classification_result in document_results: print(f"Category: {classification_result.classifications[0].category}") print(f"Confidence Score: {classification_result.classifications[0].confidence_score}")
- C#:
11. Run Your Application
- C#:
dotnet run
- Python:
python classify-text.py
- Observe the output, which should display classifications and confidence scores.
12. Clean Up
- Delete Azure resources to avoid unnecessary costs:
- Open Azure Portal (https://portal.azure.com).
- Navigate to Resource Groups.
- Select the resource group and click Delete.
This summary captures the essential steps while highlighting all configuration items and code references required for creating a custom text classification model using Azure AI Language.