Skip to content

Latest commit

Β 

History

3 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Chatbot_with_Pytorch

Chatbot_with_Pytorch Banner

A High-Performance Deep Learning Conversational AI Assistant Powered by PyTorch & NLP

Author PyTorch Python NLTK Lessons License


πŸ“‘ Table of Contents


🌟 Project Overview

Chatbot_with_Pytorch is an enterprise-ready, educational, and high-performance conversational AI system developed and architected by Rahul Chaube.

Unlike naive rule-based keyword matchers, this chatbot utilizes a Deep Multi-Layer Perceptron (MLP) implemented in PyTorch to perform statistical intent classification on tokenized and stemmed text vectors generated via NLTK.

The repository includes:

  1. Academic Deep Learning Course (LESSONS.md): Full mathematical derivations, backpropagation formulas, activation proofs, and optimization theory.
  2. Interactive Terminal CLI (chat.py): ANSI-colored console interface with slash commands (/help, /tags, /info, /verbose).
  3. Desktop Graphical Application (app.py): Modern, dark-themed GUI built with Tkinter.
  4. Configurable Training Pipeline (train.py): Hyperparameter CLI flags (--epochs, --lr, --batch-size, --hidden-size, --dropout, --seed).

🧠 Deep Learning & NLP Course Curriculum

Explore the full in-depth textbook and course guide in LESSONS.md:

Module Topic Core Concepts Covered
Module 1 NLP Foundations Word Tokenization, Porter Stemmer algorithm, Bag-of-Words (BoW) vector representation
Module 2 Neural Network Architecture Multi-Layer Perceptrons, Linear transforms, ReLU activations, Dropout regularization, Softmax
Module 3 Training & Optimization Cross-Entropy Loss mathematical formulation, Adam optimizer moment estimation, mini-batch gradient descent
Module 4 Inference & Production Dynamic confidence thresholding, Out-of-Distribution (OOD) fallbacks, Tkinter GUI engineering
Module 5 Practical Labs Hands-on exercises to build custom enterprise chatbots

πŸ”¬ Natural Language Processing (NLP) Pipeline

Natural Language Processing Pipeline

The transformation from raw human dialogue to mathematical input tensors proceeds through 4 well-defined phases:

  1. Text Tokenization: Splits input strings into discrete linguistic tokens using nltk.word_tokenize.
  2. Porter Stemming: Normalizes inflected word variants ("running", "runs", "ran") into their root lemma ("run").
  3. Vocabulary Indexing & Bag-of-Words Vectorization: Matches stems against the sorted unique corpus vocabulary $V \in \mathbb{R}^{124}$, producing a binary ${0, 1}$ feature vector.
  4. Tensor Conversion: Casts vectors into PyTorch torch.float32 tensors ready for matrix multiplication on CPU or CUDA GPU.

πŸ›οΈ PyTorch Deep Neural Network Architecture

Neural Network Architecture

The core classification engine is a 3-layer Feed-Forward Multi-Layer Perceptron (NeuralNet in model.py):

$$\mathbf{h}_1 = \text{ReLU}(\mathbf{W}_1 \vec{x} + \mathbf{b}_1)$$

$$\mathbf{h}_2 = \text{ReLU}(\mathbf{W}_2 \mathbf{h}_1 + \mathbf{b}_2)$$

$$\mathbf{z} = \mathbf{W}_3 \mathbf{h}_2 + \mathbf{b}_3$$

$$\hat{\mathbf{y}} = \text{Softmax}(\mathbf{z})$$

  • Input Dimension ($d_{\text{in}}$): $124$ features (Bag-of-Words vocabulary size).
  • Hidden Dimensions ($h_1, h_2$): $16$ units with non-linear ReLU activations and optional Dropout ($p = 0.05$).
  • Output Classes ($C$): $13$ intent classes with Softmax probability distribution.

✨ Key Advancements & Features

  • ⚑ Near-Zero Convergence: Optimized training loop achieving final loss $\le 0.000002$.
  • πŸ›‘οΈ Confidence Threshold Gating: Out-of-domain and low-confidence inputs are intercepted to prevent hallucinated answers.
  • 🎨 Modern Minimalist Visuals: High-resolution, clean vector architecture diagrams.
  • πŸ–₯️ Cross-Platform Compatibility: Fully tested on Windows (PowerShell/CMD), macOS, and Linux.

