Token Classification
Transformers
ONNX
Safetensors
distilbert
logging
text-cleaning
korean
japanese
chinese
slopdam
Instructions to use slopdam/chatter-tagger with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use slopdam/chatter-tagger with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("token-classification", model="slopdam/chatter-tagger")# pip install -U transformers accelerate # Load model directly from transformers import AutoTokenizer, AutoModelForTokenClassification tokenizer = AutoTokenizer.from_pretrained("slopdam/chatter-tagger") model = AutoModelForTokenClassification.from_pretrained("slopdam/chatter-tagger", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Use through slopdam[chatter]; chatter.py is slopdam.chatter
Browse filesFiles changed (2)
- README.md+6-12
- chatter.py+63-17
53 unmodified lines54555657585960616263646566676869707172737475767753 unmodified lines## Use```bashpip install slopdam transformers torch``````pythonfrom huggingface_hub import hf_hub_downloadimport importlib.util, sysfrom slopdam import cleanpath = hf_hub_download("slopdam/chatter-tagger", "chatter.py")spec = importlib.util.spec_from_file_location("chatter", path)chatter = importlib.util.module_from_spec(spec); spec.loader.exec_module(chatter)tagger = chatter.ChatterTagger("slopdam/chatter-tagger")tagger([clean("Heads up: cache warmed for orders"), clean("Finally block ran in auth")])# ['Cache warmed for orders', 'Finally block ran in auth']```Run slopdam's `clean` (reader profile) first: the model was trained on its output. Calling the model without the guard is not supported.## Evaluation53 unmodified lines54555657585960616263646566676869707153 unmodified lines## Use```bashpip install "slopdam[chatter]"tail -f app.log | slopdam clean --chatter``````pythonfrom slopdam.chatter import stripstrip(["Heads up: cache warmed for orders", "Finally block ran in auth", "🚀 さて、バックアップが完了しました"])# ['Cache warmed for orders', 'Finally block ran in auth', 'バックアップが完了しました']````strip` and `--chatter` run slopdam's rules (reader profile) first, since the model was trained on their output, then the model with its guard. They load this repository at a pinned revision. Calling the model without the guard is not supported. `chatter.py` here is the same module as `slopdam.chatter`, for use without installing slopdam: `ChatterTagger()(lines)` runs the model and guard on lines you have already cleaned.## Evaluation
12345678910111213141516171819202122105 unmodified lines128129130131132133134135136137138139140141142143# SPDX-License-Identifier: MIT"""Remove chatty openers ("Great news!", "Heads up:", "살펴보겠습니다.") from log lines.from slopdam import cleanfrom chatter import ChatterTaggertagger = ChatterTagger("slopdam/chatter-tagger")tagger([clean("Great news! Successfully deployed api — 3 pods")])# ['Successfully deployed api: 3 pods']Run slopdam first: the model was trained on slopdam's `reader` output. It only deletes; it neverwrites new text. Every deletion also has to pass `guard`, a fixed set of rules (one run of wholewords at the start of a line, ending in punctuation or a Korean connective, no digits, paths, filenames or key=value), so a line the model is unsure about comes back unchanged. Japanese andChinese openers are cut at full-width punctuation ("さて、", "好的,")."""from __future__ import annotationsimport reKEEP, DELETE, UPPER = 0, 1, 2MAX_PIECES = 256105 unmodified linesreturn outclass ChatterTagger:"""Load the tagger from the Hub (or a local folder) and strip openers from lines of text."""def __init__(self, path: str = "slopdam/chatter-tagger", device: str = "cpu"):import torchfrom transformers import AutoModelForTokenClassification, AutoTokenizerself.device = torch.device(device)self.tok = AutoTokenizer.from_pretrained(path)self.model = AutoModelForTokenClassification.from_pretrained(path).to(self.device)def __call__(self, texts: list, batch: int = 64) -> list:return predict(self.model, self.tok, list(texts), self.device, batch)123456789101112131415161718192021222324252627282930313233105 unmodified lines139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189# SPDX-License-Identifier: MIT"""Remove chatty openers ("Great news!", "Heads up:", "살펴보겠습니다.") with slopdam/chatter-tagger.pip install "slopdam[chatter]"from slopdam.chatter import stripstrip(["Great news! Successfully deployed api — 3 pods", "Finally block ran in auth"])# ['Successfully deployed api: 3 pods', 'Finally block ran in auth']slopdam clean --chatter < app.log`strip` runs slopdam's rules first, then the model: it was trained on slopdam's `reader` output.The model only deletes; it never writes new text. Every deletion also has to pass `guard`, a fixedset of rules (one run of whole words at the start of a line, ending in punctuation or a Koreanconnective, no digits, paths, file names or key=value), so a line the model is unsure about comesback unchanged. Japanese and Chinese openers are cut at full-width punctuation ("さて、", "好的,").The model is downloaded from the Hugging Face Hub on first use, at the revision in `REVISION`.This file has no dependency on the rest of slopdam except in `strip`, so it is also published onthe Hub as the model's `chatter.py`."""from __future__ import annotationsimport refrom functools import lru_cachefrom typing import OptionalMODEL = "slopdam/chatter-tagger"REVISION = "c0f0e70d082bd2b28d91d48f083585441193c8a0" # v2INSTALL = 'the chatter model needs the optional dependencies: pip install "slopdam[chatter]"'KEEP, DELETE, UPPER = 0, 1, 2MAX_PIECES = 256105 unmodified linesreturn outdef _load():try:import torchfrom transformers import AutoModelForTokenClassification, AutoTokenizerexcept ImportError as e:raise ImportError(INSTALL) from efrom transformers.utils import logginglogging.set_verbosity_error() # keep stderr for real errors in a log pipelogging.disable_progress_bar()return torch, AutoModelForTokenClassification, AutoTokenizerclass ChatterTagger:"""Load the tagger from the Hub (or a local folder) and strip openers from lines of text."""def __init__(self, path: str = MODEL, device: str = "cpu", revision: Optional[str] = None):torch, Model, Tokenizer = _load()if revision is None and path == MODEL:revision = REVISIONself.device = torch.device(device)self.tok = Tokenizer.from_pretrained(path, revision=revision)self.model = Model.from_pretrained(path, revision=revision).to(self.device)def __call__(self, texts: list, batch: int = 64) -> list:return predict(self.model, self.tok, list(texts), self.device, batch)@lru_cache(maxsize=None)def tagger(device: str = "cpu") -> ChatterTagger:"""The pinned model, loaded once per process."""return ChatterTagger(device=device)def strip(lines, profile: str = "reader", level: str = "basic", device: str = "cpu") -> list:"""Clean each line with slopdam's rules, then remove the opener the model finds, if any.The model was trained on `reader` output; with `dev` it still never deletes more than theguard allows, but it finds fewer openers."""from .rules import Sanitizersanitizer = Sanitizer(profile, level)cleaned = [sanitizer.clean(line) for line in lines]todo = [i for i, line in enumerate(cleaned) if line.strip()]if todo:for i, out in zip(todo, tagger(device)([cleaned[i] for i in todo])):cleaned[i] = outreturn cleaned
README.md+6-12
chatter.py+63-17