Implement a Python script to classify text using a pre-trained Transformer model.
- User
Implement a Python script to classify text using a pre-trained Transformer model.
Assistant
1. Understand the Goal: The user wants a Python script to classify text using a pre-trained Transformer model. This means using libraries like
transformers(Hugging Face) andtorch(ortensorflow).-
Identify Key Components:
- Library:
transformersis the industry standard. - Model: Need a pre-trained model fine-tuned for text classification (e.g.,
distilbert-base-uncased-finetuned-sst-2-englishfor sentiment analysis is a classic example). - Tokenizer: Needs to match the model.
- Input: A list of text strings.
- Process: Tokenize Model Inference Softmax/Argmax Label.
- Output: The predicted class and confidence score.
- Library:
-
Choose the Approach:
- Option A: Pipeline API. Easiest, quickest, best for general use.
- Option B: Direct Model Class. More control, shows understanding of the architecture.
- Option C: Custom Dataset/Training. Too complex for a "classify text" request unless specified.
Decision: Provide the
pipelineapproach first because it's the most practical "one-liner" solution. Then, provide the "Native PyTorch/Transformers" approach for users who need more control over tokenization and inference steps. This adds value by covering different skill levels/needs. -
Drafting the Code (Mental or Scratchpad):
-
Approach 1 (Pipeline):
from transformers import pipeline classifier = pipeline("text-classification") print(classifier("I love this movie.")) -
Approach 2 (Native - DistilBERT):
from transformers import AutoTokenizer, AutoModelForSequenceClassification import torch model_name = "distilbert-base-uncased-finetuned-sst-2-english" tokenizer = AutoTokenizer.from_pretrained(model_name) model = AutoModelForSequenceClassification.from_pretrained(model_name) inputs = tokenizer("I love this", return_tensors="pt") with torch.no_grad(): logits = model(**inputs).logits predicted_class_id = logits.argmax().item()
-
-
Refining the Script:
- Add error handling (try-except for imports).
- Add comments explaining the steps.
- Use a specific model name to ensure reproducibility (SST-2 for sentiment is the standard demo).
- Add a requirements list (
pip install ...).
-
Structuring the Response:
- Prerequisites: Mention
pip install transformers torch. - Method 1: The Pipeline (High-Level): Best for quick implementation.
- Method 2: The Native Approach (Low-Level): Best for granular control.
- Explanation: Briefly explain what's happening under the hood.
- Prerequisites: Mention
-
Self-Correction/Review during drafting:
- Check: Did the user specify which classification? No. I should pick Sentiment Analysis as the default example but mention how to change it.
- Check: Is GPU support needed? I should add
device=0logic or at least mention it, but keep the default CPU-compatible so it runs everywhere. - Refinement: In the native approach, make sure to map the IDs back to labels (POSITIVE/NEGATIVE) so the output is readable, rather than just printing "0" or "1". The
model.config.id2labeldictionary is useful here.
-
Final Code Construction (similar to the provided good output):
- Script 1 (Pipeline): Simple, readable.
- Script 2 (Native): Import specific classes. Tokenize with padding/truncation. Move to model. Get logits. Apply softmax. Print result.
-
Final Polish: Add clear formatting, code blocks, and instructions on how to run it. (This aligns with the provided good response).
(Self-Correction during output generation): I initially thought about using BERT, but DistilBERT is faster and lighter for a demo script. I will stick with
distilbert-base-uncased-finetuned-sst-2-english.- Generate Output. (Proceeding to generate the Python script). Here are two ways to implement text classification using a Transformer model.
Prerequisites
You will need to install the
transformerslibrary and a backend (PyTorch or TensorFlow).pip install transformers torch
Approach 1: The "Pipeline" (Easiest)
The Hugging Face
pipelinefunction is the highest-level API. It handles tokenization, model inference, and output formatting automatically. This is best for quick implementation.from transformers import pipeline def classify_with_pipeline(texts): # Initialize the classification pipeline # This downloads a default sentiment analysis model (usually DistilBERT) classifier = pipeline("text-classification", model="distilbert-base-uncased-finetuned-sst-2-english") results = classifier(texts) return results # Example Usage if __name__ == "__main__": sample_texts = [ "I absolutely loved the new movie! The acting was great.", "The food was terrible and the service was slow." ] predictions = classify_with_pipeline(sample_texts) for text, pred in zip(sample_texts, predictions): print(f"Text: {text}") print(f"Label: {pred['label']}, Score: {pred['score']:.4f}\n")
Approach 2: Native PyTorch (More Control)
This approach manually handles tokenization and model inference. This is required if you need to process data in batches manually, modify model architecture, or use specific tokenization settings.
import torch from transformers import AutoTokenizer, AutoModelForSequenceClassification # 1. Load Model and Tokenizer # We use a specific pre-trained model fine-tuned for sentiment (SST-2) model_name = "distilbert-base-uncased-finetuned-sst-2-english" tokenizer = AutoTokenizer.from_pretrained(model_name) model = AutoModelForSequenceClassification.from_pretrained(model_name) def classify_manual(texts): # 2. Tokenize Input # padding=True: Ensures all sequences in batch are same length # truncation=True: Cuts off text longer than model max length # return_tensors="pt": Returns PyTorch tensors inputs = tokenizer(texts, padding=True, truncation=True, return_tensors="pt") # 3. Inference # Use torch.no_grad() to disable gradient calculation (saves memory/speed) with torch.no_grad(): outputs = model(**inputs) # 4. Process Logits # outputs.logits are the raw scores for each class logits = outputs.logits # Apply softmax to convert logits to probabilities probabilities = torch.nn.functional.softmax(logits, dim=-1) # Get the highest probability class ID predicted_class_ids = torch.argmax(probabilities, dim=-1) # Map IDs to Labels (e.g., 0 -> NEGATIVE, 1 -> POSITIVE) results = [] for i, text in enumerate(texts): class_id = predicted_class_ids[i].item() score = probabilities[i][class_id].item() label = model.config.id2label[class_id] results.append({ "text": text, "label": label, "score": score }) return results # Example Usage if __name__ == "__main__": sample_texts = [ "The interface is intuitive and easy to use.", "I encountered a bug and lost all my progress." ] predictions = classify_manual(sample_texts) for pred in predictions: print(f"Text: {pred['text']}") print(f"Label: {pred['label']}, Score: {pred['score']:.4f}\n")How it works
- Tokenizer: Converts raw text string into numbers (Input IDs) that the model understands. It adds special tokens like
[CLS](start) and[SEP](end). - Model: A pre-trained DistilBERT model. It takes the numbers and outputs "logits" (raw predictions).
- Softmax: Converts the raw logits into probabilities (percentages) that sum to 1.0.
- Argmax: Selects the class with the highest probability.
How to use different models
If you want to classify something other than Sentiment (e.g., Spam vs Ham, or Topic Classification), simply change the
model_namevariable to a model from the Hugging Face Hub.For example, to detect emotion:
model_name = "bhadresh-savani/distilbert-base-uncased-emotion"-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.