Sync from GitHub via hub-sync
Browse filesFiles changed (2)
- README.md+7-5
- classify-dataset.py+26-15
366 unmodified lines3673683693703713723738 unmodified lines38238338438538638738819 unmodified lines4084094104114124134143 unmodified lines418419420421422366 unmodified linesA GPU is required. Run on Jobs with the vLLM image:```bashhf jobs uv run --flavor l4x1 --image vllm/vllm-openai:latest --secrets HF_TOKEN \https://huggingface.co/datasets/uv-scripts/classification/raw/main/classify-dataset.py \--input-dataset stanfordnlp/imdb \--column text \8 unmodified lines### With reasoning and label descriptions```bashhf jobs uv run --flavor l4x1 --image vllm/vllm-openai:latest --secrets HF_TOKEN \https://huggingface.co/datasets/uv-scripts/classification/raw/main/classify-dataset.py \--input-dataset user/support-tickets \--column content \19 unmodified lines| `--max-samples` | Label only the first N rows (or N random rows with `--shuffle`) || `--shuffle`, `--shuffle-seed` | Shuffle before `--max-samples` (seed default 42) |Run `uv run classify-dataset.py --help` for all options.### Good to know3 unmodified linestruncated to 4,000 characters.- **Label names matter.** Use clear, distinct names, add `--label-descriptions` when names areambiguous, and try a larger model for nuanced tasks.- **vLLM version**: `ImportError: cannot import name GuidedDecodingParams` means the vLLMversion does not match; the script requires `vllm>=0.6.6`.366 unmodified lines3673683693703713723738 unmodified lines38238338438538638738819 unmodified lines4084094104114124134143 unmodified lines418419420421422423424366 unmodified linesA GPU is required. Run on Jobs with the vLLM image:```bashhf jobs uv run --flavor l4x1 --image vllm/vllm-openai:v0.32.0 --secrets HF_TOKEN \https://huggingface.co/datasets/uv-scripts/classification/raw/main/classify-dataset.py \--input-dataset stanfordnlp/imdb \--column text \8 unmodified lines### With reasoning and label descriptions```bashhf jobs uv run --flavor l4x1 --image vllm/vllm-openai:v0.32.0 --secrets HF_TOKEN \https://huggingface.co/datasets/uv-scripts/classification/raw/main/classify-dataset.py \--input-dataset user/support-tickets \--column content \19 unmodified lines| `--max-samples` | Label only the first N rows (or N random rows with `--shuffle`) || `--shuffle`, `--shuffle-seed` | Shuffle before `--max-samples` (seed default 42) |Run `uv run classify-dataset.py --help` for all options (no GPU or vLLM needed).### Good to know3 unmodified linestruncated to 4,000 characters.- **Label names matter.** Use clear, distinct names, add `--label-descriptions` when names areambiguous, and try a larger model for nuanced tasks.- **vLLM version**: on Jobs, vLLM, PyTorch and Transformers come from the `vllm/vllm-openai`image, so the script header does not list them. For a local GPU run (Linux + CUDA), add vLLMyourself: `uv run --with vllm classify-dataset.py ...`. The script needs vLLM 0.12 or newer(`StructuredOutputsParams`).
1 unmodified line234567891011121314158 unmodified lines242526272829303112 unmodified lines4445464748495051525354555657279 unmodified lines33733833934034134234321 unmodified lines365366367368369370230 unmodified lines6016026036046056066076086096106116126136146156166176186196206216221 unmodified line# /// script# requires-python = ">=3.10"# dependencies = [# "vllm>=0.12.0", # StructuredOutputsParams API (guided_decoding removed in 0.12.0)# "transformers>=4.53.0",# "torch",# "datasets",# "huggingface-hub[hf_transfer]",# ]## [tool.hf-jobs]# image = "vllm/vllm-openai:v0.32.0"# python = "/usr/bin/python3"# env = { PYTHONPATH = "/usr/local/lib/python3.12/dist-packages" }8 unmodified linesThis script provides efficient GPU-based classification with guaranteed valid outputs,optimized for running on HF Jobs.Example:uv run classify-dataset.py \\--input-dataset imdb \\--column text \\--labels "positive,negative" \\12 unmodified linesimport loggingimport osimport sysfrom typing import Listimport torchfrom datasets import load_datasetfrom huggingface_hub import HfApi, get_tokenfrom transformers import AutoTokenizerfrom vllm import LLM, SamplingParamsfrom vllm.sampling_params import StructuredOutputsParams# Default model - SmolLM3 for good balance of speed and qualityDEFAULT_MODEL = "HuggingFaceTB/SmolLM3-3B"279 unmodified linesdef prepare_prompts(texts: List[str], labels: List[str], tokenizer: AutoTokenizer,label_descriptions: dict = None, enable_reasoning: bool = False) -> tuple[List[str], List[int]]:"""Prepare prompts using chat template for classification, filtering invalid texts."""21 unmodified linesdef main():args = parse_args()# Check authentication earlylogger.info("Checking authentication...")token = args.hf_token or (os.environ.get("HF_TOKEN") or get_token())230 unmodified linesif __name__ == "__main__":if len(sys.argv) == 1:print("Example commands:")print("\n# Simple classification:")print("uv run classify-dataset.py \\")print(" --input-dataset stanfordnlp/imdb \\")print(" --column text \\")print(" --labels 'positive,negative' \\")print(" --output-dataset user/imdb-classified")print("\n# With label descriptions:")print("uv run classify-dataset.py \\")print(" --input-dataset user/support-tickets \\")print(" --column content \\")print(" --labels 'bug,feature,question' \\")print(" --label-descriptions 'bug:something is broken or not working,feature:request for new functionality,question:asking for help or clarification' \\")print(" --output-dataset user/tickets-classified")print("\n# With reasoning mode (thinking + JSON output):")print("uv run classify-dataset.py \\")print(" --input-dataset stanfordnlp/imdb \\")print(" --column text \\")print(" --labels 'positive,negative,neutral' \\")1 unmodified line2345678910111213141516178 unmodified lines26272829303132333412 unmodified lines4748495051525354555657585960616263279 unmodified lines34334434534634734834921 unmodified lines371372373374375376377378379380381230 unmodified lines6126136146156166176186196206216226236246256266276286296306316326331 unmodified line# /// script# requires-python = ">=3.10"# dependencies = [# "datasets",# "huggingface-hub[hf_transfer]",# ]## [tool.hf-jobs]# # vllm, torch and transformers come from the image (PYTHONPATH). Listing them above makes uv# # install newer PyPI copies plus their deps. PYTHONPATH shadows the copies, but the current# # flashinfer-python wheel (0.7.2.post1) also ships a top-level b12x module that the image lacks.# # The image's vllm imports it and crashes (missing flashinfer/experimental/b12x/_api.py).# # Seen on v0.32.0 (2026-11-05); re-test before moving the tag. Local GPU: uv run --with vllm.# image = "vllm/vllm-openai:v0.32.0"# python = "/usr/bin/python3"# env = { PYTHONPATH = "/usr/local/lib/python3.12/dist-packages" }8 unmodified linesThis script provides efficient GPU-based classification with guaranteed valid outputs,optimized for running on HF Jobs.Example (local GPU, Linux + CUDA; the header leaves vllm, torch and transformers tothe HF Jobs image, so add vllm with --with; the script needs vLLM 0.12 or newer):uv run --with vllm classify-dataset.py \\--input-dataset imdb \\--column text \\--labels "positive,negative" \\12 unmodified linesimport loggingimport osimport sysfrom typing import TYPE_CHECKING, Listfrom datasets import load_datasetfrom huggingface_hub import HfApi, get_tokenif TYPE_CHECKING:from transformers import AutoTokenizer# torch, transformers and vllm are imported inside main(), after argument parsing, so# `uv run classify-dataset.py --help` works without them. On HF Jobs they come from the# vLLM image (see the header).# Default model - SmolLM3 for good balance of speed and qualityDEFAULT_MODEL = "HuggingFaceTB/SmolLM3-3B"279 unmodified linesdef prepare_prompts(texts: List[str], labels: List[str], tokenizer: "AutoTokenizer",label_descriptions: dict = None, enable_reasoning: bool = False) -> tuple[List[str], List[int]]:"""Prepare prompts using chat template for classification, filtering invalid texts."""21 unmodified linesdef main():args = parse_args()import torchfrom transformers import AutoTokenizerfrom vllm import LLM, SamplingParamsfrom vllm.sampling_params import StructuredOutputsParams# Check authentication earlylogger.info("Checking authentication...")token = args.hf_token or (os.environ.get("HF_TOKEN") or get_token())230 unmodified linesif __name__ == "__main__":if len(sys.argv) == 1:print("Example commands (local GPU runs add vLLM with --with; on HF Jobs the image provides it):")print("\n# Simple classification:")print("uv run --with vllm classify-dataset.py \\")print(" --input-dataset stanfordnlp/imdb \\")print(" --column text \\")print(" --labels 'positive,negative' \\")print(" --output-dataset user/imdb-classified")print("\n# With label descriptions:")print("uv run --with vllm classify-dataset.py \\")print(" --input-dataset user/support-tickets \\")print(" --column content \\")print(" --labels 'bug,feature,question' \\")print(" --label-descriptions 'bug:something is broken or not working,feature:request for new functionality,question:asking for help or clarification' \\")print(" --output-dataset user/tickets-classified")print("\n# With reasoning mode (thinking + JSON output):")print("uv run --with vllm classify-dataset.py \\")print(" --input-dataset stanfordnlp/imdb \\")print(" --column text \\")print(" --labels 'positive,negative,neutral' \\")
README.md+7-5
classify-dataset.py+26-15