Metadata-Version: 2.4
Name: modelbrew-cleaner
Version: 0.1.0
Summary: Fine-tuning dataset cleaner: typed PII with checksum validation, semantic dedup, label-error detection, prompt-safety and 250+ quality checks for instruction/chat/DPO data.
Author: ModelBrew
License: Apache-2.0
Project-URL: Homepage, https://modelbrew.ai
Project-URL: Hugging Face, https://huggingface.co/ModelBrew
Keywords: dataset,cleaning,fine-tuning,llm,data-quality,pii,deduplication
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: pydantic>=2
Requires-Dist: rapidfuzz>=3
Requires-Dist: numpy>=1.24
Provides-Extra: embeddings
Requires-Dist: sentence-transformers>=2.2; extra == "embeddings"
Provides-Extra: llm
Requires-Dist: anthropic>=0.30; extra == "llm"
Provides-Extra: ml
Requires-Dist: transformers>=4.40; extra == "ml"
Requires-Dist: tiktoken; extra == "ml"
Provides-Extra: all
Requires-Dist: sentence-transformers>=2.2; extra == "all"
Requires-Dist: anthropic>=0.30; extra == "all"
Requires-Dist: transformers>=4.40; extra == "all"
Requires-Dist: tiktoken; extra == "all"
Dynamic: license-file

# modelbrew-cleaner

Fine-tuning dataset cleaner by [ModelBrew](https://modelbrew.ai). One call runs 90+ quality checks over instruction, chat, prompt/completion, and DPO/preference data — then exports a cleaned file with the broken rows dropped.

Bad data breaks fine-tunes before anything else gets the chance. In our own measurements, four dataset confounds accounted for a 96.9-percentage-point swing in measured catastrophic forgetting — with the training algorithm held constant. This is the cleaner we built for our own pipeline.

## Install

```bash
pip install modelbrew-cleaner            # core checks (regex/fuzzy/statistical)
pip install "modelbrew-cleaner[embeddings]"  # + semantic near-duplicate detection
pip install "modelbrew-cleaner[llm]"         # + LLM-judge checks (needs ANTHROPIC_API_KEY)
```

## Quickstart

```python
from modelbrew_cleaner import clean_file, issue_summary, export_clean

rows = clean_file("train.jsonl")          # .jsonl, .json, or .csv
print(issue_summary(rows))                # {'critical': 4, 'warning': 7, 'suggestion': 6}

for row in rows:
    for issue in row.issues:
        print(row.row_index, issue.severity.value, issue.code, issue.message)

cleaned = export_clean(rows)              # critical rows dropped
```

Real output on the bundled dirty sample (`tests/sample_data/sample.jsonl`):

```
row 1  [warning]    duplicate_exact:     Exact duplicate row
row 2  [warning]    duplicate_near:      Near-duplicate of row 0 (93% similar)
row 3  [critical]   pii_ssn:             SSN detected
row 3  [warning]    pii_email:           Email detected
row 4  [critical]   incomplete_pair:     Output row has no instruction
row 5  [critical]   incomplete_pair:     Instruction row has no output
row 7  [critical]   prompt_injection:    Row contains prompt injection patterns — dangerous for training
row 7  [warning]    jailbreak_pattern:   Jailbreak / red-team pattern detected (instruction_override)
row 10 [warning]    unfinished_response: Response appears to end mid-sentence
```

## What it checks

- **PII with real checksum validation** — SSN, credit card (Luhn), IBAN, DEA, CUSIP, ABA routing, emails, phones, addresses, DOB, IPs; auto-redaction available
- **Duplicates** — exact, near (fuzzy), boilerplate openings, and optional embedding-based semantic dedup
- **Structural integrity** — empty/missing fields, truncated responses, tiny rows, malformed chat turns, context-window overflow per target model
- **Safety** — prompt-injection and jailbreak patterns, refusal/compliance classification, toxicity, classification markings (CUI/FOUO/etc.)
- **Label quality** — label-error detection, incoherent pairs, DPO/preference-pair inversion checks
- **Distribution** — diversity, novelty, clustering, weak-supervision signals

Severities: `critical` (drops the row on export by default), `warning`, `suggestion`. Most text problems are auto-fixable via `modelbrew_cleaner.autofix` (idempotent — property-tested).

## Formats

Instruction/output, prompt/completion, Alpaca, OpenAI chat `messages`, DPO/preference pairs. Export profiles for common trainers via `export_clean(rows, target_profile=...)`.

## Hosted version

A free hosted UI of this cleaner runs on Hugging Face: **https://huggingface.co/spaces/ModelBrew/dataset-cleaner**. Differences from the hosted version: the pip package excludes the job-queue/storage layer and the paid synthetic-generation service wiring; analysis checks are identical.

## Why we built this

We work on continual fine-tuning without catastrophic forgetting (patent-pending CRMA adapters). Measuring forgetting honestly forced us to fix our data first — see our write-up: *Your forgetting benchmark is lying to you*.

## License

Apache-2.0.
