Skip to content

KnotAditya/Chatbot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 

Repository files navigation

Chatbot (mini ChatGPT)

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.

Overview

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.

Features

  • 💬 Interactive chat loop — talk to GPT-2 from your terminal; type quit, exit, or bye to 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 Face TrainingArguments.
  • 🔎 Embedding similarity — helper functions to embed text and compute cosine similarity between two pieces of text.

Tech stack

  • Python 3
  • Hugging Face TransformersGPT2LMHeadModel, GPT2Tokenizer, TrainingArguments
  • PyTorch — model inference and tensor operations
  • GPT-2 Medium — the base language model

Project structure

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

Getting started

1. Install dependencies

pip install torch transformers

On first run, the GPT-2 Medium weights (~1.4 GB) are downloaded automatically from Hugging Face.

2. Chat with the model

cd "mini chatGPT"
python chat.py

Then just start typing:

You: Hello, who are you?
AI: ...
You: bye

How it works

  • env.py loads gpt2-medium and its tokenizer, sets the pad token to the EOS token, reads database.txt into a tokenized dataset, and defines TrainingArguments (3 epochs, batch size 8) for fine-tuning.
  • chat.py puts the model in eval mode and runs a loop that encodes your input, calls model.generate(...) with sampling, and decodes the reply. It also includes get_embedding() and cosine_similarity() for measuring how similar two pieces of text are based on the model's outputs.

Notes & ideas for improvement

  • 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.py references device before it's defined in the call to chat_with_gpt2(...); moving the device setup above the call (or passing it in) is a quick fix.
  • Wiring env.py's TrainingArguments and dataset into an actual Trainer.train() call would let the model fine-tune on database.txt and personalize its responses.

Purpose

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.

About

A command-line chatbot powered by GPT-2 Medium and Hugging Face Transformers, with fine-tuning scaffolding and embedding-similarity tooling.

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors