Spaces:
Sleeping
Sleeping
metadata
title: PDF Explainer Using RAG
emoji: π
colorFrom: blue
colorTo: green
sdk: docker
pinned: false
license: mit
short_description: A pdf explainer using retrieval-augmented generation (RAG)
π PDF Explainer Using RAG
A powerful AI-powered chatbot that allows you to upload PDF documents and ask intelligent questions about their content using Retrieval-Augmented Generation (RAG) technology.

π Features
- π€ Smart AI Assistant: Works as a general-purpose chatbot even without uploaded documents
- π€ PDF Upload & Processing: Upload single or multiple PDF documents with automatic text extraction
- π― RAG-Powered Responses: Uses advanced embedding models to find relevant document content
- π¬ Streaming Responses: Real-time streaming chat interface for smooth conversations
- π Multiple Uploads: Add more PDFs during conversations to expand the knowledge base
- π Table Support: Enhanced extraction of tables and structured content from PDFs
- π·οΈ Source Citations: Responses include filename and page number references
- π³ Docker Ready: Easy deployment with Docker containerization
π οΈ Technologies Used
- Frontend: Gradio - Interactive web interface
- LLM: Groq with Llama 3.1 8B Instant model
- PDF Processing: PyMuPDF4LLM - Optimized for LLM workflows
- Vector Database: ChromaDB - Efficient similarity search
- Embeddings: BGE-small-en-v1.5 - High-quality text embeddings
- Text Chunking: LangChain Text Splitters - Intelligent text segmentation
π Prerequisites
- Python 3.8+
- Groq API key (free at console.groq.com)
π§ Installation
Local Setup
Clone the repository:
git clone https://github.com/your-username/pdf-explainer-using-rag.git cd pdf-explainer-using-ragCreate virtual environment:
python -m venv proj_env source proj_env/bin/activate # On Windows: proj_env\Scripts\activateInstall dependencies:
pip install -r requirements.txtSet up environment variables:
# Create .env file echo "GROQ_API_KEY=your_groq_api_key_here" > .envRun the application:
cd app python app.pyAccess the application: Open your browser and go to
http://localhost:7860
Docker Setup
Build the Docker image:
docker build -t pdf-explainer .Run the container:
docker run -p 7860:7860 -e GROQ_API_KEY=your_groq_api_key_here pdf-explainerAccess the application: Open your browser and go to
http://localhost:7860
π― Usage
Getting Started
- Open the application in your web browser
- Start chatting immediately - the AI works as a general assistant without any uploads
- Upload PDFs (optional) using the file upload section
- Ask questions about your documents - the AI will automatically find and use relevant content
Example Workflows
General Chat (No PDFs needed):
User: "What are the benefits of renewable energy?"
AI: [Provides general knowledge response]
Document-Specific Questions (After uploading PDFs):
User: "What is the main conclusion of the research paper?"
AI: "According to the research paper (research_paper.pdf, Page 15),
the main conclusion is that renewable energy adoption..."
Multi-Document Analysis:
User: "Compare the methodologies mentioned in both documents"
AI: "Comparing the methodologies:
From methodology_paper.pdf (Page 3): [methodology A details]
From comparison_study.pdf (Page 7): [methodology B details]..."
π Project Structure
pdf-explainer-using-rag/
βββ app/
β βββ app.py # Main Gradio application
β βββ llm.py # LLM integration with RAG
β βββ retrieval.py # PDF processing and vector operations
βββ Dockerfile # Docker configuration
βββ .dockerignore # Docker ignore rules
βββ .gitignore # Git ignore rules
βββ requirements.txt # Python dependencies
βββ README.md # This file
βοΈ Configuration
Environment Variables
| Variable | Description | Required |
|---|---|---|
GROQ_API_KEY |
Your Groq API key for LLM access | Yes |
Customizable Parameters
In retrieval.py:
chunk_size: Text chunk size (default: 500)chunk_overlap: Overlap between chunks (default: 150)top_k: Number of retrieved documents (default: 5)
In llm.py:
model: Groq model name (default: "llama-3.1-8b-instant")temperature: Response creativity (default: 0.7)
π How It Works
- PDF Upload: Documents are parsed using PyMuPDF4LLM with markdown formatting
- Text Processing: Content is cleaned and split into semantic chunks
- Embedding: Text chunks are converted to vectors using BGE embeddings
- Storage: Vectors and metadata are stored in ChromaDB
- Retrieval: User questions trigger similarity search for relevant chunks
- Generation: LLM generates responses using retrieved context and chat history
π Deployment Options
Local Development
- Run directly with Python for development and testing
Docker Container
- Production-ready containerized deployment
- Includes pre-downloaded embedding models for faster startup
Cloud Deployment
- Compatible with any cloud platform supporting Docker
- Requires Groq API key as environment variable
π€ Contributing
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request