arnel8888's picture
Added project files
cc8152f verified
|
Raw
History Blame Contribute Delete
6.42 kB
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

πŸ“‹ Prerequisites

πŸ”§ Installation

Local Setup

  1. Clone the repository:

    git clone https://github.com/your-username/pdf-explainer-using-rag.git
    cd pdf-explainer-using-rag
    
  2. Create virtual environment:

    python -m venv proj_env
    source proj_env/bin/activate  # On Windows: proj_env\Scripts\activate
    
  3. Install dependencies:

    pip install -r requirements.txt
    
  4. Set up environment variables:

    # Create .env file
    echo "GROQ_API_KEY=your_groq_api_key_here" > .env
    
  5. Run the application:

    cd app
    python app.py
    
  6. Access the application: Open your browser and go to http://localhost:7860

Docker Setup

  1. Build the Docker image:

    docker build -t pdf-explainer .
    
  2. Run the container:

    docker run -p 7860:7860 -e GROQ_API_KEY=your_groq_api_key_here pdf-explainer
    
  3. Access the application: Open your browser and go to http://localhost:7860

🎯 Usage

Getting Started

  1. Open the application in your web browser
  2. Start chatting immediately - the AI works as a general assistant without any uploads
  3. Upload PDFs (optional) using the file upload section
  4. 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

  1. PDF Upload: Documents are parsed using PyMuPDF4LLM with markdown formatting
  2. Text Processing: Content is cleaned and split into semantic chunks
  3. Embedding: Text chunks are converted to vectors using BGE embeddings
  4. Storage: Vectors and metadata are stored in ChromaDB
  5. Retrieval: User questions trigger similarity search for relevant chunks
  6. 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

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request