# srkchaithanya/chatpdf architecture

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

- Interactive diagram: https://gitdiagram.com/srkchaithanya/chatpdf
- Repository: https://github.com/srkchaithanya/chatpdf

## Overview

##### Project Type and Purpose
- This is a single-page, full-stack–style Streamlit application (“ChatPDF”) for interactive Q&A over PDF documents.
- It combines a lightweight web UI (Streamlit) with a server-side pipeline: PDF ingestion → text chunking → embedding → vector storage → retrieval + LLM (Google Gemini)–based QA.

##### File Structure
- Top-level files only:
– app.py: Entrypoint; holds UI code, orchestrates the pipeline.
– requirements.txt: Lists dependencies (Streamlit, PyPDF2, LangChain, FAISS, Google AI SDK).
– README.md: Explains overall architecture and usage.
- No nested folders—everything lives in app.py, but conceptually it splits into modules/pipeline stages.

##### README for Insights
- Confirms a 5-step pipeline: PDF Upload & Extraction → Text Chunking → Embedding → FAISS Storage → Question Answering.
- External dependencies: Google Generative AI (Gemini) for embeddings + LLM, FAISS local vector index.
- UI is a Streamlit sidebar + main chat area.

##### Main Components
a. Streamlit UI Layer
- File-uploader widget (PDF input)
b. PDF Processing Module
- PyPDF2 Text Extractor
- LangChain RecursiveCharacterTextSplitter
c. Embedding Service
- GoogleGenerativeAIEmbeddings (calls Google AI API)
d. Vector Store
- FAISS index (persisted on local disk under faiss_index/)
e. Question-Answering Chain
- LangChain load_qa_chain using gemini-2.0-flash LLM
- Prompt template enforcing context-only answers
f. Configuration Store
- .env / environment variables (GOOGLE_API_KEY)

##### Relationships and Data Flow
1) User uploads PDF(s) via Streamlit UI →
2) app.py invokes PDF Processor → raw text
3) Text Splitter → list of chunks
4) Embedding Service → embedding vectors
5) Store embeddings in FAISS (local FS)
6) On user question:
a) Query UI →
b) FAISS similarity search → top-K chunks →
c) Pass chunks + question → QA Chain (Google LLM) →
d) Return answer → UI

Also: Embedding Service and QA Chain each call out to Google AI REST endpoints.

##### Architectural Patterns & Design Principles
- Pipeline pattern: clear stages of data transformation.
- Separation of concerns: UI vs. PDF ingestion vs. vector store vs. LLM.
- Externalize secrets via .env.
- Local persistence of index for fast retrieval (caching).

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Streamlit UI Layer
    subgraph "Streamlit UI Layer"
        FU["File Uploader"]:::frontend
        PB["Process Button"]:::frontend
        QI["Question Input"]:::frontend
        AO["Answer Output"]:::frontend
    end

    %% Backend Pipeline
    subgraph "Backend Pipeline"
        PP["PDF Processing Module"]:::data-processing
        TC["Text Chunking Module"]:::data-processing
        ES["Embedding Service"]:::data-processing
        VS["Vector Store Handler"]:::data-processing
        QC["QA Chain"]:::data-processing
    end

    %% External Services
    subgraph "External Services"
        GAE["Google AI Embeddings API"]:::external
        GALLM["Google AI LLM API"]:::external
    end

    %% Storage
    subgraph "Storage"
        FAISS["FAISS Index"]:::storage
    end

    %% Configuration
    subgraph "Configuration"
        ENV[".env / GOOGLE_API_KEY"]:::config
    end

    %% Project Docs & Dependencies
    subgraph "Project Docs & Dependencies"
        README["Project Documentation"]:::docs
        REQ["Dependency Definitions"]:::docs
    end

    %% Data Flow
    FU -->|"upload_pdf()"| PP
    PP -->|"extract_text()"| TC
    TC -->|"chunks"| ES
    ES -->|"embed()"| FAISS
    ES -->|"call embed API"| GAE
    QI -->|"ask_question()"| VS
    VS -->|"similarity_search()"| FAISS
    VS -->|"retrieve_chunks()"| QC
    QC -->|"generate_answer()"| GALLM
    QC -->|"answer"| AO
    ENV -.-> ES
    ENV -.-> QC

    %% Click Events
    click FU "https://github.com/srkchaithanya/chatpdf/blob/main/app.py"
    click PB "https://github.com/srkchaithanya/chatpdf/blob/main/app.py"
    click QI "https://github.com/srkchaithanya/chatpdf/blob/main/app.py"
    click AO "https://github.com/srkchaithanya/chatpdf/blob/main/app.py"
    click PP "https://github.com/srkchaithanya/chatpdf/blob/main/app.py"
    click TC "https://github.com/srkchaithanya/chatpdf/blob/main/app.py"
    click ES "https://github.com/srkchaithanya/chatpdf/blob/main/app.py"
    click VS "https://github.com/srkchaithanya/chatpdf/blob/main/app.py"
    click QC "https://github.com/srkchaithanya/chatpdf/blob/main/app.py"
    click ENV "https://github.com/srkchaithanya/chatpdf/blob/main/app.py"
    click README "https://github.com/srkchaithanya/chatpdf/blob/main/README.md"
    click REQ "https://github.com/srkchaithanya/chatpdf/blob/main/requirements.txt"

    %% Styles
    classDef frontend fill:#ADD8E6,stroke:#000,color:#000;
    classDef data-processing fill:#90EE90,stroke:#000,color:#000;
    classDef storage fill:#D3D3D3,stroke:#000,color:#000;
    classDef external fill:#FFA500,stroke:#000,color:#000;
    classDef config fill:#FFD700,stroke:#000,color:#000;
    classDef docs fill:#F0E68C,stroke:#000,color:#000;
```
