Skip to content

About

Train a character-level GPT on newline-delimited text files and generate text that follows the character patterns, structure, and common sequences learned from the dataset.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Character-Level Transformer

Train a character-level transformer on newline-delimited text with PyTorch, then generate names, words, or other short strings. Training supports CPU, CUDA and Apple Silicon (MPS).

Demo · Browser training counterpart

Character-Level Transformer preview

Installation

Requires Python 3.10 or later. Run these commands from the repository root:

python -m venv .venv

Activate the environment with source .venv/bin/activate on macOS/Linux or .venv\Scripts\Activate.ps1 in PowerShell, then install:

python -m pip install -e . onnx onnxscript

The ONNX packages are needed to export the JavaScript bundle when saving a run. For CUDA, install the appropriate PyTorch build using the PyTorch installation instructions.

Usage

Open the interactive menu:

python -m character_level_transformer

Or train directly:

python -m character_level_transformer --dataset us_baby_names.txt --steps 3000

Use one sample per line in a UTF-8 text file. Bare dataset names resolve inside datasets/; --dataset PATH also accepts a path to your own file.

Command Purpose
python -m character_level_transformer --models Inspect, generate from, resume or delete saved runs
python -m character_level_transformer --compare Compare CPU and accelerator training
python -m character_level_transformer --help Show all training options

The installed character-level-transformer command is an alternative to python -m character_level_transformer.

Saved models

Runs save under models/ by default, with numbered folders when a name already exists. Use --output PATH to choose an output location or --no-save to disable saving.

File Contents
.model.pt Model weights and tokenizer for Python inference
.resume.pt Dataset, optimizer and training state needed to resume
.model ONNX bundle and metadata for JavaScript inference

The resume file requires its matching model file. To run an exported bundle with Node.js, install the root JavaScript dependencies:

npm ci
node scripts/run_js_bundle.js models/us_baby_names/us_baby_names.model --samples 40 --temperature 0.7

Generation uses an embedded Bloom filter to reject source-line matches. False positives can also reject novel strings; accepted outputs are not a guarantee of real-world originality.

Datasets

File Non-empty lines
datasets/us_baby_names.txt 104,819
datasets/english_words.txt 370,105

Web demo

The inference app is in site/. Requires Node.js 22.12 or later.

cd site
npm ci
npm run dev

Development

From the repository root, with the Python environment activated:

python -m pip install -e ".[dev]"
python -m unittest discover -s tests
ruff check character_level_transformer scripts tests
ruff format --check character_level_transformer scripts tests
mdformat --check README.md docs

Node.js and npm ci at the repository root are required for JavaScript bundle integration tests. The site has separate npm run check, npm run typecheck and npm run build commands, run from site/.

Experiments

The default model uses causal self-attention, RMSNorm, SwiGLU and standard additive residual connections. Experimental options are documented separately:

License

MIT.

About

Train a character-level GPT on newline-delimited text files and generate text that follows the character patterns, structure, and common sequences learned from the dataset.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages