# itzadityapandey/pdf-answerbot architecture

> Architecture diagram of the GitHub repository itzadityapandey/pdf-answerbot, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-04). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/itzadityapandey/pdf-answerbot
- Repository: https://github.com/itzadityapandey/pdf-answerbot

## Overview

##### Project Type and Purpose
- This is a full-stack (backend-heavy) application with a web-based UI. Its primary purpose is to process PDF files – extracting text, generating embeddings, storing those embeddings in a FAISS vector database, and supporting a chat interface (built on Gradio) for querying the content.
- The README highlights key features like PDF upload, text extraction, vector storage, question-answering using AI, and a Gradio-based UI.

##### Main Components
- Client/User Interface:
- Gradio-based Web UI: This is implemented in the functions/gradio_app.py file. It provides the interactive chat and file upload interface.
- Application Backend:
- Main Orchestrator (app.py): This is the central script that ties the process together. It handles reading the PDFs, processing them, and managing the workflow.
- PDF Processing Logic: Although not explicitly divided into multiple modules, the code in app.py (and possibly functions within the functions folder) is responsible for PDF parsing, text extraction, and splitting into chunks.
- Embeddings and Data Storage:
- Embedding Generation: Uses LangChain along with the Google Gemini AI (via langchain_google_genai and google-generativeai) to transform text into embeddings.
- FAISS Vector Database: Stores the generated embeddings for fast similarity search/retrieval.
- External Configurations & Services:
- Environment Configuration (.env & netlify.toml): The .env file holds sensitive API keys (e.g., GOOGLE_API_KEY), and netlify.toml suggests there might be deployment or hosting configurations via Netlify.
- Third-party Libraries/Services: Libraries like PyPDF2, Gradio, langchain, and FAISS are core to the application’s functionality.

##### Relationships and Data Flow
- A user interacts with the Gradio UI (via functions/gradio_app.py) to upload PDFs and pose questions.
- app.py orchestrates the workflow:
- It receives the uploaded PDFs from the UI.
- The PDFs are processed to extract text using libraries like PyPDF2.
- Extracted text is chunked and then converted into embeddings with Google Gemini AI, aided by LangChain.
- The resulting embeddings are stored in a FAISS vector database (faiss_index directory auto-created).
- When a user asks a question, the system retrieves the relevant embeddings/context from the FAISS store.
- The retrieved context is then used to generate and deliver answers back through the UI.

##### Architectural Patterns and Design Principles
- Modularity: Although relatively small, the separation of the Gradio app (UI) and the main application logic (app.py) promotes a modular design.
- Layering: The system can be viewed in layers – a presentation/UI layer (Gradio), an application/service layer (PDF processing and orchestration with app.py), and a data layer (FAISS index storage).
- External Configuration: The use of a .env file indicates adherence to best practices by externalizing configuration details.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Presentation Layer
    subgraph "Presentation Layer"
        USER("User"):::frontend
        GRADIO("Gradio UI (functions/gradio_app.py)"):::frontend
        USER -->|"uploadsPDF/asksQuestions"| GRADIO
    end

    %% Application Layer
    subgraph "Application Layer"
        APP("Main Application (app.py)"):::backend
        PDF_PROC("PDF Processing & Text Extraction"):::backend
        EMB_GEN("Embedding Generation (LangChain+Google Gemini AI)"):::backend
        APP -->|"processPDF"| PDF_PROC
        PDF_PROC -->|"extract&chunk"| EMB_GEN
    end

    %% Data Layer
    subgraph "Data Layer"
        FAISS_DB("FAISS Vector Database"):::data
    end

    %% External Configurations
    EXT("External Configurations & Deployment (netlify.toml)"):::external
    APP -->|"readsConfig"| EXT

    %% Flow Connections between Layers
    GRADIO -->|"sendsFile/Query"| APP
    EMB_GEN -->|"storesEmbeddings"| FAISS_DB
    FAISS_DB -->|"retrievesContext"| EMB_GEN
    EMB_GEN -->|"generatesAnswer"| APP
    APP -->|"sendsResponse"| GRADIO

    %% Legend
    subgraph "Legend"
        L1["Frontend: Blue"]:::frontend
        L2["Backend: Green"]:::backend
        L3["Data: Yellow"]:::data
        L4["External: Red"]:::external
    end

    %% Click Events
    click GRADIO "https://github.com/itzadityapandey/pdf-answerbot/blob/main/functions/gradio_app.py"
    click APP "https://github.com/itzadityapandey/pdf-answerbot/blob/main/app.py"
    click EXT "https://github.com/itzadityapandey/pdf-answerbot/blob/main/netlify.toml"

    %% Styles
    classDef frontend fill:#b3e5fc,stroke:#0277bd,stroke-width:2px;
    classDef backend fill:#c8e6c9,stroke:#2e7d32,stroke-width:2px;
    classDef data fill:#fff9c4,stroke:#f9a825,stroke-width:2px;
    classDef external fill:#ffcdd2,stroke:#c62828,stroke-width:2px;
```
