Commit
Β·
011bc8a
1
Parent(s):
1d07414
README.md
Browse files
README.md
CHANGED
|
@@ -3,9 +3,9 @@ viewer: false
|
|
| 3 |
tags: [uv-script, classification, vllm, structured-outputs, gpu-required]
|
| 4 |
---
|
| 5 |
|
| 6 |
-
# Dataset Classification
|
| 7 |
|
| 8 |
-
|
| 9 |
|
| 10 |
## π Quick Start
|
| 11 |
|
|
@@ -22,21 +22,25 @@ That's it! No installation, no setup - just `uv run`.
|
|
| 22 |
|
| 23 |
## π Requirements
|
| 24 |
|
| 25 |
-
- **GPU Required**:
|
| 26 |
- Python 3.10+
|
| 27 |
- UV (will handle all dependencies automatically)
|
| 28 |
-
- vLLM >= 0.6.6
|
| 29 |
|
| 30 |
## π― Features
|
| 31 |
|
| 32 |
-
- **Guaranteed valid outputs** using
|
| 33 |
-
- **Zero-shot classification**
|
| 34 |
-
- **GPU-optimized**
|
| 35 |
-
- **Default model**: HuggingFaceTB/SmolLM3-3B (fast 3B model
|
| 36 |
- **Robust text handling** with preprocessing and validation
|
| 37 |
-
- **Three prompt styles** for different use cases
|
| 38 |
- **Automatic progress tracking** and detailed statistics
|
| 39 |
- **Direct Hub integration** - read and write datasets seamlessly
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
## π» Usage
|
| 42 |
|
|
@@ -62,20 +66,50 @@ uv run classify-dataset.py \
|
|
| 62 |
**Optional:**
|
| 63 |
|
| 64 |
- `--model`: Model to use (default: **`HuggingFaceTB/SmolLM3-3B`** - a fast 3B parameter model)
|
| 65 |
-
- `--
|
|
|
|
| 66 |
- `--split`: Dataset split to process (default: `train`)
|
| 67 |
- `--max-samples`: Limit samples for testing
|
|
|
|
|
|
|
| 68 |
- `--temperature`: Generation temperature (default: 0.1)
|
| 69 |
- `--guided-backend`: Backend for guided decoding (default: `outlines`)
|
| 70 |
- `--hf-token`: Hugging Face token (or use `HF_TOKEN` env var)
|
| 71 |
|
| 72 |
-
###
|
| 73 |
|
| 74 |
-
|
| 75 |
-
- **detailed**: Emphasizes exact category matching
|
| 76 |
-
- **reasoning**: Includes brief analysis before classification
|
| 77 |
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
## π Examples
|
| 81 |
|
|
@@ -96,8 +130,8 @@ uv run classify-dataset.py \
|
|
| 96 |
--input-dataset user/support-tickets \
|
| 97 |
--column content \
|
| 98 |
--labels "bug,feature_request,question,other" \
|
| 99 |
-
--
|
| 100 |
-
--
|
| 101 |
```
|
| 102 |
|
| 103 |
### News Categorization
|
|
@@ -111,11 +145,54 @@ uv run classify-dataset.py \
|
|
| 111 |
--model meta-llama/Llama-3.2-3B-Instruct
|
| 112 |
```
|
| 113 |
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
-
This
|
| 117 |
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
# Run on L4 GPU with vLLM image
|
| 120 |
hf jobs uv run \
|
| 121 |
--flavor l4x1 \
|
|
@@ -125,6 +202,7 @@ hf jobs uv run \
|
|
| 125 |
--column text \
|
| 126 |
--labels "positive,negative" \
|
| 127 |
--output-dataset user/imdb-classified
|
|
|
|
| 128 |
|
| 129 |
### GPU Flavors
|
| 130 |
- `t4-small`: Budget option for smaller models
|
|
@@ -135,6 +213,27 @@ hf jobs uv run \
|
|
| 135 |
|
| 136 |
## π§ Advanced Usage
|
| 137 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
### Using Different Models
|
| 139 |
|
| 140 |
By default, this script uses **HuggingFaceTB/SmolLM3-3B** - a fast, efficient 3B parameter model that's perfect for most classification tasks. You can easily use any other instruction-tuned model:
|
|
@@ -147,7 +246,7 @@ uv run classify-dataset.py \
|
|
| 147 |
--labels "contract,patent,brief,memo,other" \
|
| 148 |
--output-dataset user/legal-classified \
|
| 149 |
--model Qwen/Qwen2.5-7B-Instruct
|
| 150 |
-
|
| 151 |
|
| 152 |
### Large Datasets
|
| 153 |
|
|
@@ -166,15 +265,16 @@ uv run classify-dataset.py \
|
|
| 166 |
- **SmolLM3-3B (default)**: ~50-100 texts/second on A10
|
| 167 |
- **7B models**: ~20-50 texts/second on A10
|
| 168 |
- vLLM automatically optimizes batching for best throughput
|
|
|
|
| 169 |
|
| 170 |
## π€ How It Works
|
| 171 |
|
| 172 |
-
1. **vLLM**: Provides efficient GPU batch inference
|
| 173 |
-
2. **Guided Decoding**: Uses outlines to guarantee valid label outputs
|
| 174 |
3. **Structured Generation**: Constrains model outputs to exact label choices
|
| 175 |
4. **UV**: Handles all dependencies automatically
|
| 176 |
|
| 177 |
-
The script loads your dataset, preprocesses texts, classifies each one
|
| 178 |
|
| 179 |
## π Troubleshooting
|
| 180 |
|
|
@@ -212,43 +312,28 @@ If you see `ImportError: cannot import name 'GuidedDecodingParams'`:
|
|
| 212 |
- The script specifies the correct version in its dependencies
|
| 213 |
- UV should automatically install the correct version
|
| 214 |
|
| 215 |
-
## π¬ Advanced
|
| 216 |
|
| 217 |
-
For
|
| 218 |
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
uv run prepare_arxiv_2024.py
|
| 224 |
-
```
|
| 225 |
|
| 226 |
-
|
|
|
|
|
|
|
| 227 |
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
|
|
|
|
|
|
| 233 |
```
|
| 234 |
|
| 235 |
-
This script demonstrates:
|
| 236 |
-
|
| 237 |
-
- Using `run_uv_job()` from the Python API
|
| 238 |
-
- Classifying into modern ML trends (reasoning, agents, multimodal, robotics, etc.)
|
| 239 |
-
- Handling authentication and job monitoring
|
| 240 |
-
|
| 241 |
-
The classification categories include:
|
| 242 |
-
|
| 243 |
-
- `reasoning_systems`: Chain-of-thought, reasoning, problem solving
|
| 244 |
-
- `agents_autonomous`: Agents, tool use, autonomous systems
|
| 245 |
-
- `multimodal_models`: Vision-language, audio, multi-modal
|
| 246 |
-
- `robotics_embodied`: Robotics, embodied AI, manipulation
|
| 247 |
-
- `efficient_inference`: Quantization, distillation, edge deployment
|
| 248 |
-
- `alignment_safety`: RLHF, alignment, safety, interpretability
|
| 249 |
-
- `generative_models`: Diffusion, generation, synthesis
|
| 250 |
-
- `foundational_other`: Other foundational ML/AI research
|
| 251 |
-
|
| 252 |
## π License
|
| 253 |
|
| 254 |
This script is provided as-is for use with the UV Scripts organization.
|
|
|
|
| 3 |
tags: [uv-script, classification, vllm, structured-outputs, gpu-required]
|
| 4 |
---
|
| 5 |
|
| 6 |
+
# Dataset Classification Script
|
| 7 |
|
| 8 |
+
GPU-accelerated text classification for Hugging Face datasets with guaranteed valid outputs through structured generation.
|
| 9 |
|
| 10 |
## π Quick Start
|
| 11 |
|
|
|
|
| 22 |
|
| 23 |
## π Requirements
|
| 24 |
|
| 25 |
+
- **GPU Required**: Uses GPU-accelerated inference
|
| 26 |
- Python 3.10+
|
| 27 |
- UV (will handle all dependencies automatically)
|
| 28 |
+
- vLLM >= 0.6.6
|
| 29 |
|
| 30 |
## π― Features
|
| 31 |
|
| 32 |
+
- **Guaranteed valid outputs** using structured generation with guided decoding
|
| 33 |
+
- **Zero-shot classification** without training data required
|
| 34 |
+
- **GPU-optimized** for maximum throughput and efficiency
|
| 35 |
+
- **Default model**: HuggingFaceTB/SmolLM3-3B (fast 3B model with thinking capabilities)
|
| 36 |
- **Robust text handling** with preprocessing and validation
|
|
|
|
| 37 |
- **Automatic progress tracking** and detailed statistics
|
| 38 |
- **Direct Hub integration** - read and write datasets seamlessly
|
| 39 |
+
- **Label descriptions** support for providing context to improve accuracy
|
| 40 |
+
- **Reasoning mode** for interpretable classifications with thinking traces
|
| 41 |
+
- **JSON output parsing** for reliable extraction from reasoning mode
|
| 42 |
+
- **Optimized batching** with vLLM's automatic batch processing
|
| 43 |
+
- **Multiple guided backends** - supports outlines, xgrammar, and more
|
| 44 |
|
| 45 |
## π» Usage
|
| 46 |
|
|
|
|
| 66 |
**Optional:**
|
| 67 |
|
| 68 |
- `--model`: Model to use (default: **`HuggingFaceTB/SmolLM3-3B`** - a fast 3B parameter model)
|
| 69 |
+
- `--label-descriptions`: Provide descriptions for each label to improve classification accuracy
|
| 70 |
+
- `--enable-reasoning`: Enable reasoning mode with thinking traces (adds reasoning column)
|
| 71 |
- `--split`: Dataset split to process (default: `train`)
|
| 72 |
- `--max-samples`: Limit samples for testing
|
| 73 |
+
- `--shuffle`: Shuffle dataset before selecting samples (useful for random sampling)
|
| 74 |
+
- `--shuffle-seed`: Random seed for shuffling (default: 42)
|
| 75 |
- `--temperature`: Generation temperature (default: 0.1)
|
| 76 |
- `--guided-backend`: Backend for guided decoding (default: `outlines`)
|
| 77 |
- `--hf-token`: Hugging Face token (or use `HF_TOKEN` env var)
|
| 78 |
|
| 79 |
+
### Label Descriptions
|
| 80 |
|
| 81 |
+
Provide context for your labels to improve classification accuracy:
|
|
|
|
|
|
|
| 82 |
|
| 83 |
+
```bash
|
| 84 |
+
uv run classify-dataset.py \
|
| 85 |
+
--input-dataset user/support-tickets \
|
| 86 |
+
--column content \
|
| 87 |
+
--labels "bug,feature,question,other" \
|
| 88 |
+
--label-descriptions "bug:something is broken,feature:request for new functionality,question:asking for help,other:anything else" \
|
| 89 |
+
--output-dataset user/tickets-classified
|
| 90 |
+
```
|
| 91 |
+
|
| 92 |
+
The model uses these descriptions to better understand what each label represents, leading to more accurate classifications.
|
| 93 |
+
|
| 94 |
+
### Reasoning Mode
|
| 95 |
+
|
| 96 |
+
Enable thinking traces for interpretable classifications:
|
| 97 |
+
|
| 98 |
+
```bash
|
| 99 |
+
uv run classify-dataset.py \
|
| 100 |
+
--input-dataset stanfordnlp/imdb \
|
| 101 |
+
--column text \
|
| 102 |
+
--labels "positive,negative,neutral" \
|
| 103 |
+
--enable-reasoning \
|
| 104 |
+
--output-dataset user/imdb-with-reasoning
|
| 105 |
+
```
|
| 106 |
+
|
| 107 |
+
When `--enable-reasoning` is used:
|
| 108 |
+
- The model generates step-by-step reasoning using SmolLM3's thinking capabilities
|
| 109 |
+
- Output includes three columns: `classification`, `reasoning`, and `parsing_success`
|
| 110 |
+
- Final answer must be in JSON format: `{"label": "chosen_label"}`
|
| 111 |
+
- Useful for understanding complex classification decisions
|
| 112 |
+
- Trade-off: Slower but more interpretable
|
| 113 |
|
| 114 |
## π Examples
|
| 115 |
|
|
|
|
| 130 |
--input-dataset user/support-tickets \
|
| 131 |
--column content \
|
| 132 |
--labels "bug,feature_request,question,other" \
|
| 133 |
+
--label-descriptions "bug:code or product not working as expected,feature_request:asking for new functionality,question:seeking help or clarification,other:general comments or feedback" \
|
| 134 |
+
--output-dataset user/tickets-classified
|
| 135 |
```
|
| 136 |
|
| 137 |
### News Categorization
|
|
|
|
| 145 |
--model meta-llama/Llama-3.2-3B-Instruct
|
| 146 |
```
|
| 147 |
|
| 148 |
+
### Complex Classification with Reasoning
|
| 149 |
+
|
| 150 |
+
```bash
|
| 151 |
+
uv run classify-dataset.py \
|
| 152 |
+
--input-dataset user/customer-feedback \
|
| 153 |
+
--column text \
|
| 154 |
+
--labels "very_positive,positive,neutral,negative,very_negative" \
|
| 155 |
+
--label-descriptions "very_positive:extremely satisfied,positive:generally satisfied,neutral:mixed feelings,negative:dissatisfied,very_negative:extremely dissatisfied" \
|
| 156 |
+
--enable-reasoning \
|
| 157 |
+
--output-dataset user/feedback-analyzed
|
| 158 |
+
```
|
| 159 |
|
| 160 |
+
This combines label descriptions with reasoning mode for maximum interpretability.
|
| 161 |
|
| 162 |
+
### ArXiv ML Research Classification
|
| 163 |
+
|
| 164 |
+
Classify academic papers into machine learning research areas:
|
| 165 |
+
|
| 166 |
+
```bash
|
| 167 |
+
# Fast classification with random sampling
|
| 168 |
+
uv run classify-dataset.py \
|
| 169 |
+
--input-dataset librarian-bots/arxiv-metadata-snapshot \
|
| 170 |
+
--column abstract \
|
| 171 |
+
--labels "llm,computer_vision,reinforcement_learning,optimization,theory,other" \
|
| 172 |
+
--label-descriptions "llm:language models and NLP,computer_vision:image and video processing,reinforcement_learning:RL and decision making,optimization:training and efficiency,theory:theoretical ML foundations,other:other ML topics" \
|
| 173 |
+
--output-dataset user/arxiv-ml-classified \
|
| 174 |
+
--split "train[:10000]" \
|
| 175 |
+
--max-samples 100 \
|
| 176 |
+
--shuffle
|
| 177 |
+
|
| 178 |
+
# With reasoning for nuanced classification
|
| 179 |
+
uv run classify-dataset.py \
|
| 180 |
+
--input-dataset librarian-bots/arxiv-metadata-snapshot \
|
| 181 |
+
--column abstract \
|
| 182 |
+
--labels "multimodal,agents,reasoning,safety,efficiency" \
|
| 183 |
+
--label-descriptions "multimodal:vision-language and cross-modal models,agents:autonomous agents and tool use,reasoning:reasoning and planning systems,safety:alignment and safety research,efficiency:model optimization and deployment" \
|
| 184 |
+
--enable-reasoning \
|
| 185 |
+
--output-dataset user/arxiv-frontier-research \
|
| 186 |
+
--split "train[:1000]" \
|
| 187 |
+
--max-samples 50
|
| 188 |
+
```
|
| 189 |
+
|
| 190 |
+
The reasoning mode is particularly valuable for academic abstracts where papers often span multiple topics and require careful analysis to determine the primary focus.
|
| 191 |
+
|
| 192 |
+
## π Running on HF Jobs
|
| 193 |
+
|
| 194 |
+
Optimized for [Hugging Face Jobs](https://huggingface.co/docs/hub/spaces-gpu-jobs) (requires Pro subscription or Team/Enterprise organization):
|
| 195 |
+
```bash
|
| 196 |
# Run on L4 GPU with vLLM image
|
| 197 |
hf jobs uv run \
|
| 198 |
--flavor l4x1 \
|
|
|
|
| 202 |
--column text \
|
| 203 |
--labels "positive,negative" \
|
| 204 |
--output-dataset user/imdb-classified
|
| 205 |
+
```
|
| 206 |
|
| 207 |
### GPU Flavors
|
| 208 |
- `t4-small`: Budget option for smaller models
|
|
|
|
| 213 |
|
| 214 |
## π§ Advanced Usage
|
| 215 |
|
| 216 |
+
### Random Sampling
|
| 217 |
+
|
| 218 |
+
When working with ordered datasets, use `--shuffle` with `--max-samples` to get a representative sample:
|
| 219 |
+
|
| 220 |
+
```bash
|
| 221 |
+
# Get 50 random reviews instead of the first 50
|
| 222 |
+
uv run classify-dataset.py \
|
| 223 |
+
--input-dataset stanfordnlp/imdb \
|
| 224 |
+
--column text \
|
| 225 |
+
--labels "positive,negative" \
|
| 226 |
+
--output-dataset user/imdb-sample \
|
| 227 |
+
--max-samples 50 \
|
| 228 |
+
--shuffle \
|
| 229 |
+
--shuffle-seed 123 # For reproducibility
|
| 230 |
+
```
|
| 231 |
+
|
| 232 |
+
This is especially important for:
|
| 233 |
+
- Chronologically ordered datasets (news, papers, social media)
|
| 234 |
+
- Pre-sorted datasets (by rating, category, etc.)
|
| 235 |
+
- Testing on diverse samples before processing the full dataset
|
| 236 |
+
|
| 237 |
### Using Different Models
|
| 238 |
|
| 239 |
By default, this script uses **HuggingFaceTB/SmolLM3-3B** - a fast, efficient 3B parameter model that's perfect for most classification tasks. You can easily use any other instruction-tuned model:
|
|
|
|
| 246 |
--labels "contract,patent,brief,memo,other" \
|
| 247 |
--output-dataset user/legal-classified \
|
| 248 |
--model Qwen/Qwen2.5-7B-Instruct
|
| 249 |
+
```
|
| 250 |
|
| 251 |
### Large Datasets
|
| 252 |
|
|
|
|
| 265 |
- **SmolLM3-3B (default)**: ~50-100 texts/second on A10
|
| 266 |
- **7B models**: ~20-50 texts/second on A10
|
| 267 |
- vLLM automatically optimizes batching for best throughput
|
| 268 |
+
- Performance scales with GPU memory and compute capability
|
| 269 |
|
| 270 |
## π€ How It Works
|
| 271 |
|
| 272 |
+
1. **vLLM**: Provides efficient GPU batch inference with automatic batching
|
| 273 |
+
2. **Guided Decoding**: Uses outlines backend to guarantee valid label outputs
|
| 274 |
3. **Structured Generation**: Constrains model outputs to exact label choices
|
| 275 |
4. **UV**: Handles all dependencies automatically
|
| 276 |
|
| 277 |
+
The script loads your dataset, preprocesses texts, classifies each one with guaranteed valid outputs, then saves the results as a new column in the output dataset.
|
| 278 |
|
| 279 |
## π Troubleshooting
|
| 280 |
|
|
|
|
| 312 |
- The script specifies the correct version in its dependencies
|
| 313 |
- UV should automatically install the correct version
|
| 314 |
|
| 315 |
+
## π¬ Advanced Workflows
|
| 316 |
|
| 317 |
+
For complex real-world workflows that integrate UV scripts with the Python HF Jobs API, see the [ArXiv ML Trends example](examples/arxiv-workflow/). This demonstrates:
|
| 318 |
|
| 319 |
+
- **Multi-stage pipelines**: Data preparation β GPU classification β Analysis
|
| 320 |
+
- **Python API orchestration**: Using `run_uv_job()` to manage GPU jobs programmatically
|
| 321 |
+
- **Production patterns**: Error handling, parallel execution, and incremental updates
|
| 322 |
+
- **Cost optimization**: Choosing appropriate compute resources for each task
|
|
|
|
|
|
|
| 323 |
|
| 324 |
+
```python
|
| 325 |
+
# Example: Submit a classification job via Python API
|
| 326 |
+
from huggingface_hub import run_uv_job
|
| 327 |
|
| 328 |
+
job = run_uv_job(
|
| 329 |
+
script="https://huggingface.co/datasets/uv-scripts/classification/raw/main/classify-dataset.py",
|
| 330 |
+
args=["--input-dataset", "my/dataset", "--labels", "A,B,C"],
|
| 331 |
+
flavor="l4x1",
|
| 332 |
+
image="vllm/vllm-openai:latest"
|
| 333 |
+
)
|
| 334 |
+
result = job.wait()
|
| 335 |
```
|
| 336 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 337 |
## π License
|
| 338 |
|
| 339 |
This script is provided as-is for use with the UV Scripts organization.
|