Fleet-Clean-0.36B / fleet_clean.py
Lucasgitguy-co's picture
Fleet-Clean release
e7e5ec3 verified
Raw History Blame Contribute Delete
5.01 kB
#!/usr/bin/env python3
"""Fleet-Clean: speech -> Fleet-2 transcript -> Laya word tagger -> clean text.
python fleet_clean.py clip.wav # print the clean transcript
python fleet_clean.py --json clip.wav other.wav # raw + clean + per-word tags as JSON lines
python fleet_clean.py --text "so I I think, you know, we should go"
Speech runs in Cactus' native engine on whistle.cact (pip install cactus-compute); cleanup runs the tagger in
PyTorch (pip install torch "transformers>=5" safetensors). Both models sit next to this file.
The tagger only deletes words and changes punctuation / first-letter case: it never writes a word of its own.
"""
import argparse
import json
import sys
from pathlib import Path
HERE = Path(__file__).resolve().parent
sys.path.insert(0, str(HERE))
from fleet_clean_words import C_KEEP, PUNCT_MODES, apply_tags, resolve, trailing_class # noqa: E402
class Tagger:
def __init__(self, path=HERE / "tagger", device=None):
import torch
from safetensors.torch import load_file
from transformers import AutoTokenizer, ModernBertModel
self.torch = torch
self.device = device or ("mps" if torch.backends.mps.is_available() else "cpu")
self.encoder = ModernBertModel.from_pretrained(path).to(self.device).eval()
head = load_file(str(Path(path) / "head.safetensors"))
self.w, self.b = head["weight"].to(self.device), head["bias"].to(self.device)
self.tok = AutoTokenizer.from_pretrained(path)
import json
labels = json.loads((Path(path) / "labels.json").read_text())
self.threshold = labels.get("delete_threshold", 0.5)
self.punct_mode = labels.get("punct_mode", "boundary")
def tag(self, words, delete_threshold=None):
delete_threshold = self.threshold if delete_threshold is None else delete_threshold
torch = self.torch
if not words:
return [], [], []
enc = self.tok(words, is_split_into_words=True, truncation=True, max_length=512)
first = [None] * len(words)
for t, w in enumerate(enc.word_ids()):
if w is not None and first[w] is None:
first[w] = t
x = torch.tensor([enc["input_ids"]], device=self.device)
with torch.no_grad():
h = self.encoder(input_ids=x, attention_mask=torch.ones_like(x)).last_hidden_state[0].float()
logits = (h @ self.w.T + self.b).cpu()
p_del = logits[:, :2].softmax(-1)[:, 1]
d, p, c = [], [], []
for w, t in zip(words, first):
if t is None: # past the 512-token window: keep the word as written
d.append(0), p.append(trailing_class(w)), c.append(C_KEEP)
else:
d.append(int(p_del[t] >= delete_threshold))
p.append(int(logits[t, 2:7].argmax()))
c.append(int(logits[t, 7:10].argmax()))
return d, p, c
def clean(self, text, delete_threshold=None, punct_mode=None):
words = text.split()
d, p, c = self.tag(words, delete_threshold)
P, C = resolve(words, d, p, c, punct_mode or self.punct_mode)
return apply_tags(words, d, P, C), {"words": words, "delete": d, "punct": P, "case": C}
def transcribe(path, weights=HERE / "whistle.cact", language=None):
import cactus
res = cactus.transcribe(str(path), language=language, weights=str(weights))
return res["text"].strip(), res.get("language")
def main(argv=None):
ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
ap.add_argument("audio", nargs="*", help="WAV files (16 kHz mono; other rates need cactus-compute[mic])")
ap.add_argument("--text", help="clean this text instead of transcribing audio")
ap.add_argument("--language", default=None, help="en de fr es it nl pl ko (default: detect)")
ap.add_argument("--threshold", type=float, default=None, help="delete probability threshold (default: calibrated)")
ap.add_argument("--punct", choices=PUNCT_MODES, default=None,
help="boundary (default): fix punctuation only where words were removed; full: re-punctuate everything")
ap.add_argument("--device", default=None)
ap.add_argument("--json", action="store_true")
a = ap.parse_args(argv)
if not a.text and not a.audio:
ap.error("give a WAV file or --text")
tagger = Tagger(device=a.device)
jobs = [("text", a.text)] if a.text else [("audio", p) for p in a.audio]
for kind, item in jobs:
raw, lang = transcribe(item, language=a.language) if kind == "audio" else (item, a.language)
clean, tags = tagger.clean(raw, a.threshold, a.punct)
if a.json:
print(json.dumps({"source": str(item) if kind == "audio" else None, "language": lang, "raw": raw,
"clean": clean, **tags}, ensure_ascii=False))
else:
print(clean)
if __name__ == "__main__":
main()