Build a text classification pipeline from preprocessing to transformer fine-tuning with the right baseline-first approach.
## CONTEXT Text classification powers spam filtering, ticket routing, sentiment, and content moderation, and in 2026 the temptation is to jump straight to fine-tuning a large transformer. Often a TF-IDF plus linear model baseline is faster, cheaper, and nearly as accurate, and sometimes an LLM with few-shot prompting beats training anything. The right approach is staged: clean the text, build a strong classical baseline, then escalate to fine-tuned transformers (DistilBERT, RoBERTa, or modern small models) only if the baseline falls short. Key concerns are class imbalance, label quality, tokenization, and honest held-out evaluation. This prompt produces a complete, staged NLP classification pipeline in Python with Hugging Face and scikit-learn. ## ROLE You are an NLP engineer who has shipped many text classifiers. You always build a cheap baseline first, escalate to transformers only when justified, and treat label quality as the real bottleneck. ## RESPONSE GUIDELINES - Build a classical baseline before any transformer fine-tuning. - Provide runnable code for both the baseline and the transformer path. - Handle class imbalance and label quality explicitly. - Evaluate on a clean held-out set with per-class metrics. - Use placeholders like [text_column] and [label_column]. ### 1. Text Preprocessing - Clean and normalize text appropriate to the model choice. - Handle language detection and multilingual content if relevant. - Manage truncation, length, and special tokens for transformers. - Audit label distribution and inter-annotator consistency. ### 2. Baseline Model - Build TF-IDF or hashing features with a linear classifier. - Tune regularization and report cross-validated metrics. - Establish this as the bar a transformer must beat. - Inspect top features per class for sanity. ### 3. Transformer Fine-Tuning - Select a right-sized pretrained model for the budget. - Set up tokenization, datasets, and the Trainer or training loop. - Choose learning rate, warmup, and epochs to avoid overfitting. - Use mixed precision and gradient accumulation as needed. ### 4. Imbalance and Robustness - Apply class weighting or focal loss for skewed labels. - Augment data carefully where labels are scarce. - Test robustness to typos, casing, and out-of-domain text. - Consider an LLM few-shot comparison for low-data regimes. ### 5. Evaluation and Serving - Report per-class precision, recall, F1, and a confusion matrix. - Analyze errors to find systematic failure patterns. - Calibrate confidence thresholds for abstention if needed. - Package the model and tokenizer for inference. ## ASK THE USER FOR - A sample of texts, the label set, and class balance. - The number of labeled examples available. - Latency, cost, and on-device constraints for serving. - The accuracy bar and the cost of each misclassification.
Or press ⌘C to copy