# srivathsav999/ai-planet-assignment architecture

> Architecture diagram of the GitHub repository srivathsav999/ai-planet-assignment, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-20). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/srivathsav999/ai-planet-assignment
- Repository: https://github.com/srivathsav999/ai-planet-assignment

## Overview

##### Project type and purpose
- This is a full-stack web application called “ai-planet-assignment.”
- Purpose: Allow users to upload PDF documents via a React UI and ask AI-powered questions about their content.
- Frontend: React app with drag-and-drop file upload and chat interface.
- Backend: FastAPI service handling file storage, text extraction, database persistence, and Q&A via LlamaIndex/OpenAI.

##### File structure
- Top-level directories:
- backend/ – FastAPI code, models, database config, QA engine, utils, upload directory
- frontend/ – React code, components, API service, static assets, Vite config
- Backend:
- main.py (controllers/routes: health, /upload, /ask)
- qa_engine.py (business logic: build index, call OpenAI)
- database.py & models.py (SQLAlchemy async, documents table)
- uploads/ (file system storage)
- Frontend:
- src/components – ChatInterface.jsx, FileUpload.jsx, ThemeToggle.jsx
- src/services/api.js – axios or fetch wrappers to call FastAPI endpoints
- index.html, App.jsx, styling files
- Config/build files:
- backend/requirements.txt, .gitignore
- frontend/package.json, vite.config.js, eslint.config.js

##### README for additional insights
- Endpoints: GET / (health), POST /upload, POST /ask
- Tech stack details: React, FastAPI, SQLAlchemy (SQLite), PyMuPDF, LlamaIndex, OpenAI
- Database: documents table with id, filename, content, created_at
- Error handling and CORS notes

##### Detailed capturing of essential elements
- Do not oversimplify: break down the backend into its submodules (controllers vs. extraction vs. QA vs. persistence).
- Local filesystem storage separate from the DB.
- Async operations where relevant (e.g., file I/O, DB access, API calls).
- Configurations (CORS, .env for API key).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% User and Network
    UserBrowser("User Browser"):::external
    UserBrowser -->|"interacts via HTTP"| ReactApp

    %% Frontend Zone
    subgraph "Frontend Zone"
        direction TB
        ReactApp("React SPA"):::frontend
        ChatInterface["ChatInterface.jsx"]:::frontend
        FileUpload["FileUpload.jsx"]:::frontend
        ThemeToggle["ThemeToggle.jsx"]:::frontend
        APIService["api.js"]:::frontend
        MainApp["main.jsx & App.jsx"]:::frontend
        HTMLTemplate["index.html"]:::frontend
        ViteConfig["vite.config.js"]:::frontend
        ESLintConfig["eslint.config.js"]:::frontend
        ReactApp -->|uses| MainApp
        MainApp -->|renders| ChatInterface
        MainApp -->|renders| FileUpload
        MainApp -->|renders| ThemeToggle
        MainApp -->|calls| APIService
        ReactApp -->|template| HTMLTemplate
        ReactApp -->|build config| ViteConfig
        ReactApp -->|lint config| ESLintConfig
    end

    %% Backend Zone
    UserBrowser -->|"POST /upload, /ask"| FastAPI
    ReactApp -->|"POST /upload, /ask"| FastAPI

    subgraph "Backend Zone"
        direction TB
        subgraph "API Layer"
            direction TB
            FastAPI("FastAPI API (main.py)"):::backend
        end

        subgraph "Processing Layer"
            direction TB
            Utils["utils.py (PDF Extraction)"]:::backend
            QAEngine["qa_engine.py"]:::backend
            FastAPI -->|calls| Utils
            FastAPI -->|calls| QAEngine
        end

        subgraph "Persistence Layer"
            direction TB
            DatabaseConfig["database.py"]:::backend
            Models["models.py"]:::backend
            SQLiteDB("SQLite DB"):::database
            UploadsDir("uploads/ (PDF Storage)"):::storage
            FastAPI -->|uses| DatabaseConfig
            FastAPI -->|uses| Models
            FastAPI -->|saves files| UploadsDir
            DatabaseConfig -->|connects to| SQLiteDB
            Models -->|defines| SQLiteDB
        end

        subgraph "Config & Dependencies"
            direction TB
            Requirements["requirements.txt"]:::backend
            Gitignore[".gitignore"]:::backend
            FastAPI -->|deps| Requirements
            FastAPI -->|excludes| Gitignore
        end
    end

    %% External Service
    QAEngine -->|"calls OpenAI API"| OpenAI("OpenAI API"):::external

    %% Response flow
    FastAPI -->|"returns JSON answer"| ReactApp
    ReactApp -->|"displays answer"| ChatInterface

    %% Click Events
    click ReactApp "https://github.com/srivathsav999/ai-planet-assignment/tree/main/frontend/"
    click ChatInterface "https://github.com/srivathsav999/ai-planet-assignment/blob/main/frontend/src/components/ChatInterface.jsx"
    click FileUpload "https://github.com/srivathsav999/ai-planet-assignment/blob/main/frontend/src/components/FileUpload.jsx"
    click ThemeToggle "https://github.com/srivathsav999/ai-planet-assignment/blob/main/frontend/src/components/ThemeToggle.jsx"
    click APIService "https://github.com/srivathsav999/ai-planet-assignment/blob/main/frontend/src/services/api.js"
    click MainApp "https://github.com/srivathsav999/ai-planet-assignment/blob/main/frontend/src/main.jsx"
    click HTMLTemplate "https://github.com/srivathsav999/ai-planet-assignment/blob/main/frontend/index.html"
    click ViteConfig "https://github.com/srivathsav999/ai-planet-assignment/blob/main/frontend/vite.config.js"
    click ESLintConfig "https://github.com/srivathsav999/ai-planet-assignment/blob/main/frontend/eslint.config.js"
    click FastAPI "https://github.com/srivathsav999/ai-planet-assignment/blob/main/backend/main.py"
    click Utils "https://github.com/srivathsav999/ai-planet-assignment/blob/main/backend/utils.py"
    click QAEngine "https://github.com/srivathsav999/ai-planet-assignment/blob/main/backend/qa_engine.py"
    click DatabaseConfig "https://github.com/srivathsav999/ai-planet-assignment/blob/main/backend/database.py"
    click Models "https://github.com/srivathsav999/ai-planet-assignment/blob/main/backend/models.py"
    click UploadsDir "https://github.com/srivathsav999/ai-planet-assignment/tree/main/backend/uploads/"

    %% Styles
    classDef frontend fill:#ADD8E6,stroke:#000,stroke-width:1px
    classDef backend fill:#87CEFA,stroke:#000,stroke-width:1px
    classDef database fill:#90EE90,stroke:#000,stroke-width:1px
    classDef storage fill:#D3D3D3,stroke:#000,stroke-width:1px
    classDef external fill:#FFA500,stroke:#000,stroke-width:1px
```
