How to create a text classifier in Azure AI & Machine Learning
This tutorial shows how to create a text classifier using Azure AI & Machine Learning to categorize messages or reviews. Learning to train, register and expose a model as an endpoint is useful to automate content triage, customer support or feedback analysis.
Prerequisites
- Azure account with permissions to create resources (Azure Machine Learning Workspace).
- Azure CLI and Azure ML extension installed or access to the Azure Machine Learning studio.
- Python 3.8+ and pip; packages: azure-ai-ml, scikit-learn, pandas.
- Simple text dataset with a "text" column and a "label" column (CSV).
Step 1: Prepare the environment and the dataset
Why: it is important to have a reproducible environment and a clean dataset. We will create a virtualenv, install dependencies and prepare a CSV with labeled examples.
python -m venv venv
source venv/bin/activate # ou venv\Scripts\activate no Windows
pip install --upgrade pip
pip install azure-ai-ml scikit-learn pandas joblib
# Exemplo mínimo de dataset (criar text_labels.csv):
# text,label
# "Entrega atrasada",complaint
# "Excelente produto",praise
Step 2: Train a model locally (example with scikit-learn)
Why: training locally allows fast iteration. We use a Pipeline with CountVectorizer and LogisticRegression and save the model with joblib.
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.pipeline import make_pipeline
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.linear_model import LogisticRegression
import joblib
df = pd.read_csv('text_labels.csv')
X_train, X_test, y_train, y_test = train_test_split(df['text'], df['label'], test_size=0.2, random_state=42)
model = make_pipeline(CountVectorizer(), LogisticRegression(max_iter=1000))
model.fit(X_train, y_train)
print('Acurácia:', model.score(X_test, y_test))
joblib.dump(model, 'text_classifier.joblib')
Step 3: Register the model in Azure Machine Learning
Why: registering models makes versioning and deployment easier. We will use azure-ai-ml to register the saved model file.
from azure.ai.ml import MLClient
from azure.identity import DefaultAzureCredential
from azure.ai.ml.entities import Model
# Defina estas variáveis de ambiente ou substitua diretamente
subscription_id = ''
resource_group = ''
workspace_name = ''
ml_client = MLClient(DefaultAzureCredential(), subscription_id, resource_group, workspace_name)
model = Model(path='text_classifier.joblib', name='text-classifier', description='Classifier scikit-learn simple')
registered_model = ml_client.models.create_or_update(model)
print('Model registered:', registered_model.name, registered_model.version)
Step 4: Create a container image or real-time endpoint
Why: exposing the model as an endpoint allows invoking predictions from applications. Here I briefly describe how to create a real-time endpoint with a minimal scoring script.
# scoring_script.py (mínimo)
import json
import joblib
model = None
def init():
global model
model = joblib.load('text_classifier.joblib')
def run(raw_data):
data = json.loads(raw_data)
texts = data.get('texts', [])
preds = model.predict(texts)
return json.dumps({'predictions': preds.tolist()})
Then, in Azure ML, create a Deployment (real-time) pointing to the registered model and the scoring script. In the studio or via CLI/azure-ai-ml, configure the image/container with dependencies (scikit-learn) and upload the model file and the scoring script.
Step 5: Test the endpoint with a request
Why: validating the endpoint ensures the end-to-end pipeline works. We use curl or requests in Python to send texts and get classes.
# Exemplo em Python usando requests (obter URL e chave do endpoint no Azure ML)
import requests
import json
endpoint_url = 'https:///score'
api_key = ''
headers = {'Content-Type': 'application/json', 'Authorization': f'Bearer {api_key}'}
payload = json.dumps({'texts': ['Entrega atrasada e sem resposta', 'Adorei a qualidade']})
resp = requests.post(endpoint_url, headers=headers, data=payload)
print(resp.json())
Check the result
Confirm that the model responds to the endpoint and that the predictions make sense: the JSON response should contain the expected labels. Check local metrics (accuracy, confusion matrix) and the deployment logs for common errors such as missing dependencies or incorrect paths.
Conclusion
You now have a basic flow: train locally, register the model in Azure Machine Learning and expose it as a real-time endpoint. Next steps: improve the dataset, use cross-validation, experiment with pre-trained NLP models or convert the pipeline to a Job in Azure ML for automated training. Tip: start by properly labeling the most frequent examples to get quick gains in performance.