CoolFace
Apppublic

Neerajkadari/Context-Aware_Conversational_Intelligence_System

sourceHugging Faceupdated 5mo agoView on Hugging Face
0likes
README.md254 linesDownload Raw Back to root
1---2title: Context-Aware Conversational Intelligence System3sdk: docker4---5# Context-Aware Conversational Intelligence using Transformer Models6 7**Transforming Unstructured Data into Context-Aware Conversational Intelligence**8 9---10 11## Project Overview12 13This system is a modular AI application that combines transformer model experimentation with Retrieval-Augmented Generation (RAG) to create a dual-mode conversational intelligence system.14 15### Dual-Mode Operation16 171. **Document-Aware Assistant** — Upload PDF/DOCX files, process them into embeddings, and ask contextual questions182. **General Conversational Chatbot** — Ask any question, get explanations, reasoning, and conversational responses (no document required)19 20### Key Capabilities21 22- **Document Upload** — Drag-and-drop PDF/DOCX files for instant analysis23- **RAG Pipeline** — Extract text, generate embeddings, store in vector DB, and retrieve relevant context24- **Groq Integration** — Use Groq API (e.g., mixtral-8x7b) for fast, contextual answers25- **General Chatbot** — Full conversational AI that works even without uploaded documents26- **Model Training** — Fine-tune BERT, GPT-2, LLaMA, Mistral, and XLNet on AG News dataset27- **Model Evaluation** — Accuracy, precision, recall, F1-score, confusion matrices28- **Model Comparison** — Side-by-side performance comparison with charts and best model identification29- **Report Generation** — Automated visualizations, training curves, and performance summaries30 31---32 33## System Architecture34 35```36User Interface (Flask Web App)37        |38  AI Agent Controller39        |40  Intelligent Router41        |42  +-----+------+-------+43  |            |              |44Document QA   General Chat   Classification45(RAG Mode)    (LLM Mode)     (Transformer)46  |            |              |47ChromaDB    Groq API       BERT/GPT-248Embeddings                 LLaMA/Mistral/XLNet49```50 51---52 53## Transformer Models54 55| Model | Architecture | Base Model | Parameters | Training |56|-------|-------------|-----------|------------|----------|57| BERT | Bidirectional Transformer Encoder | bert-base-uncased | 110M | Full fine-tune |58| GPT-2 | Autoregressive Transformer Decoder | gpt2 | 124M | Full fine-tune |59| LLaMA | Decoder-based Large Language Model | TinyLlama-1.1B | 1.1B | LoRA (r=8) |60| Mistral | Transformer with Sliding Window Attention | Mistral-7B-v0.1 / Mistral-7B-Instruct-v0.2 | 7B | HF Inference API (Remote) |61| XLNet | Permutation-based Autoregressive Transformer | xlnet-base-cased | 110M | Full fine-tune |62 63---64 65## Dataset66 67**AG News** — A 4-class text classification benchmark:68 69| Class | Label |70|-------|-------|71| World | 0 |72| Sports | 1 |73| Business | 2 |74| Sci/Tech | 3 |75 76- Training samples: 200 (configurable in `config.py`)77- Test samples: 50 (configurable in `config.py`)78 79---80 81## Project Structure82 83```84project/85├── app.py                          # Flask web app + RAG engine + General chatbot86├── config.py                       # Central configuration87├── train.py                        # Model training (5 transformers) + report generation88├── requirements.txt                # Python dependencies89├── README.md                       # This file90│91├── rag/92│   ├── __init__.py93│   └── document_loader.py          # PDF/DOCX text extraction & chunking94│95├── ui/96│   ├── templates/97│   │   └── index.html              # Web UI (Chat + Upload + General chatbot)98│   └── static/                     # Static assets99│100├── models/                         # Saved model checkpoints (created by train.py)101│   ├── bert/102│   ├── gpt2/103│   ├── llama/104│   ├── mistral/105│   └── xlnet/106│107├── data/                           # Uploaded documents108├── reports/                        # Generated charts & evaluation reports109├── vector_store/                   # ChromaDB persistent storage110└── logs/                           # Application logs111```112 113---114 115## Installation116 117### Prerequisites118 119- Python 3.9 or higher120- pip package manager121- (Optional) NVIDIA GPU with CUDA for faster training122- (Optional) Groq API key for enhanced chat responses123- (Optional) HuggingFace API key (HF_TOKEN) for Mistral text classification124 125### Setup126 127```bash128# 1. Navigate to the project directory129cd "d:\major project-2"130 131# 2. Create a virtual environment (recommended)132python -m venv venv133venv\Scripts\activate    # Windows134# source venv/bin/activate  # Linux/Mac135 136# 3. Install dependencies137pip install -r requirements.txt138```139 140---141 142## How to Run143 144### Step 1: Train Models (One-time, Optional)145 146```bash147python train.py148```149 150This trains BERT, GPT-2, LLaMA, Mistral, and XLNet on the AG News dataset and generates:151- Confusion matrix heatmaps for each model152- Model comparison bar chart153- Training loss and accuracy curves154- Per-class classification reports155- JSON evaluation summary156- Best model identification157 158### Step 2: Start the Web Application159 160```bash161python app.py162```163 164Open your browser at **http://127.0.0.1:8509**165 166### Using the Application167 168#### General Chatbot Mode (No document needed)1691. Open the application1702. (Optional) Enter your Groq API key in the left panel1713. Start chatting — ask any question, get explanations, and conversational responses172 173#### Document Q&A Mode1741. **Upload a file** — Drag-and-drop or click "Browse files" to select a PDF or DOCX1752. **Enter API Key** — (Optional) Enter your Groq API key for enhanced responses1763. **Click Process** — The system extracts text, creates embeddings, and indexes chunks1774. **Chat with your file** — Ask questions about the uploaded document1785. **Get answers** — The system retrieves relevant chunks and generates contextual answers179 180---181 182## Workflow183 184```185User opens application186         ↓187Mode A: General Chat          Mode B: Document Q&A188         ↓                              ↓189User asks question             User uploads document (PDF/DOCX)190         ↓                              ↓191Groq generates                 User clicks Process192  general response                       ↓193         ↓                    System extracts text from document194Response displayed                       ↓195                              Text split into overlapping chunks196                                         ↓197                              Chunks converted to embeddings198                                         ↓199                              Embeddings stored in ChromaDB200                                         ↓201                              User asks question in chat202                                         ↓203                              Retriever finds relevant chunks204                                         ↓205                              LLM generates contextual answer206                                         ↓207                              Answer displayed in chat208```209 210---211 212## Evaluation Metrics213 214- **Accuracy** — Overall correctness215- **Precision** — Proportion of correct positive predictions216- **Recall** — Proportion of actual positives correctly identified217- **F1-Score** — Harmonic mean of precision and recall218- **Confusion Matrix** — Detailed prediction breakdown per class219 220---221 222## Generated Reports223 224After running `train.py`, the `reports/` directory will contain:225 226- `confusion_matrix_bert.png` — BERT confusion matrix heatmap227- `confusion_matrix_gpt2.png` — GPT-2 confusion matrix heatmap228- `confusion_matrix_llama.png` — LLaMA confusion matrix heatmap229- `confusion_matrix_mistral.png` — Mistral confusion matrix heatmap230- `confusion_matrix_xlnet.png` — XLNet confusion matrix heatmap231- `model_comparison.png` — Bar chart comparing all metrics232- `training_curves.png` — Training loss and validation accuracy curves233- `classification_report_*.txt` — Per-class classification reports234- `model_comparison.csv` — Comparison table as CSV235- `evaluation_summary.json` — Machine-readable metrics with best model236 237---238 239## Configuration240 241Edit `config.py` to customize:242 243- **MAX_SAMPLES_TRAIN / MAX_SAMPLES_TEST** — Dataset size244- **MODELS dict** — Learning rate, batch size, epochs per model245- **EMBEDDING_MODEL** — Sentence transformer for RAG246- **CHUNK_SIZE / TOP_K_RESULTS** — RAG retrieval parameters247- **GROQ_MODEL** — Groq model for chat responses (default: mixtral-8x7b-32768)248- **PORT** — Web server port (default: 8509)249 250---251 252## License253 254This project is for educational and research purposes (capstone project).