File size: 10,039 Bytes
4eca48d
 
 
 
 
 
 
 
 
50991d9
4eca48d
e655aaf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
03f8ad2
e655aaf
 
 
 
 
 
03f8ad2
e655aaf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
03f8ad2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
---
license: mit
title: Enterprise Rag Assistant with IBM Granite
sdk: streamlit
emoji: πŸš€
colorFrom: red
colorTo: red
pinned: true
short_description: Retrieval-Augmented Generation app using IBM Granite Model
sdk_version: 1.46.1
---
# πŸ“„ Enterprise Rag Assistant with IBM Granite

A powerful Retrieval-Augmented Generation (RAG) application that allows you to upload PDF documents and ask intelligent questions about their content using IBM's Granite AI model.

## 🌟 Features

- **PDF Text Extraction**: Extract text from PDF documents with detailed progress tracking
- **Intelligent Chunking**: Split documents into manageable chunks with overlap for better context preservation
- **Semantic Search**: Find relevant content using advanced sentence embeddings
- **AI-Powered Q&A**: Generate accurate answers using IBM Granite language model
- **Interactive UI**: User-friendly Streamlit interface with real-time status updates
- **GPU/CPU Support**: Automatically detects and utilizes available hardware
- **Memory Optimization**: Efficient processing for large documents

## πŸš€ Quick Start

### Prerequisites

- Python 3.8 or higher
- pip package manager
- At least 4GB RAM (8GB+ recommended)
- Optional: CUDA-compatible GPU for faster processing

### Installation

1. **Clone the repository:**
```bash
git clone https://huggingface.co/spaces/SimranShaikh/enterprise-rag-assistant.git
cd pdf-rag-granite
```

2. **Create a virtual environment:**
```bash
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate
```

3. **Install dependencies:**
```bash
pip install -r requirements.txt
```

4. **Run the application:**
```bash
streamlit run app.py
```

5. **Open your browser** and navigate to `http://localhost:8501`

## πŸ“¦ Dependencies

Create a `requirements.txt` file with the following content:

```txt
streamlit>=1.28.0
PyPDF2>=3.0.1
sentence-transformers>=2.2.2
transformers>=4.30.0
torch>=2.0.0
numpy>=1.24.0
scikit-learn>=1.3.0
```

## πŸ”§ Usage

### Step 1: Load Models
1. Click the **"πŸ€– Load Models"** button
2. Wait for the models to download and load (this may take a few minutes on first run)
3. Models are cached locally for faster subsequent loads

### Step 2: Upload PDF
1. Click **"Browse files"** and select your PDF document
2. Supported formats: PDF files only
3. Maximum recommended size: 50MB

### Step 3: Process PDF
1. Click **"πŸ“– Process PDF"** after uploading
2. The system will:
   - Extract text from all pages
   - Split text into overlapping chunks
   - Generate embeddings for semantic search
   - Display processing progress

### Step 4: Ask Questions
1. Type your question in the text input field
2. Click **"πŸ” Get Answer"**
3. View the AI-generated answer and source references

### Example Questions
- "What is the main topic of this document?"
- "Summarize the key findings"
- "What are the recommendations mentioned?"
- "Who are the main authors or contributors?"
- "What methodology was used?"

## πŸ—οΈ Architecture

```
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   PDF Upload    │───▢│  Text Extraction │───▢│   Text Chunking β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                                         β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   User Query    │───▢│ Semantic Search  │◀───│   Embeddings    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚                       β”‚
         β”‚              β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
         └─────────────▢│  Answer Gen.    β”‚
                        β”‚ (IBM Granite)   β”‚
                        β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```

## πŸ”§ Configuration

### Model Configuration

You can modify the models used in the `SimplePDFRAG` class:

```python
# Embedding model options
embedding_model = SentenceTransformer('all-MiniLM-L6-v2')  # Default
# embedding_model = SentenceTransformer('all-mpnet-base-v2')  # Better quality

# Language model options
model_name = "ibm-granite/granite-3-2b-instruct"  # Default
# model_name = "ibm-granite/granite-3-8b-instruct"  # Better performance
# model_name = "google/flan-t5-base"  # Alternative
```

