A small command-line chatbot built on GPT-2 Medium using Hugging Face Transformers — a hands-on experiment in fine-tuning and chatting with a language model locally.
This is a minimal "mini ChatGPT": a terminal chat loop powered by OpenAI's GPT-2 Medium model, loaded and run through the Hugging Face Transformers library and PyTorch. You type a message, the model generates a response, and the conversation continues until you exit.
The project also includes the scaffolding to fine-tune GPT-2 on your own text data and a small utility for comparing text similarity using the model's embeddings.
- 💬 Interactive chat loop — talk to GPT-2 from your terminal; type
quit,exit, orbyeto stop. - 🎲 Sampled generation — uses temperature (0.7), no-repeat n-grams, and sampling for more natural, less repetitive replies.
- ⚡ GPU support — automatically uses CUDA if available, otherwise runs on CPU.
- 🏋️ Fine-tuning setup — load a custom text dataset (
database.txt) and configure training via Hugging FaceTrainingArguments. - 🔎 Embedding similarity — helper functions to embed text and compute cosine similarity between two pieces of text.
- Python 3
- Hugging Face Transformers —
GPT2LMHeadModel,GPT2Tokenizer,TrainingArguments - PyTorch — model inference and tensor operations
- GPT-2 Medium — the base language model
Chatbot/
└── mini chatGPT/
├── chat.py # Interactive chat loop + embedding/cosine-similarity helpers
├── env.py # Loads GPT-2 Medium, tokenizer, dataset, and training config
├── database.txt # Custom training data for fine-tuning
└── logs/ # TensorBoard training logs
pip install torch transformersOn first run, the GPT-2 Medium weights (~1.4 GB) are downloaded automatically from Hugging Face.
cd "mini chatGPT"
python chat.pyThen just start typing:
You: Hello, who are you?
AI: ...
You: bye
env.pyloadsgpt2-mediumand its tokenizer, sets the pad token to the EOS token, readsdatabase.txtinto a tokenized dataset, and definesTrainingArguments(3 epochs, batch size 8) for fine-tuning.chat.pyputs the model in eval mode and runs a loop that encodes your input, callsmodel.generate(...)with sampling, and decodes the reply. It also includesget_embedding()andcosine_similarity()for measuring how similar two pieces of text are based on the model's outputs.
- GPT-2 Medium is a general-purpose model from 2019 — out of the box it won't be as coherent as modern chat models, so responses are best treated as an experiment.
chat.pyreferencesdevicebefore it's defined in the call tochat_with_gpt2(...); moving the device setup above the call (or passing it in) is a quick fix.- Wiring
env.py'sTrainingArgumentsand dataset into an actualTrainer.train()call would let the model fine-tune ondatabase.txtand personalize its responses.
A personal learning project for getting hands-on with language models — loading pretrained weights, running text generation, working with embeddings, and setting up a fine-tuning pipeline.