Skip to content

H0NEYP0T-466/GPT

Repository files navigation

GitHub License GitHub Stars GitHub Forks GitHub Issues GitHub Pull Requests Contributions Welcome

Last Commit Commit Activity Repo Size Code Size

Top Language Languages Count

Discussions Documentation Open Source Love

GPT

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.

Architecture Diagram


πŸ”— Links


πŸ“‘ Table of Contents


πŸš€ Installation

Prerequisites

  • Python 3.10+
  • Node.js 20+
  • npm (comes with Node.js)
  • CUDA-capable GPU (optional, for faster training/inference)

Backend Setup

cd backend
python -m venv venv

# Windows
venv\Scripts\activate

# macOS/Linux
source venv/bin/activate

pip install torch fastapi uvicorn numpy pydantic starlette

Frontend Setup

npm install

⚑ Usage

Run the Backend (FastAPI + Uvicorn)

cd backend
venv\Scripts\activate   # Windows
python main.py          # starts on http://localhost:8010 with reload

Generate text:

curl "http://localhost:8010/generate?tokens=100"

Or open http://localhost:8010/generate in a browser.

Run the Frontend (Vite + React)

npm run dev        # starts on http://localhost:5173
npm run build      # production build
npm run preview    # serve production build

Train the Model

cd backend
python training/train.py

Trains 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.

Run Standalone Inference

cd backend
python run.py

Loads model/GPT.pt and prints 1,000 generated tokens to stdout.


✨ Features

  • 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

πŸ“‚ Project Structure

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

πŸ›  Tech Stack

Languages

Python TypeScript JavaScript

Frameworks & Libraries

PyTorch FastAPI React Vite

Backend / API

Uvicorn Pydantic Starlette

DevOps / CI / Tools

Node.js npm Oxlint Git


πŸ“¦ Dependencies & Packages

Runtime Dependencies (Python)

Click to expand
Package Version Description
PyPI - torch 2.12.1 Deep learning framework
PyPI - fastapi 0.138.1 Modern web framework for APIs
PyPI - uvicorn 0.49.0 ASGI server
PyPI - numpy 2.4.6 Numerical computing
PyPI - pydantic 2.13.4 Data validation
PyPI - starlette 1.3.1 ASGI framework (FastAPI dependency)
PyPI - jinja2 3.1.6 Templating engine
PyPI - annotated-types 0.7.0 Type annotation utilities

Runtime Dependencies (Node.js)

Click to expand
Package Version Description
npm - react ^19.2.7 UI library
npm - react-dom ^19.2.7 React DOM renderer

Dev / Build / Test Dependencies (Node.js)

Click to expand
Package Version Description
npm - vite ^8.1.0 Build tool & dev server
npm - typescript ~6.0.2 Type-safe JavaScript
npm - @vitejs/plugin-react ^6.0.2 Vite React plugin
npm - oxlint ^1.69.0 Linter
npm - @types/react ^19.2.17 React type definitions
npm - @types/react-dom ^19.2.3 React DOM type definitions
npm - @types/node ^24.13.2 Node.js type definitions

🀝 Contributing

We welcome contributions! Please read our Contributing Guide for details on how to submit pull requests, report issues, and follow our code of conduct.


πŸ“œ License

This project is licensed under the MIT License β€” see the LICENSE file for details.


πŸ›‘ Security

Please review our Security Policy for information on reporting vulnerabilities and our responsible disclosure process.


πŸ“ Code of Conduct

This project adheres to the Contributor Covenant Code of Conduct. By participating, you are expected to uphold this code.


Made with ❀ by H0NEYP0T-466

About

πŸ€– From-scratch GPT in PyTorch β€” bigram, single-head & multi-head attention β€’ FastAPI /generate endpoint β€’ React + Vite frontend β€’ Character-level tokenizer β€’ CUDA support

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

3 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors