Skip to main content

Production Workflows for Transformer-based Multi-label Text Classification

Project description

tlmtc: Transfer Learning for Multi-label Text Classification

License CI

tlmtc (Transfer Learning for Multi-label Text Classification) is an opinionated Python package that provides production-ready, end-to-end workflows for fine-tuning pretrained encoder-only transformer models for robust multi-label text classification.

In applied settings, text classification is rarely a simple single-label task or a mutually exclusive multiclass problem. A clinical note may map to several ICD codes at once. A customer-support ticket can span multiple issue categories for routing, prioritization, and analytics. A contract may contain several clause types, and a litigation document can raise multiple legal issues. In RAG and LLM evaluation, a single answer may need several concurrent quality labels. A threat-intelligence report can mention multiple tactics, techniques, and vulnerabilities in the same document. Across domains, useful text labels often overlap, co-occur, and vary in prevalence.

tlmtc turns these use cases into repeatable training and prediction workflows. It prepares multi-label text data, fine-tunes transformer classifiers, tunes hyperparameters and decision thresholds, evaluates model performance, writes reports, and applies trained models to new data — all exposed through a small workflow-oriented API.

Key features

  • Parameter-efficient fine-tuning via LoRA (PEFT), with optional full fine-tuning
  • Automatic detection of single-text and paired-text inputs for encoder and cross-encoder-style classification
  • Customizable Optuna-based hyperparameter tuning, optionally on a smaller proxy model for efficiency
  • Carry-over from proxy to larger target model with optional automatic learning-rate scaling
  • Global and label-specific threshold optimization for calibrated multi-label decisions
  • Iterative stratified data splitting for multi-label datasets
  • Custom class-weighted loss for handling label imbalance
  • Comprehensive evaluation suite with global and label-specific multi-label metrics
  • Publication-ready reporting through tables and graphs
  • End-to-end prediction workflow that reloads trained models, metadata, labels, and thresholds automatically
  • Automatic persistence and reuse of data splits, Optuna studies, trained models, thresholds, and run metadata
  • Highly configurable through a small workflow-oriented Python API and CLI
  • CPU and multi-GPU training and prediction support

Installation

We recommend installing tlmtc in a dedicated uv environment with the full extra:

uv add "tlmtc[full]"

The full extra installs the optional deep-learning dependencies required for training and prediction, including PyTorch, PEFT, and Accelerate.

Using pip?
pip install "tlmtc[full]"
Installing from source?
uv add "tlmtc[full] @ git+https://github.com/saschagobel/tlmtc.git"

Or with pip:

pip install "tlmtc[full] @ git+https://github.com/saschagobel/tlmtc.git"

Quickstart

This quickstart uses a small synthetic paired-text dataset included in the repository. It mimics a requirements-engineering setting, where requirement records are paired with validation, commissioning, configuration, or field-service evidence and labeled for multiple concurrent issues. You will fine-tune a multi-label classifier and then apply the trained model to unlabeled examples.

Create a working directory and download the example data:

mkdir tlmtc-quickstart
cd tlmtc-quickstart

curl -L -o paired_example.csv \
  https://raw.githubusercontent.com/saschagobel/tlmtc/main/examples/paired_example.csv

curl -L -o paired_example_unlabeled.csv \
  https://raw.githubusercontent.com/saschagobel/tlmtc/main/examples/paired_example_unlabeled.csv

The code below assumes that both CSV files are in your current working directory. If you save them somewhere else, adjust the file paths accordingly.

Using Windows PowerShell?
New-Item -ItemType Directory -Force -Path tlmtc-quickstart
Set-Location tlmtc-quickstart

Invoke-WebRequest `
  -Uri "https://raw.githubusercontent.com/saschagobel/tlmtc/main/examples/paired_example.csv" `
  -OutFile "paired_example.csv"

Invoke-WebRequest `
  -Uri "https://raw.githubusercontent.com/saschagobel/tlmtc/main/examples/paired_example_unlabeled.csv" `
  -OutFile "paired_example_unlabeled.csv"

Fine-tune a model:

from tlmtc import train_tlmtc

train_tlmtc(
    "paired_example.csv",
    target_name="Requirements Evidence Alignment",
    checkpoint="google/bert_uncased_L-2_H-128_A-2",
    tuning_trials=5,
    use_cpu=True,
)

This quickstart intentionally uses a tiny model and only five HPO trials to keep the demo lightweight.

Use your fine-tuned model to run prediction on unlabeled data:

from tlmtc import predict_tlmtc

predict_tlmtc(
    "paired_example_unlabeled.csv",
    use_cpu=True,
)

tlmtc writes training artifacts to train_outputs/ and prediction artifacts to prediction_outputs/. Evaluation reports are written to train_outputs/<run_id>/evaluation/.

Prefer the CLI?
tlmtc train \
  --raw-csv paired_example.csv \
  --target_name "Requirements Evidence Alignment" \
  --checkpoint google/bert_uncased_L-2_H-128_A-2 \
  --tuning-trials 5 \
  --use-cpu

tlmtc predict \
  --prediction-csv paired_example_unlabeled.csv \
  --use-cpu
Try your own data

Your training CSV must include:

  • a text column
  • at least two binary label_-prefixed columns

Add a text_pair column for paired-input classification. tlmtc detects and handles this automatically.

For prediction, provide an unlabeled CSV with the same input columns used during training. For a paired-text model, this means both text and text_pair.

Contributing

Contributions are welcome. Please read CONTRIBUTING.md before opening an issue or pull request.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tlmtc-0.1.0.tar.gz (1.2 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tlmtc-0.1.0-py3-none-any.whl (50.4 kB view details)

Uploaded Python 3

File details

Details for the file tlmtc-0.1.0.tar.gz.

File metadata

  • Download URL: tlmtc-0.1.0.tar.gz
  • Upload date:
  • Size: 1.2 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for tlmtc-0.1.0.tar.gz
Algorithm Hash digest
SHA256 4bc705062261048f390e39acd05da577d98b0f3a260b3cf718c0965c8e80d512
MD5 403d04959f533f25e922f21e0716b3bc
BLAKE2b-256 decad921dce4d223a16f99f65b7a66f1cf705371391e5116d41f4615a4dda3f5

See more details on using hashes here.

Provenance

The following attestation bundles were made for tlmtc-0.1.0.tar.gz:

Publisher: release.yml on saschagobel/tlmtc

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tlmtc-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: tlmtc-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 50.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for tlmtc-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 000a7e293a1a53be10baa25ebf1dfbdb2505d971097199273dd8a6733675d284
MD5 10db571ab5a4432a4b02e2e4d9a8a6ae
BLAKE2b-256 ebbeb0cba12f126b550eb58c3ac41ac51c4f328384f6443018981a4e34e6a279

See more details on using hashes here.

Provenance

The following attestation bundles were made for tlmtc-0.1.0-py3-none-any.whl:

Publisher: release.yml on saschagobel/tlmtc

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page