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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0932def
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
---
license: mit
title: PDF Insight PRO
sdk: docker
emoji: πŸ’»
colorFrom: blue
colorTo: green
short_description: Agentic RAG APP
---

# PDF Insight Pro

An advanced PDF document analysis tool that combines RAG (Retrieval Augmented Generation) with agentic search capabilities to provide intelligent answers to queries about PDF documents.

## Table of Contents

- [Overview](#overview)
- [Features](#features)
- [RAG SYSTEM PERFORMANCE](#rag-system-metrics)
- [Architecture](#architecture)
- [Technical Stack](#technical-stack)
- [Installation](#installation)
- [Usage](#usage)
- [API Endpoints](#api-endpoints)
- [Deployment](#deployment)
- [Android App](#android-app)
- [License](#license)

## Overview

PDF Insight Pro is a sophisticated document analysis tool that allows users to upload PDF documents and ask questions about their content. The system uses state-of-the-art RAG techniques, combining document chunking, embedding generation, similarity search, and LLM processing to provide accurate and contextually relevant answers.

The application employs an agentic approach that can augment the document's information with web search capabilities when needed, ensuring comprehensive and up-to-date responses.

## Features

- **PDF Document Processing**: Upload and process PDF documents with automated text extraction and chunking
- **Agentic RAG System**: Combines document retrieval with powerful LLM reasoning
- **Web Search Integration**: Verifies document information with Tavily search API integration
- **Session Management**: Persistent session handling for chat history and document context
- **Multiple LLM Support**: Choose from different language models (Llama 4 Scout, Llama 3.1, Llama 3.3)
- **FastAPI Backend**: High-performance API with async support
- **Responsive UI**: User-friendly interface adaptable to different screen sizes
- **Docker Containerization**: Easy deployment with containerized application
- **Hugging Face Integration**: Automatic deployment to Hugging Face Spaces
- **Android Application**: Native mobile client


## RAG System Metrics

1. **Key Metrics Overview**:

| Metric                              | Value   |
| ----------------------------------- | ------- |
| **Semantic Similarity (Mean)**      | `0.852` |
| **ROUGE-L F1 Score (Mean)**         | `0.395` |
| **Semantic Similarity (Max)**       | `1.000` |
| **ROUGE-L F1 Score (Max)**          | `1.000` |
| **Semantic Similarity (Min)**       | `0.592` |
| **ROUGE-L F1 Score (Min)**          | `0.099` |
| **Standard Deviation (Similarity)** | `0.089` |
| **Standard Deviation (ROUGE-L F1)** | `0.217` |


2. **Quantile Distribution**:

| Percentile | Semantic Similarity | ROUGE-L F1 Score |
| ---------- | ------------------- | ---------------- |
| **25%**    | `0.7946`            | `0.2516`         |
| **50%**    | `0.8732`            | `0.3256`         |
| **75%**    | `0.9181`            | `0.4951`         |


3. **Evaluation Status**:

| Status | Count | Percentage |
| ------ | ----- | ---------- |
| PASS | `64`  | `85.3%`    |
| FAIL | `11`  | `14.7%`    |

## Architecture

The application follows a modular architecture with these main components:

### System Architecture Diagram

```mermaid
---
config:
  theme: forest
  look: neo
  layout: dagre
---
flowchart TD
 subgraph subGraph0["Presentation Layer"]
    direction TB
        Browser["Web Browser UI"]
        Android["Android WebView Client"]
  end
 subgraph subGraph1["API Layer"]
    direction TB
        APIGateway["FastAPI Entrypoints"]
        ChatRoutes["chat_routes.py"]
        SessionRoutes["session_routes.py"]
        UploadRoutes["upload_routes.py"]
        UtilityRoutes["utility_routes.py"]
        AppMain["app.py"]
  end
 subgraph subGraph2["Config & Models"]
    direction TB
        ConfigLoader["config.py"]
        DataModels["models.py"]
  end
 subgraph subGraph3["Service Layer"]
    direction TB
        RAGService["rag_service.py"]
        LLMService["llm_service.py"]
        SessionService["session_service.py"]
  end
 subgraph subGraph4["Utility Layer"]
    direction TB
        TextProc["text_processing.py"]
        FaissUtil["faiss_utils.py"]
        SessionUtil["session_utils.py"]
  end
 subgraph Storage["Storage"]
    direction TB
        UploadStore["/uploads (PDFs & sessions)"]
        FAISSIndex["FAISS Index (ephemeral/disk)"]
  end
 subgraph subGraph6["Docker Container"]
    direction TB
        subGraph1
        subGraph2
        subGraph3
        subGraph4
        Storage
  end
 subgraph subGraph7["External & DevOps"]
    direction TB
        GroqAPI["Groq LLM API"]
        TavilyAPI["Tavily Web Search API"]
        CI["GitHub Actions CI/CD"]
        HFS["HuggingFace Spaces"]
        DockerfileNode["Dockerfile"]
  end
 subgraph subGraph8["Static Assets"]
    direction TB
        StaticApp["Static Web App"]
  end
    Browser -- HTTP JSON --> StaticApp
    StaticApp -- HTTP JSON --> AppMain
    Android -- HTTP JSON --> AppMain
    AppMain -- routes --> ChatRoutes & SessionRoutes & UploadRoutes & UtilityRoutes
    ChatRoutes -- calls --> RAGService
    SessionRoutes -- calls --> SessionService
    UploadRoutes -- calls --> TextProc
    UtilityRoutes -- calls --> SessionUtil
    RAGService -- uses --> LLMService & FaissUtil
    RAGService -- calls --> GroqAPI & TavilyAPI
    LLMService -- uses --> ConfigLoader
    SessionService -- uses --> SessionUtil
    TextProc -- writes/reads --> UploadStore
    SessionUtil -- writes/reads --> UploadStore
    FaissUtil -- reads/writes --> FAISSIndex
    CI -- build & deploy --> DockerfileNode
    DockerfileNode -- deploy --> HFS
     Browser:::frontend
     Android:::frontend
     APIGateway:::api
     ChatRoutes:::api
     SessionRoutes:::api
     UploadRoutes:::api
     UtilityRoutes:::api
     AppMain:::api
     ConfigLoader:::service
     DataModels:::service
     RAGService:::service
     LLMService:::service
     SessionService:::service
     TextProc:::util
     FaissUtil:::util
     SessionUtil:::util
     UploadStore:::util
     FAISSIndex:::util
     GroqAPI:::external
     TavilyAPI:::external
     CI:::devops
     HFS:::devops
     DockerfileNode:::devops
     StaticApp:::frontend
    classDef frontend fill:#CCE5FF,stroke:#333,stroke-width:1px
    classDef api fill:#DFFFD6,stroke:#333,stroke-width:1px
    classDef service fill:#FFE5B4,stroke:#333,stroke-width:1px
    classDef util fill:#E3E4FA,stroke:#333,stroke-width:1px
    classDef external fill:#E0E0E0,stroke:#333,stroke-width:1px
    classDef devops fill:#CCFFFF,stroke:#333,stroke-width:1px
    click Android "https://github.com/jatin-mehra119/pdf-insight-beta/blob/main/Android%20App/app/src/main/res/layout/activity_splash.xml"
    click ChatRoutes "https://github.com/jatin-mehra119/pdf-insight-beta/blob/main/api/chat_routes.py"
    click SessionRoutes "https://github.com/jatin-mehra119/pdf-insight-beta/blob/main/api/session_routes.py"
    click UploadRoutes "https://github.com/jatin-mehra119/pdf-insight-beta/blob/main/api/upload_routes.py"
    click UtilityRoutes "https://github.com/jatin-mehra119/pdf-insight-beta/blob/main/api/utility_routes.py"
    click AppMain "https://github.com/jatin-mehra119/pdf-insight-beta/blob/main/app.py"
    click ConfigLoader "https://github.com/jatin-mehra119/pdf-insight-beta/blob/main/configs/config.py"
    click DataModels "https://github.com/jatin-mehra119/pdf-insight-beta/blob/main/models/models.py"
    click RAGService "https://github.com/jatin-mehra119/pdf-insight-beta/blob/main/services/rag_service.py"
    click LLMService "https://github.com/jatin-mehra119/pdf-insight-beta/blob/main/services/llm_service.py"
    click SessionService "https://github.com/jatin-mehra119/pdf-insight-beta/blob/main/services/session_service.py"
    click TextProc "https://github.com/jatin-mehra119/pdf-insight-beta/blob/main/utils/text_processing.py"
    click FaissUtil "https://github.com/jatin-mehra119/pdf-insight-beta/blob/main/utils/faiss_utils.py"
    click SessionUtil "https://github.com/jatin-mehra119/pdf-insight-beta/blob/main/utils/session_utils.py"
    click CI "https://github.com/jatin-mehra119/pdf-insight-beta/blob/main/.github/workflows/sync_to_hf.yml"
    click DockerfileNode "https://github.com/jatin-mehra119/pdf-insight-beta/tree/main/Dockerfile"
    click StaticApp "https://github.com/jatin-mehra119/pdf-insight-beta/blob/main/static/js/app.js"
```

### Backend Components

1. **PDF Processing Module** (`preprocessing.py`):
   - Document loading and text extraction using PyMuPDF
   - Intelligent chunking with metadata preservation
   - Embedding generation with sentence transformers
   - FAISS vector index for similarity search

2. **RAG Engine**:
   - Context retrieval based on semantic similarity
   - LLM integration using Groq API
   - Agentic processing with tool-calling capabilities
   - Web search augmentation with Tavily API

3. **API Layer** (`app.py`):
   - FastAPI framework for REST endpoints
   - Session management and persistence
   - File upload and processing
   - Chat interface and history management

### Workflow

1. **Document Processing**:
   - User uploads a PDF document
   - System extracts text using PyMuPDF
   - Text is chunked into semantically meaningful segments
   - Embeddings are generated for each chunk
   - A FAISS index is built for efficient similarity search

2. **Query Processing**:
   - User submits a question about the document
   - System retrieves relevant chunks using semantic similarity
   - Relevant chunks are combined into a context window
   - Context and query are sent to the LLM for processing
   - Optional: Web search integration for fact verification

3. **Response Generation**:
   - LLM generates a response based on the provided context
   - If web search is enabled, additional information may be incorporated
   - Response is returned to the user
   - Chat history is updated and persisted

## Project Structure

The project is organized into a modular architecture with clear separation of concerns:

```
PDF-Insight-Beta/
β”œβ”€β”€ app.py                      # Main FastAPI application entry point
β”œβ”€β”€ gen_dataset.py             # Dataset generation and RAG evaluation scripts
β”œβ”€β”€ test_RAG.ipynb            # Jupyter notebook for RAG system testing and metrics
β”œβ”€β”€ requirements.txt           # Python dependencies
β”œβ”€β”€ Dockerfile                 # Container configuration for deployment
β”œβ”€β”€ LICENSE                    # MIT license file
β”œβ”€β”€ README.md                 # Project documentation
β”œβ”€β”€ README_hf.md              # Hugging Face Spaces specific documentation
β”œβ”€β”€
β”œβ”€β”€ api/                       # API route handlers (modular FastAPI routes)
β”‚   β”œβ”€β”€ __init__.py           # Exports all route handlers
β”‚   β”œβ”€β”€ chat_routes.py        # Chat and conversation management endpoints
β”‚   β”œβ”€β”€ session_routes.py     # Session lifecycle management
β”‚   β”œβ”€β”€ upload_routes.py      # PDF upload and processing endpoints
β”‚   └── utility_routes.py     # Utility endpoints (models, health checks)
β”œβ”€β”€
β”œβ”€β”€ configs/                   # Configuration management
β”‚   └── config.py             # Centralized configuration and environment variables
β”œβ”€β”€
β”œβ”€β”€ models/                    # Pydantic data models
β”‚   └── models.py             # Request/response models for API validation
β”œβ”€β”€
β”œβ”€β”€ services/                  # Core business logic services
β”‚   β”œβ”€β”€ __init__.py           # Service module initialization
β”‚   β”œβ”€β”€ llm_service.py        # Language model integration and management
β”‚   β”œβ”€β”€ rag_service.py        # RAG implementation with agentic capabilities
β”‚   └── session_service.py    # Session persistence and management
β”œβ”€β”€
β”œβ”€β”€ utils/                     # Utility functions and helpers
β”‚   β”œβ”€β”€ __init__.py           # Utility module initialization
β”‚   β”œβ”€β”€ faiss_utils.py        # FAISS vector database operations
β”‚   β”œβ”€β”€ session_utils.py      # Session data serialization/deserialization
β”‚   └── text_processing.py    # PDF text extraction and chunking utilities
β”œβ”€β”€
β”œβ”€β”€ static/                    # Frontend web application
β”‚   β”œβ”€β”€ index.html            # Main web interface
β”‚   β”œβ”€β”€ css/
β”‚   β”‚   └── styles.css        # Application styling and responsive design
β”‚   └── js/
β”‚       └── app.js            # Frontend JavaScript for user interactions
β”œβ”€β”€
β”œβ”€β”€ development_scripts/       # Legacy and development utilities
β”‚   β”œβ”€β”€ app.py               # Original monolithic application (deprecated)
β”‚   └── preprocessing.py      # Original preprocessing functions (deprecated)
β”œβ”€β”€
β”œβ”€β”€ uploads/                   # Temporary storage for uploaded files and sessions
β”‚   β”œβ”€β”€ *.pdf                # Uploaded PDF documents
β”‚   └── *_session.pkl        # Serialized session data
β”œβ”€β”€
└── Android App/              # Native Android application
    β”œβ”€β”€ app/                  # Android app source code
    β”‚   β”œβ”€β”€ src/main/java/com/jatinmehra/  # Java source files
    β”‚   β”œβ”€β”€ src/main/res/     # Android resources (layouts, drawables, etc.)
    β”‚   └── AndroidManifest.xml  # Android app configuration
    β”œβ”€β”€ gradle/               # Gradle build system files
    └── build.gradle          # Project build configuration
```

### Key Components Description

#### Core Application Files
- **`app.py`**: Main FastAPI application that orchestrates all components and sets up the web server
- **`gen_dataset.py`**: Comprehensive evaluation script for RAG system performance using the neural-bridge dataset
- **`test_RAG.ipynb`**: Interactive Jupyter notebook for testing RAG capabilities and analyzing metrics

#### API Layer (`api/`)
- **`chat_routes.py`**: Handles chat interactions, query processing, and conversation flow
- **`session_routes.py`**: Manages session lifecycle, history retrieval, and cleanup operations
- **`upload_routes.py`**: Processes PDF uploads, text extraction, and document indexing
- **`utility_routes.py`**: Provides system utilities like model listing and health checks

#### Configuration (`configs/`)
- **`config.py`**: Centralizes all application settings, API keys, model configurations, and environment variables

#### Data Models (`models/`)
- **`models.py`**: Defines Pydantic models for request/response validation and API documentation

#### Business Logic (`services/`)
- **`llm_service.py`**: Manages language model interactions, prompt engineering, and response generation
- **`rag_service.py`**: Implements the core RAG pipeline with agentic search capabilities and tool integration
- **`session_service.py`**: Handles session persistence, chat history, and user context management

#### Utilities (`utils/`)
- **`faiss_utils.py`**: Provides FAISS vector database operations for similarity search and indexing
- **`session_utils.py`**: Handles session serialization, deserialization, and data persistence
- **`text_processing.py`**: PDF text extraction, intelligent chunking, and preprocessing utilities

#### Frontend (`static/`)
- **`index.html`**: Responsive web interface with modern UI design
- **`styles.css`**: CSS styling with mobile-first responsive design principles
- **`app.js`**: JavaScript for dynamic interactions, file uploads, and chat functionality

#### Mobile Application (`Android App/`)
- **Native Android client**: WebView-based mobile application that interfaces with the web app
- **Java source code**: Activity management, splash screen, and WebView configuration
- **Android resources**: UI layouts, icons, and mobile-specific configurations

## Technical Stack

### Backend
- **Python 3.12**: Core programming language
- **FastAPI**: API framework with async support
- **PyMuPDF**: PDF processing library
- **LangChain**: Framework for LLM application development
- **FAISS**: Vector similarity search library from Facebook AI
- **Sentence Transformers**: Text embedding generation
- **Groq API**: LLM inference service
- **Tavily API**: Web search integration
- **Uvicorn**: ASGI server

### Frontend
- **HTML/CSS/JavaScript**: Core web technologies
- **Font Awesome**: Icon library
- **Highlight.js**: Code syntax highlighting
- **Marked.js**: Markdown rendering
- **Responsive Design**: Mobile-friendly interface

*Note: The frontend was developed with assistance from Claude 3.7 AI.*

### DevOps
- **Docker**: Containerization
- **GitHub Actions**: CI/CD pipeline
- **Hugging Face Spaces**: Deployment platform

## Installation

### Prerequisites
- Python 3.12+
- API keys for Groq and Tavily

### Local Setup

1. Clone the repository:
```bash
git clone https://github.com/Jatin-Mehra119/PDF-Insight-Beta.git
cd PDF-Insight-Beta
```

2. Create and activate 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. Create a `.env` file with your API keys:
```
GROQ_API_KEY=your_groq_api_key
TAVILY_API_KEY=your_tavily_api_key
```

5. Run the application:
```bash
uvicorn app:app --host 0.0.0.0 --port 8000 --reload
```

### Docker Deployment

1. Build the Docker image:
```bash
docker build -t pdf-insight-pro .
```

2. Run the container:
```bash
docker run -p 7860:7860 \
  --mount type=secret,id=GROQ_API_KEY,dst=/run/secrets/GROQ_API_KEY \
  --mount type=secret,id=TAVILY_API_KEY,dst=/run/secrets/TAVILY_API_KEY \
  pdf-insight-pro
```

## Usage

1. Open the application in your browser at `http://localhost:8000`
2. Upload a PDF document using the interface
3. Wait for processing to complete
4. Ask questions about the document in the chat interface
5. Toggle the "Use web search" option for enhanced responses

## API Endpoints

- **GET `/`**: Redirect to static HTML interface
- **POST `/upload-pdf`**: Upload and process a PDF document
  - Returns a session ID for subsequent queries
- **POST `/chat`**: Send a query about the uploaded document
  - Requires session ID from previous upload
  - Optional parameter to enable web search
- **POST `/chat-history`**: Retrieve chat history for a session
- **POST `/clear-history`**: Clear chat history for a session
- **POST `/remove-pdf`**: Remove PDF and session data
- **GET `/models`**: List available language models

## Deployment

### Hugging Face Spaces

This project is configured for automatic deployment to Hugging Face Spaces using GitHub Actions. The workflow in `.github/workflows/sync_to_hf.yml` handles the deployment process.

To deploy to your own space:

1. Fork this repository
2. Create a Hugging Face Space
3. Add your Hugging Face token as a GitHub secret named `HF_TOKEN`
4. Update the username and space name in the workflow file
5. Push to the main branch to trigger deployment

## Android App

The repository includes an Android application that serves as a mobile interface to the web application. Rather than implementing a native client with direct API integration, the Android app utilizes a WebView component to load the deployed web interface from Hugging Face Spaces. This approach ensures consistency between the web and mobile experiences while reducing maintenance overhead.

### Android App Features

- WebView integration to the deployed web application
- Splash screen with app branding
- Responsive design that adapts to the mobile interface
- Native Android navigation and user experience
- Direct access to the full functionality of the web application

### Implementation Details

The Android app is implemented using Java and consists of:
- SplashActivity: Displays the app logo and transitions to the main activity
- MainActivity: Contains a WebView component that loads the deployed web application
- WebView configuration: Enables JavaScript, DOM storage, and handles file uploads

## License

MIT