--- 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 ```bash # 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) ```bash 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 ```bash 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).