# deepfakedetect-org/deepfakedetect architecture

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

- Interactive diagram: https://gitdiagram.com/deepfakedetect-org/deepfakedetect
- Repository: https://github.com/deepfakedetect-org/deepfakedetect

## Overview

##### Main Components
- Frontend: Built with Next.js and React (located in the “client” directory). This layer handles the user interface – pages like sign-in, sign-up, landing page, user dashboard, and others. Note its integration with packages such as react-dropzone for video uploads, Radix UI (for toasts, sliders, dropdown menus), and Tailwind CSS for styling.
- Backend: Implemented with Node.js and Express (found in the “server” directory). This layer comprises API routes (e.g., userRegistration.ts, videoProcessing.ts, fetchData.ts), middleware (for security and request handling), and configuration files (e.g., dbConnection, Cloudinary, multerConfig).
- AI/ML Components: Integrated Python scripts and models for deepfake detection. Under “server/utils/package” and “server/utils/python_models”, key AI components (e.g., ConvNeXt models, facial recognition through MTCNN/FaceNet) are used for actual video analysis.
- External Services:
- Cloudinary – for video storage and retrieval.
- MongoDB – for storing user profiles, video processing logs, and analysis results.

##### Relationships and Data Flow
- Users interact with the Frontend, triggering video uploads and authentication workflows.
- From the Frontend, HTTP API calls (using axios) are sent to the Backend.
- The Backend handles these requests by processing video uploads with multer, validates them (using zod), and then either stores files on Cloudinary or retrieves data from MongoDB through Mongoose.
- When a video is uploaded, it is then passed to the Python-powered AI components. This step can be represented either as a service call between Node.js and the Python scripts or through an internal job queue.
- The AI/ML component analyzes the video and returns detection results back to the backend.
- Finally, the Backend returns these results to the Frontend where they are displayed in real time (e.g., using react-toast notifications).

##### Architectural Patterns
- Separation of concerns is evident; the UI layer (Next.js) is kept completely separate from the API and processing logic (Node.js/Express).
- The system appears to embrace a microservice-like structure for handling specific tasks (e.g., video processing in Python vs. route handling in Node.js).
- Use of RESTful endpoints for communication between the Frontend and Backend.
- Integration of external services (Cloudinary and MongoDB) to manage storage and persist data, supporting scalability under a SaaS model.

##### Final Checklist
- Clear labels for Client (Frontend), Server (Backend, API routes, middleware), AI/ML Module (Python and PyTorch models), and External Services (Cloudinary, MongoDB).
- Notes/captions for architectural patterns (REST API, separation of presentation/business logic, microservice approach) and identify specific technologies (Next.js, Express, PyTorch, Cloudinary, MongoDB).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client Layer
    subgraph "Client Layer (Frontend)"
        FE["Client (Next.js + React)"]:::frontend
        Pages["Pages (client/src/app)"]:::frontend
        Components["Components (client/src/components)"]:::frontend
    end

    %% Server Layer
    subgraph "Server Layer (Backend API)"
        API["API Server (Node.js & Express)"]:::backend
        UR["UserRegistration Route"]:::backend
        VP["VideoProcessing Route"]:::backend
        FD["DataFetching Route"]:::backend
        MW["Middleware & Config"]:::backend
    end

    %% AI/ML Layer
    subgraph "AI/ML Layer"
        AI["Deepfake Detection Engine (Python)"]:::ai
        ConvNeXt["ConvNeXt_model.py"]:::ai
        Prediction["prediction.py"]:::ai
        Preprocessing["preprocessing.py"]:::ai
        Models["Python Models (.pth files)"]:::ai
    end

    %% External Services
    subgraph "External Services"
        Cloudinary["Cloudinary (Media Storage)"]:::external
        MongoDB["MongoDB (Data Storage)"]:::external
    end

    %% Data Flow
    FE -->|"sendsHTTPrequests"| API
    API -->|"handles"| UR
    API -->|"handles"| VP
    API -->|"handles"| FD
    API -->|"invokes"| MW

    VP -->|"calls"| AI
    AI -->|"returnsDetectionResults"| VP

    API -->|"uploadsVideos"| Cloudinary
    API -->|"storesAndFetchesData"| MongoDB

    %% Composition connections
    Pages --- FE
    Components --- FE

    %% Styles
    classDef frontend fill:#f9e79f,stroke:#d4ac0d,stroke-width:2px;
    classDef backend fill:#aed6f1,stroke:#3498db,stroke-width:2px;
    classDef ai fill:#d5f5e3,stroke:#27ae60,stroke-width:2px;
    classDef external fill:#f5b7b1,stroke:#e74c3c,stroke-width:2px;

    %% Click Events
    click FE "https://github.com/deepfakedetect-org/deepfakedetect/tree/main/client"
    click Pages "https://github.com/deepfakedetect-org/deepfakedetect/tree/main/client/src/app"
    click Components "https://github.com/deepfakedetect-org/deepfakedetect/tree/main/client/src/components"
    click API "https://github.com/deepfakedetect-org/deepfakedetect/tree/main/server"
    click UR "https://github.com/deepfakedetect-org/deepfakedetect/blob/main/server/routes/userRegistration.ts"
    click VP "https://github.com/deepfakedetect-org/deepfakedetect/blob/main/server/routes/videoProcessing.ts"
    click FD "https://github.com/deepfakedetect-org/deepfakedetect/blob/main/server/routes/fetchData.ts"
    click MW "https://github.com/deepfakedetect-org/deepfakedetect/blob/main/server/config/multerConfig.ts"
    click Cloudinary "https://github.com/deepfakedetect-org/deepfakedetect/blob/main/server/config/cloudinary.ts"
    click MongoDB "https://github.com/deepfakedetect-org/deepfakedetect/blob/main/server/config/dbConnection.ts"
    click ConvNeXt "https://github.com/deepfakedetect-org/deepfakedetect/blob/main/server/utils/package/ConvNeXt_model.py"
    click Prediction "https://github.com/deepfakedetect-org/deepfakedetect/blob/main/server/utils/package/prediction.py"
    click Preprocessing "https://github.com/deepfakedetect-org/deepfakedetect/blob/main/server/utils/package/preprocessing.py"
    click AI "https://github.com/deepfakedetect-org/deepfakedetect/tree/main/server/utils/package"
    click Models "https://github.com/deepfakedetect-org/deepfakedetect/tree/main/server/utils/python_models"
```
