Neerajkadari's picture
Update README.md
3f19756 verified
|
Raw History Blame Contribute Delete
8.56 kB
metadata
title: Context-Aware Conversational Intelligence System
sdk: docker

Context-Aware Conversational Intelligence using Transformer Models

Transforming Unstructured Data into Context-Aware Conversational Intelligence


Project Overview

This system is a modular AI application that combines transformer model experimentation with Retrieval-Augmented Generation (RAG) to create a dual-mode conversational intelligence system.

Dual-Mode Operation

  1. Document-Aware Assistant β€” Upload PDF/DOCX files, process them into embeddings, and ask contextual questions
  2. General Conversational Chatbot β€” Ask any question, get explanations, reasoning, and conversational responses (no document required)

Key Capabilities

  • Document Upload β€” Drag-and-drop PDF/DOCX files for instant analysis
  • RAG Pipeline β€” Extract text, generate embeddings, store in vector DB, and retrieve relevant context
  • Groq Integration β€” Use Groq API (e.g., mixtral-8x7b) for fast, contextual answers
  • General Chatbot β€” Full conversational AI that works even without uploaded documents
  • Model Training β€” Fine-tune BERT, GPT-2, LLaMA, Mistral, and XLNet on AG News dataset
  • Model Evaluation β€” Accuracy, precision, recall, F1-score, confusion matrices
  • Model Comparison β€” Side-by-side performance comparison with charts and best model identification
  • Report Generation β€” Automated visualizations, training curves, and performance summaries

System Architecture

User Interface (Flask Web App)
        |
  AI Agent Controller
        |
  Intelligent Router
        |
  +-----+------+-------+
  |            |              |
Document QA   General Chat   Classification
(RAG Mode)    (LLM Mode)     (Transformer)
  |            |              |
ChromaDB    Groq API       BERT/GPT-2
Embeddings                 LLaMA/Mistral/XLNet

Transformer Models

Model Architecture Base Model Parameters Training
BERT Bidirectional Transformer Encoder bert-base-uncased 110M Full fine-tune
GPT-2 Autoregressive Transformer Decoder gpt2 124M Full fine-tune
LLaMA Decoder-based Large Language Model TinyLlama-1.1B 1.1B LoRA (r=8)
Mistral Transformer with Sliding Window Attention Mistral-7B-v0.1 / Mistral-7B-Instruct-v0.2 7B HF Inference API (Remote)
XLNet Permutation-based Autoregressive Transformer xlnet-base-cased 110M Full fine-tune

Dataset

AG News β€” A 4-class text classification benchmark:

Class Label
World 0
Sports 1
Business 2
Sci/Tech 3
  • Training samples: 200 (configurable in config.py)
  • Test samples: 50 (configurable in config.py)

Project Structure

project/
β”œβ”€β”€ app.py                          # Flask web app + RAG engine + General chatbot
β”œβ”€β”€ config.py                       # Central configuration
β”œβ”€β”€ train.py                        # Model training (5 transformers) + report generation
β”œβ”€β”€ requirements.txt                # Python dependencies
β”œβ”€β”€ README.md                       # This file
β”‚
β”œβ”€β”€ rag/
β”‚   β”œβ”€β”€ __init__.py
β”‚   └── document_loader.py          # PDF/DOCX text extraction & chunking
β”‚
β”œβ”€β”€ ui/
β”‚   β”œβ”€β”€ templates/
β”‚   β”‚   └── index.html              # Web UI (Chat + Upload + General chatbot)
β”‚   └── static/                     # Static assets
β”‚
β”œβ”€β”€ models/                         # Saved model checkpoints (created by train.py)
β”‚   β”œβ”€β”€ bert/
β”‚   β”œβ”€β”€ gpt2/
β”‚   β”œβ”€β”€ llama/
β”‚   β”œβ”€β”€ mistral/
β”‚   └── xlnet/
β”‚
β”œβ”€β”€ data/                           # Uploaded documents
β”œβ”€β”€ reports/                        # Generated charts & evaluation reports
β”œβ”€β”€ vector_store/                   # ChromaDB persistent storage
└── logs/                           # Application logs