πŸ“ Repository File Tree

Chatbot_with_Pytorch/
β”œβ”€β”€ assets/
β”‚   β”œβ”€β”€ banner.png                     # Clean minimalist project hero banner
β”‚   β”œβ”€β”€ nlp_pipeline.png               # Infographic: Tokenization to BoW tensor
β”‚   └── neural_network_architecture.png# Infographic: PyTorch MLP forward pass
β”œβ”€β”€ intents.json                       # Knowledge base with 13 intent categories
β”œβ”€β”€ model.py                           # PyTorch NeuralNet architecture class
β”œβ”€β”€ nltk_utils.py                      # Tokenization, Stemming & BoW vectorizer
β”œβ”€β”€ train.py                           # CLI-configurable PyTorch training pipeline
β”œβ”€β”€ chat.py                            # Interactive ANSI terminal CLI assistant
β”œβ”€β”€ app.py                             # Modern Tkinter desktop GUI application
β”œβ”€β”€ LESSONS.md                         # Complete deep learning & NLP coursebook
β”œβ”€β”€ data.pth                           # Serialized model checkpoint & vocabulary
β”œβ”€β”€ LICENSE                            # MIT License (Rahul Chaube)
└── README.md                          # Main repository documentation

πŸš€ Quick Start Installation

1. Clone the Repository

git clone https://github.com/Rahulchaube1/Chatbot_with_Pytorch.git
cd Chatbot_with_Pytorch

2. Set Up Virtual Environment

Using uv (Recommended / Ultra-Fast):

uv venv .venv
.\.venv\Scripts\activate
uv pip install torch nltk numpy

Using standard pip:

python -m venv .venv
.\.venv\Scripts\activate
pip install torch nltk numpy

3. Initialize Tokenizer Data (One-Time)

python -c "import nltk; nltk.download('punkt'); nltk.download('punkt_tab')"

🎯 Training the Model

Execute the training script to train the neural network on intents.json:

python train.py

Custom Hyperparameter Tuning:

python train.py --epochs 1000 --batch-size 8 --lr 0.001 --hidden-size 16 --dropout 0.05
Flag Type Default Description
--intents str intents.json Path to JSON intent dataset
--output str data.pth Target output model checkpoint file
--epochs int 1000 Total training epochs
--batch-size int 8 Mini-batch sample size
--lr float 0.001 Learning rate for Adam optimizer
--hidden-size int 16 Number of neurons per hidden layer
--dropout float 0.05 Dropout regularization probability
--seed int 42 Random seed for deterministic reproducibility

πŸ’¬ Running the Chatbot

Mode 1: Interactive Terminal CLI

python chat.py --verbose

Supported Terminal Commands:

  • /help β€” Display command list
  • /tags β€” View all 13 recognizable intent tags
  • /info β€” View active PyTorch model architecture and parameters
  • /verbose β€” Toggle live intent and confidence percentages
  • quit / exit β€” Close chat session

Mode 2: Modern Desktop GUI Application

python app.py

Launches a standalone desktop application window featuring auto-scrolling dialogue, color-coded speech bubbles, and instant response generation.


βš™οΈ Customizing & Adding New Domains

To train the chatbot for customer support, medical inquiries, e-commerce, or personal portfolios:

  1. Open intents.json.
  2. Append a new intent block:
{
  "tag": "contact_info",
  "patterns": [
    "How can I reach Rahul Chaube?",
    "Where can I contact the developer?",
    "Show me contact details"
  ],
  "responses": [
    "You can connect with Rahul Chaube via GitHub at https://github.com/Rahulchaube1!"
  ]
}
  1. Run python train.py.
  2. Your bot is immediately upgraded with the new capabilities!

πŸ‘€ Author & License

About

A high-performance deep learning conversational AI assistant built with PyTorch and NLTK, featuring interactive CLI and desktop GUI applications.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages