Skip to main content

LMSpell

LMSpell is a flexible and extensible library designed to facilitate the implementation of spell correction systems using a wide range of pre-trained language models (PLMs). It supports encoder-based, decoder-based (i.e. LLMs), and encoder-decoder-based models, providing broad compatibility across diverse architectures.

The interface of LMSpell, abstracts core PLM functionalities such as fine-tuning and inference. This abstraction enables users to integrate and experiment with different model types without requiring model-specific implementation knowledge. Internally, LMSpell dynamically loads the appropriate classes based on the selected model, ensuring a consistent and user-friendly development experience.

Installation

Requires Python 3.10+.

pip install lmspell

Decoder models (Llama, Gemma) and UnslothTrainer need the llm extra, which installs Unsloth and requires an NVIDIA GPU:

pip install "lmspell[llm]"

pip installs the default PyTorch build. For a specific CUDA version, install PyTorch first by following the PyTorch install guide, for example:

pip install torch --index-url https://download.pytorch.org/whl/cu121
pip install lmspell

From source (development)

git clone https://github.com/Nadil-K/lm-spell.git
cd lm-spell
pip install -e ".[dev]"      # add ,llm on a GPU machine
python -m pytest

Quick Start

1. Initialize the Model

from lmspell import LMSpell

# Initialize the LMSpell instance
model_instance = LMSpell("unsloth/gemma-2-9b-bnb-4bit")

The language defaults to Sinhala. Set it with language. It's used in the LLM prompt and to pick the mBART-50 language code:

model_instance = LMSpell("facebook/mbart-large-50", language="Hindi")

Besides the presets listed under Supported Models, LMSpell accepts any Hugging Face checkpoint or local folder whose architecture it supports: mT5, mBART, XLM-RoBERTa, RoBERTa (e.g. SinBERT), Llama or Gemma. The architecture is read from the checkpoint's config.json. This includes models you fine-tuned yourself:

model_instance = LMSpell("your-org/mt5-sinhala-finetuned")   # a model on the Hub
model_instance = LMSpell("demo/epoch_3")                      # a checkpoint saved by Seq2SeqTrainer / SeqLabelTrainer
model_instance = LMSpell("llama_demo")                        # a LoRA adapter saved by UnslothTrainer

2. Fine-tuning with a Trainer

A trainer class can be used to fine-tune a model. Three trainers have been provided for decoder-based, encoder-decoder-based and encoder-based models.

a. Decoder-based

from lmspell.trainers import UnslothTrainer

trainer = UnslothTrainer(
    model_instance=model_instance,
    train_path="train.csv",
    val_path="val.csv",
    exp_name="demo",
)

b. Encoder-decoder-based

from lmspell.trainers import Seq2SeqTrainer

trainer = Seq2SeqTrainer(
    model_instance=model_instance,
    train_path="train.csv",
    val_path="val.csv",
    exp_name="demo",
)

c. Encoder-based

from lmspell.trainers import SeqLabelTrainer

trainer = SeqLabelTrainer(
    model_instance=model_instance,
    train_path="train.csv",
    val_path="val.csv",
    exp_name="demo",
)

Training and validation files are CSVs with a text column (input sentence) and an expected column (corrected sentence). You can change the column names in LMSpellConfig.json; see LMSpellConfig.json.example.

Zero-width joiner (ZWJ): the mBART and mT5 tokenizers drop the zero-width joiner (U+200D) used in Sinhala, for example in ශ්‍රී. For these models LMSpell writes it as a <ZWJ> token before tokenizing, for both training and correct(), and turns it back into U+200D in the outputs. This is on by default for mBART, mT5 and mT5-XL, and off for the others; change it per model under fix_zwj in LMSpellConfig.json. A base model learns the new <ZWJ> token during fine-tuning.

Call the train method on the trainer to fine-tune the model:

trainer.train()

3. Run Inference

import pandas as pd

test_set = pd.read_csv("test.csv")
model_instance.correct(test_set)

4. Use a dataset from the Hugging Face Hub

DatasetUtils.load_hf_data loads a CSV file from a Hugging Face dataset as a pandas DataFrame. The trainers accept a DataFrame wherever they take a CSV path (train_path, val_path), and correct() accepts one directly:

