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")

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.

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.0.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.0.0
File Size Uploaded Provenance Actions
lmspell-1.0.0.tar.gz 45.1 kB With provenance Details for lmspell-1.0.0.tar.gz

Built distribution (wheel)

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

Total release size: 96.3 kB

Release files / lmspell-1.0.0.tar.gz

Download URL lmspell-1.0.0.tar.gz
Size 45.1 kB
Tags Source
SHA-256 checksum
How to use checksums
1b927ed719aa7432e527392f57ed4837ad89033e2e809aeb198513d074fd5293
BLAKE2b-256 checksum
How to use checksums
1a042216334315a46b6806f276110ae9efbe7d3e8cc3595f885628eebc09c495
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.0.0-py3-none-any.whl

Download URL lmspell-1.0.0-py3-none-any.whl
Size 51.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
92a2e99991f2cc9b909692bd525389fe8d1fd218da3e8c9a9d822fadc27c89dc
BLAKE2b-256 checksum
How to use checksums
c3c73169b25bc55dea9707710a52349d341ccda12e9904759c4297a81479c351
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.0.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.0.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.0.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