Sync from GitHub via hub-sync
Browse files- README.md +7 -5
- classify-dataset.py +26 -15
README.md
CHANGED
|
@@ -367,7 +367,7 @@ Two scripts do this. `classify-dataset.py` runs on vLLM and is the one to start
|
|
| 367 |
A GPU is required. Run on Jobs with the vLLM image:
|
| 368 |
|
| 369 |
```bash
|
| 370 |
-
hf jobs uv run --flavor l4x1 --image vllm/vllm-openai:
|
| 371 |
https://huggingface.co/datasets/uv-scripts/classification/raw/main/classify-dataset.py \
|
| 372 |
--input-dataset stanfordnlp/imdb \
|
| 373 |
--column text \
|
|
@@ -382,7 +382,7 @@ date or label. Drop both to label the whole split.
|
|
| 382 |
### With reasoning and label descriptions
|
| 383 |
|
| 384 |
```bash
|
| 385 |
-
hf jobs uv run --flavor l4x1 --image vllm/vllm-openai:
|
| 386 |
https://huggingface.co/datasets/uv-scripts/classification/raw/main/classify-dataset.py \
|
| 387 |
--input-dataset user/support-tickets \
|
| 388 |
--column content \
|
|
@@ -408,7 +408,7 @@ model must end with `{"label": "..."}`, and rows where that cannot be parsed are
|
|
| 408 |
| `--max-samples` | Label only the first N rows (or N random rows with `--shuffle`) |
|
| 409 |
| `--shuffle`, `--shuffle-seed` | Shuffle before `--max-samples` (seed default 42) |
|
| 410 |
|
| 411 |
-
Run `uv run classify-dataset.py --help` for all options.
|
| 412 |
|
| 413 |
### Good to know
|
| 414 |
|
|
@@ -418,5 +418,7 @@ Run `uv run classify-dataset.py --help` for all options.
|
|
| 418 |
truncated to 4,000 characters.
|
| 419 |
- **Label names matter.** Use clear, distinct names, add `--label-descriptions` when names are
|
| 420 |
ambiguous, and try a larger model for nuanced tasks.
|
| 421 |
-
- **vLLM version**:
|
| 422 |
-
|
|
|
|
|
|
|
|
|
| 367 |
A GPU is required. Run on Jobs with the vLLM image:
|
| 368 |
|
| 369 |
```bash
|
| 370 |
+
hf jobs uv run --flavor l4x1 --image vllm/vllm-openai:v0.32.0 --secrets HF_TOKEN \
|
| 371 |
https://huggingface.co/datasets/uv-scripts/classification/raw/main/classify-dataset.py \
|
| 372 |
--input-dataset stanfordnlp/imdb \
|
| 373 |
--column text \
|
|
|
|
| 382 |
### With reasoning and label descriptions
|
| 383 |
|
| 384 |
```bash
|
| 385 |
+
hf jobs uv run --flavor l4x1 --image vllm/vllm-openai:v0.32.0 --secrets HF_TOKEN \
|
| 386 |
https://huggingface.co/datasets/uv-scripts/classification/raw/main/classify-dataset.py \
|
| 387 |
--input-dataset user/support-tickets \
|
| 388 |
--column content \
|
|
|
|
| 408 |
| `--max-samples` | Label only the first N rows (or N random rows with `--shuffle`) |
|
| 409 |
| `--shuffle`, `--shuffle-seed` | Shuffle before `--max-samples` (seed default 42) |
|
| 410 |
|
| 411 |
+
Run `uv run classify-dataset.py --help` for all options (no GPU or vLLM needed).
|
| 412 |
|
| 413 |
### Good to know
|
| 414 |
|
|
|
|
| 418 |
truncated to 4,000 characters.
|
| 419 |
- **Label names matter.** Use clear, distinct names, add `--label-descriptions` when names are
|
| 420 |
ambiguous, and try a larger model for nuanced tasks.
|
| 421 |
+
- **vLLM version**: on Jobs, vLLM, PyTorch and Transformers come from the `vllm/vllm-openai`
|
| 422 |
+
image, so the script header does not list them. For a local GPU run (Linux + CUDA), add vLLM
|
| 423 |
+
yourself: `uv run --with vllm classify-dataset.py ...`. The script needs vLLM 0.12 or newer
|
| 424 |
+
(`StructuredOutputsParams`).
|
classify-dataset.py
CHANGED
|
@@ -2,14 +2,16 @@
|
|
| 2 |
# /// script
|
| 3 |
# requires-python = ">=3.10"
|
| 4 |
# dependencies = [
|
| 5 |
-
# "vllm>=0.12.0", # StructuredOutputsParams API (guided_decoding removed in 0.12.0)
|
| 6 |
-
# "transformers>=4.53.0",
|
| 7 |
-
# "torch",
|
| 8 |
# "datasets",
|
| 9 |
# "huggingface-hub[hf_transfer]",
|
| 10 |
# ]
|
| 11 |
#
|
| 12 |
# [tool.hf-jobs]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
# image = "vllm/vllm-openai:v0.32.0"
|
| 14 |
# python = "/usr/bin/python3"
|
| 15 |
# env = { PYTHONPATH = "/usr/local/lib/python3.12/dist-packages" }
|
|
@@ -24,8 +26,9 @@ Classify text columns in Hugging Face datasets using vLLM with structured output
|
|
| 24 |
This script provides efficient GPU-based classification with guaranteed valid outputs,
|
| 25 |
optimized for running on HF Jobs.
|
| 26 |
|
| 27 |
-
Example
|
| 28 |
-
|
|
|
|
| 29 |
--input-dataset imdb \\
|
| 30 |
--column text \\
|
| 31 |
--labels "positive,negative" \\
|
|
@@ -44,14 +47,17 @@ import argparse
|
|
| 44 |
import logging
|
| 45 |
import os
|
| 46 |
import sys
|
| 47 |
-
from typing import List
|
| 48 |
|
| 49 |
-
import torch
|
| 50 |
from datasets import load_dataset
|
| 51 |
from huggingface_hub import HfApi, get_token
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
from
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
# Default model - SmolLM3 for good balance of speed and quality
|
| 57 |
DEFAULT_MODEL = "HuggingFaceTB/SmolLM3-3B"
|
|
@@ -337,7 +343,7 @@ def validate_text(text: str) -> bool:
|
|
| 337 |
|
| 338 |
|
| 339 |
def prepare_prompts(
|
| 340 |
-
texts: List[str], labels: List[str], tokenizer: AutoTokenizer,
|
| 341 |
label_descriptions: dict = None, enable_reasoning: bool = False
|
| 342 |
) -> tuple[List[str], List[int]]:
|
| 343 |
"""Prepare prompts using chat template for classification, filtering invalid texts."""
|
|
@@ -365,6 +371,11 @@ def prepare_prompts(
|
|
| 365 |
def main():
|
| 366 |
args = parse_args()
|
| 367 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 368 |
# Check authentication early
|
| 369 |
logger.info("Checking authentication...")
|
| 370 |
token = args.hf_token or (os.environ.get("HF_TOKEN") or get_token())
|
|
@@ -601,22 +612,22 @@ def main():
|
|
| 601 |
|
| 602 |
if __name__ == "__main__":
|
| 603 |
if len(sys.argv) == 1:
|
| 604 |
-
print("Example commands:")
|
| 605 |
print("\n# Simple classification:")
|
| 606 |
-
print("uv run classify-dataset.py \\")
|
| 607 |
print(" --input-dataset stanfordnlp/imdb \\")
|
| 608 |
print(" --column text \\")
|
| 609 |
print(" --labels 'positive,negative' \\")
|
| 610 |
print(" --output-dataset user/imdb-classified")
|
| 611 |
print("\n# With label descriptions:")
|
| 612 |
-
print("uv run classify-dataset.py \\")
|
| 613 |
print(" --input-dataset user/support-tickets \\")
|
| 614 |
print(" --column content \\")
|
| 615 |
print(" --labels 'bug,feature,question' \\")
|
| 616 |
print(" --label-descriptions 'bug:something is broken or not working,feature:request for new functionality,question:asking for help or clarification' \\")
|
| 617 |
print(" --output-dataset user/tickets-classified")
|
| 618 |
print("\n# With reasoning mode (thinking + JSON output):")
|
| 619 |
-
print("uv run classify-dataset.py \\")
|
| 620 |
print(" --input-dataset stanfordnlp/imdb \\")
|
| 621 |
print(" --column text \\")
|
| 622 |
print(" --labels 'positive,negative,neutral' \\")
|
|
|
|
| 2 |
# /// script
|
| 3 |
# requires-python = ">=3.10"
|
| 4 |
# dependencies = [
|
|
|
|
|
|
|
|
|
|
| 5 |
# "datasets",
|
| 6 |
# "huggingface-hub[hf_transfer]",
|
| 7 |
# ]
|
| 8 |
#
|
| 9 |
# [tool.hf-jobs]
|
| 10 |
+
# # vllm, torch and transformers come from the image (PYTHONPATH). Listing them above makes uv
|
| 11 |
+
# # install newer PyPI copies plus their deps. PYTHONPATH shadows the copies, but the current
|
| 12 |
+
# # flashinfer-python wheel (0.7.2.post1) also ships a top-level b12x module that the image lacks.
|
| 13 |
+
# # The image's vllm imports it and crashes (missing flashinfer/experimental/b12x/_api.py).
|
| 14 |
+
# # Seen on v0.32.0 (2026-11-05); re-test before moving the tag. Local GPU: uv run --with vllm.
|
| 15 |
# image = "vllm/vllm-openai:v0.32.0"
|
| 16 |
# python = "/usr/bin/python3"
|
| 17 |
# env = { PYTHONPATH = "/usr/local/lib/python3.12/dist-packages" }
|
|
|
|
| 26 |
This script provides efficient GPU-based classification with guaranteed valid outputs,
|
| 27 |
optimized for running on HF Jobs.
|
| 28 |
|
| 29 |
+
Example (local GPU, Linux + CUDA; the header leaves vllm, torch and transformers to
|
| 30 |
+
the HF Jobs image, so add vllm with --with; the script needs vLLM 0.12 or newer):
|
| 31 |
+
uv run --with vllm classify-dataset.py \\
|
| 32 |
--input-dataset imdb \\
|
| 33 |
--column text \\
|
| 34 |
--labels "positive,negative" \\
|
|
|
|
| 47 |
import logging
|
| 48 |
import os
|
| 49 |
import sys
|
| 50 |
+
from typing import TYPE_CHECKING, List
|
| 51 |
|
|
|
|
| 52 |
from datasets import load_dataset
|
| 53 |
from huggingface_hub import HfApi, get_token
|
| 54 |
+
|
| 55 |
+
if TYPE_CHECKING:
|
| 56 |
+
from transformers import AutoTokenizer
|
| 57 |
+
|
| 58 |
+
# torch, transformers and vllm are imported inside main(), after argument parsing, so
|
| 59 |
+
# `uv run classify-dataset.py --help` works without them. On HF Jobs they come from the
|
| 60 |
+
# vLLM image (see the header).
|
| 61 |
|
| 62 |
# Default model - SmolLM3 for good balance of speed and quality
|
| 63 |
DEFAULT_MODEL = "HuggingFaceTB/SmolLM3-3B"
|
|
|
|
| 343 |
|
| 344 |
|
| 345 |
def prepare_prompts(
|
| 346 |
+
texts: List[str], labels: List[str], tokenizer: "AutoTokenizer",
|
| 347 |
label_descriptions: dict = None, enable_reasoning: bool = False
|
| 348 |
) -> tuple[List[str], List[int]]:
|
| 349 |
"""Prepare prompts using chat template for classification, filtering invalid texts."""
|
|
|
|
| 371 |
def main():
|
| 372 |
args = parse_args()
|
| 373 |
|
| 374 |
+
import torch
|
| 375 |
+
from transformers import AutoTokenizer
|
| 376 |
+
from vllm import LLM, SamplingParams
|
| 377 |
+
from vllm.sampling_params import StructuredOutputsParams
|
| 378 |
+
|
| 379 |
# Check authentication early
|
| 380 |
logger.info("Checking authentication...")
|
| 381 |
token = args.hf_token or (os.environ.get("HF_TOKEN") or get_token())
|
|
|
|
| 612 |
|
| 613 |
if __name__ == "__main__":
|
| 614 |
if len(sys.argv) == 1:
|
| 615 |
+
print("Example commands (local GPU runs add vLLM with --with; on HF Jobs the image provides it):")
|
| 616 |
print("\n# Simple classification:")
|
| 617 |
+
print("uv run --with vllm classify-dataset.py \\")
|
| 618 |
print(" --input-dataset stanfordnlp/imdb \\")
|
| 619 |
print(" --column text \\")
|
| 620 |
print(" --labels 'positive,negative' \\")
|
| 621 |
print(" --output-dataset user/imdb-classified")
|
| 622 |
print("\n# With label descriptions:")
|
| 623 |
+
print("uv run --with vllm classify-dataset.py \\")
|
| 624 |
print(" --input-dataset user/support-tickets \\")
|
| 625 |
print(" --column content \\")
|
| 626 |
print(" --labels 'bug,feature,question' \\")
|
| 627 |
print(" --label-descriptions 'bug:something is broken or not working,feature:request for new functionality,question:asking for help or clarification' \\")
|
| 628 |
print(" --output-dataset user/tickets-classified")
|
| 629 |
print("\n# With reasoning mode (thinking + JSON output):")
|
| 630 |
+
print("uv run --with vllm classify-dataset.py \\")
|
| 631 |
print(" --input-dataset stanfordnlp/imdb \\")
|
| 632 |
print(" --column text \\")
|
| 633 |
print(" --labels 'positive,negative,neutral' \\")
|