from lmspell.trainers import Seq2SeqTrainer
from lmspell.utils.dataset_utils import DatasetUtils

repo = "lm-spell/sinhala-spell-correction-dataset"

trainer = Seq2SeqTrainer(
    model_instance=model_instance,
    train_path=DatasetUtils.load_hf_data(repo, "train.csv"),
    val_path=DatasetUtils.load_hf_data(repo, "val.csv"),
    exp_name="demo",
)

model_instance.correct(DatasetUtils.load_hf_data(repo, "test_set_41p.csv"))

Gated or private datasets need a Hugging Face token from an account that has been granted access. Set it as huggingface.TOKEN in LMSpellConfig.json, set the HF_TOKEN environment variable, or run huggingface-cli login. The CSV columns must match the configured column names (text and expected by default).

Supported Models

Fine-tuned Checkpoints

Note: These checkpoints will be released upon acceptance of the paper.

LLaMA 3.1

  • unsloth/Meta-Llama-3.1-8B-bnb-4bit
  • unsloth/Meta-Llama-3.1-8B-Instruct-bnb-4bit

LLaMA 3.2

  • unsloth/Llama-3.2-1B-bnb-4bit
  • unsloth/Llama-3.2-1B-Instruct-bnb-4bit
  • unsloth/Llama-3.2-3B-bnb-4bit
  • unsloth/Llama-3.2-3B-Instruct-bnb-4bit

Gemma 2

  • unsloth/gemma-2-2b-bnb-4bit
  • unsloth/gemma-2-2b-it-bnb-4bit
  • unsloth/gemma-2-9b-bnb-4bit
  • unsloth/gemma-2-9b-it-bnb-4bit

mT5

  • google/mt5-small
  • google/mt5-base
  • google/mt5-large
  • google/mt5-xl
  • google/mt5-xxl

MBART

  • facebook/mbart-large-50

SinBERT (Sinhala)

  • NLPC-UOM/SinBERT-small
  • NLPC-UOM/SinBERT-large

XLM-R

  • FacebookAI/xlm-roberta-base
  • FacebookAI/xlm-roberta-large

Fine-tuned Models and Datasets

Fine-tuned models and datasets can be downloaded from Huggingface

Demo App

A demo app that can be used for Sinhala Spell Correction can be accessed from URL

License

This project is licensed under the Creative Commons Attribution 4.0 International License (CC BY 4.0).

Research Publication

This project resulted in a research paper published at MERCon 2026, with the published version available through IEEE Xplore.

LMSpell: Neural Spell Checking for Low-Resource Languages

The arXiv preprint includes additional experiments that could not be included in the published paper due to length limitations.

Metadata

Release files for lmspell 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for lmspell 1.1.0
File Size Uploaded Provenance Actions
lmspell-1.1.0.tar.gz 49.5 kB With provenance Details for lmspell-1.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for lmspell 1.1.0
File Interpreter ABI Platform Actions
lmspell-1.1.0-py3-none-any.whl Python 3 none any Details for lmspell-1.1.0-py3-none-any.whl

Total release size: 103.6 kB

Release files / lmspell-1.1.0.tar.gz

Download URL lmspell-1.1.0.tar.gz
Size 49.5 kB
Tags Source
SHA-256 checksum
How to use checksums
1fd058ab6d214d9a24b1aad52aa656182cc5d07af5460d7ed36f475e91a3f76b
BLAKE2b-256 checksum
How to use checksums
b2f13e768e56ea6047a90501eda83924d0399ed916e20cb1b53ae0f34f22354a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 7, 2026.

Transparency log

Release files / lmspell-1.1.0-py3-none-any.whl

Download URL lmspell-1.1.0-py3-none-any.whl
Size 54.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8d3d11c9b57827b7a5af35105b388f49a3f8bf1cd5b2276d2878857d52c65f47
BLAKE2b-256 checksum
How to use checksums
71c2d39d75e551d0865cf4eb71913400cc4635cc2c3fed6445ed5881f3e2c804
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 7, 2026.

Transparency log

Security for lmspell 1.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for lmspell-1.1.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 7, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.1.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page