A from-scratch implementation of a GPT-style (Generative Pre-trained Transformer) language model, featuring a FastAPI backend for text generation and a React + Vite frontend. Implements bigram, single-head attention, and multi-head attention variants of the transformer architecture, trained on a custom text corpus.
- Demo: http://localhost:8010/generate (local backend)
- Docs: CLAUDE.md β project architecture & data flow
- Issues: GitHub Issues
- Contributing: CONTRIBUTING.md
- Installation
- Usage
- Features
- Project Structure
- Tech Stack
- Dependencies & Packages
- Contributing
- License
- Security
- Code of Conduct
- Python 3.10+
- Node.js 20+
- npm (comes with Node.js)
- CUDA-capable GPU (optional, for faster training/inference)
cd backend
python -m venv venv
# Windows
venv\Scripts\activate
# macOS/Linux
source venv/bin/activate
pip install torch fastapi uvicorn numpy pydantic starlettenpm installcd backend
venv\Scripts\activate # Windows
python main.py # starts on http://localhost:8010 with reloadGenerate text:
curl "http://localhost:8010/generate?tokens=100"Or open http://localhost:8010/generate in a browser.
npm run dev # starts on http://localhost:5173
npm run build # production build
npm run preview # serve production buildcd backend
python training/train.pyTrains the multi-head attention model for 2,500 steps with AdamW (lr=3e-4, batch_size=64) and saves the checkpoint to backend/model/GPT.pt.
cd backend
python run.pyLoads model/GPT.pt and prints 1,000 generated tokens to stdout.
- Three model variants: vanilla bigram, single-head self-attention, multi-head self-attention
- Causal masking for autoregressive generation
- Character-level tokenization with dynamic vocabulary built from the dataset
- Position embeddings for attention-based variants
- FastAPI endpoint (
/generate) with CORS enabled - CUDA acceleration with automatic CPU fallback
- Configurable context window (256 tokens) and generation temperature
- React + Vite frontend ready for integration
GPT/
βββ backend/
β βββ attention/
β β βββ multi_head/
β β β βββ Multihead_attention.py # Multi-head self-attention model
β β βββ single_head/
β β βββ biModel.py # Single-head self-attention model
β βββ config/
β β βββ preprocessing.py # Text cleaning utility
β β βββ tokenizer.py # Character-level encoder/decoder + batching
β βββ dataset/
β β βββ input.txt # Training corpus (~1.1 MB)
β βββ model/
β β βββ GPT.pt # Trained model checkpoint
β βββ module/
β β βββ bigramModel.py # Vanilla bigram (no attention)
β βββ training/
β β βββ train.py # Training loop (AdamW, 2500 steps)
β βββ main.py # FastAPI app with /generate endpoint
β βββ run.py # Standalone inference script
β βββ venv/ # Python virtual environment
βββ src/ # React frontend (Vite boilerplate)
β βββ App.tsx
β βββ main.tsx
β βββ assets/
βββ public/ # Static frontend assets
βββ index.html # Vite entry point
βββ package.json
βββ vite.config.ts
βββ tsconfig.json
βββ tsconfig.app.json
βββ tsconfig.node.json
βββ CLAUDE.md # Project architecture documentation
βββ README.md
Click to expand
Click to expand
We welcome contributions! Please read our Contributing Guide for details on how to submit pull requests, report issues, and follow our code of conduct.
This project is licensed under the MIT License β see the LICENSE file for details.
Please review our Security Policy for information on reporting vulnerabilities and our responsible disclosure process.
This project adheres to the Contributor Covenant Code of Conduct. By participating, you are expected to uphold this code.
Made with β€ by H0NEYP0T-466