### Chunking Parameters

Adjust text chunking settings:

```python
def chunk_text(self, text, chunk_size=400, overlap=50):
    # chunk_size: Number of words per chunk
    # overlap: Number of overlapping words between chunks
```

### Search Parameters

Modify search behavior:

```python
def search_documents(self, query, top_k=3):
    # top_k: Number of relevant chunks to retrieve
    # min_threshold: Minimum similarity score (0.1 default)
```

## πŸ“Š Performance Tips

### For Better Performance:
- Use a GPU-enabled environment
- Increase chunk overlap for better context
- Use larger language models (8B+ parameters)
- Process smaller PDF files (< 20MB)

### Memory Management:
- The app automatically manages GPU memory
- Use the "Reset Everything" button to clear memory
- Process one PDF at a time for optimal performance

## πŸ› Troubleshooting

### Common Issues:

**1. Models not loading:**
```
Error: Model loading failed
```
- **Solution**: Check internet connection and try again
- **Alternative**: Use smaller models or CPU-only mode

**2. PDF text extraction fails:**
```
Error: No text could be extracted
```
- **Solution**: Ensure PDF contains selectable text (not just images)
- **Alternative**: Use OCR preprocessing tools

**3. Out of memory errors:**
```
Error: CUDA out of memory
```
- **Solution**: Reduce batch size or use CPU mode
- **Alternative**: Process smaller documents

**4. Slow processing:**
- **Solution**: Enable GPU acceleration
- **Alternative**: Use smaller embedding models

### Debug Mode

Enable debug logging by setting:
```python
logging.basicConfig(level=logging.DEBUG)
```

## πŸš€ Deployment

### Local Development
```bash
streamlit run app.py
```

### Docker Deployment
```dockerfile
FROM python:3.9-slim

WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt

COPY . .
EXPOSE 8501

CMD ["streamlit", "run", "app.py", "--server.port=8501", "--server.address=0.0.0.0"]
```

### Cloud Deployment

**Streamlit Cloud:**
1. Push code to GitHub
2. Connect repository to Streamlit Cloud
3. Deploy with one click

**Heroku:**
```bash
git init
heroku create your-app-name
git add .
git commit -m "Initial commit"
git push heroku main
```

## πŸ“ˆ Advanced Features

### Custom Models

Add support for custom models:

```python
def load_custom_model(self, model_path):
    """Load a custom trained model"""
    self.granite_model = AutoModelForCausalLM.from_pretrained(model_path)
    self.tokenizer = AutoTokenizer.from_pretrained(model_path)
```

### Batch Processing

Process multiple PDFs:

```python
def process_multiple_pdfs(self, pdf_files):
    """Process multiple PDFs simultaneously"""
    all_documents = []
    all_embeddings = []
    
    for pdf_file in pdf_files:
        # Process each PDF
        documents, embeddings = self.process_single_pdf(pdf_file)
        all_documents.extend(documents)
        all_embeddings.extend(embeddings)
    
    return all_documents, all_embeddings
```

### Export Results

Save Q&A sessions:

```python
def export_qa_session(self, qa_pairs, filename):
    """Export Q&A session to file"""
    import json
    with open(filename, 'w') as f:
        json.dump(qa_pairs, f, indent=2)
```

## 🀝 Contributing

We welcome contributions! Please follow these steps:

1. **Fork the repository**
2. **Create a feature branch:**
   ```bash
   git checkout -b feature/amazing-feature
   ```
3. **Make your changes and commit:**
   ```bash
   git commit -m "Add amazing feature"
   ```
4. **Push to your branch:**
   ```bash
   git push origin feature/amazing-feature
   ```
5. **Create a Pull Request**

### Development Guidelines

- Follow PEP 8 style guidelines
- Add docstrings to all functions
- Include unit tests for new features
- Update documentation as needed

## πŸ“ License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

```
MIT License

Copyright (c) 2024 Your Name

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
```

## πŸ™ Acknowledgments

- **IBM** for the Granite language models
- **Hugging Face** for the transformers library
- **Sentence Transformers** for embedding models
- **Streamlit** for the web framework
- **PyPDF2** for PDF processing


---