Fine-tune YellowMIST on your own text¶
YellowMIST models are tiny (15M parameters) character models that run on-device in 100+ languages. This notebook
shows how to make them better for your languages and text, then use the result with the yellowmist library
in Python, JavaScript, the browser or an editor plugin.
- Continue pretraining an encoder on your text (optional, helps most for a language with little web text).
- Fine-tune a task: here, a code-switching detector (the language of every word) for Yoruba–English; the same pattern works for punctuation and capitals.
- Export and use it with
ym.register(...), and push it to the Hugging Face Hub.
Runtime: a free Colab T4 GPU is enough (Runtime → Change runtime type → T4 GPU). The full run takes about 20 minutes.
!pip install -q "yellowmist[train]" datasets
1. Some text¶
Bring your own: a dict of {language label: [paragraphs]}. Labels are FLORES-style: a three-letter language code and
the script, such as yor_Latn, swh_Latn, hin_Deva. Here we take a few thousand Wikipedia paragraphs
(CC BY-SA), streamed so nothing large is downloaded.
from datasets import load_dataset
def wiki(code, n=3000):
out = []
for row in load_dataset("wikimedia/wikipedia", f"20231101.{code}", split="train", streaming=True):
out += [p for p in row["text"].split("\n") if len(p) > 80]
if len(out) >= n:
break
return out[:n]
texts = {"yor_Latn": wiki("yo"), "eng_Latn": wiki("en")}
{k: len(v) for k, v in texts.items()}
2. Load an encoder and (optionally) keep pretraining it¶
Encoders are grouped by region: WA (West Africa), ESA, AMR, AP, EU, AR, INN, INS, CY, ET, OA,
SEA. Pick the one whose languages are closest to yours. If your language is not in it, add_languages gives it a
language tag (and any new characters) before pretraining.
from yellowmist import train as T
enc = T.Encoder.load("WA")
print(len(enc.labels), "languages, e.g.", enc.labels[:8])
# enc.add_languages({"xyz_Latn": my_texts}) # only for a language the encoder has not seen
losses = T.continue_pretraining(enc, texts, steps=1000, batch=32) # ~5 min on a T4; skip if your text is small
3. Fine-tune a code-switching detector¶
Training examples are mixed text spliced from your paragraphs: single inserted words (the commonest real pattern) and
alternating runs, with 30% left in one language. Numbers and punctuation get the label other.
sw = T.finetune_switch(enc, texts, steps=3000, batch=32) # ~10 min on a T4
sw.save("my-switch") # weights + ONNX export (int8 + fp32)
4. Use it with the yellowmist library¶
ym.register points a task at your model folder (or a Hub repo). Every ym.switch(...) call for those languages now
uses it, on CPU, with ONNX Runtime: the same as the built-in models.
import yellowmist as ym
ym.register("switch", "my-switch")
for s in ["Mo fẹ́ go to the market", "Ẹ ṣé gan, I really appreciate it", "Mo ti send message si e"]:
print(" ".join(f"{w.text}/{w.lang.split('_')[0]}" for w in ym.switch(s, langs=["yor_Latn", "eng_Latn"])))
5. Punctuation and capitals (optional)¶
The same encoder can learn to put back punctuation and capitals in your text, in each language's own marks.
pu = T.finetune_punct(enc, {"yor_Latn": texts["yor_Latn"]}, steps=3000)
pu.save("my-punct")
ym.register("punct", "my-punct")
print(ym.punct("e kaaro bawo ni ise ati ebi re", "yor_Latn"))
6. Share it¶
Push the folder to the Hub as a model repo (private by default). Anyone can then use it with
ym.register("switch", "your-name/my-switch") in Python, or the same repo id in JavaScript.
from huggingface_hub import notebook_login
notebook_login()
T.push_to_hub("my-switch", "your-name/yellowmist-switch-yor-eng", private=True)
Tips¶
- More text helps most. Going from 2M to 20M characters per language lifted one of our punctuation models by 7 points on average, and up to 15 for some languages.
- Name the languages with
langs=[...]when you callym.switch: much more accurate than choosing among a whole region. - Steps: start small (a few thousand); the published Switch models used 6,000 steps of 64 examples.
- Apache-2.0. Models and code: https://huggingface.co/olaverse