A modern CLI that performs stance classification with natural language explanations using AI models (Google Gemini and OpenAI GPT).
This tool is primarily designed for the UStanceBR corpus — a collection of stance detection datasets, composed by tweets annotated with "for" or "against" labels across multiple political targets. It performs two main tasks:
- Classification + Explanation: Classifies texts and provides natural language explanations
- Metrics Calculation: Compares LLM classifications with ground truth labels
- Interactive CLI: User-friendly command-line interface with guided prompts
- Stance Classification: Classifies texts as "for" or "against" with natural language explanations
- Metrics Calculation: Accuracy, precision, recall, F1-score, and confusion matrix
- Concurrent Processing: Processes texts in batches of 50 using Promise.all for speed
- Multi-Model Support: Works with GPT-5, Gemini 2.0 Flash, Gemini 2.5 Pro, and Gemini 3 Flash Preview
- Multi-Language: Explanations available in Portuguese, English, and Spanish
- Excel Compatibility: Reads and writes Excel files with structured data
-
Install Bun (JavaScript runtime):
curl -fsSL https://bun.sh/install | bashFor more options, visit the Bun installation guide.
-
Clone the Repository:
git clone https://github.com/yourusername/label-explainer.git cd label-explainer -
Install Dependencies:
bun install
-
Set Up API Keys: Create a
.envfile in the root directory:# For Google Gemini models: GOOGLE_GENERATIVE_AI_API_KEY=your_google_api_key_here # For OpenAI GPT models: OPENAI_API_KEY=your_openai_api_key_here
- Get Google API key: Google AI Studio
- Get OpenAI API key: OpenAI Platform
Your input Excel files should have the following structure:
- Column A: Tweet/text to classify
The CLI will prompt you for the input file path when you run it.
When using the metrics command, you'll need two files:
- Generated file: Output from the classify command (text + LLM label)
- Original file: Ground truth labels (text in Column A, label in Column C)
Start the interactive CLI:
bun run src/cli.tsThe CLI provides two main commands:
Run stance classification on a dataset with the following interactive prompts:
- Model selection: Choose from GPT-5, Gemini 2.0 Flash, Gemini 2.5 Pro, or Gemini 3 Flash Preview
- Target selection: Select the classification target (bolsonaro, cloroquina, coronavac, globo, igreja, lula)
- Language: Choose explanation language (Portuguese, English, Spanish)
- Input file: Path to your Excel file with texts to classify
- Output file: Path for the output Excel file with classifications and explanations
The classifier processes texts in batches of 50 concurrently using Promise.all for optimal performance.
Calculate classification accuracy metrics by comparing generated labels with ground truth:
- Accuracy, Precision, Recall, F1-Score per class
- Confusion Matrix visualization
- Macro-averaged metrics
- Optional JSON export for detailed analysis
gpt-5- OpenAI's latest modelgemini-2.0-flash- Fast and efficientgemini-2.5-pro- More accurate but slowergemini-3-flash-preview- Google's latest flash model (preview)
Default targets for UStanceBR corpus:
bolsonarocloroquinacoronavacgloboigrejalula
The CLI classifier performs the following steps:
- Load Data: Reads texts from the input Excel file
- Batch Processing: Processes texts in batches of 50 concurrently
- Classify with LLM: Uses AI to classify each text's stance (for/against)
- Generate Explanations: Provides natural language explanations for each classification
- Save Results: Outputs Excel file with texts, labels, and explanations
- Show Summary: Displays classification distribution statistics
label-explainer/
├── src/
│ ├── prompts/ # AI prompt templates
│ │ └── classification.ts # Prompt for classifying and explaining
│ ├── services/ # Core services
│ │ └── excel.ts # Excel file operations
│ ├── utils/ # Utility functions
│ │ ├── common.ts # Common utilities
│ │ └── models.ts # AI model configurations
│ └── cli.ts # Interactive CLI (classify & metrics)
└── README.md
The CLI generates Excel files with the following columns:
| Column | Content |
|---|---|
| A | Original text |
| B | LLM-generated label (for/against) |
| C | LLM explanation |
Output files are named based on your input (e.g., output-bolsonaro-gpt-5.xlsx)
- Update
src/utils/models.tswith your model configuration - Add the model type to
ModelTypetype definition - Add the model to
VALID_MODELSinsrc/cli.ts
The classification prompt is stored in src/prompts/classification.ts. You can customize:
- Classification criteria for "for" and "against" labels
- Implicit stance detection rules
- Response format and language
MIT — do what you want, just give credit ✨
Built for processing the UStanceBR corpus and designed to be extensible for other NLP tasks.