Installation

Prerequisites

  • Python 3.9 or higher
  • pip package manager
  • (Optional) NVIDIA GPU with CUDA for faster training
  • (Optional) Groq API key for enhanced chat responses
  • (Optional) HuggingFace API key (HF_TOKEN) for Mistral text classification

Setup

# 1. Navigate to the project directory
cd "d:\major project-2"

# 2. Create a virtual environment (recommended)
python -m venv venv
venv\Scripts\activate    # Windows
# source venv/bin/activate  # Linux/Mac

# 3. Install dependencies
pip install -r requirements.txt

How to Run

Step 1: Train Models (One-time, Optional)

python train.py

This trains BERT, GPT-2, LLaMA, Mistral, and XLNet on the AG News dataset and generates:

  • Confusion matrix heatmaps for each model
  • Model comparison bar chart
  • Training loss and accuracy curves
  • Per-class classification reports
  • JSON evaluation summary
  • Best model identification

Step 2: Start the Web Application

python app.py

Open your browser at http://127.0.0.1:8509

Using the Application

General Chatbot Mode (No document needed)

  1. Open the application
  2. (Optional) Enter your Groq API key in the left panel
  3. Start chatting β€” ask any question, get explanations, and conversational responses

Document Q&A Mode

  1. Upload a file β€” Drag-and-drop or click "Browse files" to select a PDF or DOCX
  2. Enter API Key β€” (Optional) Enter your Groq API key for enhanced responses
  3. Click Process β€” The system extracts text, creates embeddings, and indexes chunks
  4. Chat with your file β€” Ask questions about the uploaded document
  5. Get answers β€” The system retrieves relevant chunks and generates contextual answers

Workflow

User opens application
         ↓
Mode A: General Chat          Mode B: Document Q&A
         ↓                              ↓
User asks question             User uploads document (PDF/DOCX)
         ↓                              ↓
Groq generates                 User clicks Process
  general response                       ↓
         ↓                    System extracts text from document
Response displayed                       ↓
                              Text split into overlapping chunks
                                         ↓
                              Chunks converted to embeddings
                                         ↓
                              Embeddings stored in ChromaDB
                                         ↓
                              User asks question in chat
                                         ↓
                              Retriever finds relevant chunks
                                         ↓
                              LLM generates contextual answer
                                         ↓
                              Answer displayed in chat

Evaluation Metrics

  • Accuracy β€” Overall correctness
  • Precision β€” Proportion of correct positive predictions
  • Recall β€” Proportion of actual positives correctly identified
  • F1-Score β€” Harmonic mean of precision and recall
  • Confusion Matrix β€” Detailed prediction breakdown per class

Generated Reports

After running train.py, the reports/ directory will contain:

  • confusion_matrix_bert.png β€” BERT confusion matrix heatmap
  • confusion_matrix_gpt2.png β€” GPT-2 confusion matrix heatmap
  • confusion_matrix_llama.png β€” LLaMA confusion matrix heatmap
  • confusion_matrix_mistral.png β€” Mistral confusion matrix heatmap
  • confusion_matrix_xlnet.png β€” XLNet confusion matrix heatmap
  • model_comparison.png β€” Bar chart comparing all metrics
  • training_curves.png β€” Training loss and validation accuracy curves
  • classification_report_*.txt β€” Per-class classification reports
  • model_comparison.csv β€” Comparison table as CSV
  • evaluation_summary.json β€” Machine-readable metrics with best model

Configuration

Edit config.py to customize:

  • MAX_SAMPLES_TRAIN / MAX_SAMPLES_TEST β€” Dataset size
  • MODELS dict β€” Learning rate, batch size, epochs per model
  • EMBEDDING_MODEL β€” Sentence transformer for RAG
  • CHUNK_SIZE / TOP_K_RESULTS β€” RAG retrieval parameters
  • GROQ_MODEL β€” Groq model for chat responses (default: mixtral-8x7b-32768)
  • PORT β€” Web server port (default: 8509)

License

This project is for educational and research purposes (capstone project).