Spaces:
Sleeping
Sleeping
File size: 14,758 Bytes
899e7fa 05d6c07 e9800a3 05d6c07 e9800a3 05d6c07 0fe16fd 05d6c07 |
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 |
---
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 Metrics](#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:
### 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/yourusername/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 |