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